Back to index

nordugrid-arc-nox  1.1.0~rc6
arcls.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <iostream>
00008 #include <string>
00009 #include <list>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/Logger.h>
00013 #include <arc/StringConv.h>
00014 #include <arc/URL.h>
00015 #include <arc/UserConfig.h>
00016 #include <arc/credential/Credential.h>
00017 #include <arc/data/DataHandle.h>
00018 #include <arc/OptionParser.h>
00019 
00020 static Arc::Logger logger(Arc::Logger::getRootLogger(), "arcls");
00021 
00022 bool arcls(const Arc::URL& dir_url,
00023            const Arc::UserConfig& usercfg,
00024            bool show_details,
00025            bool show_urls,
00026            bool show_meta,
00027            int recursion,
00028            int timeout) {
00029   if (!dir_url) {
00030     logger.msg(Arc::ERROR, "Invalid URL: %s", dir_url.fullstr());
00031     return false;
00032   }
00033   if (dir_url.Protocol() == "urllist") {
00034     std::list<Arc::URL> dirs = Arc::ReadURLList(dir_url);
00035     if (dirs.size() == 0) {
00036       logger.msg(Arc::ERROR, "Can't read list of locations from file %s",
00037                  dir_url.Path());
00038       return false;
00039     }
00040     bool r = true;
00041     for (std::list<Arc::URL>::iterator dir = dirs.begin();
00042          dir != dirs.end(); dir++)
00043       if(!arcls(*dir, usercfg, show_details, show_urls, show_meta,
00044                recursion, timeout)) r = false;
00045     return r;
00046   }
00047 
00048   if (dir_url.IsSecureProtocol() && !Arc::Credential::IsCredentialsValid(usercfg)) {
00049     logger.msg(Arc::ERROR, "Unable to list content of %s: No valid credentials found", dir_url.str());
00050     return false;
00051   }
00052 
00053   Arc::DataHandle url(dir_url, usercfg);
00054   if (!url) {
00055     logger.msg(Arc::ERROR, "Unsupported url given");
00056     return false;
00057   }
00058   std::list<Arc::FileInfo> files;
00059   url->SetSecure(false);
00060   Arc::DataStatus res = url->ListFiles(files, (show_details || recursion > 0), show_urls, show_meta);
00061   if (!res.Passed()) {
00062     if (files.size() == 0) {
00063       logger.msg(Arc::ERROR, "Failed listing metafiles");
00064       if (res.Retryable())
00065         logger.msg(Arc::ERROR, "This seems like a temporary error, please try again later");
00066       return false;
00067     }
00068     logger.msg(Arc::INFO, "Warning: "
00069                "Failed listing metafiles but some information is obtained");
00070   }
00071   for (std::list<Arc::FileInfo>::iterator i = files.begin();
00072        i != files.end(); i++) {
00073     if(!show_meta || show_details || show_urls) std::cout << i->GetName();
00074     if (show_details) {
00075       switch (i->GetType()) {
00076       case Arc::FileInfo::file_type_file:
00077         std::cout << " file";
00078         break;
00079 
00080       case Arc::FileInfo::file_type_dir:
00081         std::cout << " dir";
00082         break;
00083 
00084       default:
00085         std::cout << " unknown";
00086         break;
00087       }
00088       if (i->CheckSize())
00089         std::cout << " " << i->GetSize();
00090       else
00091         std::cout << " *";
00092       if (i->CheckCreated())
00093         std::cout << " " << i->GetCreated();
00094       else
00095         std::cout << " *";
00096       if (i->CheckValid())
00097         std::cout << " " << i->GetValid();
00098       else
00099         std::cout << " *";
00100       if (i->CheckCheckSum())
00101         std::cout << " " << i->GetCheckSum();
00102       else
00103         std::cout << " *";
00104       if (i->CheckLatency())
00105         std::cout << " " << i->GetLatency();
00106     }
00107     if(!show_meta || show_details || show_urls) std::cout << std::endl;
00108     if (show_urls)
00109       for (std::list<Arc::URL>::const_iterator u = i->GetURLs().begin();
00110            u != i->GetURLs().end(); u++)
00111         std::cout << "\t" << *u << std::endl;
00112     if (show_meta) {
00113       std::map<std::string, std::string> md = i->GetMetaData();
00114       for (std::map<std::string, std::string>::iterator mi = md.begin(); mi != md.end(); ++mi)
00115         std::cout<<mi->first<<":"<<mi->second<<std::endl;
00116     }
00117     // Do recursion
00118     else if (recursion > 0)
00119       if (i->GetType() == Arc::FileInfo::file_type_dir) {
00120         Arc::URL suburl = dir_url;
00121         if (suburl.Path()[suburl.Path().length() - 1] != '/')
00122           suburl.ChangePath(suburl.Path() + "/" + i->GetName());
00123         else
00124           suburl.ChangePath(suburl.Path() + i->GetName());
00125         std::cout << suburl.str() << ":" << std::endl;
00126         arcls(suburl, usercfg, show_details, show_urls, show_meta, recursion - 1, timeout);
00127         std::cout << std::endl;
00128       }
00129   }
00130   return true;
00131 }
00132 
00133 int main(int argc, char **argv) {
00134 
00135   setlocale(LC_ALL, "");
00136 
00137   Arc::LogStream logcerr(std::cerr);
00138   logcerr.setFormat(Arc::ShortFormat);
00139   Arc::Logger::getRootLogger().addDestination(logcerr);
00140   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00141 
00142   Arc::ArcLocation::Init(argv[0]);
00143 
00144   Arc::OptionParser options(istring("url"),
00145                             istring("The arcls command is used for listing "
00146                                     "files in grid storage elements "
00147                                     "and file\nindex catalogues."));
00148 
00149   bool longlist = false;
00150   options.AddOption('l', "long", istring("long format (more information)"),
00151                     longlist);
00152 
00153   bool locations = false;
00154   options.AddOption('L', "locations", istring("show URLs of file locations"),
00155                     locations);
00156 
00157   bool metadata = false;
00158   options.AddOption('m', "metadata", istring("display all available metadata"),
00159         metadata);
00160 
00161   int recursion = 0;
00162   options.AddOption('r', "recursive",
00163                     istring("operate recursively up to specified level"),
00164                     istring("level"), recursion);
00165 
00166   int timeout = 20;
00167   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00168                     istring("seconds"), timeout);
00169 
00170   std::string conffile;
00171   options.AddOption('z', "conffile",
00172                     istring("configuration file (default ~/.arc/client.conf)"),
00173                     istring("filename"), conffile);
00174 
00175   std::string debug;
00176   options.AddOption('d', "debug",
00177                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00178                     istring("debuglevel"), debug);
00179 
00180   bool version = false;
00181   options.AddOption('v', "version", istring("print version information"),
00182                     version);
00183 
00184   std::list<std::string> params = options.Parse(argc, argv);
00185 
00186   // If debug is specified as argument, it should be set before loading the configuration.
00187   if (!debug.empty())
00188     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00189 
00190   Arc::UserConfig usercfg(conffile);
00191   if (!usercfg) {
00192     logger.msg(Arc::ERROR, "Failed configuration initialization");
00193     return 1;
00194   }
00195   usercfg.UtilsDirPath(Arc::UserConfig::ARCUSERDIRECTORY);
00196 
00197   if (debug.empty() && !usercfg.Verbosity().empty())
00198     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00199 
00200   if (version) {
00201     std::cout << Arc::IString("%s version %s", "arcls", VERSION) << std::endl;
00202     return 0;
00203   }
00204 
00205   if (params.size() != 1) {
00206     logger.msg(Arc::ERROR, "Wrong number of parameters specified");
00207     return 1;
00208   }
00209 
00210   std::list<std::string>::iterator it = params.begin();
00211 
00212   if(!arcls(*it, usercfg, longlist, locations, metadata, recursion, timeout))
00213     return 1;
00214 
00215   return 0;
00216 }