Skip to content

Fix #50678: files extracted by ZipArchive class lost their original mtime #1291

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
size_t path_cleaned_len;
cwd_state new_state;
zend_string *file_basename;
struct utimbuf newtimebuf;
struct utimbuf *newtime = &newtimebuf;
php_stream_wrapper *wrapper;

new_state.cwd = CWD_STATE_ALLOC(1);
new_state.cwd[0] = '\0';
Expand Down Expand Up @@ -262,6 +265,14 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
php_stream_close(stream);
n = zip_fclose(zf);

/* try to set mtime of extracted file; don't mind if that fails */
newtime->modtime = sb.mtime;
newtime->actime = time(NULL);
wrapper = php_stream_locate_url_wrapper(fullpath, NULL, 0);
if (wrapper && wrapper->wops) {
wrapper->wops->stream_metadata(wrapper, fullpath, PHP_STREAM_META_TOUCH, newtime, NULL);
}

done:
efree(fullpath);
zend_string_release(file_basename);
Expand Down
26 changes: 26 additions & 0 deletions ext/zip/tests/bug50678.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Bug #50678 (files extracted by ZipArchive class loose their original modified time)
--SKIPIF--
<?php
if (!extension_loaded('zip')) print 'skip zip extension not available';
?>
--FILE--
<?php
$dest = __DIR__ . '/bug50678';
mkdir($dest);
$zip = new ZipArchive();
$zip->open(__DIR__ . '/bug50678.zip');
$zip->extractTo($dest);
$zip->close();
$filename = $dest . '/bug50678.txt';
// check that the mtime is properly set, if the extracted file is writable
var_dump(!is_writable($filename) || filemtime($filename) == 1432163274);
?>
--CLEAN--
<?php
$dest = __DIR__ . '/bug50678';
unlink($dest . '/bug50678.txt');
rmdir($dest);
?>
--EXPECT--
bool(true)
Binary file added ext/zip/tests/bug50678.zip
Binary file not shown.