]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fixed quota stream's fseek method
authorVincent Petry <pvince81@owncloud.com>
Fri, 25 Oct 2013 10:09:46 +0000 (12:09 +0200)
committerVincent Petry <pvince81@owncloud.com>
Fri, 25 Oct 2013 10:09:46 +0000 (12:09 +0200)
- Added missing return statement
- Added missing support for SEEK_END
- Fixes #5524

lib/private/files/stream/quota.php
tests/lib/files/stream/quota.php

index 53d8a03d30f6e135d517ef869ef7593fd94ea76b..87301c2361478d60cd9d1f40a141945115728ff1 100644 (file)
@@ -66,12 +66,22 @@ class Quota {
        }
 
        public function stream_seek($offset, $whence = SEEK_SET) {
-               if ($whence === SEEK_SET) {
+               if ($whence === SEEK_END){
+                       // go to the end to find out last position's offset
+                       $oldOffset = $this->stream_tell();
+                       if (fseek($this->source, 0, $whence) !== 0){
+                               return false;
+                       }
+                       $whence = SEEK_SET;
+                       $offset = $this->stream_tell() + $offset;
+                       $this->limit += $oldOffset - $offset;
+               }
+               else if ($whence === SEEK_SET) {
                        $this->limit += $this->stream_tell() - $offset;
                } else {
                        $this->limit -= $offset;
                }
-               fseek($this->source, $offset, $whence);
+               return !fseek($this->source, $offset, $whence);
        }
 
        public function stream_tell() {
index 22d3e93592c7de4285e608111327a45f969434dd..b11f0ac74c0b4f3f2295f66585cd87d95a8f639b 100644 (file)
@@ -75,4 +75,76 @@ class Quota extends \PHPUnit_Framework_TestCase {
                rewind($stream);
                $this->assertEquals('qwerty', fread($stream, 100));
        }
+
+       public function testFseekReturnsSuccess() {
+               $stream = $this->getStream('w+', 100);
+               fwrite($stream, '0123456789');
+               $this->assertEquals(0, fseek($stream, 3, SEEK_SET));
+               $this->assertEquals(0, fseek($stream, -1, SEEK_CUR));
+               $this->assertEquals(0, fseek($stream, -4, SEEK_END));
+       }
+
+       public function testWriteAfterSeekEndWithEnoughSpace() {
+               $stream = $this->getStream('w+', 100);
+               fwrite($stream, '0123456789');
+               fseek($stream, -3, SEEK_END);
+               $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
+       }
+
+       public function testWriteAfterSeekEndWithNotEnoughSpace() {
+               $stream = $this->getStream('w+', 13);
+               fwrite($stream, '0123456789');
+               // seek forward first to potentially week out
+               // potential limit calculation errors
+               fseek($stream, 4, SEEK_SET);
+               // seek to the end
+               fseek($stream, -3, SEEK_END);
+               $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdef', fread($stream, 100));
+       }
+
+       public function testWriteAfterSeekSetWithEnoughSpace() {
+               $stream = $this->getStream('w+', 100);
+               fwrite($stream, '0123456789');
+               fseek($stream, 7, SEEK_SET);
+               $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
+       }
+
+       public function testWriteAfterSeekSetWithNotEnoughSpace() {
+               $stream = $this->getStream('w+', 13);
+               fwrite($stream, '0123456789');
+               fseek($stream, 7, SEEK_SET);
+               $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdef', fread($stream, 100));
+       }
+
+       public function testWriteAfterSeekCurWithEnoughSpace() {
+               $stream = $this->getStream('w+', 100);
+               fwrite($stream, '0123456789');
+               rewind($stream);
+               fseek($stream, 3, SEEK_CUR);
+               fseek($stream, 5, SEEK_CUR);
+               fseek($stream, -1, SEEK_CUR);
+               $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
+       }
+
+       public function testWriteAfterSeekCurWithNotEnoughSpace() {
+               $stream = $this->getStream('w+', 13);
+               fwrite($stream, '0123456789');
+               rewind($stream);
+               fseek($stream, 3, SEEK_CUR);
+               fseek($stream, 5, SEEK_CUR);
+               fseek($stream, -1, SEEK_CUR);
+               $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
+               rewind($stream);
+               $this->assertEquals('0123456abcdef', fread($stream, 100));
+       }
 }