]> source.dussan.org Git - nextcloud-server.git/commitdiff
files_encryption: Fix getFileSize()
authorMarkus Goetz <markus@woboq.com>
Sat, 5 Oct 2013 16:00:46 +0000 (18:00 +0200)
committerMarkus Goetz <markus@woboq.com>
Mon, 21 Oct 2013 14:02:33 +0000 (16:02 +0200)
For certain file sizes, we rounded to the wrong chunk number
and the returned bogus results. This should fix
https://github.com/owncloud/mirall/issues/1009

apps/files_encryption/lib/util.php
apps/files_encryption/tests/util.php

index a2aa305b64c960f1aedceefd73f3681237ee8aa1..121b1c4f573d8e3dee4ac0a4d4422da8400e431a 100644 (file)
@@ -587,11 +587,18 @@ class Util {
                ) {
 
                        // get the size from filesystem
-                       $fullPath = $this->view->getLocalFile($path);
                        $size = $this->view->filesize($path);
 
+                       // fast path, else the calculation for $lastChunkNr is bogus
+                       if ($size === 0) {
+                               \OC_FileProxy::$enabled = $proxyStatus;
+                               return 0;
+                       }
+
                        // calculate last chunk nr
-                       $lastChunkNr = floor($size / 8192);
+                       // next highest is end of chunks, one subtracted is last one
+                       // we have to read the last chunk, we can't just calculate it (because of padding etc)
+                       $lastChunkNr = ceil($size/ 8192) - 1;
                        $lastChunkSize = $size - ($lastChunkNr * 8192);
 
                        // open stream
index bf45b9c1d34abf39f3fada406ea5d21d06a96047..acd3ff810c64271fba7aba4c0e271a6acc14f7b2 100755 (executable)
@@ -231,6 +231,34 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
                $this->view->unlink($this->userId . '/files/' . $filename);
        }
 
+       /**
+        * @brief Test that data that is read by the crypto stream wrapper
+        */
+       function testGetFileSize() {
+               \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
+
+               $filename = 'tmp-' . time();
+               $externalFilename = '/' . $this->userId . '/files/' . $filename;
+
+               // Test for 0 byte files
+               $problematicFileSizeData = "";
+               $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
+               $this->assertTrue(is_int($cryptedFile));
+               $this->assertEquals($this->util->getFileSize($externalFilename), 0);
+               $decrypt = $this->view->file_get_contents($externalFilename);
+               $this->assertEquals($problematicFileSizeData, $decrypt);
+               $this->view->unlink($this->userId . '/files/' . $filename);
+
+               // Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009
+               $problematicFileSizeData = str_pad("", 18377, "abc");
+               $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
+               $this->assertTrue(is_int($cryptedFile));
+               $this->assertEquals($this->util->getFileSize($externalFilename), 18377);
+               $decrypt = $this->view->file_get_contents($externalFilename);
+               $this->assertEquals($problematicFileSizeData, $decrypt);
+               $this->view->unlink($this->userId . '/files/' . $filename);
+       }
+
        function testIsSharedPath() {
                $sharedPath = '/user1/files/Shared/test';
                $path = '/user1/files/test';