Back to index

nordugrid-arc-nox  1.1.0~rc6
arcrm.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 <string>
00008 #include <list>
00009 
00010 #include <arc/ArcLocation.h>
00011 #include <arc/Logger.h>
00012 #include <arc/StringConv.h>
00013 #include <arc/URL.h>
00014 #include <arc/UserConfig.h>
00015 #include <arc/credential/Credential.h>
00016 #include <arc/data/DataHandle.h>
00017 #include <arc/data/DataMover.h>
00018 #include <arc/OptionParser.h>
00019 
00020 static Arc::Logger logger(Arc::Logger::getRootLogger(), "arcrm");
00021 
00022 bool arcrm(const Arc::URL& file_url,
00023            const Arc::UserConfig& usercfg,
00024            bool errcont,
00025            int timeout) {
00026   if (!file_url) {
00027     logger.msg(Arc::ERROR, "Invalid URL: %s", file_url.str());
00028     return false;
00029   }
00030   if (file_url.Protocol() == "filelist") {
00031     std::list<Arc::URL> files = Arc::ReadURLList(file_url);
00032     if (files.size() == 0) {
00033       logger.msg(Arc::ERROR, "Can't read list of locations from file %s",
00034                  file_url.Path());
00035       return false;
00036     }
00037     bool r = true;
00038     for (std::list<Arc::URL>::iterator file = files.begin();
00039          file != files.end(); file++) {
00040       if (!arcrm(*file, usercfg, errcont, timeout))
00041         r = false;
00042     }
00043     return r;
00044   }
00045 
00046   if (file_url.IsSecureProtocol() && !Arc::Credential::IsCredentialsValid(usercfg)) {
00047     logger.msg(Arc::ERROR, "Unable to remove file (%s): No valid credentials found", file_url.str());
00048     return false;
00049   }
00050 
00051   Arc::DataHandle url(file_url, usercfg);
00052   if (!url) {
00053     logger.msg(Arc::ERROR, "Unsupported url given");
00054     return false;
00055   }
00056   // only one try
00057   url->SetTries(1);
00058   Arc::DataMover mover;
00059   Arc::DataStatus res =  mover.Delete(*url,errcont);
00060   if (!res.Passed()) {
00061     logger.msg(Arc::ERROR, "Delete failed: %s", std::string(res));
00062     if (res.Retryable())
00063       logger.msg(Arc::ERROR, "This seems like a temporary error, please try again later");
00064     return false;
00065   }
00066   return true;
00067 }
00068 
00069 int main(int argc, char **argv) {
00070 
00071   setlocale(LC_ALL, "");
00072 
00073   Arc::LogStream logcerr(std::cerr);
00074   logcerr.setFormat(Arc::ShortFormat);
00075   Arc::Logger::getRootLogger().addDestination(logcerr);
00076   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00077 
00078   Arc::ArcLocation::Init(argv[0]);
00079 
00080   Arc::OptionParser options(istring("url"),
00081                             istring("The arcrm command deletes files and on "
00082                                     "grid storage elements."));
00083 
00084   bool force = false;
00085   options.AddOption('f', "force",
00086                     istring("remove logical file name registration even "
00087                             "if not all physical instances were removed"),
00088                     force);
00089 
00090   int timeout = 20;
00091   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00092                     istring("seconds"), timeout);
00093 
00094   std::string conffile;
00095   options.AddOption('z', "conffile",
00096                     istring("configuration file (default ~/.arc/client.conf)"),
00097                     istring("filename"), conffile);
00098 
00099   std::string debug;
00100   options.AddOption('d', "debug",
00101                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00102                     istring("debuglevel"), debug);
00103 
00104   bool version = false;
00105   options.AddOption('v', "version", istring("print version information"),
00106                     version);
00107 
00108   std::list<std::string> params = options.Parse(argc, argv);
00109 
00110   // If debug is specified as argument, it should be set before loading the configuration.
00111   if (!debug.empty())
00112     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00113 
00114   Arc::UserConfig usercfg(conffile);
00115   if (!usercfg) {
00116     logger.msg(Arc::ERROR, "Failed configuration initialization");
00117     return 1;
00118   }
00119   usercfg.UtilsDirPath(Arc::UserConfig::ARCUSERDIRECTORY);
00120 
00121   if (debug.empty() && !usercfg.Verbosity().empty())
00122     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00123 
00124   if (version) {
00125     std::cout << Arc::IString("%s version %s", "arcrm", VERSION) << std::endl;
00126     return 0;
00127   }
00128 
00129   if (params.size() != 1) {
00130     logger.msg(Arc::ERROR, "Wrong number of parameters specified");
00131     return 1;
00132   }
00133 
00134   std::list<std::string>::iterator it = params.begin();
00135   if (!arcrm(*it, usercfg, force, timeout))
00136     return 1;
00137 
00138   return 0;
00139 }