that calculates a max length of zero. (Thanks again Marcus).
}
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;
}
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);
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.
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
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;
}
}