XrdOucName2Name.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                     X r d O u c N a m 2 N a m e . c c                      */
00004 /*                                                                            */
00005 /* (c) 2006 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010   
00011 //         $Id: XrdOucName2Name.cc 34000 2010-06-21 06:49:56Z ganis $
00012 
00013 const char *XrdOucName2NameCVSID = "$Id: XrdOucName2Name.cc 34000 2010-06-21 06:49:56Z ganis $";
00014   
00015 // This file implements an instance of the XrdOucName2Name abstract class.
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 /*                        I m p l e m e n t a t i o n                         */
00048 /******************************************************************************/
00049 /******************************************************************************/
00050 /*                           C o n s t r u c t o r                            */
00051 /******************************************************************************/
00052   
00053 XrdOucN2N::XrdOucN2N(XrdSysError *erp, const char *lpfx, const char *rpfx)
00054 {
00055    eDest = erp;
00056 
00057 // Local root must not have any trailing slahes
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 // Remote root must not have any trailing slases unless it a URL
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 /*                               l f n 2 p f n                                */
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 /*                               l f n 2 r f n                                */
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 /*                             c o n c a t _ f n                              */
00101 /******************************************************************************/
00102   
00103 int XrdOucN2N::concat_fn(const char *prefix, // String to prefix path
00104                          const int   pfxlen, // Length of prefix string
00105                          const char *path,   // String to suffix prefix
00106                                char *buffer, // Resulting buffer
00107                                int   blen)   // The buffer length
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 /*                               p f n 2 l f n                                */
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 /*                    X r d O u c g e t N a m e 2 N a m e                     */
00140 /******************************************************************************/
00141   
00142 XrdOucName2Name *XrdOucgetName2Name(XrdOucgetName2NameArgs)
00143 {
00144    return (XrdOucName2Name *)new XrdOucN2N(eDest, lroot, rroot);
00145 }

Generated on Tue Jul 5 14:46:50 2011 for ROOT_528-00b_version by  doxygen 1.5.1