cvs: php4 /main streams.c

From: Date: Sat, 16 Mar 2002 20:05:08 +0000
Subject: cvs: php4 /main streams.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-10262@lists.php.net to get a copy of this message
helly Sat Mar 16 15:05:08 2002 EDT Modified files: /php4/main streams.c Log: -copy_stream_to_stream returns size if maxlen == PHP_STREAM_COPY_ALL #had to commit myself to do the rest... Index: php4/main/streams.c diff -u php4/main/streams.c:1.23 php4/main/streams.c:1.24 --- php4/main/streams.c:1.23 Sat Mar 16 13:52:03 2002 +++ php4/main/streams.c Sat Mar 16 15:05:08 2002 @@ -83,14 +83,14 @@ */ return fclose(stream->stdiocast); } - + php_stream_flush(stream); } - + ret = stream->ops->close(stream, call_dtor); stream->abstract = NULL; - - if (call_dtor) { + + if (call_dtor) { /* tidy up any FILE* that might have been fdopened */ if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) { fclose(stream->stdiocast); @@ -209,7 +209,7 @@ PHPAPI int php_stream_seek(php_stream *stream, off_t offset, int whence) { - if (stream->ops->seek) { + if (stream->ops->seek) { return stream->ops->seek(stream, offset, whence); } @@ -227,7 +227,7 @@ } return 0; } - + zend_error(E_WARNING, "streams of type %s do not support seeking", stream->ops->label); return -1; } @@ -245,7 +245,7 @@ if (buf) *buf = NULL; - + if (maxlen == 0) return 0; @@ -258,7 +258,7 @@ * buffering layer. * */ if ( php_stream_is(src, PHP_STREAM_IS_STDIO) && - php_stream_tell(src) == 0 && + php_stream_tell(src) == 0 && SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0)) { struct stat sbuf; @@ -268,7 +268,7 @@ if (maxlen > sbuf.st_size || maxlen == 0) maxlen = sbuf.st_size; - + srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0); if (srcfile != (void*)MAP_FAILED) { @@ -278,7 +278,7 @@ memcpy(*buf, srcfile, maxlen); ret = maxlen; } - + munmap(srcfile, maxlen); return ret; } @@ -286,7 +286,7 @@ /* fall through - we might be able to copy in smaller chunks */ } #endif - + ptr = *buf = pemalloc(persistent, step); max_len = step; @@ -322,24 +322,24 @@ 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 * buffering layer. * */ if ( php_stream_is(src, PHP_STREAM_IS_STDIO) && - php_stream_tell(src) == 0 && + php_stream_tell(src) == 0 && SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0)) { struct stat sbuf; if (fstat(srcfd, &sbuf) == 0) { void *srcfile; - + if (maxlen > sbuf.st_size || maxlen == 0) maxlen = sbuf.st_size; - + srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0); if (srcfile != (void*)MAP_FAILED) { haveread = php_stream_write(dest, srcfile, maxlen); @@ -350,7 +350,7 @@ /* fall through - we might be able to copy in smaller chunks */ } #endif - + while(1) { readchunk = sizeof(buf); @@ -366,18 +366,22 @@ towrite = didread; writeptr = buf; haveread += didread; - + while(towrite) { didwrite = php_stream_write(dest, writeptr, towrite); if (didwrite == 0) return 0; /* error */ - + towrite -= didwrite; writeptr += didwrite; } } else { - return 0; /* error */ + if ( !maxlen) { + return haveread; + } else { + return 0; /* error */ + } } if (maxlen - haveread == 0) { @@ -386,7 +390,7 @@ } return haveread; - + } /* }}} */ @@ -414,7 +418,7 @@ fclose(fp); zend_error(E_WARNING, "%s(): unable to allocate stream", get_active_function_name(TSRMLS_C)); - + return NULL; } return NULL; @@ -424,7 +428,7 @@ { FILE *fp; php_stream *stream; - + fp = tmpfile(); if (fp == NULL) { zend_error(E_WARNING, "tmpfile(): %s", strerror(errno)); @@ -465,7 +469,7 @@ php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract; assert(data != NULL); - + #if HAVE_FLUSHIO if (data->last_op == 'r') { fseek(data->file, 0, SEEK_CUR); @@ -481,7 +485,7 @@ php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract; assert(data != NULL); - + if (buf == NULL && count == 0) { /* check for EOF condition */ if (feof(data->file)) { @@ -495,7 +499,7 @@ fseek(data->file, 0, SEEK_CUR); data->last_op = 'r'; #endif - + return fread(buf, 1, count, data->file); } @@ -517,7 +521,7 @@ } efree(data); - + return ret; } @@ -526,7 +530,7 @@ php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract; assert(data != NULL); - + return fflush(data->file); } @@ -538,7 +542,7 @@ if (offset == 0 && whence == SEEK_CUR) return ftell(data->file); - + return fseek(data->file, offset, whence); } @@ -553,14 +557,14 @@ } data->last_op = 'r'; #endif - + return fgets(buf, size, data->file); } static int php_stdiop_cast(php_stream *stream, int castas, void **ret) { int fd; php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract; - + assert(data != NULL); switch (castas) { @@ -659,7 +663,7 @@ || exec_fname_length<=0) { /* [no active file] or no path */ pathbuf = estrdup(path); - } else { + } else { pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1); memcpy(pathbuf, path, path_length); pathbuf[path_length] = DEFAULT_DIR_SEPARATOR; @@ -714,7 +718,7 @@ char *realpath = NULL; realpath = expand_filepath(filename, NULL TSRMLS_CC); - + fp = fopen(realpath, mode); if (fp) { @@ -774,7 +778,7 @@ { int flags = castas & PHP_STREAM_CAST_MASK; castas &= ~PHP_STREAM_CAST_MASK; - + if (castas == PHP_STREAM_AS_STDIO) { if (stream->stdiocast) { if (ret) { @@ -851,7 +855,7 @@ php_stream_free(stream, 0); } } - + return SUCCESS; } /* }}} */ @@ -928,10 +932,10 @@ PHPAPI php_stream *php_stream_open_wrapper(char *path, char *mode, int options, char **opened_path TSRMLS_DC) { php_stream *stream = NULL; - + if (opened_path) *opened_path = NULL; - + if (!path || !*path) return NULL; @@ -939,7 +943,7 @@ stream = php_stream_open_url(path, mode, options, opened_path TSRMLS_CC); goto out; } - + if ((options & USE_PATH) && PG(include_path) != NULL) { stream = php_stream_fopen_with_path(path, mode, PG(include_path), opened_path TSRMLS_CC); goto out; @@ -947,7 +951,7 @@ if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) && (!php_checkuid(path, mode, CHECKUID_CHECK_MODE_PARAM))) return NULL; - + stream = php_stream_fopen(path, mode, opened_path TSRMLS_CC); out: if (stream == NULL && (options & REPORT_ERRORS)) {

« previous php.cvs (#10262) next »