feat(tree): added a simple function to save tree snapshots compressed to the disk

This commit is contained in:
lisk77 2025-08-24 23:52:06 +02:00
parent e52740a03b
commit b7710d02a9
3 changed files with 43 additions and 0 deletions

View file

@ -231,3 +231,39 @@ void walk(const char* base, const char* base_rel, const char* rel, PathBuffer* p
closedir(dir);
}
void save_tree(const char* path, PathBuffer* tree) {
char tmp[1024];
size_t offset = 0;
FILE* fp = fopen(path, "wb");
if (!fp) { perror("ERROR: cannot open path in save_tree!\n"); return; }
offset += snprintf(tmp, sizeof(tmp), "%zu:", tree->len);
for (size_t idx = 0; idx < tree->len; idx++) {
size_t remaining = sizeof(tmp) - offset;
int written = snprintf(tmp + offset, remaining, "%s:", tree->paths[idx]);
if (written < 0 || (size_t)written >= remaining) {
perror("ERROR: buffer overflow in save_tree!\n");
fclose(fp);
return;
}
offset += (size_t)written;
}
uLong originalLen = strlen(tmp) + 1;
uLong compressedLen = compressBound(originalLen);
Bytef compressed[compressedLen];
if (compress(compressed, &compressedLen, (const Bytef*)tmp, originalLen) != Z_OK) {
perror("ERROR: compression failed in save_tree!");
return;
}
fwrite(compressed, sizeof(Bytef), compressedLen, fp);
fclose(fp);
}