00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdOucName2NameCVSID = "$Id: XrdOucName2Name.cc 34000 2010-06-21 06:49:56Z ganis $";
00014
00015
00016
00017 #include <errno.h>
00018
00019 #include "XrdSys/XrdSysError.hh"
00020 #include "XrdOuc/XrdOucName2Name.hh"
00021 #include "XrdSys/XrdSysPlatform.hh"
00022
00023 class XrdOucN2N : public XrdOucName2Name
00024 {
00025 public:
00026
00027 virtual int lfn2pfn(const char *lfn, char *buff, int blen);
00028
00029 virtual int lfn2rfn(const char *lfn, char *buff, int blen);
00030
00031 virtual int pfn2lfn(const char *lfn, char *buff, int blen);
00032
00033 XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx);
00034
00035 private:
00036 int concat_fn(const char *prefix, int pfxlen,
00037 const char *path, char *buffer, int blen);
00038
00039 XrdSysError *eDest;
00040 char *LocalRoot;
00041 int LocalRootLen;
00042 char *RemotRoot;
00043 int RemotRootLen;
00044 };
00045
00046
00047
00048
00049
00050
00051
00052
00053 XrdOucN2N::XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx)
00054 {
00055 eDest = erp;
00056
00057
00058
00059 if (!lpfx) {LocalRoot = 0; LocalRootLen = 0;}
00060 else if (!(LocalRootLen = strlen(lpfx))) LocalRoot = 0;
00061 else {LocalRoot = strdup(lpfx);
00062 while(LocalRootLen && LocalRoot[LocalRootLen-1] == '/')
00063 {LocalRootLen--; LocalRoot[LocalRootLen] = '\0';}
00064 }
00065
00066
00067
00068 if (!rpfx) {RemotRoot = 0; RemotRootLen = 0;}
00069 else if (!(RemotRootLen = strlen(rpfx))) RemotRoot = 0;
00070 else {RemotRoot = strdup(rpfx);
00071 if (*RemotRoot == '/')
00072 while(RemotRootLen && RemotRoot[RemotRootLen-1] == '/')
00073 {RemotRootLen--; RemotRoot[RemotRootLen] = '\0';}
00074 }
00075 }
00076
00077
00078
00079
00080
00081 int XrdOucN2N::lfn2pfn(const char *lfn, char *buff, int blen)
00082 {
00083 if (concat_fn(LocalRoot, LocalRootLen, lfn, buff, blen))
00084 return eDest->Emsg("glp",-ENAMETOOLONG,"generate local path",lfn);
00085 return 0;
00086 }
00087
00088
00089
00090
00091
00092 int XrdOucN2N::lfn2rfn(const char *lfn, char *buff, int blen)
00093 {
00094 if (concat_fn(RemotRoot, RemotRootLen, lfn, buff, blen))
00095 return eDest->Emsg("grp",-ENAMETOOLONG,"generate remote path",lfn);
00096 return 0;
00097 }
00098
00099
00100
00101
00102
00103 int XrdOucN2N::concat_fn(const char *prefix,
00104 const int pfxlen,
00105 const char *path,
00106 char *buffer,
00107 int blen)
00108 {
00109 int addslash = (*path != '/');
00110 int pathlen = strlen(path);
00111
00112 if ((pfxlen + addslash + pathlen) >= blen) return -1;
00113
00114 if (pfxlen) {strcpy(buffer, prefix); buffer += pfxlen;}
00115 if (addslash) {*buffer = '/'; buffer++;}
00116 strcpy(buffer, path);
00117 return 0;
00118 }
00119
00120
00121
00122
00123
00124 int XrdOucN2N::pfn2lfn(const char *pfn, char *buff, int blen)
00125 {
00126 char *tp;
00127
00128 if (!LocalRoot
00129 || strncmp(pfn, LocalRoot, LocalRootLen)
00130 || pfn[LocalRootLen] != '/')
00131 tp = (char *)pfn;
00132 else tp = (char *)(pfn+LocalRootLen);
00133
00134 if (strlcpy(buff, tp, blen) >= (unsigned int)blen) return ENAMETOOLONG;
00135 return 0;
00136 }
00137
00138
00139
00140
00141
00142 XrdOucName2Name *XrdOucgetName2Name(XrdOucgetName2NameArgs)
00143 {
00144 return (XrdOucName2Name *)new XrdOucN2N(eDest, lroot, rroot);
00145 }