This commit is contained in:
Your Name 2025-05-17 11:26:59 +12:00
parent 446cb7be90
commit d101a700aa

View File

@ -6,6 +6,7 @@
#include <sstream>
#include <vector>
#include "xxhash.hpp"
#include <sys/stat.h> // 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 <string>\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<unsigned>(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<const char*>(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<unsigned>(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<const char*>()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)) {