diff --git a/src/generator.cpp b/src/generator.cpp index 15c4014..ec5bacf 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -6,6 +6,7 @@ #include #include #include "xxhash.hpp" +#include // For file permissions namespace fs = std::filesystem; @@ -37,6 +38,10 @@ void generate_file_code(const std::string& source, const std::string& destfolder oss << in.rdbuf(); std::string filedata = oss.str(); uint64_t hash = fnv1a_64(filedata.data(), filedata.size()); + + // Get source file permissions + fs::perms src_perms = fs::status(src).permissions(); + // Write HPP std::ofstream hpp(dest / hppname); hpp << "#pragma once\n#include \nnamespace " << ns << " {\nbool recreate_file(std::string destination_folder);\n}\n"; @@ -71,6 +76,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) { cpp << std::dec; cpp << "static const size_t filedata_len = " << filedata.size() << ";\n"; cpp << "static uint64_t file_hash = " << hash << "U;\n"; + cpp << "static std::filesystem::perms file_perms = std::filesystem::perms(" << static_cast(src_perms) << ");\n"; cpp << R"cpp( bool recreate_file(std::string destination_folder) { namespace fs = std::filesystem; @@ -87,6 +93,9 @@ bool recreate_file(std::string destination_folder) { if (needs_write) { std::ofstream out(outpath, std::ios::binary); out.write(reinterpret_cast(filedata), filedata_len); + out.close(); + // Set the file permissions + fs::permissions(outpath, file_perms); } if (!fs::exists(outpath)) { @@ -188,6 +197,9 @@ static uint64_t fnv1a_64(const void* data, size_t len) { oss << in.rdbuf(); std::string filedata = oss.str(); uint64_t hash = fnv1a_64(filedata.data(), filedata.size()); + // Get file permissions + fs::perms file_perms = fs::status(file).permissions(); + std::string rel = fs::relative(file, src).string(); std::string var = sanitize(rel); cpp << "static const unsigned char data_" << var << "[] = {"; @@ -202,6 +214,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) { cpp << "static const size_t len_" << var << " = " << filedata.size() << ";\n"; cpp << "static uint64_t hash_" << var << " = " << hash << "U;\n"; cpp << "static const char* rel_" << var << " = \"" << rel << "\";\n"; + cpp << "static std::filesystem::perms perms_" << var << " = std::filesystem::perms(" << static_cast(file_perms) << ");\n"; } // Write recreate_tree using heredoc style cpp << R"cpp( @@ -226,6 +239,9 @@ bool recreate_tree(std::string destination_folder) { if (needs_write) { std::ofstream out(outpath, std::ios::binary); out.write(reinterpret_cast()cpp" << "data_" << var << R"cpp(), )cpp" << "len_" << var << R"cpp(); + out.close(); + // Set the file permissions + fs::permissions(outpath, )cpp" << "perms_" << var << R"cpp(); } if (!fs::exists(outpath)) {