Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-01 22:41:11

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 StormStorageMaker : public StorageMaker {
0014     /* getTURL: Executes a prepare to get script and extracts the physical file path */
0015     std::string getTURL(const std::string &surl) const {
0016       std::string client;
0017       if (char *p = std::getenv("CMS_STORM_PTG_CLIENT"))
0018         client = p;
0019       else
0020         throw cms::Exception("StormStorageMaker") << "$CMS_STORM_PTG_CLIENT has no value";
0021 
0022       // Command
0023       std::string comm(client + " srm:" + surl + " 2>&1");
0024       LogDebug("StormStorageMaker") << "command: " << comm << std::endl;
0025 
0026       FILE *pipe = popen(comm.c_str(), "r");
0027       if (!pipe)
0028         throw cms::Exception("StormStorageMaker") << "failed to execute PtG command: " << comm;
0029 
0030       // Get output
0031       int ch;
0032       std::string output;
0033       while ((ch = getc(pipe)) != EOF)
0034         output.push_back(ch);
0035       pclose(pipe);
0036 
0037       LogDebug("StormStorageMaker") << "output: " << output << std::endl;
0038 
0039       // Extract TURL if possible.
0040       size_t start = output.find("FilePath:", 0);
0041       if (start == std::string::npos)
0042         throw cms::Exception("StormStorageMaker") << "no turl found in command '" << comm << "' output:\n" << output;
0043 
0044       start += 9;
0045       std::string turl(output, start, output.find_first_of('\n', start) - start);
0046       LogDebug("StormStorageMaker") << "file to open: " << turl << std::endl;
0047       return turl;
0048     }
0049 
0050   public:
0051     std::unique_ptr<Storage> open(const std::string &proto,
0052                                   const std::string &surl,
0053                                   int mode,
0054                                   const AuxSettings &) const override {
0055       const StorageFactory *f = StorageFactory::get();
0056       StorageFactory::ReadHint readHint = f->readHint();
0057       StorageFactory::CacheHint cacheHint = f->cacheHint();
0058 
0059       if (readHint != StorageFactory::READ_HINT_UNBUFFERED || cacheHint == StorageFactory::CACHE_HINT_STORAGE)
0060         mode &= ~IOFlags::OpenUnbuffered;
0061       else
0062         mode |= IOFlags::OpenUnbuffered;
0063 
0064       std::string path = getTURL(surl);
0065       auto file = std::make_unique<File>(path, mode);
0066       return f->wrapNonLocalFile(std::move(file), proto, path, mode);
0067     }
0068 
0069     bool check(const std::string & /*proto*/,
0070                const std::string &path,
0071                const AuxSettings &,
0072                IOOffset *size = nullptr) const override {
0073       struct stat st;
0074       if (stat(getTURL(path).c_str(), &st) != 0)
0075         return false;
0076 
0077       if (size)
0078         *size = st.st_size;
0079 
0080       return true;
0081     }
0082   };
0083 }  // namespace edm::storage
0084 
0085 using namespace edm::storage;
0086 DEFINE_EDM_PLUGIN(StorageMakerFactory, StormStorageMaker, "storm");