-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinfmt.h
560 lines (469 loc) · 14.5 KB
/
binfmt.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*!
* license: MIT, see LICENSE.txt
* author: Pascal Eberlein
* version: 1.2
* date: 27.01.2022
*/
#ifndef BINFMT__BINFMT_H_
#define BINFMT__BINFMT_H_
#define LOCK_FREE
#define CAPTURE_ERRORS
#include <vector>
#include <functional>
#include <fcntl.h>
#include <filesystem>
#include <mutex>
#include <unistd.h>
#include <utility>
#ifdef CAPTURE_ERRORS
#include <cstring>
#endif
/*!
* \addtogroup binfmt
* @{
*/
namespace binfmt {
using Path = std::filesystem::path;
using LockGuard = std::lock_guard<std::mutex>;
//! SizeType enum
enum SizeType {
Byte = 1,
KiloByte = SizeType::Byte * 1000,
MegaByte = SizeType::KiloByte * 1000,
GigaByte = SizeType::MegaByte * 1000
};
#ifndef LOCK_FREE
#define LG(mtx) LockGuard ___lg(mtx)
#endif
//! Class to generate a checksum from a buffer
struct Checksum {
/*!
* Generate a basic Checksum
* @param data const char*
* @param length length of data in bytes
* @return uint32 checksum
*/
static uint32_t Generate(const char *data, uint32_t length) {
uint32_t r = 0;
do // NOLINT(altera-unroll-loops)
{
r += *data++;
} while (--length != 0U);
return -r;
}
/*!
* Generate a basic Checksum
* @param data const char*
* @param length length of data in bytes
* @return uint32 checksum
*/
static uint32_t Generate(const std::string &data) {
return Checksum::Generate(data.c_str(), data.size());
}
};
//! BinaryFile header structure which should be directly used or inherited from
struct BinaryFileHeaderBase {
uint32_t magic{0xBEEF};
uint32_t version{0x0001};
uint32_t maxEntries{0};
uint32_t count = 0;
uint32_t offset = 0;
BinaryFileHeaderBase() = default;
explicit BinaryFileHeaderBase(uint32_t magic, uint32_t version,
uint32_t maxEntries)
: magic(magic), version(version), maxEntries(maxEntries) {}
BinaryFileHeaderBase(uint32_t magic, uint32_t version, uint32_t count,
uint32_t offset, uint32_t maxEntries)
: magic(magic), version(version), maxEntries(maxEntries), count(count),
offset(offset) {}
explicit BinaryFileHeaderBase(char *data) {
auto *base = reinterpret_cast<BinaryFileHeaderBase *>(data);
magic = base->magic;
version = base->version;
count = base->count;
offset = base->offset;
maxEntries = base->maxEntries;
}
BinaryFileHeaderBase &
operator=(char *data) // NOLINT(fuchsia-overloaded-operator)
{
auto *other = reinterpret_cast<BinaryFileHeaderBase *>(data);
magic = other->magic;
version = other->version;
count = other->count;
offset = other->offset;
maxEntries = other->maxEntries;
return *this;
}
};
template <typename EntryType> struct BinaryEntryContainer {
uint32_t checksum = 0;
EntryType entry;
BinaryEntryContainer() = default;
explicit BinaryEntryContainer(const EntryType &entry)
: checksum(Checksum::Generate(
(const char *)&entry,
sizeof(EntryType))), // NOLINT(google-readability-casting)
entry(entry) {}
bool isEntryValid() {
return Checksum::Generate(
(const char *)&entry,
sizeof(EntryType)) == // NOLINT(google-readability-casting)
checksum;
}
};
enum class ErrorCode {
OK = 0,
OPEN_ERROR,
MAGIC_MISMATCH,
SEEK_ERROR,
READ_ERROR,
WRITE_ERROR,
SYNC_ERROR,
TRUNCATE_ERROR,
};
template <typename HeaderType, typename EntryType, typename ContainerType>
class BinaryFile {
const uint32_t m_u32HeaderSize = sizeof(HeaderType);
const uint32_t m_u32EntrySize = sizeof(EntryType);
const uint32_t m_u32ContainerSize = sizeof(ContainerType);
Path m_Path;
HeaderType m_ExpectedHeader;
HeaderType m_CurrentHeader;
#ifndef LOCK_FREE
std::mutex m_Mutex;
#endif
int32_t m_Fd = -1;
ErrorCode m_ErrorCode = ErrorCode::OK;
#ifdef CAPTURE_ERRORS
std::vector<std::string> m_Errors{};
#endif
protected:
void onSysCallError(ErrorCode i_ErrorCode,
ErrorCode *o_pErrorCode = nullptr) {
if (o_pErrorCode != nullptr) {
*o_pErrorCode = i_ErrorCode;
}
#ifdef CAPTURE_ERRORS
const char *error = strerror(errno);
if (error != nullptr) {
m_Errors.emplace_back(std::string(error));
}
#endif
}
bool sync(ErrorCode *o_pErrorCode) {
bool bOk = fsync(m_Fd) == 0;
if (!bOk) {
onSysCallError(ErrorCode::SYNC_ERROR, o_pErrorCode);
}
return bOk;
}
template <typename DataType>
bool writeData(DataType i_Data, uint32_t i_u32ByteOffset,
ErrorCode *o_pErrorCode = nullptr) {
bool bOk = pwrite(m_Fd, &i_Data, sizeof(DataType), i_u32ByteOffset) ==
sizeof(DataType);
bOk ? (void)sync(o_pErrorCode)
: onSysCallError(ErrorCode::WRITE_ERROR, o_pErrorCode);
return bOk;
}
template <typename DataType>
bool writeVector(std::vector<DataType> i_Data, uint32_t i_u32ByteOffset,
ErrorCode *o_pErrorCode = nullptr) {
auto expectedWriteSize = i_Data.size() * sizeof(DataType);
bool bOk = pwrite(m_Fd, &i_Data[0], expectedWriteSize, i_u32ByteOffset) ==
expectedWriteSize;
bOk ? (void)sync(o_pErrorCode) : onSysCallError(ErrorCode::WRITE_ERROR);
return bOk;
}
bool truncate(uint32_t i_u32Size, ErrorCode *o_pErrorCode = nullptr) {
bool bOk = ftruncate(m_Fd, i_u32Size) == 0;
bOk ? (void)sync(o_pErrorCode) : onSysCallError(ErrorCode::TRUNCATE_ERROR);
return bOk;
}
template <typename DataType>
bool read(DataType &o_Data, uint32_t i_u32ByteOffset,
ErrorCode *o_pErrorCode = nullptr) {
bool bOk = pread(m_Fd, &o_Data, sizeof(DataType),
static_cast<off_t>(i_u32ByteOffset)) ==
static_cast<ssize_t>(sizeof(DataType));
bOk ? (void)sync(o_pErrorCode) : onSysCallError(ErrorCode::READ_ERROR);
return bOk;
}
template <typename DataType>
bool readVector(std::vector<DataType> &o_Data, uint32_t i_u32ByteOffset,
ErrorCode *o_pErrorCode = nullptr) {
auto expectedReadSize = o_Data.size() * sizeof(DataType);
bool bOk = pread(m_Fd, &o_Data[0], expectedReadSize, i_u32ByteOffset) ==
static_cast<ssize_t>(expectedReadSize);
bOk ? (void)sync(o_pErrorCode) : onSysCallError(ErrorCode::READ_ERROR);
return bOk;
}
uint32_t getByteOffsetFromIndex(uint32_t index) {
return m_u32HeaderSize + (index * m_u32ContainerSize);
}
uint32_t getCurrentByteOffset() {
return m_u32HeaderSize + (m_u32ContainerSize * m_CurrentHeader.offset);
}
bool readHeader(ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return read<HeaderType>(m_CurrentHeader, 0, o_pErrorCode);
}
virtual void onVersionOlder(){};
virtual void onVersionNewer(){};
virtual void onMaxEntriesChanged(){};
bool checkHeader(ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
if (m_CurrentHeader.magic != m_ExpectedHeader.magic) {
*o_pErrorCode = ErrorCode::MAGIC_MISMATCH;
return false;
}
if (m_CurrentHeader.version < m_ExpectedHeader.version) {
onVersionOlder();
} else if (m_CurrentHeader.version > m_ExpectedHeader.version) {
onVersionNewer();
}
if (m_CurrentHeader.maxEntries != m_ExpectedHeader.maxEntries) {
onMaxEntriesChanged();
}
return true;
}
bool writeHeader(ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return writeData(m_ExpectedHeader, 0, o_pErrorCode);
}
bool fixHeader(ErrorCode *o_pErrorCode = nullptr) {
if (writeHeader(o_pErrorCode)) {
if (readHeader(o_pErrorCode)) {
return checkHeader(o_pErrorCode);
}
}
return false;
}
void initialize() {
m_Fd = open(m_Path.c_str(), O_RDWR | O_CREAT,
0644); // NOLINT(hicpp-signed-bitwise)
if (m_Fd < 0) {
m_ErrorCode = ErrorCode::OPEN_ERROR;
#ifdef CAPTURE_ERRORS
m_Errors.emplace_back(strerror(errno));
#endif
}
if (m_ErrorCode == ErrorCode::OK) {
if (!readHeader(&m_ErrorCode) || !checkHeader(&m_ErrorCode)) {
(void)fixHeader(&m_ErrorCode);
}
}
}
public:
BinaryFile(Path i_Path, HeaderType i_Header)
: m_Path(std::move(i_Path)), m_ExpectedHeader(i_Header) {
initialize();
};
~BinaryFile() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
writeData(m_CurrentHeader, 0, nullptr);
close(m_Fd);
};
[[nodiscard]] ErrorCode getErrorCode() const { return m_ErrorCode; }
virtual void beforeAppend(ContainerType /*i_Container*/) {
if (m_CurrentHeader.maxEntries == 0) {
return;
}
if (m_CurrentHeader.offset == m_CurrentHeader.maxEntries) {
m_CurrentHeader.offset = 0;
}
};
virtual void beforeAppend(const std::vector<ContainerType> &i_Containers){};
virtual void onAppendSuccess(ContainerType /*i_Container*/){};
virtual void
onAppendSuccess(const std::vector<ContainerType> & /*i_Containers*/){};
virtual void onAppendFailure(ContainerType i_Container){};
virtual void
onAppendFailure(const std::vector<ContainerType> &i_Containers){};
ErrorCode append(ContainerType i_Container) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
beforeAppend(i_Container);
ErrorCode r = ErrorCode::OK;
if (writeData<ContainerType>(i_Container, getCurrentByteOffset(), &r)) {
(void)sync(&r);
m_CurrentHeader.offset++;
m_CurrentHeader.count++;
onAppendSuccess(i_Container);
} else {
onAppendFailure(i_Container);
}
return r;
}
bool _append(std::vector<ContainerType> &i_Containers,
ErrorCode *o_pErrorCode = nullptr) {
bool r = false;
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
if (m_CurrentHeader.offset + i_Containers.size() >
m_CurrentHeader.maxEntries &&
m_CurrentHeader.maxEntries != 0) {
auto writeableEntries =
m_CurrentHeader.maxEntries - m_CurrentHeader.offset;
auto entriesToWrite = std::vector<ContainerType>(
i_Containers.begin(), i_Containers.begin() + writeableEntries);
if (writeVector(entriesToWrite, getCurrentByteOffset(), o_pErrorCode)) {
m_CurrentHeader.offset = 0;
m_CurrentHeader.count += writeableEntries;
i_Containers = std::vector<ContainerType>(
i_Containers.begin() + writeableEntries, i_Containers.end());
r = _append(i_Containers, o_pErrorCode);
}
} else {
if (writeVector(i_Containers, getCurrentByteOffset(), o_pErrorCode)) {
(i_Containers.size() == m_CurrentHeader.maxEntries)
? m_CurrentHeader.offset = 0
: m_CurrentHeader.offset += i_Containers.size();
m_CurrentHeader.count += i_Containers.size();
}
}
return r;
}
ErrorCode append(std::vector<ContainerType> &i_Containers) {
ErrorCode r = ErrorCode::OK;
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
beforeAppend(i_Containers);
if (_append(i_Containers, &r)) {
onAppendSuccess(i_Containers);
} else {
onAppendFailure(i_Containers);
}
return r;
}
ErrorCode append(EntryType i_Entry) {
ContainerType container(i_Entry);
return append(container);
}
ErrorCode append(const std::vector<EntryType> &i_Entries) {
std::vector<ContainerType> containers;
// NOLINTNEXTLINE(altera-unroll-loops)
for (auto &entry : i_Entries) {
containers.template emplace_back(ContainerType(entry));
}
return append(containers);
}
uint32_t getEntryCountFromFileSize() {
return (getFileSize() - m_u32HeaderSize) / m_u32ContainerSize;
}
uint32_t getEntryCount() { return m_CurrentHeader.count; }
bool getEntriesFromTo(std::vector<ContainerType> &o_Containers,
uint32_t i_u32Start, uint32_t i_u32End,
ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
o_Containers.resize(i_u32End - i_u32Start);
return readVector(o_Containers, getByteOffsetFromIndex(i_u32Start),
o_pErrorCode);
}
bool getEntry(uint32_t i_u32Index, ContainerType &o_Container,
ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return read(o_Container, getByteOffsetFromIndex(i_u32Index), o_pErrorCode);
}
bool getEntriesFrom(std::vector<ContainerType> &o_Containers,
uint32_t i_u32Index, uint32_t i_u32Count,
ErrorCode *o_pErrorCode = nullptr) {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
o_Containers.resize(i_u32Count);
return readVector(o_Containers, getByteOffsetFromIndex(i_u32Index),
o_pErrorCode);
}
bool
getEntriesChunked(std::function<void(std::vector<ContainerType>)> i_Callback,
uint32_t i_u32Begin = 0, uint32_t i_u32End = 0,
uint32_t i_u32ChunkSize = 100000,
ErrorCode *o_pErrorCode = nullptr) {
std::vector<ContainerType> tmp(i_u32ChunkSize);
uint32_t entryCount = getEntryCount();
if (i_u32End == 0) {
i_u32End = entryCount;
}
uint32_t rdCnt = 0;
// NOLINTNEXTLINE(altera-unroll-loops,altera-id-dependent-backward-branch)
while (i_u32Begin < i_u32End) {
if (entryCount < i_u32ChunkSize) {
rdCnt = entryCount;
} else {
rdCnt = i_u32ChunkSize;
}
if (getEntriesFromTo(tmp, i_u32Begin, i_u32Begin + rdCnt, o_pErrorCode)) {
i_u32Begin += rdCnt;
i_Callback(tmp);
} else {
return false;
}
}
return true;
}
bool removeEntryAtEnd() {
m_CurrentHeader.count--;
m_CurrentHeader.offset--;
return truncate(m_CurrentHeader.offset * m_u32ContainerSize +
m_u32HeaderSize);
}
uint32_t getOffset() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return m_CurrentHeader.offset;
}
virtual uint32_t getFileSize() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return std::filesystem::file_size(m_Path);
}
bool deleteFile() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
close(m_Fd);
return std::filesystem::remove(m_Path);
}
HeaderType getHeader() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return m_CurrentHeader;
}
Path getPath() { return m_Path; }
uint32_t getHeaderSize() { return m_u32HeaderSize; }
uint32_t getContainerSize() { return m_u32ContainerSize; }
uint32_t getEntrySize() { return m_u32EntrySize; }
bool clear() {
#ifndef LOCK_FREE
LG(m_Mutex);
#endif
return truncate(m_u32HeaderSize);
}
bool isEmpty() { return getEntryCount() == 0; }
bool getAllEntries(std::vector<ContainerType> &o_Containers) {
return getEntriesFrom(o_Containers, 0, getEntryCount());
}
};
} // namespace binfmt
/*! @} */
#endif // BINFMT__BINFMT_H_