Skip to content

ZipArchive deletes zip file with no contents and doesn't update stat cache #8781

Closed

Description

@timint

Description

There is a problem with ZipArchive deleting empty archives without notice upon successful close. And statcache not updated.

<?php
    $tmp_file = tempnam(sys_get_temp_dir(), '');
    var_dump(is_file($tmp_file)); // returns true

    $zip = new ZipArchive;
    $zip->open($tmp_file, ZipArchive::OVERWRITE);
    $zip->close(); // returns true

    var_dump(is_file($tmp_file)); // returns true
    var_dump(file_get_contents($tmp_file)); // returns error file does not exist

I expected the defined file to remain. If I can't count on that I expected the statcache to be updated.

PHP Version

PHP 8.1.6

Operating System

Windows 11 x64

Activity

  1. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    Confirmed

    I expected the defined file to remain.

    This the default behavior of the libzip library, (perhaps since empty file are not valid zip archive), so nothing we can change in php

    If I can't count on that I expected the statcache to be updated.

    PR pending

  2. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    Fixed in standalone pecl extension
    pierrejoye/php_zip@fac6809

  3. added 2 commits that reference this issue on Jun 15, 2022
    54139a9
    cf96d4c
  4. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    See PR #8783

  5. cmb69 commented on Jun 15, 2022

    @cmb69
    Member

    perhaps since empty file are not valid zip archive

    I don't think this is true. On Windows, I can create an empty.zip via the explorer, and the specification says:

    4.1.2 ZIP files SHOULD contain at least one file and MAY contain multiple files.

    "SHOULD contain" is not "MUST contain".

    We may consider to still create such an empty archive, even if libzip would delete the file. Or maybe upstream would reconsider the behavior for empty archives.

  6. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    perhaps since empty file are not valid zip archive

    I don't think this is true

    I mean "empty file" (size=0) are not valid, not about "empty archive" ;)

  7. cmb69 commented on Jun 15, 2022

    @cmb69
    Member

    Yeah, but this ticket is about empty archives:

        $zip = new ZipArchive;
        $zip->open($tmp_file, ZipArchive::OVERWRITE);
        $zip->close(); // returns true
  8. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    Yes, but the example use a "empty file" (tempnam) which is not a valid archive.

    And indeed, the library doesn't allow to create an empty archive, but I don't think we can fix this in the extension.

    P.S. opening an empty archive works (and it is not deleted during the close)

    For memory, upstream code: https://github.com/nih-at/libzip/blob/v1.9.0/lib/zip_close.c#L65

  9. cmb69 commented on Jun 15, 2022

    @cmb69
    Member

    Yes, but the example use a "empty file" (tempnam) which is not a valid archive.

    Oh, right!

    And indeed, the library doesn't allow to create an empty archive, but I don't think we can fix this in the extension.

    Thinking about it, that should really be fixed upstream. For instance, given that I have an archive, and delete some entries, after ::close() the archive is gone, if there are no more entries. At least that happens with libzip 1.7.3 (I'm feeling bad, because winlibs are so far behind).

  10. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    libzip discussion nih-at/libzip#299

  11. added 2 commits that reference this issue on Jun 15, 2022
    390538a
    c76374d
  12. added a commit that references this issue on Jun 15, 2022
    a46e87a
  13. remicollet commented on Jun 15, 2022

    @remicollet
    Member

    Followup: add documentation of library expected /documented behavior in our documentation
    php/doc-en#1628

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions