Belle II Software development
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 const size_t bufsize = 1024 * 1024 * sizeof(Bytef);
96 Bytef* buf = (Bytef*) malloc(bufsize);
97 if (!buf) {
98 fclose(fp);
99 return "";
100 }
101 while (true) {
102 i = fread((void*) buf, 1, bufsize, fp);
103 if (ferror(fp)) {
104 free(buf);
105 fclose(fp);
106 return "";
107 }
108 if (i > 0) sum = adler32(sum, buf, i);
109 if (feof(fp)) break;
110 }
111 fclose(fp);
112 free(buf);
113 // Adler32 checksums hex digests ARE zero padded although
114 // HLT legacy presentation may differ.
115 sprintf(hexdigest, "%08lx", sum);
116 chksum = hexdigest;
117 } else {
118 chksum = "";
119 }
120 return chksum;
121}
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 159 of file FileSystem.cc.

160{
161 std::vector<std::string> dirs;
162 if (getenv("BELLE2_LOCAL_DIR")) {
163 dirs.emplace_back(getenv("BELLE2_LOCAL_DIR"));
164 }
165 if (getenv("BELLE2_RELEASE_DIR")) {
166 dirs.emplace_back(getenv("BELLE2_RELEASE_DIR"));
167 }
168 return findFile(path, dirs, silent);
169}
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...

◆ 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", "validation" or "starterkit".
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 171 of file FileSystem.cc.

172{
173 std::vector<std::string> dirs;
174 std::string envVar = "BELLE2_" + boost::to_upper_copy(dataType) + "_DATA_DIR";
175 if (getenv(envVar.c_str())) {
176 dirs.emplace_back(getenv(envVar.c_str()));
177 }
178 std::string result = findFile(path, dirs, true);
179 if (result.empty() && !silent)
180 B2ERROR("findFile(): Could not find data file. You may want to use the 'b2install-data' tool to get the file."
181 << LogVar("path", path) << LogVar("data type", dataType));
182 return result;
183}

◆ 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 123 of file FileSystem.cc.

124{
125 // check given directories
126 string fullpath;
127 for (auto dir : dirs) {
128 if (dir.empty())
129 continue;
130 fs::path dir_path = dir;
131 if (fs::path(path).is_absolute())
132 dir_path += path;
133 else
134 dir_path /= path;
135 fullpath = dir_path.string();
136 if (fileExists(fullpath)) {
137 if (isSymLink(fullpath) or isSymLink(dir))
138 return fullpath;
139 else
140 return fs::canonical(fullpath).string();
141 }
142 }
143
144 // check local directory
145 fullpath = fs::absolute(path).string();
146 if (fileExists(fullpath)) {
147 if (isSymLink(fullpath))
148 return fullpath;
149 else
150 return fs::canonical(fullpath).string();
151 }
152
153 // nothing found
154 if (!silent)
155 B2ERROR("findFile(): Could not find file." << LogVar("path", path));
156 return string("");
157}
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: