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 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 if (!i) break;
102 sum = adler32(sum, buf, i);
103 }
104 fclose(fp);
105 free(buf);
106 // Adler32 checksums hex digests ARE zero padded although
107 // HLT legacy presentation may differ.
108 sprintf(hexdigest, "%08lx", sum);
109 chksum = hexdigest;
110 } else {
111 chksum = "";
112 }
113 return chksum;
114}
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 152 of file FileSystem.cc.

153{
154 std::vector<std::string> dirs;
155 if (getenv("BELLE2_LOCAL_DIR")) {
156 dirs.emplace_back(getenv("BELLE2_LOCAL_DIR"));
157 }
158 if (getenv("BELLE2_RELEASE_DIR")) {
159 dirs.emplace_back(getenv("BELLE2_RELEASE_DIR"));
160 }
161 return findFile(path, dirs, silent);
162}
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 164 of file FileSystem.cc.

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

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

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