From: Wez Furlong Date: Mon, 24 Feb 2003 21:40:23 +0000 (+0000) Subject: MFB: Bunch of streams related fixes. X-Git-Tag: RELEASE_0_5~777 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=088e2692c3d1e680fd3d9306c4adb417e761acff;p=php MFB: Bunch of streams related fixes. --- diff --git a/ext/standard/file.c b/ext/standard/file.c index 810367f694..b4f118b76c 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -2384,7 +2384,7 @@ PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC) int ret = FAILURE; srcstream = php_stream_open_wrapper(src, "rb", - ENFORCE_SAFE_MODE | REPORT_ERRORS, + STREAM_DISABLE_OPEN_BASEDIR | REPORT_ERRORS, NULL); if (!srcstream) { diff --git a/main/php_streams.h b/main/php_streams.h index 5e22792682..194172c1c3 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -406,6 +406,9 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show /* this flag is used when only the headers from HTTP request are to be fetched */ #define STREAM_ONLY_GET_HEADERS 512 +/* don't apply open_basedir checks */ +#define STREAM_DISABLE_OPEN_BASEDIR 1024 + /* Antique - no longer has meaning */ #define IGNORE_URL_WIN 0 diff --git a/main/streams/cast.c b/main/streams/cast.c index 66acd141a3..aa6e6d12b2 100644 --- a/main/streams/cast.c +++ b/main/streams/cast.c @@ -208,7 +208,12 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show return FAILURE; #endif - if (flags & PHP_STREAM_CAST_TRY_HARD) { + if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) { + if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) { + return FAILURE; + } + goto exit_success; + } else if (flags & PHP_STREAM_CAST_TRY_HARD) { php_stream *newstream; newstream = php_stream_fopen_tmpfile(); diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index d45e6a1098..cf85586d78 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -603,7 +603,7 @@ static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, char DIR *dir = NULL; php_stream *stream = NULL; - if (php_check_open_basedir(path TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) { return NULL; } @@ -637,7 +637,7 @@ static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, ch return php_stream_fopen_with_path_rel(path, mode, PG(include_path), opened_path, options); } - if (php_check_open_basedir(path TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) { return NULL; } @@ -702,7 +702,7 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char } - if (php_check_open_basedir(filename TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(filename TSRMLS_CC)) { return NULL; } @@ -722,7 +722,7 @@ not_relative_path: /* Absolute path open */ if (IS_ABSOLUTE_PATH(filename, filename_length)) { - if (php_check_open_basedir(filename TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(filename TSRMLS_CC)) { return NULL; } @@ -748,7 +748,7 @@ not_relative_path: free(cwd); - if (php_check_open_basedir(trypath TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(trypath TSRMLS_CC)) { return NULL; } if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC)) == 0) { @@ -764,7 +764,7 @@ not_relative_path: if (!path || (path && !*path)) { - if (php_check_open_basedir(path TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) { return NULL; } @@ -809,7 +809,7 @@ not_relative_path: } snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename); - if (php_check_open_basedir(trypath TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(trypath TSRMLS_CC)) { stream = NULL; goto stream_done; } diff --git a/main/streams/streams.c b/main/streams/streams.c index ae33818b79..e798684d18 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -801,6 +801,16 @@ static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size { size_t didwrite = 0, towrite, justwrote; + /* if we have a seekable stream we need to ensure that data is written at the + * current stream->position. This means invalidating the read buffer and then + * performing a low-level seek */ + if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { + stream->readpos = stream->writepos = 0; + + stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC); + } + + while (count > 0) { towrite = count; if (towrite > stream->chunk_size) @@ -817,8 +827,6 @@ static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size * buffered from fifos and sockets */ if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && !php_stream_is_filtered(stream)) { stream->position += justwrote; - stream->writepos = 0; - stream->readpos = 0; } } else { break; @@ -947,10 +955,6 @@ PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC) PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC) { - /* not moving anywhere */ - if ((offset == 0 && whence == SEEK_CUR) || (offset == stream->position && whence == SEEK_SET)) - return 0; - /* handle the case where we are in the buffer */ if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) { switch(whence) { @@ -974,8 +978,6 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ } } - /* invalidate the buffer contents */ - stream->readpos = stream->writepos = 0; if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { int ret; @@ -995,6 +997,10 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) { if (ret == 0) stream->eof = 0; + + /* invalidate the buffer contents */ + stream->readpos = stream->writepos = 0; + return ret; } /* else the stream has decided that it can't support seeking after all;