Skip to content

Commit dd9c80e

Browse files
committed
Merge branch 'PHP-5.6'
* PHP-5.6: Fix bug #65701: Do not use cache for file file copy
2 parents 0cdc57b + 774f163 commit dd9c80e

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php
16681668
return FAILURE;
16691669
}
16701670

1671-
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, ctx)) {
1671+
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET | PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
16721672
case -1:
16731673
/* non-statable stream */
16741674
goto safe_to_copy;

ext/standard/tests/file/bug65701.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test for bug #65701: copy() doesn't work when destination filename is created by tempnam()
3+
--CREDITS--
4+
Boro Sitnikovski <buritomath@yahoo.com>
5+
--FILE--
6+
<?php
7+
$file_path = dirname(__FILE__) . "/bug65701/";
8+
9+
mkdir($file_path);
10+
11+
$src = $file_path . '/srcbug65701_file.txt';
12+
$dst = tempnam($file_path, 'dstbug65701_file.txt');
13+
14+
file_put_contents($src, "Hello World");
15+
16+
copy($src, $dst);
17+
var_dump(filesize($dst));
18+
?>
19+
--CLEAN--
20+
<?php
21+
$file_path = dirname(__FILE__) . "/bug65701/";
22+
foreach (scandir($file_path) as $file) {
23+
if (strpos($file, "bug65701") !== false) {
24+
unlink($file_path . $file);
25+
}
26+
}
27+
rmdir($file_path);
28+
?>
29+
--EXPECT--
30+
int(11)

main/php_streams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ END_EXTERN_C()
373373
/* Flags for url_stat method in wrapper ops */
374374
#define PHP_STREAM_URL_STAT_LINK 1
375375
#define PHP_STREAM_URL_STAT_QUIET 2
376+
#define PHP_STREAM_URL_STAT_NOCACHE 4
376377

377378
/* change the blocking mode of stream: value == 1 => blocking, value == 0 => non-blocking. */
378379
#define PHP_STREAM_OPTION_BLOCKING 1

main/streams/streams.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,36 +1924,40 @@ PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf
19241924
const char *path_to_open = path;
19251925
int ret;
19261926

1927-
/* Try to hit the cache first */
1928-
if (flags & PHP_STREAM_URL_STAT_LINK) {
1929-
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
1930-
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
1931-
return 0;
1932-
}
1933-
} else {
1934-
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
1935-
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
1936-
return 0;
1927+
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
1928+
/* Try to hit the cache first */
1929+
if (flags & PHP_STREAM_URL_STAT_LINK) {
1930+
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
1931+
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
1932+
return 0;
1933+
}
1934+
} else {
1935+
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
1936+
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
1937+
return 0;
1938+
}
19371939
}
19381940
}
19391941

19401942
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0 TSRMLS_CC);
19411943
if (wrapper && wrapper->wops->url_stat) {
19421944
ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context TSRMLS_CC);
19431945
if (ret == 0) {
1944-
/* Drop into cache */
1945-
if (flags & PHP_STREAM_URL_STAT_LINK) {
1946-
if (BG(CurrentLStatFile)) {
1947-
efree(BG(CurrentLStatFile));
1948-
}
1949-
BG(CurrentLStatFile) = estrdup(path);
1950-
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
1951-
} else {
1952-
if (BG(CurrentStatFile)) {
1953-
efree(BG(CurrentStatFile));
1946+
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
1947+
/* Drop into cache */
1948+
if (flags & PHP_STREAM_URL_STAT_LINK) {
1949+
if (BG(CurrentLStatFile)) {
1950+
efree(BG(CurrentLStatFile));
1951+
}
1952+
BG(CurrentLStatFile) = estrdup(path);
1953+
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
1954+
} else {
1955+
if (BG(CurrentStatFile)) {
1956+
efree(BG(CurrentStatFile));
1957+
}
1958+
BG(CurrentStatFile) = estrdup(path);
1959+
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
19541960
}
1955-
BG(CurrentStatFile) = estrdup(path);
1956-
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
19571961
}
19581962
}
19591963
return ret;

0 commit comments

Comments
 (0)