CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Utilities/StorageFactory/plugins/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 <cstdio>
00010 #include <sys/stat.h>
00011 
00012 class StormStorageMaker : public StorageMaker
00013 {
00014   /* getTURL: Executes a prepare to get script and extracts the physical file path */
00015   std::string getTURL (const std::string &surl)
00016   {
00017     std::string client;
00018     if (char *p = getenv("CMS_STORM_PTG_CLIENT"))
00019       client = p;
00020     else
00021       throw cms::Exception("StormStorageMaker")
00022         << "$CMS_STORM_PTG_CLIENT has no value";
00023 
00024     // Command
00025     std::string comm(client + " srm:" + surl + " 2>&1"); 
00026     LogDebug("StormStorageMaker") << "command: " << comm << std::endl;
00027 
00028     FILE *pipe = popen(comm.c_str(), "r");
00029     if(! pipe)
00030       throw cms::Exception("StormStorageMaker")
00031         << "failed to execute PtG 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("StormStorageMaker") << "output: " << output << std::endl;
00042  
00043     // Extract TURL if possible.
00044     size_t start = output.find("FilePath:", 0);
00045     if (start == std::string::npos)
00046       throw cms::Exception("StormStorageMaker")
00047         << "no turl found in command '" << comm << "' output:\n" << output;
00048 
00049     start += 9;
00050     std::string turl(output, start, output.find_first_of("\n", start) - start); 
00051     LogDebug("StormStorageMaker") << "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   {
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     std::string path = getTURL(surl);
00072     File *file = new File (path, mode); 
00073     return f->wrapNonLocalFile (file, proto, path, mode);
00074   }
00075 
00076   virtual bool check (const std::string &/*proto*/,
00077                       const std::string &path,
00078                       IOOffset *size = 0)
00079   {
00080     struct stat st;
00081     if (stat (getTURL(path).c_str(), &st) != 0)
00082       return false;
00083 
00084     if (size)
00085       *size = st.st_size;
00086 
00087     return true;
00088   }
00089 };
00090 
00091 DEFINE_EDM_PLUGIN (StorageMakerFactory, StormStorageMaker, "storm");