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

@ -54,3 +54,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Find and link zlib
find_package(ZLIB REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE ZLIB::ZLIB)

View file

@ -8,6 +8,8 @@
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <zlib.h>
typedef struct {
char** paths;
@ -20,5 +22,6 @@ void add_path(PathBuffer*, char*);
void free_path_buffer(PathBuffer*);
char* find_root(char*);
void walk(const char*, const char*, const char*, PathBuffer*);
void save_tree(const char*, PathBuffer*);
#endif // TREE_H

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);
}