CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Utilities/DCacheAdaptor/plugins/DCacheStorageMaker.cc

Go to the documentation of this file.
00001 #include "Utilities/StorageFactory/interface/StorageMaker.h"
00002 #include "Utilities/StorageFactory/interface/StorageMakerFactory.h"
00003 #include "Utilities/StorageFactory/interface/StorageFactory.h"
00004 #include "Utilities/DCacheAdaptor/interface/DCacheFile.h"
00005 #include "FWCore/Utilities/interface/Exception.h"
00006 #include <unistd.h>
00007 #include <dcap.h>
00008 
00009 class DCacheStorageMaker : public StorageMaker
00010 {
00015   static std::string normalise (const std::string &proto, const std::string &path)
00016   {
00017     size_t p = path.find("/pnfs");
00018     if (p < 3)
00019       return (proto == "gsidcap") ? proto + ':' + path.substr(p) : path.substr(p);
00020 
00021     // remove multiple "/"
00022     p = path.find_first_not_of('/');
00023 
00024     // it's url, return the full thing
00025     return proto + "://" + path.substr(p);
00026   }
00027 
00028 public:
00029 
00032   virtual Storage *open (const std::string &proto,
00033                          const std::string &path,
00034                          int mode)
00035   {
00036     StorageFactory *f = StorageFactory::get();
00037     StorageFactory::ReadHint readHint = f->readHint();
00038     StorageFactory::CacheHint cacheHint = f->cacheHint();
00039 
00040     if (readHint != StorageFactory::READ_HINT_UNBUFFERED
00041         || cacheHint == StorageFactory::CACHE_HINT_STORAGE)
00042       mode &= ~IOFlags::OpenUnbuffered;
00043     else
00044       mode |= IOFlags::OpenUnbuffered;
00045 
00046     Storage *file = new DCacheFile(normalise(proto, path), mode);
00047     return f->wrapNonLocalFile(file, proto, std::string(), mode);
00048   }
00049 
00050   virtual void stagein (const std::string &proto,
00051                         const std::string &path)
00052   {
00053     std::string npath = normalise(proto, path);
00054     if (dc_stage(npath.c_str(), 0, 0) != 0) {
00055       cms::Exception ex("FileStageInError");
00056       ex << "Cannot stage in file '" << npath
00057          << "', error was: " << dc_strerror(dc_errno)
00058          << " (dc_errno=" << dc_errno << ")";
00059       ex.addContext("Calling DCacheStorageMaker::stagein()");
00060       throw ex;
00061     }
00062   }
00063 
00064   virtual bool check (const std::string &proto,
00065                       const std::string &path,
00066                       IOOffset *size = 0)
00067   {
00068     std::string testpath (normalise (proto, path));
00069     if (dc_access (testpath.c_str (), R_OK) != 0)
00070       return false;
00071 
00072     if (size)
00073     {
00074       struct stat64 buf;
00075       if (dc_stat64 (testpath.c_str (), &buf) != 0)
00076         return false;
00077         
00078       *size = buf.st_size;
00079     }
00080 
00081     return true;
00082   }
00083 
00084   virtual void setTimeout(unsigned int timeout) {
00085     if (timeout != 0) dc_setOpenTimeout(timeout);
00086   }
00087 };
00088 
00089 DEFINE_EDM_PLUGIN (StorageMakerFactory, DCacheStorageMaker, "dcache");
00090 DEFINE_EDM_PLUGIN (StorageMakerFactory, DCacheStorageMaker, "dcap");
00091 DEFINE_EDM_PLUGIN (StorageMakerFactory, DCacheStorageMaker, "gsidcap");