XRootD
XrdOucName2Name.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c N a m 2 N a m e . c c */
4 /* */
5 /* (c) 2006 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 // This file implements an instance of the XrdOucName2Name and the
32 // XrdOucName2NameVec abstract classes.
33 
34 #include <cerrno>
35 
36 #include "XrdSys/XrdSysError.hh"
38 #include "XrdSys/XrdSysPlatform.hh"
39 
40 /******************************************************************************/
41 /* G l o b a l s */
42 /******************************************************************************/
43 
45 
46 /******************************************************************************/
47 /* XrdOucName2Name & XrdOucName2NameVec Class Definition */
48 /******************************************************************************/
49 
51 {
52 public:
53 
54 virtual int lfn2pfn(const char *lfn, char *buff, int blen);
55 
56 virtual int lfn2rfn(const char *lfn, char *buff, int blen);
57 
58 virtual int pfn2lfn(const char *lfn, char *buff, int blen);
59 
60 virtual std::vector<std::string *> *n2nVec(const char *lfn);
61 
62  XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx);
63 
64 private:
65 int concat_fn(const char *prefix, int pfxlen,
66  const char *path, char *buffer, int blen);
67 
68 XrdSysError *eDest;
69 char *LocalRoot;
70 int LocalRootLen;
71 char *RemotRoot;
72 int RemotRootLen;
73 };
74 
75 /******************************************************************************/
76 /* I m p l e m e n t a t i o n */
77 /******************************************************************************/
78 /******************************************************************************/
79 /* C o n s t r u c t o r */
80 /******************************************************************************/
81 
82 XrdOucN2N::XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx)
83 {
84  eDest = erp;
85 
86 // Local root must not have any trailing slahes
87 //
88  if (!lpfx) {LocalRoot = 0; LocalRootLen = 0;}
89  else if (!(LocalRootLen = strlen(lpfx))) LocalRoot = 0;
90  else {LocalRoot = strdup(lpfx);
91  while(LocalRootLen && LocalRoot[LocalRootLen-1] == '/')
92  {LocalRootLen--; LocalRoot[LocalRootLen] = '\0';}
93  }
94 
95 // Remote root must not have any trailing slases unless it a URL
96 //
97  if (!rpfx) {RemotRoot = 0; RemotRootLen = 0;}
98  else if (!(RemotRootLen = strlen(rpfx))) RemotRoot = 0;
99  else {RemotRoot = strdup(rpfx);
100  if (*RemotRoot == '/')
101  while(RemotRootLen && RemotRoot[RemotRootLen-1] == '/')
102  {RemotRootLen--; RemotRoot[RemotRootLen] = '\0';}
103  }
104 }
105 
106 /******************************************************************************/
107 /* l f n 2 p f n */
108 /******************************************************************************/
109 
110 int XrdOucN2N::lfn2pfn(const char *lfn, char *buff, int blen)
111 {
112  if (concat_fn(LocalRoot, LocalRootLen, lfn, buff, blen))
113  return eDest->Emsg("glp",-ENAMETOOLONG,"generate local path",lfn);
114  return 0;
115 }
116 
117 /******************************************************************************/
118 /* l f n 2 r f n */
119 /******************************************************************************/
120 
121 int XrdOucN2N::lfn2rfn(const char *lfn, char *buff, int blen)
122 {
123  if (concat_fn(RemotRoot, RemotRootLen, lfn, buff, blen))
124  return eDest->Emsg("grp",-ENAMETOOLONG,"generate remote path",lfn);
125  return 0;
126 }
127 
128 /******************************************************************************/
129 /* c o n c a t _ f n */
130 /******************************************************************************/
131 
132 int XrdOucN2N::concat_fn(const char *prefix, // String to prefix path
133  const int pfxlen, // Length of prefix string
134  const char *path, // String to suffix prefix
135  char *buffer, // Resulting buffer
136  int blen) // The buffer length
137 {
138  int addslash = (*path != '/');
139  int pathlen = strlen(path);
140 
141  if ((pfxlen + addslash + pathlen) >= blen) return -1;
142 
143  if (pfxlen) {strcpy(buffer, prefix); buffer += pfxlen;}
144  if (addslash) {*buffer = '/'; buffer++;}
145  strcpy(buffer, path);
146  return 0;
147 }
148 
149 /******************************************************************************/
150 /* p f n 2 l f n */
151 /******************************************************************************/
152 
153 int XrdOucN2N::pfn2lfn(const char *pfn, char *buff, int blen)
154 {
155  char *tp;
156 
157  if (!LocalRoot
158  || strncmp(pfn, LocalRoot, LocalRootLen)
159  || pfn[LocalRootLen] != '/')
160  tp = (char *)pfn;
161  else tp = (char *)(pfn+LocalRootLen);
162 
163  if (strlcpy(buff, tp, blen) >= (unsigned int)blen) return ENAMETOOLONG;
164  return 0;
165 }
166 
167 /******************************************************************************/
168 /* n 2 n V e c */
169 /******************************************************************************/
170 
171 std::vector<std::string *> *XrdOucN2N::n2nVec(const char *lfn)
172 {
173  char pfnBuff[2048];
174  std::string *s;
175 
176 // Perform translation
177 //
178  if (lfn2pfn(lfn, pfnBuff, sizeof(pfnBuff))) return 0;
179 
180 // Return a vector of one
181 //
182  s = new std::string(pfnBuff);
183  return new std::vector<std::string *>(1, s);
184 }
185 
186 /******************************************************************************/
187 /* X r d O u c g e t N a m e 2 N a m e */
188 /******************************************************************************/
189 
191 {
192  (void)confg; (void)parms;
193 
194  XrdOucN2N *n2nP = new XrdOucN2N(eDest, lroot, rroot);
196  return (XrdOucName2Name *)n2nP;
197 }
static XrdSysError eDest(0,"crypto_")
XrdOucName2Name * XrdOucgetName2Name(XrdOucgetName2NameArgs)
XrdOucName2NameVec * XrdOucN2NVec_P
#define XrdOucgetName2NameArgs
size_t strlcpy(char *dst, const char *src, size_t sz)
XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx)
virtual std::vector< std::string * > * n2nVec(const char *lfn)
virtual int lfn2rfn(const char *lfn, char *buff, int blen)
virtual int lfn2pfn(const char *lfn, char *buff, int blen)
virtual int pfn2lfn(const char *lfn, char *buff, int blen)
XrdOucName2NameVec *Name2NameVec;.
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95