XrdClientPrep.cc

Go to the documentation of this file.
00001 //         $Id: XrdClientPrep.cc 35287 2010-09-14 21:19:35Z ganis $
00002 
00003 const char *XrdClientPrepCVSID = "$Id: XrdClientPrep.cc 35287 2010-09-14 21:19:35Z ganis $";
00004 
00005 #include <ctype.h>
00006 #include <limits.h>
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 #include <errno.h>
00010 #include <sys/param.h>
00011 #include <sys/types.h>
00012 
00013 #include "XProtocol/XProtocol.hh"
00014 #include "XrdSys/XrdSysHeaders.hh"
00015 #include "XrdClient/XrdClientAdmin.hh"
00016 #include "XrdClient/XrdClientEnv.hh"
00017   
00018 /******************************************************************************/
00019 /*                                  m a i n                                   */
00020 /******************************************************************************/
00021   
00022 int main(int argc, char **argv)
00023 {
00024    extern char *optarg;
00025    extern int  optind, opterr;
00026    extern void Fatal(const char *, XrdClientAdmin *);
00027    extern void Usage(int);
00028    static const int MaxPathLen = MAXPATHLEN+1;
00029    XrdClientAdmin *Admin;
00030    FILE *Stream = 0;
00031    char c, Target[512], buff[16384], *bp, *sp, *theBuff = buff;
00032    char *inFile = 0;
00033    kXR_char Prty = 0, Opts = 0;
00034    long Debug = 0;
00035    int setDebug = 0, didPrep = 0, theBsz = sizeof(buff)-2, bsz, slen, rc;
00036 
00037 // Process the options
00038 //
00039    opterr = 0;
00040    if (argc > 1 && '-' == *argv[1]) 
00041       while ((c = getopt(argc,argv,"d:f:p:sStw")) && ((unsigned char)c != 0xff))
00042      { switch(c)
00043        {
00044        case 'd': Debug = atol(optarg); setDebug = 1;
00045                  break;
00046        case 'f': inFile = optarg;
00047                  break;
00048        case 'p': Prty = kXR_char(atoi(optarg));
00049                  break;
00050        case 's': Opts |= kXR_stage;
00051                  break;
00052        case 'S': Opts |=(kXR_stage|kXR_coloc);
00053                  break;
00054        case 't': Opts |= kXR_fresh;
00055                  break;
00056        case 'w': Opts |= kXR_wmode;
00057                  break;
00058        default:  cerr <<"xprep: Invalid option '-" <<argv[optind-1] <<"'" <<endl;
00059                  Usage(1);
00060        }
00061      }
00062 
00063 // Make sure a host has been specified
00064 //
00065    if (optind >= argc || !isalnum(*argv[optind]))
00066       {cerr <<"xprep: target host name not specified" <<endl;
00067        Usage(1);
00068       }
00069 
00070 // Get the target host
00071 //
00072    strcpy(Target, "root://");
00073    strcat(Target, argv[optind]);
00074    strcat(Target, "//prep");
00075    optind++;
00076 
00077 // Initialize the the admin (we need only one)
00078 //
00079 // Establish debugging level
00080 //
00081    EnvPutInt(NAME_DEBUG, (setDebug ? Debug : -1));
00082    Admin = new XrdClientAdmin(Target);
00083    EnvPutInt(NAME_DEBUG, (setDebug ? Debug :  0));
00084    if (!Admin->Connect()) Fatal("Connect", Admin);
00085 
00086 // If an infile was specified, make sure we can open it
00087 //
00088    if (inFile && !(Stream = fopen(inFile, "r")))
00089       {cerr <<"xprep: " <<strerror(errno) <<" opening " <<inFile <<endl;
00090        exit(4);
00091       }
00092 
00093 // If co0=location wanted, tyhen we must save the first file
00094 //
00095    if (Opts & kXR_coloc && optind < argc)
00096       {sp = argv[optind]; slen = strlen(sp);
00097        strcpy(buff+1, sp); theBuff += slen+1; theBsz -= (slen+1); optind++;
00098       }
00099 
00100 // Pre-process any command line paths at this point
00101 //
00102    do {bp = theBuff; bsz = theBsz;
00103        while(optind < argc)
00104             {sp = argv[optind]; slen = strlen(sp);
00105              if (bsz <= slen) break;
00106              *bp++ = '\n';      strcpy(bp, sp); bp += slen; bsz -= (slen+1);
00107              optind++;
00108             }
00109        if (bp == theBuff) break;
00110        if (!Admin->Prepare(buff+1, Opts, Prty)) Fatal("Prepare", Admin);
00111        didPrep = 1;
00112       } while(optind < argc);
00113 
00114 // If colocating, make sure we have the anchor file
00115 //
00116    if (Opts & kXR_coloc && theBuff == buff)
00117       {if (!Stream || !(sp = fgets(buff+1, MaxPathLen, Stream))) inFile = 0;
00118           else {slen = strlen(sp); theBsz -= (slen+1); theBuff += slen+1;}
00119       } else theBuff++;
00120 
00121 // Process the file
00122 //
00123    if (inFile)
00124       {do {bp = theBuff; bsz = theBsz;
00125            while(bsz >= MaxPathLen)
00126                 {if (!(sp = fgets(bp, MaxPathLen, Stream))) break;
00127                     {slen = strlen(sp); bsz -= slen; bp += slen;}
00128                 }
00129            if (bp == theBuff) break;
00130            if (!Admin->Prepare(buff+1, Opts, Prty)) Fatal("Prepare", Admin);
00131            didPrep = 1;
00132           } while(!feof(Stream) && !ferror(Stream));
00133        if ((rc = ferror(Stream)))
00134           {cerr <<"xprep: Error " <<rc <<" reading " <<inFile <<endl;
00135            exit(4);
00136           }
00137        fclose(Stream);
00138       }
00139 
00140 // If coloc is active, make sure we actually did a prepare
00141 //
00142    if (!didPrep)
00143       {if (theBuff > buff+1)
00144           {*theBuff = '\0';
00145            if (!Admin->Prepare(buff+1, Opts, Prty)) Fatal("Prepare", Admin);
00146           } else {cerr <<"xprep: No files to prepare were specified" <<endl;
00147                   Usage(1);
00148                  }
00149       }
00150 
00151 // All done
00152 //
00153    exit(0);
00154 }
00155 
00156 /******************************************************************************/
00157 /*                                 F a t a l                                  */
00158 /******************************************************************************/
00159 
00160 void Fatal(const char *What, XrdClientAdmin *theAdmin)
00161 {
00162 char *etext = theAdmin->LastServerError()->errmsg;
00163 
00164 // Print a message and exit
00165 //
00166    if (etext && *etext) cerr <<"xprep: " <<What <<" failed; " <<etext <<endl;
00167       else              cerr <<"xprep: " <<What <<" failed"           <<endl;
00168    exit(16);
00169 }
00170 
00171 /******************************************************************************/
00172 /*                                 U s a g e                                  */
00173 /******************************************************************************/
00174   
00175 void Usage(int rc)
00176 {
00177 cerr <<"\nUsage:   xprep [options] host[:port][,...] [path [...]]\n";
00178 cerr <<"\nOptions: [-d n] [-f fn] [-p prty] [-s] [-S] [-w]" <<endl;
00179 exit(rc);
00180 }

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