Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:52

0001 #define ML_DEBUG 1
0002 #include "Utilities/StorageFactory/interface/StorageMaker.h"
0003 #include "Utilities/StorageFactory/interface/StorageMakerFactory.h"
0004 #include "Utilities/StorageFactory/interface/StorageFactory.h"
0005 #include "Utilities/StorageFactory/interface/File.h"
0006 #include "FWCore/Utilities/interface/EDMException.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include <cstdlib>
0009 #include <cstdio>
0010 #include <sys/stat.h>
0011 
0012 namespace edm::storage {
0013   class StormLcgGtStorageMaker : public StorageMaker {
0014     /* getTURL: Executes lcg-gt and extracts the physical file path */
0015     std::string getTURL(const std::string &surl) const {
0016       // PrepareToGet timeout
0017       std::string timeout("300");
0018       if (char *p = std::getenv("CMS_STORM_LCG_GT_TIMEOUT"))
0019         timeout = p;
0020 
0021       /* Build the command line:
0022     -b => no BDII contacted
0023     -T srmv2 => necessary with -b 
0024     -t timeout */
0025       std::string comm("lcg-gt -b -T srmv2 -t " + timeout + " srm:" + surl + " file 2>&1");
0026       LogDebug("StormLCGStorageMaker") << "command: " << comm << std::endl;
0027 
0028       FILE *pipe = popen(comm.c_str(), "r");
0029       if (!pipe)
0030         throw cms::Exception("StormLCGStorageMaker") << "failed to execute lcg-gt command: " << comm;
0031 
0032       // Get output
0033       int ch;
0034       std::string output;
0035       while ((ch = getc(pipe)) != EOF)
0036         output.push_back(ch);
0037       pclose(pipe);
0038 
0039       LogDebug("StormLCGStorageMaker") << "output: " << output << std::endl;
0040 
0041       // Extract TURL if possible.
0042       size_t start = output.find("file:", 0);
0043       if (start == std::string::npos)
0044         throw cms::Exception("StormLCGStorageMaker") << "no turl found in command '" << comm << "' output:\n" << output;
0045 
0046       start += 5;
0047       std::string turl(output, start, output.find_first_of('\n', start) - start);
0048       LogDebug("StormLCGStorageMaker") << "file to open: " << turl << std::endl;
0049       return turl;
0050     }
0051 
0052   public:
0053     std::unique_ptr<Storage> open(const std::string &proto,
0054                                   const std::string &surl,
0055                                   int mode,
0056                                   const AuxSettings &) const override {
0057       const StorageFactory *f = StorageFactory::get();
0058       StorageFactory::ReadHint readHint = f->readHint();
0059       StorageFactory::CacheHint cacheHint = f->cacheHint();
0060 
0061       if (readHint != StorageFactory::READ_HINT_UNBUFFERED || cacheHint == StorageFactory::CACHE_HINT_STORAGE)
0062         mode &= ~IOFlags::OpenUnbuffered;
0063       else
0064         mode |= IOFlags::OpenUnbuffered;
0065 
0066       std::string path = getTURL(surl);
0067       auto file = std::make_unique<File>(path, mode);
0068       return f->wrapNonLocalFile(std::move(file), proto, path, mode);
0069     }
0070 
0071     bool check(const std::string & /*proto*/,
0072                const std::string &path,
0073                const AuxSettings &,
0074                IOOffset *size = nullptr) const override {
0075       struct stat st;
0076       if (stat(getTURL(path).c_str(), &st) != 0)
0077         return false;
0078 
0079       if (size)
0080         *size = st.st_size;
0081 
0082       return true;
0083     }
0084   };
0085 }  // namespace edm::storage
0086 
0087 using namespace edm::storage;
0088 DEFINE_EDM_PLUGIN(StorageMakerFactory, StormLcgGtStorageMaker, "storm-lcg");