 |
Belle II Software
release-05-01-25
|
11 #include <framework/core/FileCatalog.h>
12 #include <framework/dataobjects/FileMetaData.h>
13 #include <framework/logging/Logger.h>
14 #include <framework/utilities/FileSystem.h>
15 #include <framework/utilities/EnvironmentVariables.h>
16 #include <boost/filesystem.hpp>
21 namespace fs = boost::filesystem;
36 if (fileCatalog ==
"NONE")
return;
39 if (fileCatalog.empty()) {
40 const std::string path{
"~/Belle2FileCatalog.xml"};
41 if (fs::exists(path)) {
47 if (fileCatalog.empty()) {
48 fileCatalog =
"Belle2FileCatalog.xml";
52 m_fileName = fs::absolute(fileCatalog, fs::initial_path<fs::path>()).c_str();
61 if (!file.is_open())
return false;
66 std::string physicalFileName;
67 if (entry.
read(file, physicalFileName)) fileMap[entry.
getLfn()] = std::make_pair(physicalFileName, entry);
69 }
catch (std::exception& e) {
70 B2ERROR(
"Errors occured while reading " <<
m_fileName <<
71 ", maybe it is corrupted? Note that your .root files should be unaffected. (Error details: " << e.what() <<
")");
82 if (!file.is_open())
return false;
84 file <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
85 file <<
"<FileCatalog>\n";
86 for (
const auto& entry : fileMap) entry.second.second.write(file, entry.second.first);
87 file <<
"</FileCatalog>\n";
97 if (metaData.
getLfn().empty()) {
98 B2ERROR(
"Cannot register a file without a valid LFN");
105 B2ERROR(
"Locking of file catalog " <<
m_fileName <<
" failed.");
112 B2ERROR(
"Failed to read file catalog " <<
m_fileName);
117 for (
auto it = fileMap.begin(); it != fileMap.end(); ++it) {
118 auto&& [lfn, value] = *it;
119 if (!oldLFN.empty() and oldLFN == lfn) {
124 if (metaData.
getLfn() == lfn) {
125 B2WARNING(
"A file with the same LFN is already registered and will be overwritten in the catalog."
126 <<
LogVar(
"LFN", lfn) <<
LogVar(
"old PFN", value.first) <<
LogVar(
"new PFN", fileName));
133 fileMap[metaData.
getLfn()] = std::make_pair(fileName, metaData);
136 B2ERROR(
"Failed to write file catalog " <<
m_fileName);
153 B2ERROR(
"Locking of file catalog " <<
m_fileName <<
" failed.");
160 B2ERROR(
"Failed to read file catalog " <<
m_fileName);
165 auto iEntry = fileMap.find(fileName);
166 if (iEntry != fileMap.end()) {
167 metaData = iEntry->second.second;
168 if (!iEntry->second.first.empty()) fileName = iEntry->second.first;
171 for (
const auto& entry : fileMap) {
172 if (fileName.compare(entry.second.first) == 0) {
173 metaData = entry.second.second;
184 std::string fileName = lfn;
187 B2DEBUG(100,
"No LFN " << lfn <<
" found in the file catalog.");
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
This class provides an interface to the file (metadata) catalog.
virtual std::string getPhysicalFileName(const std::string &lfn)
Get the physical file name for the LFN.
virtual bool getMetaData(std::string &fileName, FileMetaData &metaData)
Get the metadata of a file with given (logical) file name.
std::string m_fileName
Name of the file catalog file.
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.
Class to store variables with their name which were sent to the logging service.
bool writeCatalog(const FileMap &fileMap)
Write the file catalog to the local file.
Helper class for locking a file.
virtual bool registerFile(const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
Register a file in the (local) file catalog.
bool readCatalog(FileMap &fileMap)
Read the file catalog from the local file.
FileCatalog()
Constructor: locate local database file.
std::map< std::string, std::pair< std::string, FileMetaData > > FileMap
Map with file catalog content.