runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
StorageManager.cpp
Go to the documentation of this file.
1 #include <StorageManager.h>
2 
4  message(_message) {
5 }
6 const char* StorageManagerException::what() const throw() {
7  return message.c_str();
8 }
9 
11 
12 }
13 
15  StorageManagerException("At least one storage location has to be given!") {
16 }
17 
19 
21  StorageManagerException("No storage device was found with more than the given threshold of free space!") {
22 }
23 
25 
26 StorageManager::StorageManager(double minFreeThreshold_):
27  minFreeThreshold(minFreeThreshold_) {
28 }
29 
30 void StorageManager::addStorageLocation(const std::string & path) {
31  storageLocations.emplace_back(path);
32  if (storageLocations.size() == 1) {
33  currentLocation = storageLocations.begin();
34  }
35 }
36 
38  if (storageLocations.empty()) {
39  throw NoStorageLocationGiven();
40  }
41  double currentFree = systemHelper.getDiskFreePercent(*currentLocation);
42  if (currentFree >= minFreeThreshold) {
43  return *currentLocation;
44  }
45  for(auto it = storageLocations.begin(); it != storageLocations.end(); ++it) {
46  double thisFree = systemHelper.getDiskFreePercent(*it);
47  if (thisFree > currentFree) {
48  currentFree = thisFree;
49  currentLocation = it;
50  }
51  }
52  if (currentFree >= minFreeThreshold) {
53  return *currentLocation;
54  }
55  throw NoSpaceLeftOnStorage();
56 }
std::string getPreferredStorageLocation()
Ask for a good location to store data.
Base class for all StorageManager exceptions.
All defined storage locations have insufficient free space.
void addStorageLocation(const std::string &path)
Add a new storage location.
StorageManagerException(const std::string &_message)
Storage location was requested but none have been defined.
StorageManager(double minFreeThreshold_=.05)
Construct a StorageManager.
virtual const char * what() const
double getDiskFreePercent(const std::string &path) const
Get free fraction of the disk behind the given path.