From: Wez Furlong Date: Sun, 22 Dec 2002 18:21:55 +0000 (+0000) Subject: MFH fix for Bug #21131 X-Git-Tag: php-4.3.0~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aaba243f4101cddd0194ec2abad314f4eef9b2a0;p=php MFH fix for Bug #21131 --- diff --git a/ext/standard/tests/file/userstreams.phpt b/ext/standard/tests/file/userstreams.phpt index 12c5fe1399..efc2880f9a 100644 --- a/ext/standard/tests/file/userstreams.phpt +++ b/ext/standard/tests/file/userstreams.phpt @@ -97,7 +97,11 @@ class mystream $split = parse_url($path); $this->varname = $split["host"]; - $this->position = 0; + + if (strchr($mode, 'a')) + $this->position = strlen($GLOBALS[$this->varname]); + else + $this->position = 0; return true; } @@ -301,6 +305,15 @@ while(!feof($fp)) { if ($fail_count == 0) { echo "FGETS: OK\n"; } + +/* One final test to see if the position is respected when opened for append */ +$fp = fopen("test://lyrics", "a+"); +rewind($fp); +var_dump(ftell($fp)); +$data = fgets($fp); +fclose($fp); +echo $data . "\n"; + ?> --EXPECT-- Not Registered @@ -308,3 +321,5 @@ Registered Registered SEEK: OK FGETS: OK +int(0) +...and the road becomes my bride diff --git a/main/streams.c b/main/streams.c index abfdd52255..1f84f4a9ee 100755 --- a/main/streams.c +++ b/main/streams.c @@ -1317,8 +1317,12 @@ PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STRE stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode); - if (stream && self->is_pipe) { - stream->flags |= PHP_STREAM_FLAG_NO_SEEK; + if (stream) { + if (self->is_pipe) { + stream->flags |= PHP_STREAM_FLAG_NO_SEEK; + } else { + stream->position = ftell(file); + } } return stream; @@ -2412,6 +2416,16 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio } } } + + if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && strchr(mode, 'a')) { + fpos_t newpos = 0; + + /* if opened for append, we need to revise our idea of the initial file position */ + if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos TSRMLS_CC)) { + stream->position = newpos; + } + } + if (stream == NULL && (options & REPORT_ERRORS)) { display_wrapper_errors(wrapper, path, "failed to create stream" TSRMLS_CC); }