00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010   
00011 
00012 
00013 #include <errno.h>
00014 #include <fcntl.h>
00015 #include <unistd.h>
00016 #include <utime.h>
00017 #include <sys/stat.h>
00018 #include <sys/types.h>
00019 
00020 #include "XrdFrm/XrdFrmTrace.hh"
00021 #include "XrdFrm/XrdFrmUtils.hh"
00022 #include "XrdOuc/XrdOucUtils.hh"
00023 #include "XrdSys/XrdSysError.hh"
00024 #include "XrdSys/XrdSysHeaders.hh"
00025 #include "XrdSys/XrdSysPlatform.hh"
00026 
00027 const char *XrdFrmUtilsCVSID = "$Id: XrdFrmUtils.cc 35287 2010-09-14 21:19:35Z ganis $";
00028 
00029 using namespace XrdFrm;
00030   
00031 
00032 
00033 
00034   
00035 char XrdFrmUtils::Ask(char dflt, const char *Msg1, const char *Msg2,
00036                                  const char *Msg3)
00037 {
00038    const char *Hint;
00039    char Answer[8];
00040    int n;
00041 
00042    Hint = (dflt == 'y' ? " (y | n | a): " : " (n | y | a): ");
00043 
00044    do {cerr <<"frm_admin: " <<Msg1 <<Msg2 <<Msg3 <<Hint;
00045        cin.getline(Answer, sizeof(Answer));
00046        if (!*Answer) return dflt;
00047 
00048        n = strlen(Answer);
00049        if (!strncmp("yes",  Answer, n)) return 'y';
00050        if (!strncmp("no",   Answer, n)) return 'n';
00051        if (!strncmp("abort",Answer, n)) return 'a';
00052       } while(1);
00053    return 'a';
00054 }
00055   
00056 
00057 
00058 
00059   
00060 int XrdFrmUtils::chkURL(const char *Url)
00061 {
00062    const char *Elem;
00063 
00064 
00065 
00066    if (!(Elem = index(Url, ':'))) return 0;
00067    if (Elem[1] != '/' || Elem[2] != '/') return 0;
00068    if (!(Elem = index(Elem+3, '/')) || Elem[1] != '/') return 0;
00069    Elem++;
00070 
00071 
00072 
00073    while(Elem[1] == '/') Elem++;
00074    return Elem - Url;
00075 }
00076 
00077 
00078 
00079 
00080   
00081 char *XrdFrmUtils::makePath(const char *iName, const char *Path, int Mode)
00082 {
00083    char *bPath;
00084    int rc;
00085 
00086 
00087 
00088    bPath = XrdOucUtils::genPath(Path, iName, "frm");
00089 
00090 
00091 
00092    if (Mode > 0 && (rc = XrdOucUtils::makePath(bPath, Mode)))
00093       {Say.Emsg("makePath", rc, "create directory", bPath);
00094        return 0;
00095       }
00096 
00097 
00098 
00099    return bPath;
00100 }
00101 
00102 
00103 
00104 
00105   
00106 char *XrdFrmUtils::makeQDir(const char *Path, int Mode)
00107 {
00108    char qPath[1032], qLink[2048];
00109    int n, lksz, rc;
00110 
00111 
00112 
00113    strcpy(qPath, Path);
00114    n = strlen(qPath);
00115    if (qPath[n-1] != '/') qPath[n++] = '/';
00116    strcpy(qPath+n, "Queues/");
00117 
00118 
00119 
00120    if ((lksz = readlink(qPath, qLink, sizeof(qLink))) > 0)
00121       {qLink[lksz] = '\0';
00122        if (qLink[lksz-1] != '/') {qLink[lksz++] = '/'; qLink[lksz++] = '\0';}
00123        if (*qLink == '/') strcpy(qPath, qLink);
00124           else strcpy(qPath+n, qLink);
00125       }
00126 
00127 
00128 
00129    if (Mode > 0 && (rc = XrdOucUtils::makePath(qPath, Mode)))
00130       {Say.Emsg("makeQDir", rc, "create directory", qPath);
00131        return 0;
00132       }
00133 
00134 
00135 
00136    return strdup(qPath);
00137 }
00138 
00139 
00140 
00141 
00142   
00143 int XrdFrmUtils::MapM2O(const char *Nop, const char *Pop)
00144 {
00145    int Options = 0;
00146 
00147 
00148 
00149    if (index(Pop, 'w')) Options |= XrdFrmRequest::makeRW;
00150       if (*Nop != '-')
00151          {if (index(Pop, 's') ||  index(Pop, 'n'))
00152              Options |= XrdFrmRequest::msgSucc;
00153           if (index(Pop, 'f') || !index(Pop, 'q'))
00154              Options |= XrdFrmRequest::msgFail;
00155          }
00156 
00157 
00158 
00159    return Options;
00160 }
00161   
00162 
00163 
00164 
00165   
00166 int XrdFrmUtils::MapR2Q(char Opc, int *Flags)
00167 {
00168 
00169 
00170 
00171    switch(Opc)
00172          {case 0  :
00173           case '+': return XrdFrmRequest::stgQ;
00174           case '^': if (Flags) *Flags = XrdFrmRequest::Purge;
00175           case '&': return XrdFrmRequest::migQ;
00176           case '<': return XrdFrmRequest::getQ;
00177           case '=': if (Flags) *Flags |= XrdFrmRequest::Purge;
00178           case '>': return XrdFrmRequest::putQ;
00179           default:  break;
00180          }
00181    return XrdFrmRequest::nilQ;
00182 }
00183   
00184 
00185 
00186 
00187   
00188 int XrdFrmUtils::MapV2I(const char *vName, XrdFrmRequest::Item &ICode)
00189 {
00190    static struct ITypes {const char *IName; XrdFrmRequest::Item ICode;}
00191                  ITList[] = {{"lfn",    XrdFrmRequest::getLFN},
00192                              {"lfncgi", XrdFrmRequest::getLFNCGI},
00193                              {"mode",   XrdFrmRequest::getMODE},
00194                              {"obj",    XrdFrmRequest::getOBJ},
00195                              {"objcgi", XrdFrmRequest::getOBJCGI},
00196                              {"op",     XrdFrmRequest::getOP},
00197                              {"prty",   XrdFrmRequest::getPRTY},
00198                              {"qwt",    XrdFrmRequest::getQWT},
00199                              {"rid",    XrdFrmRequest::getRID},
00200                              {"tod",    XrdFrmRequest::getTOD},
00201                              {"note",   XrdFrmRequest::getNOTE},
00202                              {"tid",    XrdFrmRequest::getUSER}};
00203    static const int ITNum = sizeof(ITList)/sizeof(struct ITypes);
00204    int i;
00205 
00206 
00207 
00208    for (i = 0; i < ITNum; i++)
00209        if (!strcmp(vName, ITList[i].IName))
00210           {ICode = ITList[i].ICode; return 1;}
00211    return 0;
00212 }
00213   
00214 
00215 
00216 
00217   
00218 int XrdFrmUtils::Unique(const char *lkfn, const char *myProg)
00219 {
00220    static const int Mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
00221    FLOCK_t lock_args;
00222    int myFD, rc;
00223 
00224 
00225 
00226    if ((myFD = open(lkfn, O_RDWR|O_CREAT, Mode)) < 0)
00227       {Say.Emsg("Unique",errno,"open",lkfn); return 0;}
00228 
00229 
00230 
00231    bzero(&lock_args, sizeof(lock_args));
00232    lock_args.l_type =  F_WRLCK;
00233 
00234 
00235 
00236    do {rc = fcntl(myFD,F_SETLK,&lock_args);}
00237        while(rc < 0 && errno == EINTR);
00238    if (rc < 0) 
00239       {Say.Emsg("Unique", errno, "obtain the run lock on", lkfn);
00240        Say.Emsg("Unique", "Another", myProg, "may already be running!");
00241        close(myFD);
00242        return 0;
00243       }
00244 
00245 
00246 
00247    return 1;
00248 }
00249   
00250 
00251 
00252 
00253   
00254 int XrdFrmUtils::Utime(const char *Path, time_t tVal)
00255 {
00256    struct utimbuf tbuf = {tVal, tVal};
00257    int rc;
00258 
00259 
00260 
00261    do {rc = utime(Path, &tbuf);} while(rc && errno == EINTR);
00262    if (rc) Say.Emsg("Utils", errno, "set utime for pfn", Path);
00263 
00264 
00265 
00266    return rc == 0;
00267 }