CMS 3D CMS Logo

StormStorageMaker.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 StormStorageMaker : public StorageMaker
00012 {
00013   /* getTURL: Executes a prepare to get script and extracts the physical file path */
00014   std::string getTURL (const std::string &surl)
00015   {
00016     std::string client;
00017     if (char *p = getenv("CMS_STORM_PTG_CLIENT"))
00018       client = p;
00019     else
00020       throw cms::Exception("StormStorageMaker")
00021         << "$CMS_STORM_PTG_CLIENT has no value";
00022 
00023     // Command
00024     std::string comm(client + " srm:" + surl + " 2>&1"); 
00025     LogDebug("StormStorageMaker") << "command: " << comm << std::endl;
00026 
00027     FILE *pipe = popen(comm.c_str(), "r");
00028     if(! pipe)
00029       throw cms::Exception("StormStorageMaker")
00030         << "failed to execute PtG command: "
00031         << comm;
00032 
00033     // Get output
00034     int ch;
00035     std::string output;
00036     while ((ch = getc(pipe)) != EOF)
00037       output.push_back(ch);
00038     pclose(pipe);
00039 
00040     LogDebug("StormStorageMaker") << "output: " << output << std::endl;
00041  
00042     // Extract TURL if possible.
00043     size_t start = output.find("FilePath:", 0);
00044     if (start == std::string::npos)
00045       throw cms::Exception("StormStorageMaker")
00046         << "no turl found in command '" << comm << "' output:\n" << output;
00047 
00048     start += 9;
00049     std::string turl(output, start, output.find_first_of("\n", start) - start); 
00050     LogDebug("StormStorageMaker") << "file to open: " << turl << std::endl;
00051     return turl;
00052   }
00053 
00054 
00055 public:
00056   virtual Storage *open (const std::string &proto,
00057                          const std::string &surl,
00058                          int mode,
00059                          const std::string &tmpdir)
00060   {
00061     StorageFactory *f = StorageFactory::get();
00062     StorageFactory::ReadHint readHint = f->readHint();
00063     StorageFactory::CacheHint cacheHint = f->cacheHint();
00064 
00065     if (readHint != StorageFactory::READ_HINT_UNBUFFERED
00066         || cacheHint == StorageFactory::CACHE_HINT_STORAGE)
00067       mode &= ~IOFlags::OpenUnbuffered;
00068     else
00069       mode |= IOFlags::OpenUnbuffered;
00070 
00071     return new File (getTURL(surl), mode); 
00072   }
00073 
00074   virtual bool check (const std::string &proto,
00075                       const std::string &path,
00076                       IOOffset *size = 0)
00077   {
00078     struct stat st;
00079     if (stat (getTURL(path).c_str(), &st) != 0)
00080       return false;
00081 
00082     if (size)
00083       *size = st.st_size;
00084 
00085     return true;
00086   }
00087 };
00088 
00089 DEFINE_EDM_PLUGIN (StorageMakerFactory, StormStorageMaker, "storm");

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