XrdCommandLine.cc

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // XrdCommandLine
00003 //
00004 // Author: Fabrizio Furano (INFN Padova, 2005)
00005 // Rewritten by Elvin Sindrilaru <elvin.alin.sindrilaru@cern.ch>
00006 // with mods from Lukasz Janyst <ljanyst@cern.ch> (CERN, 2010)
00007 //
00008 // A command line tool for xrootd environments. The executable normally
00009 // is named xrd.
00010 //------------------------------------------------------------------------------
00011 
00012 #include "XrdClient/XrdClientUrlInfo.hh"
00013 #include "XrdClient/XrdClient.hh"
00014 #include "XrdClient/XrdClientAdmin.hh"
00015 #include "XrdClient/XrdClientDebug.hh"
00016 #include "XrdClient/XrdClientEnv.hh"
00017 #include "XrdOuc/XrdOucTokenizer.hh"
00018 #include "XrdSys/XrdSysHeaders.hh"
00019 
00020 #include <stdio.h>
00021 #include <unistd.h>
00022 #include <stdarg.h>
00023 #include <sstream>
00024 #include <string>
00025 #include <signal.h>
00026 #include <iomanip>
00027 
00028 #ifdef HAVE_READLINE
00029 #include <readline/readline.h>
00030 #include <readline/history.h>
00031 
00032 // define NOMACROS prevents the insanity of some courses implementations from
00033 // breaking this app
00034 #define NOMACROS
00035 #include <curses.h>
00036 #undef NOMACROS
00037 
00038 #include <term.h>
00039 #endif
00040 
00041 #define XRDCLI_VERSION "(C) 2004-2010 by the Xrootd group. Xrootd version: "XrdVSTRING
00042 
00043 //------------------------------------------------------------------------------
00044 // Some globals
00045 //------------------------------------------------------------------------------
00046 char           *opaqueinfo   = 0; // opaque info to be added to urls
00047 kXR_unt16       xrd_wr_flags = kXR_async | kXR_mkpath | kXR_open_updt | kXR_new;
00048 char           *initialhost  = 0;
00049 XrdClient      *genclient    = 0;
00050 XrdClientAdmin *genadmin     = 0;
00051 XrdOucString    currentpath  = "/";
00052 XrdOucString    cmdline_cmd;
00053 
00054 //------------------------------------------------------------------------------
00055 // Interrupt signal handler
00056 //------------------------------------------------------------------------------
00057 void CtrlCHandler(int sig)
00058 {
00059     std::cerr << std::endl;
00060     std::cerr << "Please use 'exit' to terminate this program." << std::endl;
00061 }
00062 
00063 //------------------------------------------------------------------------------
00064 // Commandline help message
00065 //------------------------------------------------------------------------------
00066 void PrintUsage()
00067 {
00068     std::cerr << "usage: xrd [host]";
00069     std::cerr << "[-DSparmname stringvalue] ... [-DIparmname intvalue] ";
00070     std::cerr << "[-O<opaque info>] [command]" << std::endl;
00071 
00072     std::cerr << " -DSparmname stringvalue     :         ";
00073     std::cerr << "override the default value of an internal XrdClient setting ";
00074     std::cerr << "(of string type)" << std::endl;
00075 
00076     std::cerr << " -DIparmname intvalue        :         ";
00077     std::cerr << "override the default value of an internal  XrdClient setting ";
00078     std::cerr << "(of int type)" << std::endl;
00079 
00080     std::cerr << " -O     :         adds some opaque information to any used ";
00081     std::cerr << "xrootd url" << std::endl;
00082 
00083     std::cerr << " -h     :         this help screen" << std::endl;
00084 
00085     std::cerr <<std::endl << " where:" << std::endl;
00086 
00087     std::cerr << "   parmname     is the name of an internal parameter";
00088     std::cerr << std::endl;
00089 
00090     std::cerr << "   stringvalue  is a string to be assigned to an internal ";
00091     std::cerr << "parameter" << std::endl;
00092 
00093     std::cerr << "   intvalue     is an int to be assigned to an internal ";
00094     std::cerr << "parameter" << std::endl;
00095 
00096     std::cerr << "   command      is a command line to be executed. in this ";
00097     std::cerr << "case the host is mandatory." << std::endl;
00098 
00099     std::cerr << std::endl;
00100 }
00101 
00102 //------------------------------------------------------------------------------
00103 // Commandline prompt
00104 //------------------------------------------------------------------------------
00105 void BuildPrompt( std::stringstream &s )
00106 {
00107     s.clear();
00108     if (genadmin)
00109     {
00110         s << "root://" << genadmin->GetCurrentUrl().Host;
00111         s << ":" << genadmin->GetCurrentUrl().Port;
00112         s << "/" << currentpath;
00113     }
00114     s << "> ";
00115 }
00116 
00117 //------------------------------------------------------------------------------
00118 // Out own primitive implementation of GNU readline
00119 //------------------------------------------------------------------------------
00120 #ifndef HAVE_READLINE
00121 char *readline(const char *prompt)
00122 {
00123     std::cout << prompt << std::flush;
00124     std::string input;
00125     std::getline( std::cin, input );
00126 
00127     if( !cin.good() )
00128         return 0;
00129 
00130     char *linebuf = (char *)malloc( input.size()+1 );
00131     strncpy( linebuf, input.c_str(), input.size()+1 );
00132 
00133     return linebuf;
00134 }
00135 #endif
00136 
00137 //------------------------------------------------------------------------------
00138 // Print help
00139 //------------------------------------------------------------------------------
00140 void PrintHelp()
00141 {
00142     std::cout << std::endl << XRDCLI_VERSION << std::endl << std::endl;
00143     std::cout << "Usage: xrd [-O<opaque_info>] [-DS<var_name> stringvalue] ";
00144     std::cout << "[-DI<var_name> integervalue] [host[:port]] [batchcommand]";
00145     std::cout << std::endl << std::endl;
00146 
00147     std::cout << "List of available commands:" << std::endl << std::endl;
00148     std::cout << std::left;
00149     std::cout << std::setw(55) << "cat <filename> [xrdcp parameters]";
00150     std::cout << "Output a file on standard output using xrdcp. <filename> ";
00151     std::cout << "can be a root:// URL." << std::endl;
00152 
00153     std::cout << std::setw(55)  << "cd <dirname>";
00154     std::cout << "Change the current directory. Note: no existence check is ";
00155     std::cout << "performed." << std::endl;
00156 
00157     std::cout << std::setw(55)  << "chmod <fileordirname> <user> <group> <other>";
00158     std::cout << "Modify file permissions." << std::endl;
00159 
00160     std::cout << std::setw(55)  << "connect [hostname[:port]]";
00161     std::cout << "Connect to the specified host." << std::endl;
00162 
00163     std::cout << std::setw(55)  << "cp <fileordirname> <fileordirname> [xrdcp parameters]";
00164     std::cout << "Copies a file using xrdcp. <fileordirname> are always ";
00165     std::cout << "relative to the" << std::endl;
00166     std::cout << std::setw(55)  << " " << "current remote path. Also, they ";
00167     std::cout << "can be root:// URLs specifying any other host." << std::endl;
00168 
00169     std::cout << std::setw(55)  << "dirlist [dirname]";
00170     std::cout << "Get the requested directory listing." << std::endl;
00171 
00172     std::cout << std::setw(55)  << "dirlistrec [dirname]";
00173     std::cout << "Get the requested recursive directory listing.";
00174     std::cout << std::endl;
00175 
00176     std::cout << std::setw(55)  << "envputint <varname> <intval>";
00177     std::cout << "Put an integer in the internal environment." << std::endl;
00178 
00179     std::cout << std::setw(55)  << "envputstring <varname> <stringval>";
00180     std::cout << "Put a string in the internal environment." << std::endl;
00181 
00182     std::cout << std::setw(55)  << "exit";
00183     std::cout << "Exits from the program." << std::endl;
00184 
00185     std::cout << std::setw(55) << "help";
00186     std::cout << "This help screen." << std::endl;
00187 
00188     std::cout << std::setw(55) << "stat [fileordirname]";
00189     std::cout << "Get info about the given file or directory path.";
00190     std::cout << std::endl;
00191 
00192     std::cout << std::setw(55) << "statvfs [vfilesystempath]";
00193     std::cout << "Get info about a virtual file system." << std::endl;
00194 
00195     std::cout << std::setw(55) << "existfile <filename>";
00196     std::cout << "Test if the specified file exists." << std::endl;
00197 
00198     std::cout << std::setw(55) << "existdir <dirname>";
00199     std::cout << "Test if the specified directory exists." << std::endl;
00200 
00201     std::cout << std::setw(55) << "getchecksum <filename>";
00202     std::cout << "Get the checksum for the specified file." << std::endl;
00203 
00204     std::cout << std::setw(55) << "isfileonline <filename>";
00205     std::cout << "Test if the specified file is online." << std::endl;
00206 
00207     std::cout << std::setw(55) << "locatesingle <filename> <writable>";
00208     std::cout << "Give a location of the given file in the currently ";
00209     std::cout << "connected cluster." << std::endl;
00210     std::cout << std::setw(55) << " " << "if writable is true only a ";
00211     std::cout << "writable location is searched" << std::endl;
00212     std::cout << std::setw(55) << " " << "but, if no writable locations ";
00213     std::cout << "are found, the result is negative but may" << std::endl;
00214     std::cout << std::setw(55) << " " << "propose a non writable one.";
00215     std::cout << std::endl;
00216 
00217     std::cout << std::setw(55) << "locateall <filename>";
00218     std::cout << "Give all the locations of the given file in the currently";
00219     std::cout << "connected cluster." << std::endl;
00220 
00221     std::cout << std::setw(55) << "mv <filename1> <filename2>";
00222     std::cout << "Move filename1 to filename2 locally to the same server.";
00223     std::cout << std::endl;
00224 
00225     std::cout << std::setw(55) << "mkdir <dirname> [user] [group] [other]";
00226     std::cout << "Creates a directory." << std::endl;
00227 
00228     std::cout << std::setw(55) << "rm <filename>";
00229     std::cout << "Remove a file." << std::endl;
00230 
00231     std::cout << std::setw(55) << "rmdir <dirname>";
00232     std::cout << "Removes a directory." << std::endl;
00233 
00234     std::cout << std::setw(55) << "prepare <filename> <options> <priority>";
00235     std::cout << "Stage a file in." << std::endl;
00236 
00237     std::cout << std::setw(55)  << "query <reqcode> <parms>";
00238     std::cout << "Obtain server information." << std::endl;
00239 
00240     std::cout << std::setw(55)  << "queryspace <logicalname>";
00241     std::cout << "Obtain space information." << std::endl;
00242 
00243     std::cout << std::setw(55)  << "truncate <filename> <length>";
00244     std::cout << "Truncate a file." << std::endl;
00245 
00246     std::cout << std::endl << "For further information, please read the ";
00247     std::cout << "xrootd protocol documentation." << std::endl;
00248 
00249     std::cout << std::endl;
00250 }
00251 
00252 //------------------------------------------------------------------------------
00253 // Check the answer of the server
00254 //------------------------------------------------------------------------------
00255 bool CheckAnswer(XrdClientAbs *gencli)
00256 {
00257     if (!gencli->LastServerResp()) return false;
00258 
00259     switch (gencli->LastServerResp()->status)
00260     {
00261         case kXR_ok:
00262             return true;
00263 
00264         case kXR_error:
00265             std::cout << "Error " << gencli->LastServerError()->errnum;
00266             std::cout << ": " << gencli->LastServerError()->errmsg << std::endl;
00267             std::cout << std::endl;
00268             return false;
00269 
00270         default:
00271             std::cout << "Server response: " << gencli->LastServerResp()->status;
00272             std::cout << std::endl;
00273             return true;
00274 
00275     }
00276 }
00277 
00278 //------------------------------------------------------------------------------
00279 // Print the output from locate
00280 //------------------------------------------------------------------------------
00281 void PrintLocateInfo(XrdClientLocate_Info &loc)
00282 {
00283     std::cout << "InfoType: ";
00284     switch (loc.Infotype)
00285     {
00286         case XrdClientLocate_Info::kXrdcLocNone:
00287             std::cout << "none" << std::endl;
00288             break;
00289         case XrdClientLocate_Info::kXrdcLocDataServer:
00290             std::cout << "kXrdcLocDataServer" << std::endl;
00291             break;
00292         case XrdClientLocate_Info::kXrdcLocDataServerPending:
00293             std::cout << "kXrdcLocDataServerPending" << std::endl;
00294             break;
00295         case XrdClientLocate_Info::kXrdcLocManager:
00296             std::cout << "kXrdcLocManager" << std::endl;
00297             break;
00298         case XrdClientLocate_Info::kXrdcLocManagerPending:
00299             std::cout << "kXrdcLocManagerPending" << std::endl;
00300             break;
00301         default:
00302             std::cout << "Invalid Infotype" << std::endl;
00303     }
00304     std::cout << "CanWrite: ";
00305     if (loc.CanWrite) std::cout << "true" << std::endl;
00306     else std::cout << "false" << std::endl;
00307     std::cout << "Location: '" << loc.Location << "'" << std::endl;
00308 }
00309 
00310 //------------------------------------------------------------------------------
00311 // process the "EXISTDIR" command
00312 //------------------------------------------------------------------------------
00313 void executeExistDir(XrdOucTokenizer &tkzer)
00314 {
00315     int retval = 0;
00316 
00317     if (!genadmin)
00318     {
00319         std::cout << "Not connected to any server." << std::endl << std::endl;
00320         retval = 1;
00321     }
00322 
00323     if (!retval)
00324     {
00325         char *fname = tkzer.GetToken(0, 0);
00326         XrdOucString pathname;
00327 
00328         if (fname)
00329         {
00330             if (fname[0] == '/')
00331                 pathname = fname;
00332             else
00333                 pathname = currentpath + "/" + fname;
00334         }
00335         else pathname = currentpath;
00336 
00337         // Try to issue the request
00338         vecBool vb;
00339         vecString vs;
00340         vs.Push_back(pathname);
00341         genadmin->ExistDirs(vs, vb);
00342 
00343         // Check the answer
00344         if (vb[0] && (vb.GetSize() >= 1))
00345             std::cout << "The directory exists." << std::endl;
00346         else
00347             std::cout << "Directory not found." << std::endl;
00348         std::cout << std::endl;
00349         return;
00350     }
00351 }
00352 
00353 //------------------------------------------------------------------------------
00354 // process the "CD" command
00355 //------------------------------------------------------------------------------
00356 void executeCd(XrdOucTokenizer &tkzer)
00357 {
00358     char *parmname = tkzer.GetToken(0, 0);
00359     XrdOucString pathname;
00360 
00361     if (!parmname || !strlen(parmname))
00362     {
00363         std::cout << "A directory name is needed." << std::endl << std::endl;
00364     }
00365     else
00366     {
00367 
00368         // Quite trivial directory processing
00369         if (!strcmp(parmname, ".."))
00370         {
00371             if (currentpath == "/") return;
00372 
00373             int pos = currentpath.rfind('/');
00374 
00375             if (pos != STR_NPOS)
00376                 currentpath.erase(pos);
00377 
00378             if (!currentpath.length())
00379                 currentpath = "/";
00380 
00381             return;
00382         }
00383         else if (!strcmp(parmname, "."))
00384         {
00385             return;
00386         }
00387 
00388         if (!currentpath.length() || (currentpath[currentpath.length()-1] != '/'))
00389             currentpath += "/";
00390 
00391         XrdOucString tmpPath;
00392         if (parmname[0] == '/')
00393             tmpPath = parmname;
00394         else
00395             tmpPath = currentpath + parmname;
00396 
00397         // Verify if tmpPath really exists
00398         vecBool vb;
00399         vecString vs;
00400         vs.Push_back(tmpPath);
00401         genadmin->ExistDirs(vs, vb);
00402 
00403         // Now check the answer
00404         if (CheckAnswer(genadmin))
00405             currentpath = tmpPath;
00406         else
00407             std::cout << "The directory does not exist." << std::endl << std::endl;
00408         return;
00409     }
00410 }
00411 
00412 //------------------------------------------------------------------------------
00413 // process the "ENVPUTINT" command
00414 //------------------------------------------------------------------------------
00415 void executeEnvPutInt(XrdOucTokenizer &tkzer)
00416 {
00417     char *parmname = tkzer.GetToken(0, 0),
00418           *val = tkzer.GetToken(0, 1);
00419 
00420     if (!parmname || !val)
00421     {
00422         std::cout << "Please provide command parameters:envputint <varname> ";
00423         std::cout << "<intvalue>" << std::endl << std::endl;
00424     }
00425     else
00426     {
00427         EnvPutInt(parmname, atoi(val));
00428         DebugSetLevel(EnvGetLong(NAME_DEBUG));
00429     }
00430     return;
00431 }
00432 
00433 //------------------------------------------------------------------------------
00434 // process the "ENVPUTSTRING" command
00435 //------------------------------------------------------------------------------
00436 void executeEnvPutString(XrdOucTokenizer &tkzer)
00437 {
00438     char *parmname = tkzer.GetToken(0, 0),
00439           *val = tkzer.GetToken(0, 1);
00440 
00441     if (!parmname || !val)
00442     {
00443         std::cout << "Please provide command parameters:envputstring <varname> ";
00444         std::cout << "<stringvalue>" << std::endl << std::endl;
00445     }
00446     else
00447         EnvPutString(parmname, val);
00448     return;
00449 }
00450 
00451 //------------------------------------------------------------------------------
00452 // process the "HELP" command
00453 //------------------------------------------------------------------------------
00454 void executeHelp(XrdOucTokenizer &)
00455 {
00456     PrintHelp();
00457 }
00458 
00459 //------------------------------------------------------------------------------
00460 // process the "CONNECT" command
00461 //------------------------------------------------------------------------------
00462 void executeConnect(XrdOucTokenizer &tkzer)
00463 {
00464     int retval = 0;
00465     char *host = initialhost;
00466 
00467     // If no host was given, then pretend one
00468     if (!host)
00469     {
00470         host = tkzer.GetToken(0, 1);
00471         if (!host || !strlen(host))
00472         {
00473             std::cout << "A hostname is needed to connect somewhere.";
00474             std::cout << std::endl << std::endl;
00475             retval = 1;
00476         }
00477     }
00478 
00479     if (!retval)
00480     {
00481         // Init the instance
00482         if (genadmin) delete genadmin;
00483         XrdOucString h(host);
00484         h  = "root://" + h;
00485         h += "//dummy";
00486 
00487         genadmin = new XrdClientAdmin(h.c_str());
00488 
00489         // Then connect
00490         if (!genadmin->Connect())
00491         {
00492             delete genadmin;
00493             genadmin = 0;
00494         }
00495     }
00496 }
00497 
00498 //------------------------------------------------------------------------------
00499 // process the "DIRLISTREC" command
00500 //------------------------------------------------------------------------------
00501 void executeDirListRec(XrdOucTokenizer &tkzer)
00502 {
00503     XrdClientVector<XrdOucString> pathq;
00504     int retval = 0;
00505 
00506     if (!genadmin)
00507     {
00508         std::cout << "Not connected to any server." << std::endl << std::endl;
00509         retval = 1;
00510     }
00511 
00512     if (!retval)
00513     {
00514         char *dirname = tkzer.GetToken(0, 0);
00515         XrdOucString path;
00516 
00517         if (dirname)
00518         {
00519             if (dirname[0] == '/')
00520                 path = dirname;
00521             else
00522             {
00523                 if ((currentpath.length() > 0) &&
00524                         (currentpath[currentpath.length()-1] != '/'))
00525                     path = currentpath + "/" + dirname;
00526                 else
00527                     path = currentpath + dirname;
00528 
00529             }
00530         }
00531         else path = currentpath;
00532 
00533         if (!path.length())
00534         {
00535             std::cout << "The current path is an empty string. Assuming '/'.";
00536             std::cout << std::endl << std::endl;
00537             path = '/';
00538         }
00539 
00540         // Initialize the queue with this path
00541         pathq.Push_back(path);
00542 
00543         while (pathq.GetSize() > 0)
00544         {
00545             XrdOucString pathtodo = pathq.Pop_back();
00546 
00547             // Now try to issue the request
00548             XrdClientVector<XrdClientAdmin::DirListInfo> nfo;
00549             genadmin->DirList(pathtodo.c_str(), nfo, true);
00550 
00551             // Now check the answer
00552             if (!CheckAnswer(genadmin))
00553             {
00554                 retval = 1;
00555                 std::cout << "In server ";
00556                 std::cout << genadmin->GetCurrentUrl().HostWPort;
00557                 std::cout << " or in some of its child nodes." << std::endl;
00558                 break;
00559             }
00560 
00561             for (int i = 0; i < nfo.GetSize(); i++)
00562             {
00563 
00564                 if ((nfo[i].flags & kXR_isDir) &&
00565                         (nfo[i].flags & kXR_readable) &&
00566                         (nfo[i].flags & kXR_xset))
00567                 {
00568 
00569                     // The path has not to be pushed if it's already present
00570                     // This may happen if several servers have the same path
00571                     bool foundpath = false;
00572                     for (int ii = 0; ii < pathq.GetSize(); ii++)
00573                     {
00574                         if (nfo[i].fullpath == pathq[ii])
00575                         {
00576                             foundpath = true;
00577                             break;
00578                         }
00579                     }
00580 
00581                     if (!foundpath)
00582                         pathq.Push_back(nfo[i].fullpath);
00583                     else
00584                         // If the path is already present in the queue
00585                         // then it was already printed as well.
00586                         continue;
00587 
00588                 }
00589 
00590                 char ts[256];
00591                 strcpy(ts, "n/a");
00592 
00593                 struct tm *t = gmtime(&nfo[i].modtime);
00594                 strftime(ts, 255, "%F %T", t);
00595 
00596                 char cflgs[16];
00597                 memset(cflgs, 0, 16);
00598 
00599                 if (nfo[i].flags & kXR_isDir)
00600                     strcat(cflgs, "d");
00601                 else strcat(cflgs, "-");
00602 
00603                 if (nfo[i].flags & kXR_readable)
00604                     strcat(cflgs, "r");
00605                 else strcat(cflgs, "-");
00606 
00607                 if (nfo[i].flags & kXR_writable)
00608                     strcat(cflgs, "w");
00609                 else strcat(cflgs, "-");
00610 
00611                 if (nfo[i].flags & kXR_xset)
00612                     strcat(cflgs, "x");
00613                 else strcat(cflgs, "-");
00614 
00615                 printf( "%s(%03ld) %12lld %s %s\n",
00616                         cflgs, nfo[i].flags, nfo[i].size,
00617                         ts, nfo[i].fullpath.c_str());
00618             }
00619         }
00620     }
00621     std::cout << std::endl;
00622     return;
00623 }
00624 
00625 //------------------------------------------------------------------------------
00626 // process the "DIRLIST" command
00627 //------------------------------------------------------------------------------
00628 void executeDirList(XrdOucTokenizer &tkzer)
00629 {
00630     int retval = 0;
00631 
00632     if (!genadmin)
00633     {
00634         std::cout << "Not connected to any server." << std::endl << std::endl;
00635         retval = 1;
00636     }
00637 
00638     if (!retval)
00639     {
00640         char *dirname = tkzer.GetToken(0, 0);
00641         XrdOucString path;
00642 
00643         if (dirname)
00644         {
00645             if (dirname[0] == '/')
00646                 path = dirname;
00647             else
00648             {
00649                 if ((currentpath.length() > 0) &&
00650                         (currentpath[currentpath.length()-1] != '/'))
00651                     path = currentpath + "/" + dirname;
00652                 else
00653                     path = currentpath + dirname;
00654 
00655             }
00656         }
00657         else path = currentpath;
00658 
00659         if (!path.length())
00660         {
00661             std::cout << "The current path is an empty string. Assuming '/'.";
00662             std::cout << std::endl << std::endl;
00663             path = '/';
00664         }
00665 
00666         // Now try to issue the request
00667         XrdClientVector<XrdClientAdmin::DirListInfo> nfo;
00668         genadmin->DirList(path.c_str(), nfo, true);
00669 
00670         // Now check the answer
00671         if (!CheckAnswer(genadmin))
00672         {
00673             retval = 1;
00674             std::cout << "In server " << genadmin->GetCurrentUrl().HostWPort;
00675             std::cout << " or in some of its child nodes." << std::endl;
00676             //nfo.Clear();
00677         }
00678 
00679         for (int i = 0; i < nfo.GetSize(); i++)
00680         {
00681             char ts[256];
00682             strcpy(ts, "n/a");
00683 
00684             struct tm *t = gmtime(&nfo[i].modtime);
00685             strftime(ts, 255, "%F %T", t);
00686 
00687             char cflgs[16];
00688             memset(cflgs, 0, 16);
00689 
00690             if (nfo[i].flags & kXR_isDir)
00691                 strcat(cflgs, "d");
00692             else strcat(cflgs, "-");
00693 
00694             if (nfo[i].flags & kXR_readable)
00695                 strcat(cflgs, "r");
00696             else strcat(cflgs, "-");
00697 
00698             if (nfo[i].flags & kXR_writable)
00699                 strcat(cflgs, "w");
00700             else strcat(cflgs, "-");
00701 
00702             if (nfo[i].flags & kXR_xset)
00703                 strcat(cflgs, "x");
00704             else strcat(cflgs, "-");
00705 
00706             printf( "%s(%03ld) %12lld %s %s\n",
00707                     cflgs, nfo[i].flags, nfo[i].size, ts,
00708                     nfo[i].fullpath.c_str());
00709         }
00710         std::cout << std::endl;
00711     }
00712     return;
00713 }
00714 
00715 //------------------------------------------------------------------------------
00716 // process the "LOCATESINGLE" command
00717 //------------------------------------------------------------------------------
00718 void executeLocateSingle(XrdOucTokenizer &tkzer)
00719 {
00720     int retval = 0;
00721 
00722     if (!genadmin)
00723     {
00724         std::cout << "Not connected to any server." <<std::endl;
00725         retval = 1;
00726     }
00727 
00728     if (!retval)
00729     {
00730         char *fname = tkzer.GetToken(0, 0);
00731         XrdOucString pathname;
00732 
00733         if (fname)
00734         {
00735             if (fname[0] == '/')
00736                 pathname = fname;
00737             else
00738                 pathname = currentpath + "/" + fname;
00739         }
00740         else pathname = currentpath;
00741 
00742         char *writable = tkzer.GetToken(0, 1);
00743         bool wrt = false;
00744 
00745         if (writable)
00746         {
00747             wrt = true;
00748             if (!strcmp(writable, "false") ||
00749                     !strcmp(writable, "0")) wrt = false;
00750             else
00751                 std::cout << "Checking for a writable location." <<std::endl;
00752         }
00753 
00754         // Now try to issue the request
00755         XrdClientLocate_Info loc;
00756         bool r;
00757         r = genadmin->Locate((kXR_char *)pathname.c_str(), loc, wrt);
00758         if (!r)
00759             std::cout << "No matching files were found." <<std::endl;
00760 
00761         PrintLocateInfo(loc);
00762         std::cout << std::endl;
00763     }
00764     return;
00765 
00766 }
00767 
00768 //------------------------------------------------------------------------------
00769 // process the "LOCATEALL" command
00770 //------------------------------------------------------------------------------
00771 void executeLocateAll(XrdOucTokenizer &tkzer)
00772 {
00773     int retval = 0;
00774 
00775     if (!genadmin)
00776     {
00777         std::cout << "Not connected to any server." <<std::endl << std::endl;
00778         retval = 1;
00779     }
00780 
00781     if (!retval)
00782     {
00783         char *fname = tkzer.GetToken(0, 0);
00784         XrdOucString pathname;
00785 
00786         if (fname)
00787         {
00788             if (fname[0] == '/')
00789                 pathname = fname;
00790             else
00791                 pathname = currentpath + "/" + fname;
00792         }
00793         else pathname = currentpath;
00794 
00795         // Now try to issue the request
00796         XrdClientVector<XrdClientLocate_Info> loc;
00797         bool r;
00798         r = genadmin->Locate((kXR_char *)pathname.c_str(), loc);
00799         if (!r)
00800             std::cout << "No matching files were found." <<std::endl;
00801 
00802         for (int ii = 0; ii < loc.GetSize(); ii++)
00803         {
00804             std::cout << std::endl << std::endl;
00805             std::cout << "------------- Location #" << ii+1 << std::endl;
00806             PrintLocateInfo(loc[ii]);
00807         }
00808         std::cout << std::endl;
00809     }
00810 }
00811 
00812 //------------------------------------------------------------------------------
00813 // process the "STAT"  command
00814 //------------------------------------------------------------------------------
00815 void executeStat(XrdOucTokenizer &tkzer)
00816 {
00817 
00818     int retval = 0;
00819 
00820     if (!genadmin)
00821     {
00822         std::cout << "Not connected to any server." << std::endl << std::endl;
00823         retval = 1;
00824     }
00825 
00826     if (!retval)
00827     {
00828         char *fname = tkzer.GetToken(0, 0);
00829         XrdOucString pathname;
00830 
00831         if (fname)
00832         {
00833             if (fname[0] == '/')
00834                 pathname = fname;
00835             else
00836                 pathname = currentpath + "/" + fname;
00837         }
00838         else pathname = currentpath;
00839 
00840         // Now try to issue the request
00841         long id, flags, modtime;
00842         long long size;
00843         genadmin->Stat(pathname.c_str(), id, size, flags, modtime);
00844 
00845         // Now check the answer
00846         if (!CheckAnswer(genadmin))
00847         {
00848             std::cout << "The command returned an error.";
00849             std::cout << std::endl << std::endl;
00850         }
00851         else
00852         {
00853             std::cout << "Id: " << id << " Size: " << size << " Flags: ";
00854             std::cout << flags << " Modtime: " << modtime << std::endl;
00855             std::cout <<std::endl;
00856         }
00857     }
00858 }
00859 
00860 //------------------------------------------------------------------------------
00861 // process the "STATVFS" command
00862 //------------------------------------------------------------------------------
00863 void executeStatvfs(XrdOucTokenizer &tkzer)
00864 {
00865 
00866     int retval = 0;
00867 
00868     if (!genadmin)
00869     {
00870         std::cout << "Not connected to any server." << std::endl << std::endl;
00871         retval = 1;
00872     }
00873 
00874     if (!retval)
00875     {
00876         char *fname = tkzer.GetToken(0, 0);
00877         XrdOucString pathname;
00878 
00879         if (fname)
00880         {
00881             if (fname[0] == '/')
00882                 pathname = fname;
00883             else
00884                 pathname = currentpath + "/" + fname;
00885         }
00886         else pathname = currentpath;
00887 
00888         // Now try to issue the request
00889         int rwservers = 0;
00890         long long rwfree = 0;
00891         int rwutil = 0;
00892         int stagingservers = 0;
00893         long long stagingfree = 0;
00894         int stagingutil = 0;
00895 
00896 
00897         genadmin->Stat_vfs(pathname.c_str(), rwservers, rwfree, rwutil,
00898                            stagingservers, stagingfree, stagingutil);
00899 
00900         // Now check the answer
00901         if (!CheckAnswer(genadmin))
00902         {
00903             std::cout << "The command returned an error.";
00904             std::cout << std::endl <<std::endl;
00905         }
00906         else
00907         {
00908             std::cout << "r/w nodes: " << rwservers;
00909             std::cout << " r/w free space: " << rwfree;
00910             std::cout << " r/w utilization: " << rwutil;
00911             std::cout << " staging nodes: " << rwservers;
00912             std::cout << " staging free space: " << rwfree;
00913             std::cout << " staging utilization: " << rwutil;
00914             std::cout << std::endl << std::endl;
00915         }
00916     }
00917 }
00918 
00919 //------------------------------------------------------------------------------
00920 // process the "EXISTFILE" command
00921 //------------------------------------------------------------------------------
00922 void executeExistFile(XrdOucTokenizer &tkzer)
00923 {
00924     int retval = 0;
00925     if (!genadmin)
00926     {
00927         std::cout << "Not connected to any server." << std::endl << std::endl;
00928         retval = 1;
00929     }
00930 
00931     if (!retval)
00932     {
00933         char *fname = tkzer.GetToken(0, 0);
00934         XrdOucString pathname;
00935 
00936         if (fname)
00937         {
00938             if (fname[0] == '/')
00939                 pathname = fname;
00940             else
00941                 pathname = currentpath + "/" + fname;
00942         }
00943         else pathname = currentpath;
00944 
00945         // Now try to issue the request
00946         vecBool vb;
00947         vecString vs;
00948         vs.Push_back(pathname);
00949         genadmin->ExistFiles(vs, vb);
00950 
00951         // Now check the answer
00952         if (!CheckAnswer(genadmin))
00953             retval = 1;
00954 
00955         if (vb[0] && (vb.GetSize() >= 1))
00956             std::cout << "The file exists." << std::endl;
00957         else
00958             std::cout << "File not found." << std::endl;
00959         std::cout <<std::endl;
00960     }
00961 }
00962 
00963 //------------------------------------------------------------------------------
00964 // process the "GETCHECKSUM" command
00965 //------------------------------------------------------------------------------
00966 void executeGetCheckSum(XrdOucTokenizer &tkzer)
00967 {
00968     int retval = 0;
00969 
00970     if (!genadmin)
00971     {
00972         std::cout << "Not connected to any server." << std::endl << std::endl;
00973         retval = 1;
00974     }
00975 
00976     if (!retval)
00977     {
00978         char *fname = tkzer.GetToken(0, 0);
00979         XrdOucString pathname;
00980 
00981         if (fname)
00982         {
00983             if (fname[0] == '/')
00984                 pathname = fname;
00985             else
00986                 pathname = currentpath + "/" + fname;
00987 
00988             // Now try to issue the request
00989             kXR_char *ans;
00990 
00991             genadmin->GetChecksum((kXR_char *)pathname.c_str(), &ans);
00992 
00993             // Now check the answer
00994             if (!CheckAnswer(genadmin))
00995             {
00996                 std::cout << "The command returned an error.";
00997                 std::cout << std::endl << std::endl;
00998             }
00999             else
01000             {
01001                 std::cout << "Checksum: " << ans << std::endl;
01002                 free(ans);
01003                 std::cout << std::endl;
01004             }
01005         }
01006         else
01007         {
01008             std::cout << "Please provide command parameter: getchecksum ";
01009             std::cout << "<filename>" << std::endl << std::endl;
01010         }
01011     }
01012 }
01013 
01014 //------------------------------------------------------------------------------
01015 // process the "ISFILEONLINE" command
01016 //------------------------------------------------------------------------------
01017 void executeIsFileOnline(XrdOucTokenizer &tkzer)
01018 {
01019     int retval = 0;
01020 
01021     if (!genadmin)
01022     {
01023         std::cout << "Not connected to any server." << std::endl << std::endl;
01024         retval = 1;
01025     }
01026 
01027     if (!retval)
01028     {
01029         char *fname = tkzer.GetToken(0, 0);
01030         XrdOucString pathname;
01031 
01032         if (fname)
01033         {
01034             if (fname[0] == '/')
01035                 pathname = fname;
01036             else
01037                 pathname = currentpath + "/" + fname;
01038 
01039             // Now try to issue the request
01040             vecBool vb;
01041             vecString vs;
01042             vs.Push_back(pathname);
01043             genadmin->IsFileOnline(vs, vb);
01044 
01045             // Now check the answer
01046             if (!CheckAnswer(genadmin))
01047             {
01048                 std::cout << "The command returned an error.";
01049                 std::cout << std::endl << std::endl;
01050             }
01051             else
01052             {
01053                 if (vb[0] && (vb.GetSize() >= 1))
01054                     std::cout << "The file is online." << std::endl;
01055                 else
01056                     std::cout << "The file is not online." << std::endl;
01057                 std::cout <<std::endl;
01058             }
01059         }
01060         else
01061         {
01062             std::cout << "Please provide command parameter: isfileoneline ";
01063             std::cout << "<filename>" << std::endl << std::endl;
01064         }
01065     }
01066 }
01067 
01068 //------------------------------------------------------------------------------
01069 // process the "MV" command
01070 //------------------------------------------------------------------------------
01071 void executeMv(XrdOucTokenizer &tkzer)
01072 {
01073     int retval = 0;
01074 
01075     if (!genadmin)
01076     {
01077         std::cout << "Not connected to any server." << std::endl << std::endl;
01078         retval = 1;
01079     }
01080 
01081     if (!retval)
01082     {
01083         char *fname1 = tkzer.GetToken(0, 0);
01084         char *fname2 = tkzer.GetToken(0, 0);
01085         XrdOucString pathname1, pathname2;
01086 
01087         if (!fname1 || !fname2)
01088         {
01089             std::cout << "Please provide command parameteres: mv <source> ";
01090             std::cout << "<destination>" <<std::endl <<std::endl;
01091         }
01092         else
01093         {
01094 
01095             if (fname1[0] == '/')
01096                 pathname1 = fname1;
01097             else
01098                 pathname1 = currentpath + "/" + fname1;
01099 
01100             if (fname2[0] == '/')
01101                 pathname2 = fname2;
01102             else
01103                 pathname2 = currentpath + "/" + fname2;
01104 
01105             // Now try to issue the request
01106             genadmin->Mv(pathname1.c_str(), pathname2.c_str());
01107 
01108             // Now check the answer
01109             if (!CheckAnswer(genadmin))
01110                 std::cout << "The command returned an error." <<std::endl <<std::endl;
01111         }
01112     }
01113     return;
01114 }
01115 
01116 //------------------------------------------------------------------------------
01117 // process the "MKDIR" command
01118 //------------------------------------------------------------------------------
01119 void executeMkDir(XrdOucTokenizer &tkzer)
01120 {
01121 
01122     int retval = 0;
01123 
01124     if (!genadmin)
01125     {
01126         std::cout << "Not connected to any server." << std::endl << std::endl;
01127         retval = 1;
01128     }
01129 
01130     if (!retval)
01131     {
01132         char *fname1 = tkzer.GetToken(0, 0);
01133         char *userc = tkzer.GetToken(0, 0);
01134         char *groupc = tkzer.GetToken(0, 0);
01135         char *otherc = tkzer.GetToken(0, 0);
01136 
01137         if (!fname1)
01138         {
01139             std::cout << "Please provide command parameters: mkdir <filename> ";
01140             std::cout << "[<user> <group> <other>]" << std::endl << std::endl;
01141         }
01142         else
01143         {
01144             int user = 0, group = 0, other = 0;
01145             if (userc) user = atoi(userc);
01146             if (groupc) group = atoi(groupc);
01147             if (otherc) other = atoi(otherc);
01148 
01149             XrdOucString pathname1;
01150 
01151             if (fname1[0] == '/')
01152                 pathname1 = fname1;
01153             else
01154                 pathname1 = currentpath + "/" + fname1;
01155 
01156             // Now try to issue the request
01157             genadmin->Mkdir(pathname1.c_str(), user, group, other);
01158 
01159             // Now check the answer
01160             if (!CheckAnswer(genadmin))
01161             {
01162                 std::cout << "The command returned an error.";
01163                 std::cout << std::endl << std::endl;
01164             }
01165         }
01166     }
01167 }
01168 
01169 //------------------------------------------------------------------------------
01170 // process the "CHMOD" command
01171 //------------------------------------------------------------------------------
01172 void executeChmod(XrdOucTokenizer &tkzer)
01173 {
01174 
01175     int retval = 0;
01176 
01177     if (!genadmin)
01178     {
01179         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01180         retval = 1;
01181     }
01182 
01183     if (!retval)
01184     {
01185 
01186         char *fname1 = tkzer.GetToken(0, 0);
01187         char *userc = tkzer.GetToken(0, 0);
01188         char *groupc = tkzer.GetToken(0, 0);
01189         char *otherc = tkzer.GetToken(0, 0);
01190 
01191         if (!fname1 || !userc || !groupc || !otherc)
01192         {
01193             std::cout << "Please provide command parameters: chmod <filename> ";
01194             std::cout << "<user> <group> <other>" << std::endl << std::endl;
01195         }
01196         else
01197         {
01198             int user = 0, group = 0, other = 0;
01199             if (userc) user = atoi(userc);
01200             if (groupc) group = atoi(groupc);
01201             if (otherc) other = atoi(otherc);
01202 
01203             XrdOucString pathname1;
01204 
01205             if (fname1[0] == '/')
01206                 pathname1 = fname1;
01207             else
01208                 pathname1 = currentpath + "/" + fname1;
01209 
01210             // Now try to issue the request
01211             genadmin->Chmod(pathname1.c_str(), user, group, other);
01212 
01213             // Now check the answer
01214             if (!CheckAnswer(genadmin))
01215                 std::cout << "The command returned an error." <<std::endl <<std::endl;
01216         }
01217     }
01218 }
01219 
01220 //------------------------------------------------------------------------------
01221 // process the "truncate" command
01222 //------------------------------------------------------------------------------
01223 void executeTruncate(XrdOucTokenizer &tkzer)
01224 {
01225     int retval = 0;
01226 
01227     if (!genadmin)
01228     {
01229         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01230         retval = 1;
01231     }
01232 
01233     if (!retval)
01234     {
01235 
01236         char *fname = tkzer.GetToken(0, 0);
01237         char *slen = tkzer.GetToken(0, 0);
01238 
01239         long long len = 0;
01240         XrdOucString pathname1;
01241 
01242         if (fname)
01243         {
01244             if (fname[0] == '/')
01245                 pathname1 = fname;
01246             else
01247                 pathname1 = currentpath + "/" + fname;
01248 
01249             if (slen) len = atoll(slen);
01250             else
01251             {
01252                 std::cout << "Missing parameter length." <<std::endl;
01253                 return;
01254             }
01255 
01256             if (len <= 0)
01257             {
01258                 std::cout << "Bad length." <<std::endl;
01259                 return;
01260             }
01261 
01262             // Now try to issue the request
01263             genadmin->Truncate(pathname1.c_str(), len);
01264 
01265             // Now check the answer
01266             if (!CheckAnswer(genadmin))
01267                 std::cout << "The command returned an error." <<std::endl <<std::endl;
01268         }
01269         else
01270         {
01271             std::cout << "Please provide command parameters: truncate <filename> ";
01272             std::cout << "<length>" << std::endl << std::endl;
01273             return;
01274         }
01275     }
01276 }
01277 
01278 //------------------------------------------------------------------------------
01279 // process the "RM" command
01280 //------------------------------------------------------------------------------
01281 void executeRm(XrdOucTokenizer &tkzer)
01282 {
01283     int retval = 0;
01284 
01285     if (!genadmin)
01286     {
01287         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01288         retval = 1;
01289     }
01290 
01291     if (!retval)
01292     {
01293         char *fname = tkzer.GetToken(0, 0);
01294         XrdOucString pathname;
01295 
01296         if (fname)
01297         {
01298             if (fname[0] == '/')
01299                 pathname = fname;
01300             else
01301                 pathname = currentpath + "/" + fname;
01302 
01303             // Now try to issue the request
01304             genadmin->Rm(pathname.c_str());
01305 
01306             // Now check the answer
01307             if (!CheckAnswer(genadmin))
01308             {
01309                 std::cout << "The command returned an error.";
01310                 std::cout << std::endl << std::endl;
01311             }
01312         }
01313         else
01314         {
01315             std::cout << "Please provide command parameter: rm <filename>";
01316             std::cout << std::endl << std::endl;
01317         }
01318     }
01319 }
01320 
01321 //------------------------------------------------------------------------------
01322 // process the "RMDIR" command
01323 //------------------------------------------------------------------------------
01324 void executeRmDir(XrdOucTokenizer &tkzer)
01325 {
01326     int retval = 0;
01327 
01328     if (!genadmin)
01329     {
01330         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01331         retval = 1;
01332     }
01333 
01334     if (!retval)
01335     {
01336         char *fname = tkzer.GetToken(0, 0);
01337         XrdOucString pathname;
01338 
01339         if (fname)
01340         {
01341             if (fname[0] == '/')
01342                 pathname = fname;
01343             else
01344                 pathname = currentpath + "/" + fname;
01345             // Now try to issue the request
01346             genadmin->Rmdir(pathname.c_str());
01347 
01348             // Now check the answer
01349             if (!CheckAnswer(genadmin))
01350             {
01351                 std::cout << "The command returned an error.";
01352                 std::cout << std::endl << std::endl;
01353             }
01354         }
01355         else
01356         {
01357             std::cout << "Please provide command parameter: rmdir <dirname>";
01358             std::cout << std::endl << std::endl;
01359         }
01360     }
01361 }
01362 
01363 //------------------------------------------------------------------------------
01364 // process the "PREPARE" command
01365 //------------------------------------------------------------------------------
01366 void executePrepare(XrdOucTokenizer &tkzer)
01367 {
01368     int retval = 0;
01369 
01370     if (!genadmin)
01371     {
01372         std::cout << "Not connected to any server." << std::endl << std::endl;
01373         retval = 1;
01374     }
01375 
01376     if (!retval)
01377     {
01378         char *fname1 = tkzer.GetToken(0, 0);
01379         char *optsc = tkzer.GetToken(0, 0);
01380         char *prioc = tkzer.GetToken(0, 0);
01381 
01382         if (!fname1)
01383         {
01384             std::cout << "Please provide command parameters: prepare ";
01385             std::cout << "<filename> <options> <priority>";
01386             std::cout << std::endl << std::endl;
01387         }
01388         else
01389         {
01390             int opts = 0, prio = 0;
01391             if (optsc) opts = atoi(optsc);
01392             if (prioc) prio = atoi(prioc);
01393 
01394             XrdOucString pathname1;
01395 
01396             if (fname1[0] == '/')
01397                 pathname1 = fname1;
01398             else
01399                 pathname1 = currentpath + "/" + fname1;
01400 
01401             // Now try to issue the request
01402             vecString vs;
01403             vs.Push_back(pathname1);
01404             genadmin->Prepare(vs, (kXR_char)opts, (kXR_char)prio);
01405 
01406             // Now check the answer
01407             if (!CheckAnswer(genadmin))
01408             {
01409                 std::cout << "The command returned an error.";
01410                 std::cout << std::endl << std::endl;
01411             }
01412         }
01413     }
01414 }
01415 
01416 //------------------------------------------------------------------------------
01417 // process the "CAT" command
01418 //------------------------------------------------------------------------------
01419 void executeCat(XrdOucTokenizer &tkzer)
01420 {
01421     int retval = 0;
01422 
01423     if (!genadmin)
01424     {
01425         std::cout << "Not connected to any server." << std::endl << std::endl;
01426         retval = 1;
01427     }
01428 
01429     if (!retval)
01430     {
01431         char *fname1 = tkzer.GetToken(0, 0);
01432         char *tk;
01433         XrdOucString pars;
01434 
01435         while ((tk = tkzer.GetToken(0, 0)))
01436         {
01437             pars += " ";
01438             pars += tk;
01439         }
01440 
01441         XrdOucString pathname1;
01442 
01443         if (fname1)
01444         {
01445             if ( (strstr(fname1, "root://") == fname1) ||
01446                     (strstr(fname1, "xroot://") == fname1) )
01447                 pathname1 = fname1;
01448             else if (fname1[0] == '/')
01449             {
01450                 pathname1 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01451                 pathname1 += "/";
01452                 pathname1 += fname1;
01453             }
01454             else
01455             {
01456                 pathname1 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01457                 pathname1 += "/";
01458                 pathname1 += currentpath;
01459                 pathname1 += "/";
01460                 pathname1 += fname1;
01461             }
01462         }
01463         else
01464             std::cout << "Missing parameter." <<std::endl;
01465 
01466         XrdOucString cmd;
01467         cmd = "xrdcp -s ";
01468         cmd += pathname1;
01469         cmd += pars;
01470         cmd += " -";
01471 
01472         int rt = system(cmd.c_str());
01473 
01474         std::cout << "cat returned " << rt <<std::endl <<std::endl;
01475     }
01476 }
01477 
01478 //------------------------------------------------------------------------------
01479 // process the "CP" command
01480 //------------------------------------------------------------------------------
01481 void executeCp(XrdOucTokenizer &tkzer)
01482 {
01483     int retval = 0;
01484 
01485     if (!genadmin)
01486     {
01487         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01488         retval = 1;
01489     }
01490 
01491     if (!retval)
01492     {
01493         char *fname1 = tkzer.GetToken(0, 0);
01494         char *fname2 = tkzer.GetToken(0, 0);
01495         char *tk;
01496         XrdOucString pars;
01497 
01498         while ((tk = tkzer.GetToken(0, 0)))
01499         {
01500             pars += " ";
01501             pars += tk;
01502         }
01503 
01504         XrdOucString pathname1, pathname2;
01505 
01506         if (fname1)
01507         {
01508             if ( (strstr(fname1, "root://") == fname1) ||
01509                     (strstr(fname1, "xroot://") == fname1) )
01510                 pathname1 = fname1;
01511             else if (fname1[0] == '/')
01512             {
01513                 pathname1 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01514                 pathname1 += "/";
01515                 pathname1 += fname1;
01516             }
01517             else
01518             {
01519                 pathname1 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01520                 pathname1 += "/";
01521                 pathname1 += currentpath;
01522                 pathname1 += "/";
01523                 pathname1 += fname1;
01524             }
01525 
01526             if (fname2)
01527             {
01528 
01529                 if ( (strstr(fname2, "root://") == fname2) ||
01530                         (strstr(fname2, "xroot://") == fname2) )
01531                     pathname2 = fname2;
01532                 else if (fname2[0] == '/')
01533                 {
01534                     pathname2 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01535                     pathname2 += "/";
01536                     pathname2 += fname2;
01537                 }
01538                 else
01539                 {
01540                     pathname2 = "root://" + genadmin->GetCurrentUrl().HostWPort;
01541                     pathname2 += "/";
01542                     pathname2 += currentpath;
01543                     pathname2 += "/";
01544                     pathname2 += fname2;
01545                 }
01546 
01547             }
01548             else
01549             {
01550                 std::cout << "Please provide command parameters: cp <source>";
01551                 std::cout << "<destination> [<params>]" <<std::endl <<std::endl;
01552             }
01553         }
01554         else
01555         {
01556             std::cout << "Please provide command parameters: cp <source> ";
01557             std::cout << "<destination> [<params>]" <<std::endl <<std::endl;
01558         }
01559 
01560         XrdOucString cmd;
01561         cmd = "xrdcp ";
01562         cmd += pathname1;
01563         cmd += " ";
01564         cmd += pathname2 + pars;
01565 
01566         int rt = system(cmd.c_str());
01567 
01568         std::cout << "cp returned " << rt <<std::endl <<std::endl;;
01569     }
01570 }
01571 
01572 //------------------------------------------------------------------------------
01573 // process the "QUERY" command
01574 //------------------------------------------------------------------------------
01575 void executeQuery(XrdOucTokenizer &tkzer)
01576 {
01577     int retval = 0;
01578 
01579     if (!genadmin)
01580     {
01581         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01582         retval = 1;
01583     }
01584 
01585     if (!retval)
01586     {
01587         char *reqcode = tkzer.GetToken(0, 0);
01588 
01589         if (!reqcode)
01590         {
01591             std::cout << "Please provide command parameters: query ";
01592             std::cout << "<reqcode> <params>" << std::endl << std::endl;
01593         }
01594         else
01595         {
01596 
01597             const kXR_char *args = (const kXR_char *)tkzer.GetToken(0, 0);
01598             kXR_char Resp[1024];
01599 
01600             genadmin->Query(atoi(reqcode), args, Resp, 1024);
01601 
01602             // Now check the answer
01603             if (!CheckAnswer(genadmin))
01604                 retval = 1;
01605 
01606             std::cout << Resp << std::endl << std::endl;
01607         }
01608     }
01609 }
01610 
01611 //------------------------------------------------------------------------------
01612 // process the "QUERYSPACE" command
01613 //------------------------------------------------------------------------------
01614 void executeQuerySpace(XrdOucTokenizer &tkzer)
01615 {
01616     int retval = 0;
01617 
01618     if (!genadmin)
01619     {
01620         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01621         retval = 1;
01622     }
01623 
01624     if (!retval)
01625     {
01626         char *ns = tkzer.GetToken(0, 0);
01627 
01628         if (!ns)
01629         {
01630             std::cout << "Please provide command parameters: query ";
01631             std::cout << "<logicalname>" << std::endl << std::endl;
01632         }
01633         else
01634         {
01635 
01636             long long totspace;
01637             long long totfree;
01638             long long totused;
01639             long long largestchunk;
01640 
01641             genadmin->GetSpaceInfo(ns, totspace, totfree, totused, largestchunk);
01642 
01643             // Now check the answer
01644             if (!CheckAnswer(genadmin))
01645                 retval = 1;
01646 
01647             std::cout << "Disk space approximations (MB):" << std::endl;
01648             std::cout << "Total         : " << totspace/(1024*1024) << std::endl;
01649             std::cout << "Free          : " << totfree/(1024*1024) << std::endl;
01650             std::cout << "Used          : " << totused/(1024*1024) << std::endl;
01651             std::cout << "Largest chunk : " << largestchunk/(1024*1024);
01652             std::cout << std::endl << std::endl;
01653         }
01654     }
01655 }
01656 
01657 //------------------------------------------------------------------------------
01658 // process the "DEGBUG" command
01659 //------------------------------------------------------------------------------
01660 void executeDebug(XrdOucTokenizer &tkzer)
01661 {
01662 
01663     int level = -2, retval = 0;
01664     string delim = "=";
01665     if (!genadmin)
01666     {
01667         std::cout << "Not connected to any server." <<std::endl <<std::endl;
01668         retval = 1;
01669     }
01670 
01671     if (!retval)
01672     {
01673         char *str = tkzer.GetToken(0, 0);
01674 
01675         if(str)
01676         {
01677             char *tok;
01678             tok = strtok(str,"=");
01679 
01680             if (!strcmp(tok, "-set-level"))
01681             {
01682                 tok = strtok(NULL,"=");
01683                 if (tok)
01684                     if (strspn(tok, "-0123456789") == strlen(tok))
01685                     {
01686                         level = atoi(tok);
01687                         std::cout << "The value of the debug level is: ";
01688                         std::cout << level <<std::endl <<std::endl;
01689                         EnvPutInt(NAME_DEBUG, level);
01690                         DebugSetLevel(EnvGetLong(NAME_DEBUG));
01691                         return;
01692                     }
01693             }
01694         }
01695     }
01696     std::cout << "Please provide correct parameters, eg. debug ";
01697     std::cout << "-set-level=<debug_level>" << std::endl << std::endl;
01698 }
01699 
01700 //------------------------------------------------------------------------------
01701 // Function lookup
01702 //------------------------------------------------------------------------------
01703 typedef void (*CommandCallback)(XrdOucTokenizer &);
01704 
01705 struct LookupItem
01706 {
01707     const char      *name;
01708     CommandCallback  callback;
01709 };
01710 
01711 LookupItem lookupTable[] =
01712 {
01713     {"cd",           executeCd          },
01714     {"envputint",    executeEnvPutInt   },
01715     {"envputstring", executeEnvPutString},
01716     {"help",         executeHelp        },
01717     {"connect",      executeConnect     },
01718     {"dirlistrec",   executeDirListRec  },
01719     {"dirlist",      executeDirList     },
01720     {"ls",           executeDirList     },
01721     {"locatesingle", executeLocateSingle},
01722     {"locateall",    executeLocateAll   },
01723     {"stat",         executeStat        },
01724     {"statvfs",      executeStatvfs     },
01725     {"existfile",    executeExistFile   },
01726     {"existdir",     executeExistDir    },
01727     {"getchecksum",  executeGetCheckSum },
01728     {"isfileonline", executeIsFileOnline},
01729     {"mv",           executeMv          },
01730     {"mkdir",        executeMkDir       },
01731     {"chmod",        executeChmod       },
01732     {"truncate",     executeTruncate    },
01733     {"rm",           executeRm          },
01734     {"rmdir",        executeRmDir       },
01735     {"prepare",      executePrepare     },
01736     {"cat",          executeCat         },
01737     {"cp",           executeCp          },
01738     {"query",        executeQuery       },
01739     {"queryspace",   executeQuerySpace  },
01740     {"debug",        executeDebug       },
01741     {0,              0                  }
01742 };
01743 
01744 CommandCallback lookup( char *command )
01745 {
01746     LookupItem *it = lookupTable;
01747     while( it->name != 0 )
01748     {
01749         if( strcmp( command, it->name ) == 0 )
01750             return it->callback;
01751         ++it;
01752     }
01753     return 0;
01754 }
01755 
01756 //------------------------------------------------------------------------------
01757 // Main program
01758 //------------------------------------------------------------------------------
01759 int main(int argc, char**argv)
01760 {
01761 
01762     int retval = 0;
01763 
01764     DebugSetLevel(0);
01765 
01766     // We want this tool to be able to connect everywhere
01767     // Note that the side effect of these calls here is to initialize the
01768     // XrdClient environment.
01769     // This is crucial if we want to later override its default values
01770     EnvPutString( NAME_REDIRDOMAINALLOW_RE, "*" );
01771     EnvPutString( NAME_CONNECTDOMAINALLOW_RE, "*" );
01772     EnvPutString( NAME_REDIRDOMAINDENY_RE, "" );
01773     EnvPutString( NAME_CONNECTDOMAINDENY_RE, "" );
01774 
01775     EnvPutInt( NAME_DEBUG, -1);
01776 
01777     //--------------------------------------------------------------------------
01778     // Parse the commandline
01779     //--------------------------------------------------------------------------
01780     for (int i=1; i < argc; i++)
01781     {
01782         if ( (strstr(argv[i], "-O") == argv[i]))
01783         {
01784             opaqueinfo=argv[i]+2;
01785             continue;
01786         }
01787 
01788         if ( (strstr(argv[i], "-h") == argv[i]) ||
01789                 (strstr(argv[i], "--help") == argv[i]) )
01790         {
01791             PrintUsage();
01792             exit(0);
01793             continue;
01794         }
01795 
01796         if ( (strstr(argv[i], "-DS") == argv[i]) &&
01797                 (argc >= i+2) )
01798         {
01799             std::cerr << "Overriding " << argv[i]+3 << " with value ";
01800             std::cerr << argv[i+1] << ". ";
01801             EnvPutString( argv[i]+3, argv[i+1] );
01802             std::cerr << " Final value: " << EnvGetString(argv[i]+3);
01803             std::cerr << std::endl;
01804             i++;
01805             continue;
01806         }
01807 
01808         if ( (strstr(argv[i], "-DI") == argv[i]) &&
01809                 (argc >= i+2) )
01810         {
01811             std::cerr << "Overriding '" << argv[i]+3 << "' with value ";
01812             std::cerr << argv[i+1] << ". ";
01813             EnvPutInt( argv[i]+3, atoi(argv[i+1]) );
01814             std::cerr << " Final value: " << EnvGetLong(argv[i]+3);
01815             std::cerr << std::endl;
01816             i++;
01817             continue;
01818         }
01819 
01820         // Any other par is ignored
01821         if ( (strstr(argv[i], "-") == argv[i]) && (strlen(argv[i]) > 1) )
01822         {
01823             std::cerr << "Unknown parameter " << argv[i] << std::endl;
01824             continue;
01825         }
01826 
01827         if (!initialhost) initialhost = argv[i];
01828         else
01829         {
01830             cmdline_cmd += argv[i];
01831             cmdline_cmd += " ";
01832         }
01833     }
01834 
01835 
01836     //--------------------------------------------------------------------------
01837     // Initialize the client
01838     //--------------------------------------------------------------------------
01839     DebugSetLevel(EnvGetLong(NAME_DEBUG));
01840 
01841     // if there's no command to execute from the cmdline...
01842     if (cmdline_cmd.length() == 0)
01843     {
01844         std::cout << XRDCLI_VERSION << std::endl;
01845         std::cout << "Welcome to the xrootd command line interface.";
01846         std::cout << std::endl;
01847         std::cout << "Type 'help' for a list of available commands.";
01848         std::cout << std::endl;
01849     }
01850 
01851     if (initialhost)
01852     {
01853         XrdOucString s = "root://";
01854         s += initialhost;
01855         s += "//dummy";
01856         genadmin = new XrdClientAdmin(s.c_str());
01857 
01858         // Then connect
01859         if (!genadmin->Connect())
01860         {
01861             delete genadmin;
01862             genadmin = 0;
01863         }
01864     }
01865 
01866     //--------------------------------------------------------------------------
01867     // Get the command from the std input
01868     //--------------------------------------------------------------------------
01869     while( true )
01870     {
01871         //----------------------------------------------------------------------
01872         // Parse the string
01873         //----------------------------------------------------------------------
01874         stringstream prompt;
01875         char *linebuf=0;
01876 
01877         if (cmdline_cmd.length() == 0)
01878         {
01879             BuildPrompt(prompt);
01880             linebuf = readline(prompt.str().c_str());
01881             if( !linebuf )
01882             {
01883                 std::cout << "Goodbye." << std::endl << std::endl;
01884                 break;
01885             }
01886             if( ! *linebuf)
01887             {
01888                 free(linebuf);
01889                 continue;
01890             }
01891 #ifdef HAVE_READLINE
01892             add_history(linebuf);
01893 #endif
01894         }
01895         else linebuf = strdup(cmdline_cmd.c_str());
01896 
01897         XrdOucTokenizer tkzer(linebuf);
01898         if (!tkzer.GetLine()) continue;
01899 
01900         char *cmd = tkzer.GetToken(0, 1);
01901 
01902         if (!cmd) continue;
01903 
01904         //----------------------------------------------------------------------
01905         // Execute the command
01906         //----------------------------------------------------------------------
01907         CommandCallback callback = lookup( cmd );
01908         if( !callback )
01909         {
01910             if( strcmp( cmd, "exit" ) == 0 )
01911             {
01912                 std::cout << "Goodbye." << std::endl << std::endl;
01913                 free( linebuf );
01914                 break;
01915             }
01916             std::cout << "Command not recognized." << std::endl;
01917             std::cout << "Type \"help\" for a list of commands.";
01918             std::cout <<std::endl <<std::endl;
01919         }
01920         else
01921             (*callback)( tkzer );
01922 
01923 
01924         free( linebuf );
01925 
01926         // if it was a cmd from the commandline...
01927         if (cmdline_cmd.length() > 0) break;
01928 
01929     }
01930 
01931     if (genadmin)
01932         delete genadmin;
01933 
01934     return retval;
01935 }

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