Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to enable deletion of files in stream memory zips. #332

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,34 @@ static ssize_t zip_entry_setbyindex(struct zip_t *zip,
return 0;
}

static ssize_t zip_mem_move(void *pBuf, size_t bufSize, const mz_uint64 to, const mz_uint64 from, const size_t length) {
uint8_t *dst = NULL, *src = NULL, *end = NULL;

if(!pBuf) {
return ZIP_EINVIDX;
}

end = (uint8_t *)pBuf + bufSize;

if(to > bufSize) {
return ZIP_EINVIDX;
}

if(from > bufSize) {
return ZIP_EINVIDX;
}

dst = (uint8_t *)pBuf + to;
src = (uint8_t *)pBuf + from;

if(((dst + length) > end) || ((src + length) > end)) {
return ZIP_EINVIDX;
}

memmove(dst, src, length);
return length;
}

static ssize_t zip_file_move(MZ_FILE *m_pFile, const mz_uint64 to,
const mz_uint64 from, const size_t length,
mz_uint8 *move_buf, const size_t capacity_size) {
Expand All @@ -675,10 +703,12 @@ static ssize_t zip_file_move(MZ_FILE *m_pFile, const mz_uint64 to,
return (ssize_t)length;
}

static ssize_t zip_files_move(MZ_FILE *m_pFile, mz_uint64 writen_num,
static ssize_t zip_files_move(struct zip_t *zip, mz_uint64 writen_num,
mz_uint64 read_num, size_t length) {
ssize_t n = 0;
const size_t page_size = 1 << 12; // 4K
mz_zip_internal_state *pState = zip->archive.m_pState;

mz_uint8 *move_buf = (mz_uint8 *)calloc(1, page_size);
if (!move_buf) {
return ZIP_EOOMEM;
Expand All @@ -688,8 +718,16 @@ static ssize_t zip_files_move(MZ_FILE *m_pFile, mz_uint64 writen_num,
ssize_t move_count = 0;
while ((mz_int64)length > 0) {
move_count = (length >= page_size) ? page_size : length;
n = zip_file_move(m_pFile, writen_num, read_num, move_count, move_buf,

if(pState->m_pFile) {
n = zip_file_move(pState->m_pFile, writen_num, read_num, move_count, move_buf,
page_size);
} else if(pState->m_pMem) {
n = zip_mem_move(pState->m_pMem, pState->m_mem_size, writen_num, read_num, move_count);
} else {
return ZIP_ENOFILE;
}

if (n < 0) {
moved_length = n;
goto cleanup;
Expand Down Expand Up @@ -831,9 +869,11 @@ static ssize_t zip_entries_delete_mark(struct zip_t *zip,
mz_zip_internal_state *pState = zip->archive.m_pState;
zip->archive.m_zip_mode = MZ_ZIP_MODE_WRITING;

if ((!pState->m_pFile) || MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) {
CLEANUP(deleted_entry_flag_array);
return ZIP_ENOENT;
if(pState->m_pFile) {
if (MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) {
CLEANUP(deleted_entry_flag_array);
return ZIP_ENOENT;
}
}

while (i < entry_num) {
Expand Down Expand Up @@ -866,7 +906,7 @@ static ssize_t zip_entries_delete_mark(struct zip_t *zip,
i++;
}

n = zip_files_move(pState->m_pFile, writen_num, read_num, move_length);
n = zip_files_move(zip, writen_num, read_num, move_length);
if (n != (ssize_t)move_length) {
CLEANUP(deleted_entry_flag_array);
return n;
Expand Down
Loading