From 636829345ea4ce06766b55b43adfd2b47628fada Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 16 Mar 2002 02:48:35 +0000 Subject: [PATCH] Allow php_stream_copy_to_stream to do nothing when used with code that calculates a max length of zero. (Thanks again Marcus). --- ext/ftp/php_ftp.c | 2 +- ext/standard/file.c | 2 +- main/php_streams.h | 3 ++- main/streams.c | 15 ++++++++++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index 3706dd7cb9..1012698966 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -489,7 +489,7 @@ PHP_FUNCTION(ftp_get) } php_stream_rewind(tmpstream); - if (php_stream_copy_to_stream(tmpstream, outstream, 0) == 0) { + if (php_stream_copy_to_stream(tmpstream, outstream, PHP_STREAM_COPY_ALL) == 0) { php_error(E_WARNING, "%s(): error writing %s", get_active_function_name(TSRMLS_C), local); RETVAL_FALSE; } diff --git a/ext/standard/file.c b/ext/standard/file.c index 88b1798c26..5734db3a1f 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -1658,7 +1658,7 @@ PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC) NULL TSRMLS_CC); if (srcstream && deststream) - ret = php_stream_copy_to_stream(srcstream, deststream, 0) == 0 ? FAILURE : SUCCESS; + ret = php_stream_copy_to_stream(srcstream, deststream, PHP_STREAM_COPY_ALL) == 0 ? FAILURE : SUCCESS; if (srcstream) php_stream_close(srcstream); diff --git a/main/php_streams.h b/main/php_streams.h index aae253a29f..2b4c0162bc 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -88,8 +88,9 @@ PHPAPI int php_stream_flush(php_stream *stream); PHPAPI char *php_stream_gets(php_stream *stream, char *buf, size_t maxlen); PHPAPI int php_stream_puts(php_stream *stream, char *buf); -/* copy up to maxlen bytes from src to dest. If maxlen is 0, copy until eof(src). +/* copy up to maxlen bytes from src to dest. If maxlen is PHP_STREAM_COPY_ALL, copy until eof(src). * Uses mmap if the src is a plain file and at offset 0 */ +#define PHP_STREAM_COPY_ALL -1 PHPAPI size_t php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen); /* read all data from stream and put into a buffer. Caller must free buffer when done, * according to allocopts. diff --git a/main/streams.c b/main/streams.c index 5157fe9ce7..ccafe65b8c 100755 --- a/main/streams.c +++ b/main/streams.c @@ -302,6 +302,12 @@ PHPAPI size_t php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_ int srcfd; #endif + if (maxlen == 0) + return 0; + + if (maxlen == PHP_STREAM_COPY_ALL) + maxlen = 0; + #if HAVE_MMAP /* try and optimize the case where we are copying from the start of a plain file. * We could probably make this work in more situations, but I don't trust the stdio @@ -315,11 +321,14 @@ PHPAPI size_t php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_ if (fstat(srcfd, &sbuf) == 0) { void *srcfile; + + if (maxlen > sbuf.st_size || maxlen == 0) + maxlen = sbuf.st_size; - srcfile = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, srcfd, 0); + srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0); if (srcfile != (void*)MAP_FAILED) { - haveread = php_stream_write(dest, srcfile, sbuf.st_size); - munmap(srcfile, sbuf.st_size); + haveread = php_stream_write(dest, srcfile, maxlen); + munmap(srcfile, maxlen); return haveread; } } -- 2.40.0