wez             Sun Dec 22 13:05:36 2002 EDT

  Modified files:              
    /php4/ext/standard/tests/file       userstreams.phpt 
    /php4/main  streams.c 
  Log:
  Fix for Bug #21131: fopen($file, 'a+') would incorrectly assume that
  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.
  
  
Index: php4/ext/standard/tests/file/userstreams.phpt
diff -u php4/ext/standard/tests/file/userstreams.phpt:1.8 
php4/ext/standard/tests/file/userstreams.phpt:1.9
--- php4/ext/standard/tests/file/userstreams.phpt:1.8   Tue Oct 29 09:36:49 2002
+++ php4/ext/standard/tests/file/userstreams.phpt       Sun Dec 22 13:05:36 2002
@@ -97,7 +97,11 @@
 
                $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 @@
 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
 SEEK: OK
 FGETS: OK
+int(0)
+...and the road becomes my bride
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.136 php4/main/streams.c:1.137
--- php4/main/streams.c:1.136   Thu Dec 19 15:23:50 2002
+++ php4/main/streams.c Sun Dec 22 13:05:36 2002
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: streams.c,v 1.136 2002/12/19 20:23:50 wez Exp $ */
+/* $Id: streams.c,v 1.137 2002/12/22 18:05:36 wez Exp $ */
 
 #define _GNU_SOURCE
 #include "php.h"
@@ -1317,8 +1317,12 @@
        
        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 @@
                                }
                }
        }
+
+       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);
        }



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to