]> granicus.if.org Git - php/commitdiff
Allow php_stream_copy_to_stream to do nothing when used with code
authorWez Furlong <wez@php.net>
Sat, 16 Mar 2002 02:48:35 +0000 (02:48 +0000)
committerWez Furlong <wez@php.net>
Sat, 16 Mar 2002 02:48:35 +0000 (02:48 +0000)
that calculates a max length of zero. (Thanks again Marcus).

ext/ftp/php_ftp.c
ext/standard/file.c
main/php_streams.h
main/streams.c

index 3706dd7cb99747bf449d9a8f13635811cede96ee..1012698966a452b7a6ac1681c022ecce5da73daa 100644 (file)
@@ -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;
        }
index 88b1798c26e37190084e7234c5c49f1debda82cf..5734db3a1fa29aed90fe06e0e2abd7994308cb1d 100644 (file)
@@ -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);
index aae253a29fa20e4bc2d6e7de3e2c9b976be0cff8..2b4c0162bc247433959eb67c47ffafe3f998648d 100755 (executable)
@@ -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.
index 5157fe9ce76f00a1f123ffb999005741d4488226..ccafe65b8c34427c8a6a203e1ff4975230da5dfc 100755 (executable)
@@ -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;
                        }
                }