From e641e02478f1c70a0ac7798d1cb077cd17a875f4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 17 May 2025 11:54:07 +1200 Subject: [PATCH] Ensure permissions are written. --- src/generator.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/generator.cpp b/src/generator.cpp index 3f03c35..3681a0f 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -87,15 +87,27 @@ static void base64_decode(const char* encoded_data, size_t encoded_len, unsigned // Utility function to recreate a file with proper permissions static bool _recreate_file_(const std::filesystem::path& outpath, uint64_t file_hash, std::filesystem::perms file_perms, const unsigned char* filedata, size_t filedata_len) { namespace fs = std::filesystem; - uint64_t existing_hash = 0; + bool needs_write = false; + + // Check if file exists and has correct hash if (fs::exists(outpath)) { + // Check content hash std::ifstream in(outpath, std::ios::binary); std::ostringstream oss; oss << in.rdbuf(); std::string data = oss.str(); - existing_hash = fnv1a_64(data.data(), data.size()); + uint64_t existing_hash = fnv1a_64(data.data(), data.size()); + needs_write = existing_hash != file_hash; + } else { + needs_write = true; // File doesn't exist, need to create it } - bool needs_write = !fs::exists(outpath) || existing_hash != file_hash; + + bool needs_permission_update = true; + if (!needs_write) { // we always update permissions if the file is written or changed. Othewise we check. + fs::perms current_perms = fs::status(outpath).permissions(); + needs_permission_update = current_perms != file_perms; + } + if (needs_write) { fs::create_directories(outpath.parent_path()); std::ofstream out(outpath, std::ios::binary); @@ -103,14 +115,23 @@ static bool _recreate_file_(const std::filesystem::path& outpath, uint64_t file_ out.close(); // Set the file permissions fs::permissions(outpath, file_perms); + + if (!fs::exists(outpath)) { + std::cout << "[dehydrate] " << outpath.filename() << ": created\n"; + } else { + std::cout << "[dehydrate] " << outpath.filename() << ": updated (hash changed)\n"; + } + return true; + } + + if (needs_permission_update) { + // Update only permissions + fs::permissions(outpath, file_perms); + std::cout << "[dehydrate] " << outpath.filename() << ": updated (permissions changed)\n"; + return true; } - if (!fs::exists(outpath)) { - std::cout << "[dehydrate] " << outpath.filename() << ": created\n"; - } else if (needs_write) { - std::cout << "[dehydrate] " << outpath.filename() << ": updated (hash changed)\n"; - } - return needs_write; + return false; } )cpp"; }