libzip icon indicating copy to clipboard operation
libzip copied to clipboard

Wrong file-count after zip_delete

Open ctenter-scs opened this issue 3 years ago • 6 comments

Describe the Bug The file-count reported by zip_get_num_entries is not reduced after deleting an entry with zip_delete

Expected Behavior The file-count should be reduced by one after deleting a file.

Observed Behavior The file-count is not changed after deleting a file.

To Reproduce zip_source* zipSrc = zip_source_file_create("test", 0U, 0, 0);

  zip* zip = zip_open_from_source(zipSrc, ZIP_CREATE, 0);


  zip_uint8_t data[] = { 0, 1, 2 };

  zip_source* newSource = zip_source_buffer(zip, data, 3, 0);

  zip_int64_t idx = zip_add(zip, "a", newSource);


  zip_delete(zip, idx);
  zip_source_free(newSource);

  zip_int64_t n = zip_get_num_entries(zip, 0); // expected 0 but returns 1

libzip Version 1.9.2

Operating System Linux gcc 7.3.0, Windows VS 2019

ctenter-scs avatar Aug 09 '22 12:08 ctenter-scs

Thank you for the bug report, I just committed a fix.

dillof avatar Aug 09 '22 12:08 dillof

Hi, thanks for the quick fix. I just noticed that probably we'll run into problems when iterating through files. From what I understand the indices of files are not changed by zip_delete, right? In that case how to query the max index?

ctenter-scs avatar Aug 09 '22 16:08 ctenter-scs

You are right, zip_get_num_entries() was correct, just not what you might expect.

I've reverted my change and documented the behaviour (and its rationale) in the man page.

Do you actually need to know how many undeleted files are in the archive? If so, I could add a new function that returns that. If you're only interested in whether the archive is empty (contains no or only deleted entries), I can add that (which would be more efficient than counting the entries and comparing that to 0).

dillof avatar Aug 09 '22 17:08 dillof

Thanks, I see. Yes an efficient function for checking if the archive is empty would be appreciated. As for zip_get_num_entries(), how about adding a new flag to exclude the deleted files maybe?

ctenter-scs avatar Aug 09 '22 18:08 ctenter-scs

Do you actually have a use case for this, or did zip_get_num_entries() just not behave like you expected?

We can't think of a compelling use for these features, and to avoid API bloat, we'd rather only implement them if they are going to be used.

dillof avatar Aug 19 '22 10:08 dillof

We just use it to avoid some kind of error happening on unix if the archive is closed empty and the file did not exist when opening the new zip in memory. So we check if the archive is empty and discard it in that case. We iterate over all entries and check the name-bit in the stat struct to do that. I don't mind if the functions are missing.

ctenter-scs avatar Aug 22 '22 14:08 ctenter-scs