Skip to content

Bug 65701 #459

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 1 commit 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
2 changes: 1 addition & 1 deletion ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php
return FAILURE;
}

switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, ctx)) {
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET | PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
case -1:
/* non-statable stream */
goto safe_to_copy;
Expand Down
30 changes: 30 additions & 0 deletions ext/standard/tests/file/bug65701.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Test for bug #65701: copy() doesn't work when destination filename is created by tempnam()
--CREDITS--
Boro Sitnikovski <buritomath@yahoo.com>
--FILE--
<?php
$file_path = dirname(__FILE__) . "/bug65701/";

mkdir($file_path);

$src = $file_path . '/srcbug65701_file.txt';
$dst = tempnam($file_path, 'dstbug65701_file.txt');

file_put_contents($src, "Hello World");

copy($src, $dst);
var_dump(filesize($dst));
?>
--CLEAN--
<?php
$file_path = dirname(__FILE__) . "/bug65701/";
foreach (scandir($file_path) as $file) {
if (strpos($file, "bug65701") !== false) {
unlink($file_path . $file);
}
}
rmdir($file_path);
?>
--EXPECT--
int(11)
1 change: 1 addition & 0 deletions main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ END_EXTERN_C()
/* Flags for url_stat method in wrapper ops */
#define PHP_STREAM_URL_STAT_LINK 1
#define PHP_STREAM_URL_STAT_QUIET 2
#define PHP_STREAM_URL_STAT_NOCACHE 4

/* change the blocking mode of stream: value == 1 => blocking, value == 0 => non-blocking. */
#define PHP_STREAM_OPTION_BLOCKING 1
Expand Down
48 changes: 26 additions & 22 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1915,36 +1915,40 @@ PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf
const char *path_to_open = path;
int ret;

/* Try to hit the cache first */
if (flags & PHP_STREAM_URL_STAT_LINK) {
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
return 0;
}
} else {
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
return 0;
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
/* Try to hit the cache first */
if (flags & PHP_STREAM_URL_STAT_LINK) {
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
return 0;
}
} else {
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
return 0;
}
}
}

wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0 TSRMLS_CC);
if (wrapper && wrapper->wops->url_stat) {
ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context TSRMLS_CC);
if (ret == 0) {
/* Drop into cache */
if (flags & PHP_STREAM_URL_STAT_LINK) {
if (BG(CurrentLStatFile)) {
efree(BG(CurrentLStatFile));
}
BG(CurrentLStatFile) = estrdup(path);
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
} else {
if (BG(CurrentStatFile)) {
efree(BG(CurrentStatFile));
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
/* Drop into cache */
if (flags & PHP_STREAM_URL_STAT_LINK) {
if (BG(CurrentLStatFile)) {
efree(BG(CurrentLStatFile));
}
BG(CurrentLStatFile) = estrdup(path);
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
} else {
if (BG(CurrentStatFile)) {
efree(BG(CurrentStatFile));
}
BG(CurrentStatFile) = estrdup(path);
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
}
BG(CurrentStatFile) = estrdup(path);
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
}
}
return ret;
Expand Down