Skip to content

Commit

Permalink
Zip: Consider CWD at opening an archive
Browse files Browse the repository at this point in the history
Summary: Fixes #3981

This one fixes issue [3981](#3981). To be compatible with PHP 5.5 we should consider current working directory `zip_open` and `ZipArchive::open`.

Reviewed By: @ptarjan

Differential Revision: D1625155
  • Loading branch information
DmitrySoshnikov authored and hhvm-bot committed Oct 18, 2014
1 parent 526e188 commit 043cbb0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
19 changes: 16 additions & 3 deletions hphp/runtime/ext/zip/ext_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@

namespace HPHP {

static String to_full_path(const String& filename) {
if (filename.charAt(0) == '/') {
return filename;
}
return f_getcwd().toString() + String::FromChar('/') + filename;
}

// A wrapper for `zip_open` that prepares a full path
// file name to consider current working directory.
static zip* _zip_open(const String& filename, int _flags, int* zep) {
return zip_open(to_full_path(filename).c_str(), _flags, zep);
}

class ZipStream : public File {
public:
DECLARE_RESOURCE_ALLOCATION(ZipStream);
Expand Down Expand Up @@ -100,7 +113,7 @@ class ZipStreamWrapper : public Stream::Wrapper {
}

int err;
auto z = zip_open(path.c_str(), 0, &err);
auto z = _zip_open(path, 0, &err);
if (z == nullptr) {
return nullptr;
}
Expand Down Expand Up @@ -1037,7 +1050,7 @@ static Variant HHVM_METHOD(ZipArchive, open, const String& filename,
FAIL_IF_EMPTY_STRING_ZIPARCHIVE(open, filename);

int err;
auto z = zip_open(filename.c_str(), flags, &err);
auto z = _zip_open(filename, flags, &err);
if (z == nullptr) {
return err;
}
Expand Down Expand Up @@ -1332,7 +1345,7 @@ static Variant HHVM_FUNCTION(zip_open, const String& filename) {
FAIL_IF_EMPTY_STRING(zip_open, filename);

int err;
auto z = zip_open(filename.c_str(), 0, &err);
auto z = _zip_open(filename, 0, &err);
if (z == nullptr) {
return err;
}
Expand Down
23 changes: 23 additions & 0 deletions hphp/test/slow/ext_zlib/zip_open_cdw_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$archive = "ziparchive_extractto.php.zip";
$full_path = __DIR__.'/'.$archive;

function run($filename) {
$zip = zip_open($filename);
var_dump(is_resource($zip));

$zip = new ZipArchive;
$res = $zip->open($filename);
var_dump($res);
}

$initial_cwd = getcwd();

// Relative from CWD path.
chdir(__DIR__);
run($archive);

// Absolute path.
chdir($initial_cwd);
run($full_path);
4 changes: 4 additions & 0 deletions hphp/test/slow/ext_zlib/zip_open_cdw_path.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bool(true)
bool(true)
bool(true)
bool(true)

0 comments on commit 043cbb0

Please sign in to comment.