forked from JanuszBedkowski/mandeye_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemClient.cpp
More file actions
296 lines (272 loc) · 7.44 KB
/
FileSystemClient.cpp
File metadata and controls
296 lines (272 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "FileSystemClient.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdbool.h>
#include <unistd.h>
namespace mandeye
{
FileSystemClient::FileSystemClient(const std::string& repository)
: m_repository(repository)
{
m_nextId = GetIdFromManifest();
}
nlohmann::json FileSystemClient::produceStatus()
{
nlohmann::json data;
data["FileSystemClient"]["repository"] = m_repository;
float free_mb = 0;
try
{
free_mb = CheckAvailableSpace();
}
catch(std::filesystem::filesystem_error& e)
{
data["FileSystemClient"]["error"] = e.what();
}
data["FileSystemClient"]["free_megabytes"] = free_mb;
data["FileSystemClient"]["free_str"] = ConvertToText(free_mb);
data["FileSystemClient"]["benchmarkWriteSpeed"] = m_benchmarkWriteSpeed;
try
{
data["FileSystemClient"]["m_nextId"] = m_nextId;
}
catch(std::filesystem::filesystem_error& e)
{
data["FileSystemClient"]["error"] = e.what();
}
try
{
data["FileSystemClient"]["writable"] = GetIsWritable();
}
catch(std::filesystem::filesystem_error& e)
{
data["FileSystemClient"]["error"] = e.what();
}
try
{
data["FileSystemClient"]["dirs"] = GetDirectories();
}
catch(std::filesystem::filesystem_error& e)
{
data["FileSystemClient"]["error"] = e.what();
}
return data;
}
//! Test is writable
float FileSystemClient::CheckAvailableSpace()
{
std::error_code ec;
const std::filesystem::space_info si = std::filesystem::space(m_repository, ec);
if(ec.value() == 0)
{
const float f = static_cast<float>(si.free) / (1024 * 1024);
return std::round(f);
}
return -1.f;
}
std::string FileSystemClient::ConvertToText(float mb)
{
std::stringstream tmp;
tmp << std::setprecision(1) << std::fixed << mb / 1024 << "GB";
return tmp.str();
}
int32_t FileSystemClient::GetIdFromManifest()
{
std::filesystem::path versionfn =
std::filesystem::path(m_repository) / std::filesystem::path(versionFilename);
std::ofstream versionOFstream;
versionOFstream.open(versionfn.c_str());
versionOFstream << "Version 0.6-dev" << std::endl;
std::filesystem::path manifest =
std::filesystem::path(m_repository) / std::filesystem::path(manifestFilename);
std::unique_lock<std::mutex> lck(m_mutex);
std::ifstream manifestFstream;
manifestFstream.open(manifest.c_str());
if(manifestFstream.good() && manifestFstream.is_open())
{
uint32_t id{0};
manifestFstream >> id;
return (id++);
}
std::ofstream manifestOFstream;
manifestOFstream.open(manifest.c_str());
if(manifestOFstream.good() && manifestOFstream.is_open())
{
uint32_t id{0};
manifestOFstream << id << std::endl;
return (id++);
}
//
return -1;
}
int32_t FileSystemClient::GetNextIdFromManifest()
{
std::filesystem::path manifest =
std::filesystem::path(m_repository) / std::filesystem::path(manifestFilename);
int32_t id = GetIdFromManifest();
id++;
m_nextId = id;
std::ofstream manifestOFstream;
manifestOFstream.open(manifest.c_str());
if(manifestOFstream.good() && manifestOFstream.is_open())
{
manifestOFstream << id << std::endl;
return id;
}
return id;
}
bool FileSystemClient::CreateDirectoryForContinousScanning(std::string &writable_dir, const int &id_manifest)
{
std::string ret;
if(GetIsWritable())
{
//auto id = GetNextIdFromManifest();
auto id = id_manifest;
char dirName[256];
snprintf(dirName, 256, "continousScanning_%04d", id);
std::filesystem::path newDirPath =
std::filesystem::path(m_repository) / std::filesystem::path(dirName);
std::cout << "Creating directory " << newDirPath.string() << std::endl;
std::error_code ec;
std::filesystem::create_directories(newDirPath, ec);
m_error = ec.message();
if(ec.value() == 0)
{
if(!newDirPath.string().empty()){
writable_dir = newDirPath.string();
m_currentContinousScanDirectory = writable_dir;
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
bool FileSystemClient::CreateDirectoryForStopScans(std::string &writable_dir, int &id_manifest){
std::string ret;
if(GetIsWritable())
{
id_manifest = GetNextIdFromManifest() - 1;
char dirName[256];
snprintf(dirName, 256, "stopScans_%04d", id_manifest);
std::filesystem::path newDirPath =
std::filesystem::path(m_repository) / std::filesystem::path(dirName);
std::cout << "Creating directory " << newDirPath.string() << std::endl;
std::error_code ec;
std::filesystem::create_directories(newDirPath, ec);
m_error = ec.message();
if(ec.value() == 0)
{
if(!newDirPath.string().empty()){
writable_dir = newDirPath.string();
m_currentStopScanDirectory = writable_dir;
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
std::vector<std::string> FileSystemClient::GetDirectories()
{
std::unique_lock<std::mutex> lck(m_mutex);
std::vector<std::string> fn;
if (m_currentContinousScanDirectory.empty() ||
!std::filesystem::exists(m_currentContinousScanDirectory) ||
!std::filesystem::is_directory(m_currentContinousScanDirectory)) {
// Directory is not set or invalid, return empty vector
return fn;
}
for(const auto& entry : std::filesystem::directory_iterator(m_currentContinousScanDirectory))
{
if(entry.is_regular_file() )
{
auto size = std::filesystem::file_size(entry);
float fsize = static_cast<float>(size) / (1024 * 1204);
fn.push_back(entry.path().string() + " " + std::to_string(fsize) + " Mb");
}
}
std::sort(fn.begin(), fn.end());
return fn;
}
bool FileSystemClient::GetIsWritable()
{
if(access(m_repository.c_str(), W_OK) == 0)
{
return true;
}
else
{
return false;
}
}
nlohmann::json FileSystemClient::GetConfig()
{
std::filesystem::path configPath = std::filesystem::path(m_repository) / std::filesystem::path(config);
if (!std::filesystem::exists(configPath))
{
std::cerr << "Config file does not exist at " << configPath.string() << std::endl;
return nlohmann::json();
}
nlohmann::json configJson;
std::ifstream configFile(configPath);
if (configFile.is_open())
{
try
{
configFile >> configJson;
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "Error parsing config file: " << e.what() << std::endl;
return nlohmann::json();
}
configFile.close();
return configJson;
}
else
{
std::cerr << "Failed to open config file at " << configPath.string() << std::endl;
return nlohmann::json();
}
return nlohmann::json();
}
double FileSystemClient::BenchmarkWriteSpeed(const std::string& filename, size_t fileSizeMB) {
const size_t bufferSize = 1024 * 1024; // 1 MB buffer
std::vector<char> buffer(bufferSize, 0xAA);
std::filesystem::path fileName = std::filesystem::path(m_repository)/ std::filesystem::path(filename);
std::ofstream out(fileName.string(), std::ios::binary);
if (!out) {
std::cerr << "Failed to open file for writing\n";
return 0.0;
}
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < fileSizeMB; ++i) {
out.write(buffer.data(), bufferSize);
}
out.close();
auto end = std::chrono::high_resolution_clock::now();
system("sync");
std::chrono::duration<double> elapsed = end - start;
double mbps = fileSizeMB / elapsed.count();
std::cout << "Wrote " << fileSizeMB << " MB in " << elapsed.count() << " seconds (" << mbps << " MB/s)\n";
// clear file
// Remove the file after benchmarking
// std::error_code ec;
// std::filesystem::remove(fileName, ec);
// if (ec) {
// std::cerr << "Failed to remove benchmark file: " << ec.message() << std::endl;
// }
return mbps;
}
} // namespace mandeye