CMS 3D CMS Logo

StormLCGStorageMaker.cc

Go to the documentation of this file.
00001 #define ML_DEBUG 1
00002 #include "Utilities/StorageFactory/interface/StorageMaker.h"
00003 #include "Utilities/StorageFactory/interface/StorageMakerFactory.h"
00004 #include "Utilities/StorageFactory/interface/StorageFactory.h"
00005 #include "Utilities/StorageFactory/interface/File.h"
00006 #include "FWCore/Utilities/interface/EDMException.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include <cstdlib>
00009 #include <sys/stat.h>
00010 
00011 class StormLcgGtStorageMaker : public StorageMaker
00012 {
00013   /* getTURL: Executes lcg-gt and extracts the physical file path */
00014   std::string getTURL (const std::string &surl)
00015   {
00016     // PrepareToGet timeout  
00017     std::string timeout("300");
00018     if(char *p = getenv("CMS_STORM_LCG_GT_TIMEOUT"))
00019       timeout = p;
00020 
00021     /* Build the command line:
00022         -b => no BDII contacted
00023         -T srmv2 => necessary with -b 
00024         -t timeout */
00025     std::string comm("lcg-gt -b -T srmv2 -t " + timeout + " srm:" + surl + " file 2>&1"); 
00026     LogDebug("StormLCGStorageMaker") << "command: " << comm << std::endl;
00027 
00028     FILE *pipe = popen(comm.c_str(), "r");
00029     if(! pipe)
00030       throw cms::Exception("StormLCGStorageMaker")
00031         << "failed to execute lcg-gt command: "
00032         << comm;
00033 
00034     // Get output
00035     int ch;
00036     std::string output;
00037     while ((ch = getc(pipe)) != EOF)
00038       output.push_back(ch);
00039     pclose(pipe);
00040 
00041     LogDebug("StormLCGStorageMaker") << "output: " << output << std::endl;
00042  
00043     // Extract TURL if possible.
00044     size_t start = output.find("file:", 0);
00045     if (start == std::string::npos)
00046       throw cms::Exception("StormLCGStorageMaker")
00047         << "no turl found in command '" << comm << "' output:\n" << output;
00048 
00049     start += 5;
00050     std::string turl(output, start, output.find_first_of("\n", start) - start); 
00051     LogDebug("StormLCGStorageMaker") << "file to open: " << turl << std::endl;
00052     return turl;
00053   }
00054 
00055 
00056 public:
00057   virtual Storage *open (const std::string &proto,
00058                          const std::string &surl,
00059                          int mode,
00060                          const std::string &tmpdir)
00061   {
00062     StorageFactory *f = StorageFactory::get();
00063     StorageFactory::ReadHint readHint = f->readHint();
00064     StorageFactory::CacheHint cacheHint = f->cacheHint();
00065 
00066     if (readHint != StorageFactory::READ_HINT_UNBUFFERED
00067         || cacheHint == StorageFactory::CACHE_HINT_STORAGE)
00068       mode &= ~IOFlags::OpenUnbuffered;
00069     else
00070       mode |= IOFlags::OpenUnbuffered;
00071 
00072     return new File (getTURL(surl), mode); 
00073   }
00074 
00075   virtual bool check (const std::string &proto,
00076                       const std::string &path,
00077                       IOOffset *size = 0)
00078   {
00079     struct stat st;
00080     if (stat (getTURL(path).c_str(), &st) != 0)
00081       return false;
00082 
00083     if (size)
00084       *size = st.st_size;
00085 
00086     return true;
00087   }
00088 };
00089 
00090 DEFINE_EDM_PLUGIN (StorageMakerFactory, StormLcgGtStorageMaker, "storm-lcg");

Generated on Tue Jun 9 17:48:53 2009 for CMSSW by  doxygen 1.5.4