]> source.dussan.org Git - nextcloud-server.git/commitdiff
Properly detect streamCopy errors
authorVincent Petry <pvince81@owncloud.com>
Mon, 23 Feb 2015 11:31:22 +0000 (12:31 +0100)
committerVincent Petry <pvince81@owncloud.com>
Wed, 25 Feb 2015 15:03:15 +0000 (16:03 +0100)
Now checking whether the written bytes match the number of read bytes.

lib/private/helper.php
tests/lib/files/storage/wrapper/quota.php

index 04c063145ba76c38186b3fff3c2b3d962bdf4a00..f4992744ad9992fc1d5aa1ddea2359aee5412806 100644 (file)
@@ -594,13 +594,23 @@ class OC_Helper {
                if (!$source or !$target) {
                        return array(0, false);
                }
+               $bufSize = 8192;
                $result = true;
                $count = 0;
                while (!feof($source)) {
-                       if (($c = fwrite($target, fread($source, 8192))) === false) {
+                       $buf = fread($source, $bufSize);
+                       $bytesWritten = fwrite($target, $buf);
+                       if ($bytesWritten !== false) {
+                               $count += $bytesWritten;
+                       }
+                       // note: strlen is expensive so only use it when necessary,
+                       // on the last block
+                       if ($bytesWritten === false
+                               || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf))
+                       ) {
+                               // write error, could be disk full ?
                                $result = false;
-                       } else {
-                               $count += $c;
+                               break;
                        }
                }
                return array($count, $result);
index dc4de4697db445eb8442fb901f04ce25c1a42814..8ca8f308b71546bb872028c88e014dff747b527d 100644 (file)
@@ -99,6 +99,28 @@ class Quota extends \Test\Files\Storage\Storage {
                $this->assertEquals('foobarqwe', $instance->file_get_contents('foo'));
        }
 
+       public function testStreamCopyWithEnoughSpace() {
+               $instance = $this->getLimitedStorage(16);
+               $inputStream = fopen('data://text/plain,foobarqwerty', 'r');
+               $outputStream = $instance->fopen('foo', 'w+');
+               list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
+               $this->assertEquals(12, $count);
+               $this->assertTrue($result);
+               fclose($inputStream);
+               fclose($outputStream);
+       }
+
+       public function testStreamCopyNotEnoughSpace() {
+               $instance = $this->getLimitedStorage(9);
+               $inputStream = fopen('data://text/plain,foobarqwerty', 'r');
+               $outputStream = $instance->fopen('foo', 'w+');
+               list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
+               $this->assertEquals(9, $count);
+               $this->assertFalse($result);
+               fclose($inputStream);
+               fclose($outputStream);
+       }
+
        public function testReturnFalseWhenFopenFailed() {
                $failStorage = $this->getMock(
                        '\OC\Files\Storage\Local',