xrm.cc

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //                                                                      //
00003 // xrdmn                                                                //
00004 //                                                                      //
00005 // Author: Andreas Joachim Peters (CERN,2005)                           //
00006 // A rm-like command line tool for xrootd environments                  //
00007 //                                                                      //
00008 //////////////////////////////////////////////////////////////////////////
00009 
00010 //       $Id: xrm.cc
00011 
00012 #include "XrdClient/XrdClientUrlInfo.hh"
00013 #include "XrdSys/XrdSysPthread.hh"
00014 #include "XrdClient/XrdClient.hh"
00015 #include "XrdClient/XrdClientDebug.hh"
00016 #include "XrdClient/XrdClientEnv.hh"
00017 #include "XrdClient/XrdClientAdmin.hh"
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <sys/time.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 #include <stdarg.h>
00024 
00025 extern "C" {
00026 /////////////////////////////////////////////////////////////////////
00027 // function + macro to allow formatted print via cout,cerr
00028 /////////////////////////////////////////////////////////////////////
00029 void cout_print(const char *format, ...)
00030 {
00031   char cout_buff[4096];
00032   va_list args;
00033   va_start(args, format);
00034   vsprintf(cout_buff, format,  args);
00035   va_end(args);
00036   cout << cout_buff;
00037 }
00038 
00039 void cerr_print(const char *format, ...)
00040 {
00041   char cerr_buff[4096];
00042   va_list args;
00043   va_start(args, format);
00044   vsprintf(cerr_buff, format,  args);
00045   va_end(args);
00046   cerr << cerr_buff;
00047 }
00048 
00049 #define COUT(s) do { \
00050   cout_print s;      \
00051 } while (0)
00052 
00053 #define CERR(s) do { \
00054   cerr_print s;      \
00055 } while (0)
00056 
00057 }
00058 //////////////////////////////////////////////////////////////////////
00059 
00060 
00061 #define XRDRM_VERSION            "(C) 2004 SLAC INFN xrdrm 0.1"
00062 
00063 bool summary=false;
00064 char* authz=0;
00065 char authzfilename[4096]="";
00066 struct timeval abs_start_time;
00067 struct timeval abs_stop_time;
00068 struct timezone tz;
00069 
00070 
00071 void print_summary(const char* del) {
00072   gettimeofday (&abs_stop_time, &tz);
00073   float abs_time=((float)((abs_stop_time.tv_sec - abs_start_time.tv_sec) *1000 +
00074                               (abs_stop_time.tv_usec - abs_start_time.tv_usec) / 1000));
00075 
00076   XrdOucString xdel(del);
00077 
00078   xdel.erase(xdel.rfind('?'));
00079   
00080   COUT(("[xrootd] #################################################################\n"));
00081   COUT(("[xrootd] # Deletion Name            : %s\n",xdel.c_str()));
00082   COUT(("[xrootd] # Realtime [s]             : %f\n",abs_time/1000.0));
00083   COUT(("[xrootd] ##########################################################\n"));
00084 }
00085 
00086 void PrintUsage() {
00087    cerr << "usage: xrm <file> [file2 [file3 [...] ]]"
00088      "[-DSparmname stringvalue] ... [-DIparmname intvalue] [-s] [-ns] [-v] [-authz <authz-file>] [-force]" << endl;
00089    cerr << " -s   :         silent mode, no summary output" << endl;
00090    cerr << " -v   :         display summary output" << endl <<endl;
00091    cerr << " -authz <authz-file> : set the authorization file" << endl;
00092    cerr << " -force              : set 'eternal'(1week) connect timeouts to block until xcp is executed" << endl << endl;
00093    cerr << " where:" << endl;
00094    cerr << "   parmname     is the name of an internal parameter" << endl;
00095    cerr << "   stringvalue  is a string to be assigned to an internal parameter" << endl;
00096    cerr << "   intvalue     is an int to be assigned to an internal parameter" << endl;
00097 }
00098 
00099 // Main program
00100 int main(int argc, char**argv) {
00101   char *delpath = 0;
00102   int retval = -1;
00103   unsigned int rmstartarg=0;
00104    if (argc < 2) {
00105       PrintUsage();
00106       exit(1);
00107    }
00108 
00109    DebugSetLevel(20000);
00110 
00111    // We want this tool to be able to copy from/to everywhere
00112    // Note that the side effect of these calls here is to initialize the
00113    // XrdClient environment.
00114    // This is crucial if we want to later override its default values
00115    EnvPutString( NAME_REDIRDOMAINALLOW_RE, "*" );
00116    EnvPutString( NAME_CONNECTDOMAINALLOW_RE, "*" );
00117    EnvPutString( NAME_REDIRDOMAINDENY_RE, "" );
00118    EnvPutString( NAME_CONNECTDOMAINDENY_RE, "" );
00119    EnvPutInt( NAME_CONNECTTIMEOUT , 2);
00120    EnvPutInt( NAME_RECONNECTTIMEOUT , 2);
00121    EnvPutInt( NAME_FIRSTCONNECTMAXCNT, 1);
00122    EnvPutInt( NAME_DEBUG, -1);
00123 
00124    for (int i=1; i < argc; i++) {
00125      
00126       if ( (strstr(argv[i], "-v") == argv[i])) {
00127         summary=true;
00128         continue;
00129       }
00130 
00131       if ( (strstr(argv[i], "-authz") == argv[i]) &&
00132            (argc >= i+2) ) {
00133         strcpy(authzfilename,argv[i+1]);
00134          i++;
00135          continue;
00136       }
00137 
00138       if ( (strstr(argv[i], "-force") == argv[i])) {
00139          EnvPutInt( NAME_CONNECTTIMEOUT , 60);
00140          EnvPutInt( NAME_FIRSTCONNECTMAXCNT, 7*24*60);
00141          continue;
00142       }
00143 
00144       
00145       if ( (strstr(argv[i], "-DS") == argv[i]) &&
00146            (argc >= i+2) ) {
00147         cerr << "Overriding " << argv[i]+3 << " with value " << argv[i+1] << ". ";
00148          EnvPutString( argv[i]+3, argv[i+1] );
00149          cerr << " Final value: " << EnvGetString(argv[i]+3) << endl;
00150          i++;
00151          continue;
00152       }
00153 
00154       if ( (strstr(argv[i], "-DI") == argv[i]) &&
00155            (argc >= i+2) ) {
00156         cerr << "Overriding '" << argv[i]+3 << "' with value " << argv[i+1] << ". ";
00157          EnvPutInt( argv[i]+3, atoi(argv[i+1]) );
00158          cerr << " Final value: " << EnvGetLong(argv[i]+3) << endl;
00159          i++;
00160          continue;
00161       }
00162 
00163       // Any other par is ignored
00164       if ( (strstr(argv[i], "-") == argv[i]) && (strlen(argv[i]) > 1) ) {
00165          cerr << "Unknown parameter " << argv[i] << endl;
00166          continue;
00167       }
00168 
00169       rmstartarg = i;
00170       break;
00171    }
00172    
00173 
00174    // load the authz structure from an environment variable or the given authz file
00175    authz = getenv("IO_AUTHZ");
00176    if (strlen(authzfilename)) {
00177      int fdauthz = open(authzfilename,O_RDONLY);
00178      if (!fdauthz) {
00179        cerr << "Error xcp: cannot open authz file %s " << authzfilename << endl;
00180        exit(-1);
00181      }
00182      struct stat stat;
00183      if (fstat(fdauthz, &stat)) {
00184        cerr << "Error xcp: cannot stat authz file %s " << authzfilename << endl;
00185        exit(-1);
00186      }
00187      // set 16M limit for authz files
00188      if (stat.st_size < 1024*1024*16) {
00189        authz = (char*) malloc (1024*1024*16);
00190        if (!authz) {
00191          cerr << "Error xcp: cannot allocate authz memory " << endl;
00192          exit(-1);
00193        }
00194        int nread = read(fdauthz,authz,stat.st_size);
00195        if (nread!=stat.st_size) {
00196          cerr << "Error xcp: error reading authz file %s " << authzfilename << endl;
00197          exit(-1);
00198        }
00199        // terminate the string
00200        authz[stat.st_size] = 0;
00201      } else {
00202        cerr << "Error xcp: authz file %s exceeds the 16M limit " << authzfilename << endl;
00203        exit(-1);
00204      }
00205      close(fdauthz);
00206    }
00207 
00208    DebugSetLevel(EnvGetLong(NAME_DEBUG));
00209 
00210    Info(XrdClientDebug::kNODEBUG, "main", XRDRM_VERSION);
00211     
00212 
00213    for (int loop = rmstartarg; loop < argc; loop++) {
00214      delpath = argv[loop];
00215      if (!delpath) {
00216        PrintUsage();
00217        exit(1);
00218      }
00219 
00220      XrdOucString delsrc(delpath);
00221      gettimeofday(&abs_start_time,&tz);
00222      
00223      if (authz) {
00224        // append the authz information to all root: protocol names
00225        if (delsrc.beginswith("root://")) {
00226          if (delsrc.find('?') != STR_NPOS) {
00227            delsrc += "&authz=";
00228          } else {
00229            delsrc += "?&authz=";
00230          }
00231          delsrc += (const char *)authz;
00232        }
00233      }
00234      
00235      if (delsrc.beginswith("root://")) {
00236        long id, flags, modtime;
00237        long long size;
00238        
00239        XrdOucString url(delsrc);
00240        XrdClientUrlInfo u(url);
00241        
00242        XrdClientAdmin* client = new XrdClientAdmin(url.c_str());
00243        if (!client) {
00244          CERR(("xrm: error creating admin client\n"));
00245          exit(-3);
00246        }
00247        
00248        if (!client->Connect()) {
00249          CERR(("xrm: error connecting to remote xrootd: %s\n",url.c_str()));
00250          exit(-3);
00251        }
00252        
00253        
00254        if ( ( !client->Stat(u.File.c_str(), id, size, flags, modtime ))) {
00255          CERR(("xrm: error remote source file does not exist: %s\n",delsrc.c_str()));
00256          
00257          delete client;
00258          client =0;
00259          continue;
00260        }
00261        
00262        if (client->GetCurrentUrl().IsValid()) {
00263          u.Host = client->GetCurrentUrl().Host;
00264          u.Port = client->GetCurrentUrl().Port;
00265          url = u.GetUrl();
00266        }
00267        
00268        if ( !client->Rm(u.File.c_str())) {
00269          CERR(("xrm: error removing remote file: %s\n",delsrc.c_str()));
00270          delete client;
00271          continue;
00272        }
00273        retval = 0;
00274        delete client;
00275      } else {
00276        struct stat stat_buf;
00277        int dostat = stat(delsrc.c_str(), &stat_buf);
00278        if (dostat) {
00279          
00280          CERR(("xrm: error: file %s does not exist!",delsrc.c_str()));
00281          continue;
00282        }
00283        
00284        int dorm = unlink(delsrc.c_str());
00285        if (dorm) {
00286          CERR(("xrm: error: no permissions to remove %s !",delsrc.c_str()));
00287          continue;
00288        }
00289        retval = 0;
00290      }
00291      
00292      if (summary) {
00293        print_summary(delsrc.c_str());
00294      }
00295    }
00296    return retval;
00297 }

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