Skip to content

Commit a18cec1

Browse files
bor0smalyshev
authored andcommitted
Fix bug #65701: Do not use cache for file file copy
1 parent 53c6881 commit a18cec1

File tree

5 files changed

+63
-27
lines changed

5 files changed

+63
-27
lines changed

NEWS

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2014, PHP 5.4.29
44

5+
- Core:
6+
. Fixed bug #65701 (copy() doesn't work when destination filename is created
7+
by tempnam()). (Boro Sitnikovski)
8+
. Fixed bug #67072 (Echoing unserialized "SplFileObject" crash). (Anatol)
9+
510
- DOM:
611
. Fixed bug #67081 (DOMDocumentType->internalSubset returns entire DOCTYPE tag,
712
not only the subset). (Anatol)
813

9-
- Standard:
10-
. Fixed bug #67072 (Echoing unserialized "SplFileObject" crash). (Anatol)
11-
12-
1314
?? ??? 2014, PHP 5.4.28
1415

1516
- Core:

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ PHPAPI int php_copy_file_ctx(char *src, char *dest, int src_flg, php_stream_cont
16571657
return FAILURE;
16581658
}
16591659

1660-
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, ctx)) {
1660+
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET | PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
16611661
case -1:
16621662
/* non-statable stream */
16631663
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
@@ -377,6 +377,7 @@ END_EXTERN_C()
377377
/* Flags for url_stat method in wrapper ops */
378378
#define PHP_STREAM_URL_STAT_LINK 1
379379
#define PHP_STREAM_URL_STAT_QUIET 2
380+
#define PHP_STREAM_URL_STAT_NOCACHE 4
380381

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

main/streams/streams.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,36 +1929,40 @@ PHPAPI int _php_stream_stat_path(char *path, int flags, php_stream_statbuf *ssb,
19291929
char *path_to_open = path;
19301930
int ret;
19311931

1932-
/* Try to hit the cache first */
1933-
if (flags & PHP_STREAM_URL_STAT_LINK) {
1934-
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
1935-
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
1936-
return 0;
1937-
}
1938-
} else {
1939-
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
1940-
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
1941-
return 0;
1932+
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
1933+
/* Try to hit the cache first */
1934+
if (flags & PHP_STREAM_URL_STAT_LINK) {
1935+
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
1936+
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
1937+
return 0;
1938+
}
1939+
} else {
1940+
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
1941+
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
1942+
return 0;
1943+
}
19421944
}
19431945
}
19441946

19451947
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0 TSRMLS_CC);
19461948
if (wrapper && wrapper->wops->url_stat) {
19471949
ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context TSRMLS_CC);
19481950
if (ret == 0) {
1949-
/* Drop into cache */
1950-
if (flags & PHP_STREAM_URL_STAT_LINK) {
1951-
if (BG(CurrentLStatFile)) {
1952-
efree(BG(CurrentLStatFile));
1953-
}
1954-
BG(CurrentLStatFile) = estrdup(path);
1955-
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
1956-
} else {
1957-
if (BG(CurrentStatFile)) {
1958-
efree(BG(CurrentStatFile));
1951+
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
1952+
/* Drop into cache */
1953+
if (flags & PHP_STREAM_URL_STAT_LINK) {
1954+
if (BG(CurrentLStatFile)) {
1955+
efree(BG(CurrentLStatFile));
1956+
}
1957+
BG(CurrentLStatFile) = estrdup(path);
1958+
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
1959+
} else {
1960+
if (BG(CurrentStatFile)) {
1961+
efree(BG(CurrentStatFile));
1962+
}
1963+
BG(CurrentStatFile) = estrdup(path);
1964+
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
19591965
}
1960-
BG(CurrentStatFile) = estrdup(path);
1961-
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
19621966
}
19631967
}
19641968
return ret;

0 commit comments

Comments
 (0)