Belle II Software  release-05-01-25
FileSystem.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher, Thomas Kuhr, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <string>
13 #include <vector>
14 #include <fstream>
15 
16 namespace Belle2 {
22  class FileSystem {
23  public:
42  static std::string findFile(const std::string& path, bool silent = false);
43 
65  static std::string findFile(const std::string& path, const std::string& dataType, bool silent = false);
66 
68  static bool fileExists(const std::string& filename);
70  static bool fileDirExists(const std::string& filename);
72  static bool isFile(const std::string& filename);
74  static bool isDir(const std::string& filename);
76  static bool isSymLink(const std::string& filename);
77 
79  static std::string calculateMD5(const std::string& filename);
80 
82  static std::string calculateAdler32(const std::string& filename);
83 
94  static bool loadLibrary(std::string library, bool fullname = true);
95 
99  class Lock {
100  public:
101 
107  explicit Lock(const std::string& fileName, bool readonly = false);
108 
112  ~Lock();
113 
122  bool lock(int timeout = 300, bool ignoreErrors = false);
123 
124  private:
125  int m_file;
126  bool m_readOnly;
127  };
128 
130  class TemporaryFile: public std::fstream {
131  public:
133  explicit TemporaryFile(std::ios_base::openmode mode = std::ios_base::trunc | std::ios_base::out);
135  TemporaryFile(const TemporaryFile&) = delete;
137  TemporaryFile(TemporaryFile&&) = delete;
139  TemporaryFile& operator=(const TemporaryFile&) = delete;
141  ~TemporaryFile();
143  std::string getName() const { return m_filename; }
144  private:
146  std::string m_filename;
147  };
148 
149  private:
151  FileSystem() = delete;
152 
156  static std::string findFile(const std::string& path, const std::vector<std::string>& dirs, bool silent);
157  };
159 }
Belle2::FileSystem::TemporaryFile::getName
std::string getName() const
get filename of the temporary file
Definition: FileSystem.h:151
Belle2::FileSystem::Lock::lock
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
Definition: FileSystem.cc:190
Belle2::FileSystem::fileDirExists
static bool fileDirExists(const std::string &filename)
Check if the dir containing the filename exists.
Definition: FileSystem.cc:39
Belle2::FileSystem::isFile
static bool isFile(const std::string &filename)
Check if filename points to an existing file.
Definition: FileSystem.cc:46
Belle2::FileSystem::Lock::Lock
Lock(const std::string &fileName, bool readonly=false)
Construct a Lock object for the given file.
Definition: FileSystem.cc:178
Belle2::FileSystem::FileSystem
FileSystem()=delete
no instances.
Belle2::FileSystem::TemporaryFile::~TemporaryFile
~TemporaryFile()
close file and delete on destruction
Definition: FileSystem.cc:234
Belle2::FileSystem::Lock::~Lock
~Lock()
Destructor.
Definition: FileSystem.cc:185
Belle2::FileSystem::TemporaryFile::operator=
TemporaryFile & operator=(const TemporaryFile &)=delete
prevent copy constructor
Belle2::FileSystem::calculateMD5
static std::string calculateMD5(const std::string &filename)
Calculate the MD5 checksum of a given file.
Definition: FileSystem.cc:79
Belle2::FileSystem::loadLibrary
static bool loadLibrary(std::string library, bool fullname=true)
Load a shared library.
Definition: FileSystem.cc:64
Belle2::FileSystem::isSymLink
static bool isSymLink(const std::string &filename)
Check if filename points to an existing symbolic link.
Definition: FileSystem.cc:58
Belle2::FileSystem::calculateAdler32
static std::string calculateAdler32(const std::string &filename)
Calculate the Adler-32 checksum of a given file.
Definition: FileSystem.cc:87
Belle2::FileSystem::TemporaryFile::m_filename
std::string m_filename
filename of the temporary file
Definition: FileSystem.h:154
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::FileSystem::TemporaryFile::TemporaryFile
TemporaryFile(std::ios_base::openmode mode=std::ios_base::trunc|std::ios_base::out)
construct a new temporary file
Definition: FileSystem.cc:224
Belle2::FileSystem::TemporaryFile
Helper file to create a temporary file and ensure deletion if object goes out of scope.
Definition: FileSystem.h:138
Belle2::FileSystem::Lock::m_readOnly
bool m_readOnly
if this is a read-only lock (multiple processes can hold one).
Definition: FileSystem.h:134
Belle2::FileSystem::fileExists
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:33
Belle2::FileSystem::isDir
static bool isDir(const std::string &filename)
Check if filename points to an existing directory.
Definition: FileSystem.cc:52
Belle2::FileSystem::Lock::m_file
int m_file
File descriptor of file to be locked.
Definition: FileSystem.h:133
Belle2::FileSystem::findFile
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:147