Belle II Software development
FileSystem.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <string>
11#include <vector>
12#include <fstream>
13
14namespace Belle2 {
20 class FileSystem {
21 public:
40 static std::string findFile(const std::string& path, bool silent = false);
41
63 static std::string findFile(const std::string& path, const std::string& dataType, bool silent = false);
64
66 static bool fileExists(const std::string& filename);
68 static bool fileDirExists(const std::string& filename);
70 static bool isFile(const std::string& filename);
72 static bool isDir(const std::string& filename);
74 static bool isSymLink(const std::string& filename);
75
77 static std::string calculateMD5(const std::string& filename);
78
80 static std::string calculateAdler32(const std::string& filename);
81
92 static bool loadLibrary(std::string library, bool fullname = true);
93
97 class Lock {
98 public:
99
105 explicit Lock(const std::string& fileName, bool readonly = false);
106
110 ~Lock();
111
120 bool lock(int timeout = 300, bool ignoreErrors = false);
121
122 private:
123 int m_file;
125 };
126
128 class TemporaryFile: public std::fstream {
129 public:
131 explicit TemporaryFile(std::ios_base::openmode mode = std::ios_base::trunc | std::ios_base::out);
133 TemporaryFile(const TemporaryFile&) = delete;
141 std::string getName() const { return m_filename; }
142 private:
144 std::string m_filename;
145 };
146
147 private:
149 FileSystem() = delete;
150
154 static std::string findFile(const std::string& path, const std::vector<std::string>& dirs, bool silent);
155 };
157}
Helper class for locking a file.
Definition: FileSystem.h:97
bool m_readOnly
if this is a read-only lock (multiple processes can hold one).
Definition: FileSystem.h:124
int m_file
File descriptor of file to be locked.
Definition: FileSystem.h:123
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
Definition: FileSystem.cc:189
Helper file to create a temporary file and ensure deletion if object goes out of scope.
Definition: FileSystem.h:128
~TemporaryFile()
close file and delete on destruction
Definition: FileSystem.cc:241
TemporaryFile(const TemporaryFile &)=delete
prevent assignment
std::string getName() const
get filename of the temporary file
Definition: FileSystem.h:141
std::string m_filename
filename of the temporary file
Definition: FileSystem.h:144
TemporaryFile & operator=(const TemporaryFile &)=delete
prevent copy constructor
TemporaryFile(TemporaryFile &&)=delete
and no move construction as basic_ios doesn't allow that
Utility functions related to filename validation and filesystem access.
Definition: FileSystem.h:20
static bool loadLibrary(std::string library, bool fullname=true)
Load a shared library.
Definition: FileSystem.cc:63
static bool isSymLink(const std::string &filename)
Check if filename points to an existing symbolic link.
Definition: FileSystem.cc:57
FileSystem()=delete
no instances.
static bool fileDirExists(const std::string &filename)
Check if the dir containing the filename exists.
Definition: FileSystem.cc:38
static bool isFile(const std::string &filename)
Check if filename points to an existing file.
Definition: FileSystem.cc:45
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
static std::string calculateMD5(const std::string &filename)
Calculate the MD5 checksum of a given file.
Definition: FileSystem.cc:78
static std::string calculateAdler32(const std::string &filename)
Calculate the Adler-32 checksum of a given file.
Definition: FileSystem.cc:86
static bool isDir(const std::string &filename)
Check if filename points to an existing directory.
Definition: FileSystem.cc:51
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:32
Abstract base class for different kinds of events.