]> granicus.if.org Git - php/commitdiff
Fix for Bug #21131: fopen($file, 'a+') would incorrectly assume that
authorWez Furlong <wez@php.net>
Sun, 22 Dec 2002 18:05:36 +0000 (18:05 +0000)
committerWez Furlong <wez@php.net>
Sun, 22 Dec 2002 18:05:36 +0000 (18:05 +0000)
the stream position was at offset 0.
This corrects that assumption by querying the stream for it's position
when it detects the 'a' "flag" in the mode parameter to fopen.
Also added a test for plain files and amended the userstreams test to
take this into account.

ext/standard/tests/file/userstreams.phpt
main/streams.c

index 12c5fe13999785738a1238edaf8bb520f3df8ebb..efc2880f9a68e8e66cbb9d2b922eb744e5738d0e 100644 (file)
@@ -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
index abfdd52255fe7f591fa1bd23aaf28b76bab200a2..1f84f4a9eea762ae0b178cd305b2709e6a24a3c6 100755 (executable)
@@ -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);
        }