From 4148c3dbe3d5e607c6669cd95f7400ff4636384a Mon Sep 17 00:00:00 2001 From: vgmoose Date: Mon, 19 Sep 2022 00:33:48 -0400 Subject: [PATCH] package: use zip file pos instead of info --- src/Package.cpp | 10 +++++----- src/ZipUtil.cpp | 23 ++++++++++++++++++----- src/ZipUtil.hpp | 4 ++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Package.cpp b/src/Package.cpp index ad245e8..3186a7e 100644 --- a/src/Package.cpp +++ b/src/Package.cpp @@ -127,7 +127,7 @@ bool Package::install(const char* pkg_path, const char* tmp_path) if (manifest->valid) { // get all file info from within the zip, for every path - auto infoMap = HomebrewZip->GetPathToFileInfoMapping(); + auto infoMap = HomebrewZip->GetPathToFilePosMapping(); for (int i = 0; i < manifest->entries.size(); i++) { @@ -150,7 +150,7 @@ bool Package::install(const char* pkg_path, const char* tmp_path) continue; } - auto fileInfo = mapResult->second; + auto filePos = mapResult->second; int resp = 0; switch (manifest->entries[i].operation) @@ -158,17 +158,17 @@ bool Package::install(const char* pkg_path, const char* tmp_path) case MEXTRACT: //! Simply Extract, with no checks or anything, won't be deleted upon removal info("%s : EXTRACT\n", pathCStr); - resp = HomebrewZip->Extract(ePathCStr, &fileInfo); + resp = HomebrewZip->Extract(ePathCStr, NULL, &filePos); break; case MUPDATE: info("%s : UPDATE\n", pathCStr); - resp = HomebrewZip->Extract(ePathCStr, &fileInfo); + resp = HomebrewZip->Extract(ePathCStr, NULL, &filePos); break; case MGET: info("%s : GET\n", pathCStr); struct stat sbuff; if (stat(ExtractPath.c_str(), &sbuff) != 0) //! File doesn't exist, extract - resp = HomebrewZip->Extract(ePathCStr, &fileInfo); + resp = HomebrewZip->Extract(ePathCStr, NULL, &filePos); else info("File already exists, skipping..."); break; diff --git a/src/ZipUtil.cpp b/src/ZipUtil.cpp index ac7d251..42c8075 100644 --- a/src/ZipUtil.cpp +++ b/src/ZipUtil.cpp @@ -259,10 +259,10 @@ vector UnZip::PathDump() } -std::unordered_map UnZip::GetPathToFileInfoMapping() +std::unordered_map UnZip::GetPathToFilePosMapping() { int i = 0; - std::unordered_map paths; + std::unordered_map paths; for (;;) { int code; @@ -282,18 +282,30 @@ std::unordered_map UnZip::GetPathToFileInfoMapping() if (fileInfo->uncompressed_size != 0 && fileInfo->compression_method != 0) { // info("PathDump: %s\n", fileName.c_str()); - paths[fileName] = *fileInfo; + unzGetFilePos(fileToUnzip, &paths[fileName]); } free(fileInfo); } return paths; } -int UnZip::Extract(const char* path, unz_file_info_s* fileInfo) +int UnZip::Extract(const char* path, unz_file_info_s* fileInfo, unz_file_pos* file_pos) { //check to make sure filepath or fileInfo isnt null - if (path == NULL || fileInfo == NULL) + if (path == NULL) return -1; + + if (fileInfo == NULL) { + if (file_pos == NULL) + return -1; + + // we have a file pos, seek to that file + unzGoToFilePos(fileToUnzip, file_pos); + fileInfo = GetFileInfo(); + + if (fileInfo == NULL) + return -1; + } if (unzOpenCurrentFile(fileToUnzip) != UNZ_OK) return -2; @@ -338,6 +350,7 @@ int UnZip::Extract(const char* path, unz_file_info_s* fileInfo) done += writeBytes; } + fsync(fd); close(fd); free(buffer); diff --git a/src/ZipUtil.hpp b/src/ZipUtil.hpp index 703ae55..e23e3c2 100644 --- a/src/ZipUtil.hpp +++ b/src/ZipUtil.hpp @@ -26,12 +26,12 @@ class UnZip UnZip(const char* zipPath); ~UnZip(); void Close(); - int Extract(const char* path, unz_file_info_s* fileInfo); + int Extract(const char* path, unz_file_info_s* fileInfo, unz_file_pos* file_pos = NULL); int ExtractFile(const char* internalPath, const char* path); int ExtractAll(const char* dirToExtract); int ExtractDir(const char* internalDir, const char* externalDir); std::vector PathDump(); - std::unordered_map GetPathToFileInfoMapping(); + std::unordered_map GetPathToFilePosMapping(); private: std::string GetFileName(unz_file_info_s* fileInfo);