Add metadata!

This commit is contained in:
Your Name
2025-05-03 10:14:16 +12:00
parent 16754a48d4
commit 24a4c66c13
8 changed files with 283 additions and 149 deletions

83
src/compress.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include <fstream>
#include <vector>
#include <filesystem>
#include <string>
#include <random>
#include <chrono>
#include <stdexcept>
#include <iostream> // For error reporting in TempDirectory destructor
#include "compress.hpp"
#include "hash.hpp"
#include "temp_directory.hpp"
namespace simple_object_storage {
std::string decompress_gzip(const std::string& file_path) {
std::ifstream file(file_path, std::ios::binary);
if (!file) return {};
std::vector<char> compressed((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
if (compressed.size() < 2) return {};
// Skip gzip header (10 bytes)
size_t pos = 10;
if (compressed.size() <= pos) return {};
// Prepare zlib stream
z_stream strm = {};
strm.next_in = reinterpret_cast<Bytef*>(compressed.data() + pos);
strm.avail_in = compressed.size() - pos;
if (inflateInit2(&strm, 16 + MAX_WBITS) != Z_OK) return {};
std::string out;
char buffer[4096];
int ret;
do {
strm.next_out = reinterpret_cast<Bytef*>(buffer);
strm.avail_out = sizeof(buffer);
ret = inflate(&strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
inflateEnd(&strm);
return {};
}
out.append(buffer, sizeof(buffer) - strm.avail_out);
} while (ret != Z_STREAM_END);
inflateEnd(&strm);
return out;
}
// if the file is a tgz file (we can't rely on the extension), then unpack on disk and has the contents
// with hash_directory_recursive in hash.hpp
uint64_t get_hash_from_tgz(const std::string &file_path)
{
// check if it's a gzip file
std::ifstream file(file_path, std::ios::binary);
if (!file) return 0;
char buffer[2];
file.read(buffer, 2);
if (buffer[0] != 0x1F || buffer[1] != 0x8B) return 0;
// gunzip the file to a new temporary directory
TempDirectory temp_dir_manager("tgz_unpack_"); // Creates dir and schedules cleanup
std::string temp_dir = temp_dir_manager.string(); // Get the path string to use
std::string decompressed = decompress_gzip(file_path);
// unpack the file on disk
std::string command = "tar -xzf " + file_path + " -C " + temp_dir;
int result = system(command.c_str()); // Basic tar extraction - requires 'tar' command
if (result != 0) {
std::cerr << "Error unpacking tgz file: " << file_path << std::endl;
return 0;
}
// hash the contents
return hash_directory_recursive(temp_dir);
}
} // namespace simple_object_storage