Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-01 22:40:44

0001 
0002 #include <iostream>
0003 #include <fstream>
0004 #include <string>
0005 #include <vector>
0006 #include <exception>
0007 #include <filesystem>
0008 
0009 #include <boost/program_options.hpp>
0010 
0011 #include "TFile.h"
0012 #include "TError.h"
0013 
0014 #include "Utilities/StorageFactory/interface/Storage.h"
0015 #include "Utilities/StorageFactory/interface/StorageFactory.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017 #include "FWCore/Services/interface/setupSiteLocalConfig.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Catalog/interface/InputFileCatalog.h"
0020 #include "FWCore/Catalog/interface/SiteLocalConfig.h"
0021 
0022 static int copy_files(const boost::program_options::variables_map& vm) {
0023   auto operate = edm::setupSiteLocalConfig();
0024 
0025   auto in = (vm.count("file") ? vm["file"].as<std::vector<std::string> >() : std::vector<std::string>());
0026 
0027   if (in.size() < 2) {
0028     std::cerr << "Not enough arguments!" << std::endl;
0029     std::cerr << "Usage: edmCopyUtil [file1] [file2] [file3] dest_dir" << std::endl;
0030     return 1;
0031   }
0032 
0033   std::filesystem::path destdir(in.back());
0034   in.pop_back();
0035 
0036   if (!std::filesystem::is_directory(destdir)) {
0037     std::cerr << "Last argument must be destination directory; " << destdir << " is not a directory!" << std::endl;
0038     return 1;
0039   }
0040 
0041   std::string catalogIn = (vm.count("catalog") ? vm["catalog"].as<std::string>() : std::string());
0042   edm::InputFileCatalog catalog(in, catalogIn, true);
0043   std::vector<std::string> const& filesIn = catalog.fileNames(0);
0044 
0045   for (unsigned int j = 0; j < in.size(); ++j) {
0046     std::filesystem::path pathOut = destdir;
0047     pathOut /= std::filesystem::path(in[j]).filename();
0048 
0049     std::ofstream ofs;
0050     ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0051     ofs.open(pathOut);
0052 
0053     std::unique_ptr<edm::storage::Storage> s = edm::storage::StorageFactory::get()->open(filesIn[j]);
0054     assert(s);  // StorageFactory should throw if file open fails.
0055 
0056     static unsigned int const COPYBUFSIZE = 10 * 1024 * 1024;  // 10MB buffer
0057     std::vector<char> buffer;
0058     buffer.reserve(COPYBUFSIZE);
0059 
0060     edm::storage::IOSize n;
0061     while ((n = s->read(&buffer[0], COPYBUFSIZE))) {  // Note Storage throws on error
0062       ofs.write(&buffer[0], n);
0063     }
0064     ofs.close();
0065     s->close();
0066   }
0067 
0068   return 0;
0069 }
0070 
0071 int main(int argc, char* argv[]) {
0072   gErrorIgnoreLevel = kError;
0073 
0074   boost::program_options::options_description desc("Allowed options");
0075   desc.add_options()("help,h", "print help message")(
0076       "catalog,c", boost::program_options::value<std::string>(), "catalog");
0077   boost::program_options::options_description hidden("Hidden options");
0078   hidden.add_options()("file", boost::program_options::value<std::vector<std::string> >(), "files to transfer");
0079 
0080   boost::program_options::positional_options_description p;
0081   p.add("file", -1);
0082 
0083   boost::program_options::options_description cmdline_options;
0084   cmdline_options.add(desc).add(hidden);
0085 
0086   boost::program_options::variables_map vm;
0087 
0088   try {
0089     boost::program_options::store(
0090         boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(p).run(), vm);
0091   } catch (boost::program_options::error const& x) {
0092     std::cerr << "Option parsing failure:\n" << x.what() << "\n\n";
0093     std::cerr << desc << "\n";
0094     return 1;
0095   }
0096 
0097   boost::program_options::notify(vm);
0098 
0099   if (vm.count("help")) {
0100     std::cout << desc << "\n";
0101     return 1;
0102   }
0103 
0104   int rc;
0105   try {
0106     rc = copy_files(vm);
0107   } catch (cms::Exception const& e) {
0108     std::cout << "cms::Exception caught in "
0109               << "EdmFileUtil" << '\n'
0110               << e.explainSelf();
0111     rc = 1;
0112   } catch (std::exception const& e) {
0113     std::cout << "Standard library exception caught in "
0114               << "EdmFileUtil" << '\n'
0115               << e.what();
0116     rc = 1;
0117   } catch (...) {
0118     std::cout << "Unknown exception caught in "
0119               << "EdmFileUtil";
0120     rc = 2;
0121   }
0122   return rc;
0123 }