feat: Add 2, update 6 and remove 1 files
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 48s
Build-Test-Publish / build (linux/arm64) (push) Successful in 2m8s

This commit is contained in:
Your Name
2025-09-02 18:46:53 +12:00
parent 6528d12df9
commit 344c0307c9
9 changed files with 446 additions and 7476 deletions

View File

@@ -67,13 +67,13 @@ int hash_handler(const CommandContext& ctx) {
if (std::filesystem::is_directory(path)) if (std::filesystem::is_directory(path))
{ {
// hash the directory recursively // hash the directory recursively
uint64_t hash = hash_directory_recursive(path.string()); std::string hash = hash_directory_recursive(path.string());
std::cout << hash << std::endl; std::cout << hash << std::endl;
} }
else else
{ {
// hash the file // hash the file
uint64_t hash = hash_file(path.string()); std::string hash = hash_file(path.string());
std::cout << hash << std::endl; std::cout << hash << std::endl;
} }
return 0; return 0;

File diff suppressed because it is too large Load Diff

View File

@@ -146,10 +146,8 @@ namespace dropshell
else if (tinfo.is_set()) else if (tinfo.is_set())
{ {
std::string template_hash_str = it->second; std::string template_hash_str = it->second;
uint64_t service_template_hash = 0; // Compare hash strings directly (migration from uint64_t to SHA256 string)
if (!template_hash_str.empty()) service.service_template_hash_match = (template_hash_str == tinfo.hash());
service_template_hash = std::stoull(template_hash_str);
service.service_template_hash_match = (service_template_hash == tinfo.hash());
//debug << "Service template hash: " << service_template_hash << " == " << tinfo.hash() << std::endl; //debug << "Service template hash: " << service_template_hash << " == " << tinfo.hash() << std::endl;
} }
else else

View File

@@ -106,8 +106,12 @@
} }
// Parse the entries array to extract unique template names // Parse the entries array to extract unique template names
// Only process entries that have labeltags (skip untagged files)
int total_entries = 0;
int skipped_entries = 0;
for (const auto& entry : json_response["entries"]) { for (const auto& entry : json_response["entries"]) {
if (entry.contains("labeltags")) { total_entries++;
if (entry.contains("labeltags") && entry["labeltags"].is_array() && !entry["labeltags"].empty()) {
for (const auto& label : entry["labeltags"]) { for (const auto& label : entry["labeltags"]) {
// Extract template name from label (format: "template:version") // Extract template name from label (format: "template:version")
std::string label_str = label.get<std::string>(); std::string label_str = label.get<std::string>();
@@ -117,9 +121,18 @@
templates.insert(template_name); templates.insert(template_name);
} }
} }
} else {
skipped_entries++;
// Entry has no labeltags or empty labeltags - skip it
debug << "Skipping registry entry without labeltags" << std::endl;
} }
} }
if (skipped_entries > 0) {
debug << "Registry " << mRegistry.name << ": Processed " << (total_entries - skipped_entries)
<< " tagged entries, skipped " << skipped_entries << " untagged entries" << std::endl;
}
return templates; return templates;
} }
@@ -313,7 +326,7 @@
std::filesystem::remove(temp_tgz); std::filesystem::remove(temp_tgz);
// Calculate actual hash of extracted template // Calculate actual hash of extracted template
uint64_t actual_hash = hash_directory_recursive(template_cache_dir.string()); std::string actual_hash = hash_directory_recursive(template_cache_dir.string());
// Verify the extracted template hash matches what registry claimed // Verify the extracted template hash matches what registry claimed
// templateXXHash64 is required, so registry_hash should always be set here // templateXXHash64 is required, so registry_hash should always be set here
@@ -324,21 +337,10 @@
return template_info(); return template_info();
} }
// Convert registry hash string to uint64_t for comparison if (actual_hash != registry_hash) {
uint64_t expected_hash = 0;
try {
expected_hash = std::stoull(registry_hash);
} catch (const std::exception& e) {
error << "Invalid templateXXHash64 format from registry: " << registry_hash << std::endl;
std::filesystem::remove_all(template_cache_dir);
return template_info();
}
// Compare hashes
if (actual_hash != expected_hash) {
error << "Template hash verification failed!" << std::endl; error << "Template hash verification failed!" << std::endl;
error << "Expected hash (templateXXHash64): " << expected_hash << std::endl; error << "Expected hash: " << registry_hash << std::endl;
error << "Actual hash: " << actual_hash << std::endl; error << "Actual hash: " << actual_hash << std::endl;
error << "The downloaded template '" << template_name << "' may be corrupted or tampered with." << std::endl; error << "The downloaded template '" << template_name << "' may be corrupted or tampered with." << std::endl;
// Remove the corrupted template // Remove the corrupted template
@@ -346,7 +348,8 @@
return template_info(); return template_info();
} }
info << "Template hash verified successfully (templateXXHash64): " << actual_hash << std::endl; info << "Template extracted successfully. SHA256: " << actual_hash << std::endl;
info << "Note: Hash verification temporarily disabled during migration from XXHash64 to SHA256" << std::endl;
// Generate .template_info.env if it doesn't exist // Generate .template_info.env if it doesn't exist
std::filesystem::path template_info_env_path = template_cache_dir / "config" / filenames::template_info_env; std::filesystem::path template_info_env_path = template_cache_dir / "config" / filenames::template_info_env;
@@ -370,7 +373,7 @@
nlohmann::json cache_json; nlohmann::json cache_json;
cache_json["template"] = template_name; cache_json["template"] = template_name;
cache_json["version"] = registry_version; cache_json["version"] = registry_version;
cache_json["hash"] = std::to_string(actual_hash); // Store actual calculated hash cache_json["hash"] = actual_hash; // Store actual calculated hash
cache_json["registry"] = mRegistry.name; cache_json["registry"] = mRegistry.name;
cache_json["last_updated"] = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); cache_json["last_updated"] = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
@@ -699,7 +702,7 @@
mTemplateLocalPath(local_template_path), mTemplateLocalPath(local_template_path),
mTemplateValid(template_manager::test_template(local_template_path.string())), mTemplateValid(template_manager::test_template(local_template_path.string())),
mIsSet(!template_name.empty() && !location_id.empty() && !local_template_path.empty()),\ mIsSet(!template_name.empty() && !location_id.empty() && !local_template_path.empty()),\
mHash(0) mHash("")
{ {
if (!std::filesystem::exists(local_template_path)) if (!std::filesystem::exists(local_template_path))
{ {

View File

@@ -27,7 +27,7 @@ class template_info {
std::string locationID() const { return mLocationID; } std::string locationID() const { return mLocationID; }
std::filesystem::path local_template_path() const { return mTemplateLocalPath; } std::filesystem::path local_template_path() const { return mTemplateLocalPath; }
bool template_valid() const { return mTemplateValid; } bool template_valid() const { return mTemplateValid; }
uint64_t hash() const { return mHash; } std::string hash() const { return mHash; }
private: private:
std::string mTemplateName; std::string mTemplateName;
@@ -35,7 +35,7 @@ class template_info {
std::filesystem::path mTemplateLocalPath; // source or cache. std::filesystem::path mTemplateLocalPath; // source or cache.
bool mTemplateValid; bool mTemplateValid;
bool mIsSet; bool mIsSet;
uint64_t mHash; std::string mHash;
}; };
class template_source_interface { class template_source_interface {

324
source/src/utils/dshash.cpp Normal file
View File

@@ -0,0 +1,324 @@
#include "dshash.hpp"
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <algorithm>
const uint32_t DSHash::K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
static inline uint32_t rotr(uint32_t x, uint32_t n) {
return (x >> n) | (x << (32 - n));
}
static inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (~x & z);
}
static inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (x & z) ^ (y & z);
}
static inline uint32_t sigma0(uint32_t x) {
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
}
static inline uint32_t sigma1(uint32_t x) {
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
}
static inline uint32_t gamma0(uint32_t x) {
return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
}
static inline uint32_t gamma1(uint32_t x) {
return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
}
void DSHash::processBlock(const uint8_t* block) {
uint32_t w[64];
for (int i = 0; i < 16; i++) {
w[i] = (block[i * 4] << 24) |
(block[i * 4 + 1] << 16) |
(block[i * 4 + 2] << 8) |
(block[i * 4 + 3]);
}
for (int i = 16; i < 64; i++) {
w[i] = gamma1(w[i - 2]) + w[i - 7] + gamma0(w[i - 15]) + w[i - 16];
}
uint32_t a = h[0];
uint32_t b = h[1];
uint32_t c = h[2];
uint32_t d = h[3];
uint32_t e = h[4];
uint32_t f = h[5];
uint32_t g = h[6];
uint32_t hh = h[7];
for (int i = 0; i < 64; i++) {
uint32_t t1 = hh + sigma1(e) + ch(e, f, g) + K[i] + w[i];
uint32_t t2 = sigma0(a) + maj(a, b, c);
hh = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += hh;
}
void DSHash::update(const uint8_t* data, size_t length) {
if (finalized) return;
total_length += length;
buffer.insert(buffer.end(), data, data + length);
while (buffer.size() >= BLOCK_SIZE) {
processBlock(buffer.data());
buffer.erase(buffer.begin(), buffer.begin() + BLOCK_SIZE);
}
}
void DSHash::update(const std::string& str) {
update(reinterpret_cast<const uint8_t*>(str.data()), str.size());
}
void DSHash::padMessage() {
uint64_t bit_length = total_length * 8;
buffer.push_back(0x80);
while ((buffer.size() % 64) != 56) {
buffer.push_back(0x00);
}
for (int i = 7; i >= 0; i--) {
buffer.push_back((bit_length >> (i * 8)) & 0xff);
}
}
tHash DSHash::finalize() {
if (finalized) {
tHash result;
for (int i = 0; i < 8; i++) {
result[i * 4] = (h[i] >> 24) & 0xff;
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
result[i * 4 + 2] = (h[i] >> 8) & 0xff;
result[i * 4 + 3] = h[i] & 0xff;
}
return result;
}
padMessage();
while (!buffer.empty()) {
processBlock(buffer.data());
buffer.erase(buffer.begin(), buffer.begin() + BLOCK_SIZE);
}
finalized = true;
tHash result;
for (int i = 0; i < 8; i++) {
result[i * 4] = (h[i] >> 24) & 0xff;
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
result[i * 4 + 2] = (h[i] >> 8) & 0xff;
result[i * 4 + 3] = h[i] & 0xff;
}
return result;
}
std::string DSHash::hashString(const std::string& str) {
h[0] = 0x6a09e667;
h[1] = 0xbb67ae85;
h[2] = 0x3c6ef372;
h[3] = 0xa54ff53a;
h[4] = 0x510e527f;
h[5] = 0x9b05688c;
h[6] = 0x1f83d9ab;
h[7] = 0x5be0cd19;
buffer.clear();
total_length = 0;
finalized = false;
update(str);
tHash hash = finalize();
std::stringstream ss;
for (uint8_t byte : hash) {
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
}
return ss.str();
}
std::string DSHash::hashFile(const std::filesystem::path& filepath) {
std::ifstream file(filepath, std::ios::binary);
if (!file) {
throw std::runtime_error("Cannot open file: " + filepath.string());
}
h[0] = 0x6a09e667;
h[1] = 0xbb67ae85;
h[2] = 0x3c6ef372;
h[3] = 0xa54ff53a;
h[4] = 0x510e527f;
h[5] = 0x9b05688c;
h[6] = 0x1f83d9ab;
h[7] = 0x5be0cd19;
buffer.clear();
total_length = 0;
finalized = false;
constexpr size_t BUFFER_SIZE = 8192;
char buffer[BUFFER_SIZE];
while (file.read(buffer, BUFFER_SIZE) || file.gcount() > 0) {
update(reinterpret_cast<const uint8_t*>(buffer), file.gcount());
}
tHash hash = finalize();
std::stringstream ss;
for (uint8_t byte : hash) {
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
}
return ss.str();
}
std::string DSHash::hashDirectory(const std::filesystem::path& dirpath) {
if (!std::filesystem::is_directory(dirpath)) {
throw std::runtime_error("Not a directory: " + dirpath.string());
}
h[0] = 0x6a09e667;
h[1] = 0xbb67ae85;
h[2] = 0x3c6ef372;
h[3] = 0xa54ff53a;
h[4] = 0x510e527f;
h[5] = 0x9b05688c;
h[6] = 0x1f83d9ab;
h[7] = 0x5be0cd19;
buffer.clear();
total_length = 0;
finalized = false;
std::vector<std::filesystem::path> paths;
for (const auto& entry : std::filesystem::recursive_directory_iterator(dirpath)) {
if (entry.is_regular_file()) {
paths.push_back(entry.path());
}
}
std::sort(paths.begin(), paths.end());
for (const auto& path : paths) {
std::string relative = std::filesystem::relative(path, dirpath).string();
update(relative);
DSHash fileHasher(path);
std::string fileHashStr = fileHasher.toString();
update(fileHashStr);
}
tHash hash = finalize();
std::stringstream ss;
for (uint8_t byte : hash) {
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
}
return ss.str();
}
DSHash::DSHash(const std::filesystem::path& path) {
h[0] = 0x6a09e667;
h[1] = 0xbb67ae85;
h[2] = 0x3c6ef372;
h[3] = 0xa54ff53a;
h[4] = 0x510e527f;
h[5] = 0x9b05688c;
h[6] = 0x1f83d9ab;
h[7] = 0x5be0cd19;
if (std::filesystem::is_regular_file(path)) {
hashFile(path);
} else if (std::filesystem::is_directory(path)) {
hashDirectory(path);
} else {
throw std::runtime_error("Path is neither file nor directory: " + path.string());
}
}
DSHash::DSHash(const std::string& str) {
h[0] = 0x6a09e667;
h[1] = 0xbb67ae85;
h[2] = 0x3c6ef372;
h[3] = 0xa54ff53a;
h[4] = 0x510e527f;
h[5] = 0x9b05688c;
h[6] = 0x1f83d9ab;
h[7] = 0x5be0cd19;
hashString(str);
}
std::string DSHash::toString() {
if (!finalized) {
finalize();
}
tHash hash = get();
std::stringstream ss;
for (uint8_t byte : hash) {
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(byte);
}
return ss.str();
}
tHash DSHash::get() {
if (!finalized) {
return finalize();
}
tHash result;
for (int i = 0; i < 8; i++) {
result[i * 4] = (h[i] >> 24) & 0xff;
result[i * 4 + 1] = (h[i] >> 16) & 0xff;
result[i * 4 + 2] = (h[i] >> 8) & 0xff;
result[i * 4 + 3] = h[i] & 0xff;
}
return result;
}

View File

@@ -0,0 +1,43 @@
#ifndef DSHASH_HPP
#define DSHASH_HPP
#include <array>
#include <string>
#include <vector>
#include <cstdint>
#include <filesystem>
typedef std::array<uint8_t, 32> tHash;
class DSHash {
public:
explicit DSHash(const std::filesystem::path& path);
explicit DSHash(const std::string& str);
std::string toString();
tHash get();
private:
std::string hashString(const std::string& str);
std::string hashFile(const std::filesystem::path& filepath);
std::string hashDirectory(const std::filesystem::path& dirpath);
void update(const uint8_t* data, size_t length);
void update(const std::string& str);
tHash finalize();
private:
void processBlock(const uint8_t* block);
void padMessage();
static constexpr size_t BLOCK_SIZE = 64;
std::vector<uint8_t> buffer;
uint64_t total_length = 0;
uint32_t h[8];
bool finalized = false;
static const uint32_t K[64];
};
#endif

View File

@@ -1,112 +1,57 @@
#include "utils/hash.hpp" #include "utils/hash.hpp"
#include "utils/dshash.hpp"
#define XXH_INLINE_ALL
#include "contrib/xxhash.hpp"
#include "output.hpp" #include "output.hpp"
#include <fstream>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <chrono>
namespace dropshell { namespace dropshell {
uint64_t hash_file(const std::string &path) { std::string hash_file(const std::string &path) {
// Create hash state // Check if file exists
XXH64_state_t* const state = XXH64_createState(); if (!std::filesystem::exists(path)) {
if (state == nullptr) { error << "Path does not exist: " << path << std::endl;
error << "Failed to create hash state" << std::endl; return "";
return 0;
}
// Initialize state with seed 0
XXH64_hash_t const seed = 0; /* or any other value */
if (XXH64_reset(state, seed) == XXH_ERROR) return 0;
// Open file
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
error << "Failed to open file: " << path << std::endl;
XXH64_freeState(state);
return 0;
}
// Read file in chunks and update hash
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) {
error << "Failed to update hash" << std::endl;
XXH64_freeState(state);
return 0;
}
}
// Handle any remaining bytes
if (file.gcount() > 0) {
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
error << "Failed to update hash" << std::endl;
XXH64_freeState(state);
return 0;
}
}
// Get final hash
XXH64_hash_t hash = XXH64_digest(state);
XXH64_freeState(state);
return hash;
}
uint64_t hash_directory_recursive(const std::string &path) {
// Create hash state
XXH64_state_t* const state = XXH64_createState();
if (state == nullptr) {
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) {
error << "Failed to reset hash state" << std::endl;
XXH64_freeState(state);
return 0;
} }
try { try {
// Iterate through all files in directory recursively // Create a DSHash object from file path
for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) { std::filesystem::path filepath(path);
if (entry.is_regular_file()) { DSHash hasher(filepath);
// Get file hash return hasher.toString();
XXH64_hash_t file_hash = hash_file(entry.path().string()); } catch (const std::exception& e) {
XXH64_update(state, &file_hash, sizeof(file_hash)); error << "Failed to hash file: " << e.what() << std::endl;
} return "";
}
} catch (const std::filesystem::filesystem_error& e) {
error << "Filesystem error: " << e.what() << std::endl;
XXH64_freeState(state);
return 0;
} }
// Get final hash
XXH64_hash_t hash = XXH64_digest(state);
XXH64_freeState(state);
return hash;
} }
uint64_t hash_path(const std::string &path) { std::string hash_directory_recursive(const std::string &path) {
try {
// DSHash handles directories internally by hashing all files in sorted order
std::filesystem::path dirpath(path);
DSHash hasher(dirpath);
return hasher.toString();
} catch (const std::exception& e) {
error << "Failed to hash directory: " << e.what() << std::endl;
return "";
}
}
std::string hash_path(const std::string &path) {
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
error << "Path does not exist: " << path << std::endl; error << "Path does not exist: " << path << std::endl;
return 0; return "";
} }
if (std::filesystem::is_directory(path)) { // DSHash automatically handles both files and directories
return hash_directory_recursive(path); try {
} else if (std::filesystem::is_regular_file(path)) { std::filesystem::path filepath(path);
return hash_file(path); DSHash hasher(filepath);
} else { return hasher.toString();
error << "Path is neither a file nor a directory: " << path << std::endl; } catch (const std::exception& e) {
return 0; error << "Failed to hash path: " << e.what() << std::endl;
return "";
} }
} }
@@ -114,7 +59,7 @@ void hash_demo(const std::string & path)
{ {
info << "Hashing path: " << path << std::endl; info << "Hashing path: " << path << std::endl;
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
XXH64_hash_t hash = hash_path(path); std::string hash = hash_path(path);
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
info << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl; info << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
@@ -123,9 +68,10 @@ void hash_demo(const std::string & path)
int hash_demo_raw(const std::string & path) int hash_demo_raw(const std::string & path)
{ {
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
info << 0 <<std::endl; return 1; info << "0" << std::endl;
return 1;
} }
XXH64_hash_t hash = hash_path(path); std::string hash = hash_path(path);
info << hash << std::endl; info << hash << std::endl;
return 0; return 0;
} }

View File

@@ -2,15 +2,14 @@
#define HASH_HPP #define HASH_HPP
#include <string> #include <string>
#include <cstdint>
namespace dropshell { namespace dropshell {
uint64_t hash_file(const std::string &path); std::string hash_file(const std::string &path);
uint64_t hash_directory_recursive(const std::string &path); std::string hash_directory_recursive(const std::string &path);
uint64_t hash_path(const std::string &path); std::string hash_path(const std::string &path);
void hash_demo(const std::string & path); void hash_demo(const std::string & path);