improved metadata safety when sealing -- always needs to refresh

master
Chris Punches 2025-03-26 01:18:09 -04:00
parent 1d34a62e38
commit 15360edc42
2 changed files with 30 additions and 19 deletions

View File

@ -25,6 +25,7 @@
#include <archive_entry.h> #include <archive_entry.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <metadata.hpp>
/** /**
* @brief First phase of sealing a package stage directory * @brief First phase of sealing a package stage directory
@ -57,12 +58,12 @@ int seal_final_package(const std::string &stage_dir, const std::string &output_d
* Extracts a sealed package file back to its original stage directory structure * Extracts a sealed package file back to its original stage directory structure
* by expanding the gzipped tarballs. * by expanding the gzipped tarballs.
* *
* @param package_filepath Path to the sealed package file * @param package_path Path to the sealed package file
* @param output_dir Path to extract the package stage to * @param output_dir Path to extract the package stage to
* @param force Whether to force the operation even if warnings occur * @param force Whether to force the operation even if warnings occur
* @return 0 on success, non-zero on failure * @return 0 on success, non-zero on failure
*/ */
int unseal_package(const std::string& package_filepath, const std::string& output_dir, bool force); int unseal_package(const std::string& package_path, const std::string& output_dir, bool force);
/** /**

View File

@ -1,5 +1,6 @@
#include "sealing.hpp" #include "sealing.hpp"
bool file_already_compressed(const std::string& path) bool file_already_compressed(const std::string& path)
{ {
// Convert string to filesystem path // Convert string to filesystem path
@ -430,50 +431,59 @@ bool smart_compress_component( const std::filesystem::path& stage_dir, const std
return true; return true;
} }
int seal_stage_components( const std::string& stage_dir, bool force )
int seal_stage_components(const std::string& stage_dir, bool force)
{ {
dpm_log(LOG_INFO, ("Sealing package stage: " + stage_dir).c_str()); dpm_con(LOG_INFO, ("Sealing package stage: " + stage_dir).c_str());
// First refresh the metadata to ensure it's up-to-date
dpm_con(LOG_INFO, "Refreshing metadata before sealing...");
bool metadata_refresh_result = metadata_refresh_dynamic_files(stage_dir);
if (!metadata_refresh_result) {
dpm_con(LOG_ERROR, "Failed to refresh metadata files before sealing. Aborting.");
return 1;
}
// Verify the stage directory structure // Verify the stage directory structure
std::filesystem::path stage_path( stage_dir ); std::filesystem::path stage_path(stage_dir);
if (! smart_compress_component( stage_dir, "contents" ) ) { if (!smart_compress_component(stage_dir, "contents")) {
dpm_log(LOG_FATAL, ("Failed to compress contents: " + stage_dir).c_str() ); dpm_con(LOG_FATAL, ("Failed to compress contents: " + stage_dir).c_str());
return 1; return 1;
} }
if (! smart_compress_component( stage_dir, "hooks" ) ) { if (!smart_compress_component(stage_dir, "hooks")) {
dpm_log(LOG_FATAL, ("Failed to compress hooks: " + stage_dir).c_str() ); dpm_con(LOG_FATAL, ("Failed to compress hooks: " + stage_dir).c_str());
return 1; return 1;
} }
if (! smart_compress_component( stage_dir, "metadata" ) ) { if (!smart_compress_component(stage_dir, "metadata")) {
dpm_log(LOG_FATAL, ("Failed to compress metadata: " + stage_dir).c_str() ); dpm_con(LOG_FATAL, ("Failed to compress metadata: " + stage_dir).c_str());
return 1; return 1;
} }
// Handle signatures component - check if it's an empty directory // Handle signatures component - check if it's an empty directory
if ( std::filesystem::is_directory( stage_path / "signatures" ) ) { if (std::filesystem::is_directory(stage_path / "signatures")) {
bool signatures_empty = true; bool signatures_empty = true;
// Check if signatures directory is empty // Check if signatures directory is empty
for ( const auto& entry : std::filesystem::directory_iterator( stage_path / "signatures" ) ) { for (const auto& entry : std::filesystem::directory_iterator(stage_path / "signatures")) {
signatures_empty = false; signatures_empty = false;
break; break;
} }
if ( signatures_empty ) { if (signatures_empty) {
dpm_log(LOG_INFO, "Signatures directory is empty, not compressing."); dpm_con(LOG_INFO, "Signatures directory is empty, not compressing.");
} else { } else {
dpm_log(LOG_INFO, "Compressing signatures component."); dpm_con(LOG_INFO, "Compressing signatures component.");
if (! smart_compress_component( stage_dir, "signatures" ) ) { if (!smart_compress_component(stage_dir, "signatures")) {
dpm_log(LOG_FATAL, ("Failed to compress signatures: " + stage_dir).c_str() ); dpm_con(LOG_FATAL, ("Failed to compress signatures: " + stage_dir).c_str());
return 1; return 1;
} }
} }
} }
dpm_log(LOG_INFO, "Package stage sealed successfully."); dpm_con(LOG_INFO, "Package stage sealed successfully.");
return 0; return 0;
} }