9 #include <framework/core/FileCatalog.h> 
   10 #include <framework/dataobjects/FileMetaData.h> 
   11 #include <framework/logging/Logger.h> 
   12 #include <framework/utilities/FileSystem.h> 
   13 #include <framework/utilities/EnvironmentVariables.h> 
   14 #include <boost/filesystem.hpp> 
   19 namespace fs = boost::filesystem;
 
   34   if (fileCatalog == 
"NONE") 
return;
 
   37   if (fileCatalog.empty()) {
 
   38     const std::string path{
"~/Belle2FileCatalog.xml"};
 
   39     if (fs::exists(path)) {
 
   45   if (fileCatalog.empty()) {
 
   46     fileCatalog = 
"Belle2FileCatalog.xml";
 
   50   m_fileName = fs::absolute(fileCatalog, fs::initial_path<fs::path>()).c_str();
 
   59   if (!file.is_open()) 
return false;
 
   64       std::string physicalFileName;
 
   65       if (entry.
read(file, physicalFileName)) fileMap[entry.
getLfn()] = std::make_pair(physicalFileName, entry);
 
   67   } 
catch (std::exception& e) {
 
   68     B2ERROR(
"Errors occured while reading " << 
m_fileName <<
 
   69             ", maybe it is corrupted? Note that your .root files should be unaffected. (Error details: " << e.what() << 
")");
 
   80   if (!file.is_open()) 
return false;
 
   82   file << 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 
   83   file << 
"<FileCatalog>\n";
 
   84   for (
const auto& entry : fileMap) entry.second.second.write(file, entry.second.first);
 
   85   file << 
"</FileCatalog>\n";
 
   95   if (metaData.
getLfn().empty()) {
 
   96     B2ERROR(
"Cannot register a file without a valid LFN");
 
  103     B2ERROR(
"Locking of file catalog " << 
m_fileName << 
" failed.");
 
  110     B2ERROR(
"Failed to read file catalog " << 
m_fileName);
 
  115   for (
auto it = fileMap.begin(); it != fileMap.end(); ++it) {
 
  116     auto&& lfn = (*it).first;
 
  117     if (!oldLFN.empty() and oldLFN == lfn) {
 
  122     if (metaData.
getLfn() == lfn) {
 
  123       B2WARNING(
"A file with the same LFN is already registered and will be overwritten in the catalog." 
  124                 << 
LogVar(
"LFN", lfn) << 
LogVar(
"old PFN", (*it).second.first) << 
LogVar(
"new PFN", fileName));
 
  131   fileMap[metaData.
getLfn()] = std::make_pair(fileName, metaData);
 
  134     B2ERROR(
"Failed to write file catalog " << 
m_fileName);
 
  151     B2ERROR(
"Locking of file catalog " << 
m_fileName << 
" failed.");
 
  158     B2ERROR(
"Failed to read file catalog " << 
m_fileName);
 
  163   auto iEntry = fileMap.find(fileName);
 
  164   if (iEntry != fileMap.end()) {
 
  165     metaData = iEntry->second.second;
 
  166     if (!iEntry->second.first.empty()) fileName = iEntry->second.first;
 
  169   for (
const auto& entry : fileMap) {
 
  170     if (fileName.compare(entry.second.first) == 0) {
 
  171       metaData = entry.second.second;
 
  182   std::string fileName = lfn;
 
  185     B2DEBUG(100, 
"No LFN " << lfn << 
" found in the file catalog.");
 
This class provides an interface to the file (metadata) catalog.
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
virtual bool registerFile(const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
Register a file in the (local) file catalog.
std::map< std::string, std::pair< std::string, FileMetaData > > FileMap
Map with file catalog content.
virtual bool getMetaData(std::string &fileName, FileMetaData &metaData)
Get the metadata of a file with given (logical) file name.
virtual std::string getPhysicalFileName(const std::string &lfn)
Get the physical file name for the LFN.
std::string m_fileName
Name of the file catalog file.
bool writeCatalog(const FileMap &fileMap)
Write the file catalog to the local file.
FileCatalog()
Constructor: locate local database file.
bool readCatalog(FileMap &fileMap)
Read the file catalog from the local file.
Helper class for locking a file.
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
Class to store variables with their name which were sent to the logging service.
static std::string get(const std::string &name, const std::string &fallback="")
Get the value of an environment variable or the given fallback value if the variable is not set.
Abstract base class for different kinds of events.