MCPcopy Create free account
hub / github.com/TactilityProject/Tactility / open

Method open

Tactility/Source/file/ObjectFileWriter.cpp:12–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10static const auto LOGGER = Logger("ObjectFileWriter");
11
12bool ObjectFileWriter::open() {
13 bool edit_existing = append && access(filePath.c_str(), F_OK) == 0;
14 if (append && !edit_existing) {
15 LOGGER.warn("access() to {} failed: {}", filePath, strerror(errno));
16 }
17
18 // Edit existing or create a new file
19 auto opening_file = std::unique_ptr<FILE, FileCloser>(std::fopen(filePath.c_str(), edit_existing ? "rb+" : "wb"));
20 if (opening_file == nullptr) {
21 LOGGER.error("Failed to open file {}", filePath);
22 return false;
23 }
24
25 auto file_size = getSize(opening_file.get());
26 if (file_size > 0 && edit_existing) {
27
28 // Read and parse file header
29
30 FileHeader file_header;
31 if (fread(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) {
32 LOGGER.error("Failed to read file header from {}", filePath);
33 return false;
34 }
35
36 if (file_header.identifier != OBJECT_FILE_IDENTIFIER) {
37 LOGGER.error("Invalid file type for {}", filePath);
38 return false;
39 }
40
41 if (file_header.version != OBJECT_FILE_VERSION) {
42 LOGGER.error("Unknown version for {}: {}", filePath, file_header.version);
43 return false;
44 }
45
46 // Read and parse content header
47
48 ContentHeader content_header;
49 if (fread(&content_header, sizeof(ContentHeader), 1, opening_file.get()) != 1) {
50 LOGGER.error("Failed to read content header from {}", filePath);
51 return false;
52 }
53
54 if (recordSize != content_header.recordSize) {
55 LOGGER.error("Record size mismatch for {}: expected {}, got {}", filePath, recordSize, content_header.recordSize);
56 return false;
57 }
58
59 if (recordVersion != content_header.recordVersion) {
60 LOGGER.error("Version mismatch for {}: expected {}, got {}", filePath, recordVersion, content_header.recordVersion);
61 return false;
62 }
63
64 recordsWritten = content_header.recordCount;
65 fseek(opening_file.get(), 0, SEEK_END);
66 } else {
67 FileHeader file_header;
68 if (fwrite(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) {
69 LOGGER.error("Failed to write file header for {}", filePath);

Callers

nothing calls this directly

Calls 4

getSizeFunction · 0.85
warnMethod · 0.80
errorMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected