Belle II Software light-2406-ragdoll
FileSystem Class Reference

Utility functions related to filename validation and filesystem access. More...

#include <FileSystem.h>

Classes

class  Lock
 Helper class for locking a file. More...
 
class  TemporaryFile
 Helper file to create a temporary file and ensure deletion if object goes out of scope. More...
 

Static Public Member Functions

static std::string findFile (const std::string &path, bool silent=false)
 Search for given file or directory in local or central release directory, and return absolute path if found.
 
static std::string findFile (const std::string &path, const std::string &dataType, bool silent=false)
 Search for given file or directory in the directory given by the environment variable BELLE2_<DATATYPE>_DATA_DIR or local/central release directory and return absolute path if found.
 
static bool fileExists (const std::string &filename)
 Check if the file with given filename exists.
 
static bool fileDirExists (const std::string &filename)
 Check if the dir containing the filename exists.
 
static bool isFile (const std::string &filename)
 Check if filename points to an existing file.
 
static bool isDir (const std::string &filename)
 Check if filename points to an existing directory.
 
static bool isSymLink (const std::string &filename)
 Check if filename points to an existing symbolic link.
 
static std::string calculateMD5 (const std::string &filename)
 Calculate the MD5 checksum of a given file.
 
static std::string calculateAdler32 (const std::string &filename)
 Calculate the Adler-32 checksum of a given file.
 
static bool loadLibrary (std::string library, bool fullname=true)
 Load a shared library.
 

Private Member Functions

 FileSystem ()=delete
 no instances.
 

Static Private Member Functions

static std::string findFile (const std::string &path, const std::vector< std::string > &dirs, bool silent)
 Search for given file or directory in list of directories or working directory.
 

Detailed Description

Utility functions related to filename validation and filesystem access.

Definition at line 20 of file FileSystem.h.

Member Function Documentation

◆ calculateAdler32()

std::string calculateAdler32 ( const std::string &  filename)
static

Calculate the Adler-32 checksum of a given file.

Definition at line 86 of file FileSystem.cc.

87{
88 string chksum;
89 if (not isFile(filename)) return "";
90 fs::path fullPath = fs::absolute(filename);
91 FILE* fp = fopen(fullPath.c_str(), "rb");
92 if (fp) {
93 uLong i, sum = adler32(0, 0, 0);
94 char hexdigest[9];
95 Bytef* buf = (Bytef*) malloc(1024 * 1024 * sizeof(Bytef));
96 if (!buf) {
97 fclose(fp);
98 return "";
99 }
100 while ((i = fread((void*) buf, 1, sizeof(buf), fp)) > 0) {
101 sum = adler32(sum, buf, i);
102 }
103 fclose(fp);
104 free(buf);
105 // Adler32 checksums hex digests ARE zero padded although
106 // HLT legacy presentation may differ.
107 sprintf(hexdigest, "%08lx", sum);
108 chksum = hexdigest;
109 } else {
110 chksum = "";
111 }
112 return chksum;
113}
static bool isFile(const std::string &filename)
Check if filename points to an existing file.
Definition: FileSystem.cc:45

◆ calculateMD5()

std::string calculateMD5 ( const std::string &  filename)
static

Calculate the MD5 checksum of a given file.

Definition at line 78 of file FileSystem.cc.

79{
80 if (not isFile(filename)) return "";
81 fs::path fullPath = fs::absolute(filename);
82 std::unique_ptr<TMD5> md5(TMD5::FileChecksum(fullPath.c_str()));
83 return md5->AsString();
84}

◆ fileDirExists()

bool fileDirExists ( const std::string &  filename)
static

Check if the dir containing the filename exists.

Definition at line 38 of file FileSystem.cc.

39{
40 fs::path fullPath = fs::absolute(filename);
41 fullPath.remove_filename();
42 return fs::exists(fullPath);
43}

◆ fileExists()

bool fileExists ( const std::string &  filename)
static

Check if the file with given filename exists.

Definition at line 32 of file FileSystem.cc.

33{
34 fs::path fullPath = fs::absolute(filename);
35 return fs::exists(fullPath);
36}

◆ findFile() [1/3]

std::string findFile ( const std::string &  path,
bool  silent = false 
)
static

Search for given file or directory in local or central release directory, and return absolute path if found.

If the file isn't found in either of these, absolute paths and paths relative to the current working directory are also accepted.

It is recommended to replace any relative paths you have with the return value of this function during initialization.

Parameters
pathpath to file/directory, assuming it's installed locally (e.g. /data/geometry/Belle2.xml). Leading slash is not strictly required.
silentIf true, no error message is printed when file could not be found.
Returns
absolute path to file in local directory, if it exists, otherwise abs. path to file in central release directory, or empty string if file wasn't found.

Definition at line 151 of file FileSystem.cc.

