Skip to content

Commit

Permalink
package: use zip file pos instead of info
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Sep 19, 2022
1 parent 3b7fee4 commit 4148c3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/Package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand All @@ -150,25 +150,25 @@ 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)
{
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;
Expand Down
23 changes: 18 additions & 5 deletions src/ZipUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ vector<std::string> UnZip::PathDump()
}


std::unordered_map<string, unz_file_info_s> UnZip::GetPathToFileInfoMapping()
std::unordered_map<string, unz_file_pos> UnZip::GetPathToFilePosMapping()
{
int i = 0;
std::unordered_map<string, unz_file_info_s> paths;
std::unordered_map<string, unz_file_pos> paths;
for (;;)
{
int code;
Expand All @@ -282,18 +282,30 @@ std::unordered_map<string, unz_file_info_s> 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;
Expand Down Expand Up @@ -338,6 +350,7 @@ int UnZip::Extract(const char* path, unz_file_info_s* fileInfo)
done += writeBytes;
}

fsync(fd);
close(fd);
free(buffer);

Expand Down
4 changes: 2 additions & 2 deletions src/ZipUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> PathDump();
std::unordered_map<std::string, unz_file_info_s> GetPathToFileInfoMapping();
std::unordered_map<std::string, unz_file_pos> GetPathToFilePosMapping();

private:
std::string GetFileName(unz_file_info_s* fileInfo);
Expand Down

0 comments on commit 4148c3d

Please sign in to comment.