This commit is contained in:
Your Name
2025-04-29 22:44:46 +12:00
parent 3db05c0de9
commit 1ef4a16b66
5 changed files with 8279 additions and 28 deletions

View File

@@ -1,4 +1,9 @@
#include "hash.hpp"
#include "utils/hash.hpp"
#define XXH_INLINE_ALL
#include "contrib/xxhash.hpp"
#include <fstream>
#include <filesystem>
#include <iostream>
@@ -7,24 +12,20 @@ namespace dropshell {
XXH64_hash_t hash_file(const std::string &path) {
// Create hash state
XXH64_state_t* const state = XXH64_createState();
XXH3_state_t* const state = XXH3_createState();
if (state == nullptr) {
std::cerr << "Failed to create hash state" << std::endl;
return 0;
}
// Initialize state with seed 0
if (XXH64_reset(state, 0) == XXH_ERROR) {
std::cerr << "Failed to reset hash state" << std::endl;
XXH64_freeState(state);
return 0;
}
XXH3_64bits_reset(state);
// Open file
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << path << std::endl;
XXH64_freeState(state);
XXH3_freeState(state);
return 0;
}
@@ -32,40 +33,40 @@ XXH64_hash_t hash_file(const std::string &path) {
const size_t buffer_size = 4096;
char buffer[buffer_size];
while (file.read(buffer, buffer_size)) {
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
if (XXH3_64bits_update(state, buffer, file.gcount()) == XXH_ERROR) {
std::cerr << "Failed to update hash" << std::endl;
XXH64_freeState(state);
XXH3_freeState(state);
return 0;
}
}
// Handle any remaining bytes
if (file.gcount() > 0) {
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
if (XXH3_64bits_update(state, buffer, file.gcount()) == XXH_ERROR) {
std::cerr << "Failed to update hash" << std::endl;
XXH64_freeState(state);
XXH3_freeState(state);
return 0;
}
}
// Get final hash
XXH64_hash_t hash = XXH64_digest(state);
XXH64_freeState(state);
XXH64_hash_t hash = XXH3_64bits_digest(state);
XXH3_freeState(state);
return hash;
}
XXH64_hash_t hash_directory_recursive(const std::string &path) {
// Create hash state
XXH64_state_t* const state = XXH64_createState();
XXH3_state_t* const state = XXH3_createState();
if (state == nullptr) {
std::cerr << "Failed to create hash state" << std::endl;
return 0;
}
// Initialize state with seed 0
if (XXH64_reset(state, 0) == XXH_ERROR) {
if (XXH3_64bits_reset(state) == XXH_ERROR) {
std::cerr << "Failed to reset hash state" << std::endl;
XXH64_freeState(state);
XXH3_freeState(state);
return 0;
}
@@ -75,24 +76,18 @@ XXH64_hash_t hash_directory_recursive(const std::string &path) {
if (entry.is_regular_file()) {
// Get file hash
XXH64_hash_t file_hash = hash_file(entry.path().string());
// Update directory hash with file hash
if (XXH64_update(state, &file_hash, sizeof(file_hash)) == XXH_ERROR) {
std::cerr << "Failed to update hash" << std::endl;
XXH64_freeState(state);
return 0;
}
XXH3_64bits_update(state, &file_hash, sizeof(file_hash));
}
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << std::endl;
XXH64_freeState(state);
XXH3_freeState(state);
return 0;
}
// Get final hash
XXH64_hash_t hash = XXH64_digest(state);
XXH64_freeState(state);
XXH64_hash_t hash = XXH3_64bits_digest(state);
XXH3_freeState(state);
return hash;
}