Files
simple-object-server/src/compress.cpp
Your Name 3c5fc11065 filename
2025-05-03 11:15:12 +12:00

45 lines
1.3 KiB
C++

#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 {
// 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;
int result = system("gunzip -t file.gz");
if (result != 0) { // not a gzip file.
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
// unpack the file on disk
std::string command = "tar -zxzf " + file_path + " -C " + temp_dir;
result = system(command.c_str()); // Basic tar extraction - requires 'tar' command
if (result != 0) {
return 0;
}
// hash the contents
return hash_directory_recursive(temp_dir);
}
} // namespace simple_object_storage