152{
153 std::vector<std::string> dirs;
154 if (getenv("BELLE2_LOCAL_DIR")) {
155 dirs.emplace_back(getenv("BELLE2_LOCAL_DIR"));
156 }
157 if (getenv("BELLE2_RELEASE_DIR")) {
158 dirs.emplace_back(getenv("BELLE2_RELEASE_DIR"));
159 }
160 return findFile(path, dirs, silent);
161}
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:151

◆ findFile() [2/3]

std::string findFile ( const std::string &  path,
const std::string &  dataType,
bool  silent = false 
)
static

Search for given file or directory in the directory given by the environment variable BELLE2_<DATATYPE>_DATA_DIR or local/central release directory and return absolute path if found.

If the file isn't found in either of these, absolute paths and paths relative to the current working directory are also accepted.

It is recommended to replace any relative paths you have with the return value of this function during initialization.

Parameters
pathpath to file/directory, assuming it's installed locally (e.g. /data/geometry/Belle2.xml). Leading slash is not strictly required.
dataTypetype of data, like "examples" or "validation".
silentIf true, no error message is printed when file could not be found.
Returns
absolute path to file in local directory, if it exists, otherwise abs. path to file in central release directory, or empty string if file wasn't found.

Definition at line 163 of file FileSystem.cc.

164{
165 std::vector<std::string> dirs;
166 std::string envVar = "BELLE2_" + boost::to_upper_copy(dataType) + "_DATA_DIR";
167 if (getenv(envVar.c_str())) {
168 dirs.emplace_back(getenv(envVar.c_str()));
169 }
170 std::string result = findFile(path, dirs, true);
171 if (result.empty() && !silent)
172 B2ERROR("findFile(): Could not find data file. You may want to use the 'b2install-data' tool to get the file."
173 << LogVar("path", path) << LogVar("data type", dataType));
174 return result;
175}
Class to store variables with their name which were sent to the logging service.

◆ findFile() [3/3]

std::string findFile ( const std::string &  path,
const std::vector< std::string > &  dirs,
bool  silent 
)
staticprivate

Search for given file or directory in list of directories or working directory.

Definition at line 115 of file FileSystem.cc.

116{
117 // check given directories
118 string fullpath;
119 for (auto dir : dirs) {
120 if (dir.empty())
121 continue;
122 fs::path dir_path = dir;
123 if (fs::path(path).is_absolute())
124 dir_path += path;
125 else
126 dir_path /= path;
127 fullpath = dir_path.string();
128 if (fileExists(fullpath)) {
129 if (isSymLink(fullpath) or isSymLink(dir))
130 return fullpath;
131 else
132 return fs::canonical(fullpath).string();
133 }
134 }
135
136 // check local directory
137 fullpath = fs::absolute(path).string();
138 if (fileExists(fullpath)) {
139 if (isSymLink(fullpath))
140 return fullpath;
141 else
142 return fs::canonical(fullpath).string();
143 }
144
145 // nothing found
146 if (!silent)
147 B2ERROR("findFile(): Could not find file." << LogVar("path", path));
148 return string("");
149}
static bool isSymLink(const std::string &filename)
Check if filename points to an existing symbolic link.
Definition: FileSystem.cc:57
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:32

◆ isDir()

bool isDir ( const std::string &  filename)
static

Check if filename points to an existing directory.

Definition at line 51 of file FileSystem.cc.

52{
53 fs::path fullPath = fs::absolute(filename);
54 return (fs::exists(fullPath)) && (fs::is_directory(fullPath));
55}

◆ isFile()

bool isFile ( const std::string &  filename)
static

Check if filename points to an existing file.

Definition at line 45 of file FileSystem.cc.

46{
47 fs::path fullPath = fs::absolute(filename);
48 return (fs::exists(fullPath)) && (fs::is_regular_file(fullPath));
49}

◆ isSymLink()

bool isSymLink ( const std::string &  filename)
static

Check if filename points to an existing symbolic link.

Definition at line 57 of file FileSystem.cc.

58{
59 fs::path fullPath = fs::absolute(filename);
60 return (fs::exists(fullPath)) && (fs::is_symlink(fullPath));
61}

◆ loadLibrary()

bool loadLibrary ( std::string  library,
bool  fullname = true 
)
static

Load a shared library.

Symbols are resolved only when used, slightly reducing loading time (RTLD_LAZY, see dlopen(3))

Parameters
libraryName of the library
fullnameIf false, the Library name is interpreted as short name like given to the compiler with -l. It will be expanded to lib<library>.so

Definition at line 63 of file FileSystem.cc.

64{
65 if (!fullname) library = "lib" + library + ".so";
66
67 B2DEBUG(100, "Loading shared library " << library);
68 void* libPointer = dlopen(library.c_str(), RTLD_LAZY | RTLD_GLOBAL);
69
70 if (libPointer == nullptr) {
71 B2ERROR("Could not open shared library file (error in dlopen) : " << dlerror());
72 return false;
73 }
74
75 return true;
76}

The documentation for this class was generated from the following files: