-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUtils.h
359 lines (332 loc) · 9.88 KB
/
FileUtils.h
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// Created by nbdy on 14.01.22.
//
#ifndef BINFMT__FILEUTILS_H_
#define BINFMT__FILEUTILS_H_
#include <filesystem>
namespace binfmt {
struct FileUtils {
/*!
* Create a directory if it does not exist
* @param Path
* @return false if the directory could not be created
*/
static bool CreateDirectory(const std::string &Path) {
if (!std::filesystem::exists(Path)) {
return std::filesystem::create_directory(Path);
}
return true;
}
/*!
* Delete a directory if it exists
* @param Path
* @return true if std::filesystem::remove was successful or the directory does not exist
*/
static bool DeleteDirectory(const std::string &Path) {
if (std::filesystem::exists(Path)) {
return std::filesystem::remove(Path);
}
return true;
}
/*!
* Use FCLOSE macro to close it and sync the fs
* @param FilePath
* @param Create
* @return
*/
static FILE *OpenBinaryFile(const std::string &FilePath, bool Create = false,
uint32_t offset = 0) {
FILE *r = nullptr;
auto filePath = std::filesystem::path(FilePath);
if (!std::filesystem::exists(filePath.parent_path()) && Create) {
std::filesystem::create_directories(filePath.parent_path());
}
if (std::filesystem::exists(FilePath) || Create) {
r = std::fopen(FilePath.c_str(), Create ? "wbe" : "r+be");
auto fn = fileno(r);
if (lockf(fn, F_LOCK, 0) != 0) {
(void)CloseBinaryFile(r);
r = nullptr;
}
auto sr = fseek(r, offset, SEEK_SET);
if (sr != 0) {
(void)CloseBinaryFile(r);
r = nullptr;
}
}
return r;
}
/*!
* Calls syncfs on the fp and fcloses it afterwards
* @param fp
* @return
*/
static bool CloseBinaryFile(FILE *fp) {
auto fn = fileno(fp);
(void)lockf(fn, F_ULOCK, 0);
auto r = syncfs(fn);
return fclose(fp) == 0 && r == 0;
}
/*!
* Calculate the offset of an entry
* @tparam HeaderType
* @tparam EntryType
* @param offset
* @return
*/
template <typename HeaderType, typename EntryType>
static uint32_t CalculateEntryOffset(uint32_t offset) {
return sizeof(HeaderType) + offset * sizeof(EntryType);
}
/*!
* Creates file if does not exists,
* writes the provided struct to the beginning of it
* and closes the file again
* @tparam HeaderType
* @param FilePath
* @param header
* @return false if cannot seek beginning or write header data
*/
template <typename HeaderType>
static bool WriteHeader(const std::string &FilePath, HeaderType header) {
auto *fp = OpenBinaryFile(FilePath, true);
if (fp == nullptr) {
return false;
}
auto r = fwrite(&header, sizeof(HeaderType), 1, fp);
(void)CloseBinaryFile(fp);
return r == 1;
}
/*!
* Gets the first sizeof(HeaderType) bytes from a file and returns them as
* HeaderType.
* @tparam HeaderType
* @param FilePath
* @return false if file does not exist, seek to start / reading the entry /
* closing the file failed
*/
template <typename HeaderType>
static bool GetHeader(const std::string &FilePath, HeaderType &r) {
auto *fp = OpenBinaryFile(FilePath);
if (fp == nullptr) {
return false;
}
auto ret = fread(&r, sizeof(HeaderType), 1, fp);
(void)CloseBinaryFile(fp);
return ret == 1;
}
/*!
* Write data entry to file. Skips size of HeaderType in bytes.
* @tparam EntryType
* @param FilePath
* @param entry
* @return false if file does not exist, seek to offset failed or we can't
* write the object
*/
template <typename HeaderType, typename EntryType>
static bool WriteData(const std::string &FilePath, EntryType entry,
uint32_t offset = 0) {
uint32_t o = sizeof(HeaderType) + offset * sizeof(EntryType);
FILE *fp = OpenBinaryFile(FilePath, false, o);
if (fp == nullptr) {
return false;
}
auto r = fwrite(&entry, sizeof(EntryType), 1, fp);
(void)CloseBinaryFile(fp);
return r == 1;
}
/*!
* Write data entry to file. Skips size of HeaderType in bytes.
* @tparam EntryType
* @param FilePath
* @param entry
* @return false if file does not exist, seek to offset failed or we can't
* write the object
*/
template <typename HeaderType, typename EntryType>
static bool WriteDataVector(const std::string &FilePath,
std::vector<EntryType> entries,
uint32_t offset = 0) {
uint32_t o = sizeof(HeaderType) + offset * sizeof(EntryType);
FILE *fp = OpenBinaryFile(FilePath, false, o);
if (fp == nullptr) {
std::cout << strerror(errno) << std::endl;
return false;
}
auto r = fwrite(&entries[0], sizeof(EntryType), entries.size(), fp);
(void)CloseBinaryFile(fp);
return r == entries.size();
}
/*!
*
* @tparam HeaderType
* @tparam EntryType
* @tparam EntryCount
* @param FilePath
* @return
*/
template <typename HeaderType, typename EntryType>
static bool ReadData(std::vector<EntryType> &Output,
const std::string &FilePath, uint32_t offset = 0,
uint32_t count = 0) {
auto EntrySize = sizeof(EntryType);
auto EntryCount = (GetFileSize(FilePath) - sizeof(HeaderType)) / EntrySize;
uint32_t o = sizeof(HeaderType) + offset * EntrySize;
FILE *fp = OpenBinaryFile(FilePath, false, o);
if (count > 0) {
EntryCount = count;
}
Output.resize(EntryCount);
auto c = fread(&Output[0], EntrySize, EntryCount, fp);
fclose(fp);
return c == EntryCount;
}
/*!
* Read file by object, so it should only take sizeof(EntryType) bytes of ram.
* @tparam HeaderType
* @tparam EntryType
* @param FilePath
* @param callback
* @return false if file does not exist, seek to offset failed or file close
* at exit failed
*/
template <typename HeaderType, typename EntryType>
static bool ReadDataAt(const std::string &FilePath, uint32_t offset,
EntryType &output) {
auto EntrySize = sizeof(EntryType);
uint32_t o = sizeof(HeaderType) + offset * EntrySize;
FILE *fp = OpenBinaryFile(FilePath, false, o);
if (fp == nullptr) {
return false;
}
(void)fread(&output, EntrySize, 1, fp);
(void)CloseBinaryFile(fp);
return true;
}
/*!
* Truncate a file to a certain offset
* @tparam HeaderType
* @param FilePath
* @return false if the file does not exist, seek to start of data section
* begins / truncation / file closing failed
*/
template <typename HeaderType, typename EntryType>
static bool ClearFile(const std::string &FilePath, uint32_t offset = 0) {
auto *fp = OpenBinaryFile(FilePath);
if (fp == nullptr) {
return false;
}
auto r = ftruncate(fileno(fp), GetFileSize<HeaderType, EntryType>(offset));
(void)CloseBinaryFile(fp);
return r == 0;
}
/*!
* Gets the size of a file
* @param FilePath
* @param r
* @return file size or 0 if file does not exist
*/
static uintmax_t GetFileSize(
const std::string &FilePath) { // NOLINT(misc-definitions-in-headers)
if (!std::filesystem::exists(FilePath)) {
return 0;
}
return std::filesystem::file_size(FilePath);
}
/*!
* Calculate the size of a file by its header and entry type size aswell as
* entry count
* @tparam HeaderType
* @tparam EntryType
* @param EntryCount
* @return
*/
template <typename HeaderType, typename EntryType>
static uint32_t GetFileSize(uint32_t EntryCount) {
return sizeof(HeaderType) + sizeof(EntryType) * EntryCount;
}
/*!
* Get the entry count based on the file and HeaderType / EntryType sizes
* @tparam HeaderType
* @tparam EntryType
* @param FilePath
* @return
*/
template <typename HeaderType, typename EntryType>
static uint32_t GetEntryCount(const std::string &FilePath) {
auto fs = GetFileSize(FilePath);
if (fs == 0) {
return 0;
}
return (fs - sizeof(HeaderType)) / sizeof(EntryType);
}
/*!
* Remove an entry at the specified position,
* move all entries to the beginning and truncate the file by
* sizeof(EntryType)
* @tparam HeaderType
* @tparam EntryType
* @param FilePath
* @param position
* @return
*/
template <typename HeaderType, typename EntryType>
static bool RemoveAt(const std::string &FilePath, uint32_t position) {
auto ec = GetEntryCount<HeaderType, EntryType>(FilePath);
if (ec == 0) {
return false;
}
auto *fp = OpenBinaryFile(
FilePath, false, CalculateEntryOffset<HeaderType, EntryType>(position));
if (fp == nullptr) {
return false;
}
bool ok = true;
if (position < ec) {
EntryType tmp;
auto entriesLeft = ec - position;
for (int c = 0; c < entriesLeft; c++) {
if (ReadDataAt<HeaderType>(FilePath, position + 1 + c, tmp)) {
if (!WriteData<HeaderType>(FilePath, tmp, position + c)) {
ok = false;
break;
}
} else {
ok = false;
break;
}
}
}
if (ok) {
ok = ftruncate(fileno(fp),
GetFileSize<HeaderType, EntryType>(
GetEntryCount<HeaderType, EntryType>(FilePath) - 1)) ==
0;
}
CloseBinaryFile(fp);
return ok;
}
/*!
* Delete a file if it exists
* @param FilePath
* @return false if it does not exist, else the return value of
* std::filesystem::remove
*/
static bool DeleteFile(const std::string &FilePath) {
if (!std::filesystem::exists(FilePath)) {
return false;
}
return std::filesystem::remove(FilePath);
}
static bool ReadFile(const std::string &FilePath, char *Output,
uint32_t FileSize) {
auto *fp = OpenBinaryFile(FilePath);
if (fp == nullptr) {
return false;
}
fread(Output, FileSize, 1, fp);
return CloseBinaryFile(fp);
}
};
}
#endif // BINFMT__FILEUTILS_H_