This commit is contained in:
@ -3,6 +3,8 @@
|
||||
#define XXH_INLINE_ALL
|
||||
#include "contrib/xxhash.hpp"
|
||||
|
||||
#include "output.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
@ -13,7 +15,7 @@ uint64_t hash_file(const std::string &path) {
|
||||
// Create hash state
|
||||
XXH64_state_t* const state = XXH64_createState();
|
||||
if (state == nullptr) {
|
||||
std::cerr << "Failed to create hash state" << std::endl;
|
||||
error << "Failed to create hash state" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -24,7 +26,7 @@ uint64_t hash_file(const std::string &path) {
|
||||
// Open file
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open file: " << path << std::endl;
|
||||
error << "Failed to open file: " << path << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
@ -34,7 +36,7 @@ uint64_t hash_file(const std::string &path) {
|
||||
char buffer[buffer_size];
|
||||
while (file.read(buffer, buffer_size)) {
|
||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||
std::cerr << "Failed to update hash" << std::endl;
|
||||
error << "Failed to update hash" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
@ -43,7 +45,7 @@ uint64_t hash_file(const std::string &path) {
|
||||
// Handle any remaining bytes
|
||||
if (file.gcount() > 0) {
|
||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||
std::cerr << "Failed to update hash" << std::endl;
|
||||
error << "Failed to update hash" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
@ -59,14 +61,14 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
||||
// Create hash state
|
||||
XXH64_state_t* const state = XXH64_createState();
|
||||
if (state == nullptr) {
|
||||
std::cerr << "Failed to create hash state" << std::endl;
|
||||
error << "Failed to create hash state" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialize state with seed 0
|
||||
XXH64_hash_t const seed = 0; /* or any other value */
|
||||
if (XXH64_reset(state, seed) == XXH_ERROR) {
|
||||
std::cerr << "Failed to reset hash state" << std::endl;
|
||||
error << "Failed to reset hash state" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
@ -81,7 +83,7 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
||||
}
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Filesystem error: " << e.what() << std::endl;
|
||||
error << "Filesystem error: " << e.what() << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
@ -94,7 +96,7 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
||||
|
||||
uint64_t hash_path(const std::string &path) {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cerr << "Path does not exist: " << path << std::endl;
|
||||
error << "Path does not exist: " << path << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -103,28 +105,28 @@ uint64_t hash_path(const std::string &path) {
|
||||
} else if (std::filesystem::is_regular_file(path)) {
|
||||
return hash_file(path);
|
||||
} else {
|
||||
std::cerr << "Path is neither a file nor a directory: " << path << std::endl;
|
||||
error << "Path is neither a file nor a directory: " << path << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void hash_demo(const std::string & path)
|
||||
{
|
||||
std::cout << "Hashing path: " << path << std::endl;
|
||||
info << "Hashing path: " << path << std::endl;
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
XXH64_hash_t hash = hash_path(path);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
||||
info << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
||||
}
|
||||
|
||||
int hash_demo_raw(const std::string & path)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << 0 <<std::endl; return 1;
|
||||
info << 0 <<std::endl; return 1;
|
||||
}
|
||||
XXH64_hash_t hash = hash_path(path);
|
||||
std::cout << hash << std::endl;
|
||||
info << hash << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user