diff options
Diffstat (limited to 'main/streams')
-rw-r--r-- | main/streams/cast.c | 6 | ||||
-rwxr-xr-x | main/streams/streams.c | 40 |
2 files changed, 35 insertions, 11 deletions
diff --git a/main/streams/cast.c b/main/streams/cast.c index dea72da53d..06805c709c 100644 --- a/main/streams/cast.c +++ b/main/streams/cast.c @@ -214,9 +214,9 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show newstream = php_stream_fopen_tmpfile(); if (newstream) { - size_t copied = php_stream_copy_to_stream(stream, newstream, PHP_STREAM_COPY_ALL); + size_t copied = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL); - if (copied == 0) { + if (copied == PHP_STREAM_FAILURE) { php_stream_close(newstream); } else { int retcode = php_stream_cast(newstream, castas | flags, ret, show_err); @@ -332,7 +332,7 @@ PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstr (*newstream)->open_lineno = origstream->open_lineno; #endif - if (php_stream_copy_to_stream(origstream, *newstream, PHP_STREAM_COPY_ALL) == 0) { + if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL) == PHP_STREAM_FAILURE) { php_stream_close(*newstream); *newstream = NULL; return PHP_STREAM_CRITICAL; diff --git a/main/streams/streams.c b/main/streams/streams.c index 6d9d2ecd0e..c5aebe7812 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -1297,7 +1297,8 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen return len; } -PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) +/* Returns the number of bytes moved, or PHP_STREAM_FAILURE on failure. */ +PHPAPI size_t _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) { char buf[CHUNK_SIZE]; size_t readchunk; @@ -1314,8 +1315,6 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size } if (php_stream_stat(src, &ssbuf) == 0) { - /* in the event that the source file is 0 bytes, return 1 to indicate success - * because opening the file to write had already created a copy */ if (ssbuf.sb.st_size == 0 #ifdef S_ISFIFO && !S_ISFIFO(ssbuf.sb.st_mode) @@ -1324,7 +1323,7 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size && !S_ISCHR(ssbuf.sb.st_mode) #endif ) { - return 1; + return 0; } } @@ -1338,8 +1337,14 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size mapped = php_stream_write(dest, p, mapped); php_stream_mmap_unmap(src); + + /* we've got at least 1 byte to read. + * less than 1 is an error */ - return mapped; + if (mapped > 0) { + return mapped; + } + return PHP_STREAM_FAILURE; } } @@ -1364,14 +1369,14 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size while(towrite) { didwrite = php_stream_write(dest, writeptr, towrite); if (didwrite == 0) { - return 0; /* error */ + return PHP_STREAM_FAILURE; } towrite -= didwrite; writeptr += didwrite; } } else { - return haveread; + break; } if (maxlen - haveread == 0) { @@ -1379,7 +1384,26 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size } } - return haveread; + /* we've got at least 1 byte to read. + * less than 1 is an error */ + + if (haveread > 0) { + return haveread; + } + return PHP_STREAM_FAILURE; +} + +/* Returns the number of bytes moved. + * Returns 1 when source len is 0. + * Deprecated in favor of php_stream_copy_to_stream_ex() */ +ZEND_ATTRIBUTE_DEPRECATED +PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) +{ + size_t ret = _php_stream_copy_to_stream_ex(src, dest, maxlen STREAMS_REL_CC TSRMLS_CC); + if (ret == 0 && maxlen != 0) { + return 1; + } + return ret; } /* }}} */ |