util.cpp
Go to the documentation of this file.
1#include "util.h"
2
3#include <chrono>
4#include <thread>
5
7{
8 namespace util
9 {
10 // check whether a string is a number (e.g. timestamp folders)
11 bool
12 isNumber(const std::string& s)
13 {
14 for (char const& ch : s)
15 {
16 if (std::isdigit(ch) == 0)
17 {
18 return false;
19 }
20 }
21 return true;
22 }
23
24 bool
25 checkIfBasePathExists(const std::filesystem::path& mPath)
26 {
27 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
28 {
29 return minizip::util::checkZipFile(mPath);
30 }
32 }
33
34 void
35 ensureBasePathExists(const std::filesystem::path& mPath, bool createIfNotExistent)
36 {
37 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
38 {
39 auto z = minizip::util::ensureZipFile(mPath, createIfNotExistent);
40 zipClose(z, NULL);
41 return;
42 }
43 return filesystem::util::ensureFolderInFilesystemExists(mPath, createIfNotExistent);
44 }
45
46 bool
47 checkIfFolderExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
48 {
49 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
50 {
51 return minizip::util::checkIfFolderInZipExists(mPath, p);
52 }
53
55 }
56
57 void
58 ensureFolderExists(const std::filesystem::path& mPath,
59 const std::filesystem::path& p,
60 bool createIfNotExistent)
61 {
62 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
63 {
64 return minizip::util::ensureFolderInZipExists(mPath, p, createIfNotExistent);
65 }
66
67 return filesystem::util::ensureFolderInFilesystemExists(mPath / p, createIfNotExistent);
68 }
69
70 bool
71 checkIfFileExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
72 {
73 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
74 {
75 return minizip::util::checkIfFileInZipExists(mPath, p);
76 }
77
79 }
80
81 void
82 ensureFileExists(const std::filesystem::path& mPath, const std::filesystem::path& p)
83 {
84 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
85 {
86 return minizip::util::ensureFileInZipExists(mPath, p);
87 }
88
90 }
91
92 void
93 writeDataToFile(const std::filesystem::path& mPath,
94 const std::filesystem::path& p,
95 const std::vector<unsigned char>& data)
96 {
97 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
98 {
99 return minizip::util::writeDataInFileInZipFile(mPath, p, data);
100 }
101
103 }
104
105 void
106 writeDataToFileRepeated(const std::filesystem::path& mPath,
107 const std::filesystem::path& p,
108 const std::vector<unsigned char>& data,
109 const unsigned int maxTries,
110 const unsigned int sleepTimeMs)
111 {
112 for (unsigned int i = 0; i < maxTries; ++i)
113 {
114 try
115 {
116 writeDataToFile(mPath, p, data);
117 return;
118 }
119 catch (const error::ArMemError&)
120 {
121 // wait a bit to give the filesystem enough time to manage the workload
122 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
123 }
124 }
125
126 // even after all the tries we did not succeeded. This is very bad!
127 throw error::ArMemError(
128 "ATTENTION! Even after " + std::to_string(maxTries) +
129 " tries, the memory was not able to store the instance at path '" + p.string() +
130 "'. This means this instance will be lost!");
131 }
132
133 std::vector<unsigned char>
134 readDataFromFile(const std::filesystem::path& mPath, const std::filesystem::path& p)
135 {
136 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
137 {
138 return minizip::util::readDataFromFileInZipFile(mPath, p);
139 }
140
142 }
143
144 std::vector<std::string>
145 getAllDirectories(const std::filesystem::path& mPath, const std::filesystem::path& p)
146 {
147 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
148 {
149 return {};
150 }
151
153 }
154
155 std::vector<std::string>
156 getAllFiles(const std::filesystem::path& mPath, const std::filesystem::path& p)
157 {
158 if (mPath.extension() == minizip::util::MINIZIP_SUFFIX)
159 {
160 return {};
161 }
162
164 }
165 } // namespace util
166} // namespace armarx::armem::server::ltm::disk
Base class for all exceptions thrown by the armem library.
Definition ArMemError.h:19
void ensureFolderInFilesystemExists(const std::filesystem::path &p, bool createIfNotExistent)
std::vector< std::string > getAllFilesystemDirectories(const std::filesystem::path path)
std::vector< std::string > getAllFilesystemFiles(const std::filesystem::path path)
void ensureFileInFilesystemExists(const std::filesystem::path &p)
std::vector< unsigned char > readDataFromFilesystemFile(const std::filesystem::path path)
bool checkIfFileInFilesystemExists(const std::filesystem::path &p)
void writeDataInFilesystemFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
bool checkIfFolderInFilesystemExists(const std::filesystem::path &p)
bool checkIfFolderExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:47
bool checkIfBasePathExists(const std::filesystem::path &mPath)
Definition util.cpp:25
std::vector< std::string > getAllDirectories(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:145
void ensureBasePathExists(const std::filesystem::path &mPath, bool createIfNotExistent)
Definition util.cpp:35
std::vector< std::string > getAllFiles(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:156
bool checkIfFileExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:71
void ensureFolderExists(const std::filesystem::path &mPath, const std::filesystem::path &p, bool createIfNotExistent)
Definition util.cpp:58
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:134
bool isNumber(const std::string &s)
Definition util.cpp:12
void writeDataToFile(const std::filesystem::path &mPath, const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition util.cpp:93
void ensureFileExists(const std::filesystem::path &mPath, const std::filesystem::path &p)
Definition util.cpp:82
void writeDataToFileRepeated(const std::filesystem::path &mPath, const std::filesystem::path &p, const std::vector< unsigned char > &data, const unsigned int maxTries, const unsigned int sleepTimeMs)
Definition util.cpp:106