From 894856ce01daa5fd1e6a8b7dd3a62a41bd24df42 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Nov 2013 15:51:51 +0100 Subject: [PATCH 1/5] make sure that we keep the correct timestamp and etag after encryoption/decryption --- apps/files_encryption/lib/util.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 5e855abd973..5800d3ed108 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -717,17 +717,17 @@ class Util { // Encrypt unencrypted files foreach ($found['encrypted'] as $encryptedFile) { - //get file info - $fileInfo = \OC\Files\Filesystem::getFileInfo($encryptedFile['path']); - //relative to data//file $relPath = Helper::stripUserFilesPath($encryptedFile['path']); + //get file info + $fileInfo = \OC\Files\Filesystem::getFileInfo($relPath); + //relative to /data $rawPath = $encryptedFile['path']; //get timestamp - $timestamp = $this->view->filemtime($rawPath); + $timestamp = $fileInfo['mtime']; //enable proxy to use OC\Files\View to access the original file \OC_FileProxy::$enabled = true; @@ -768,10 +768,10 @@ class Util { $this->view->rename($relPath . '.part', $relPath); - $this->view->chroot($fakeRoot); - //set timestamp - $this->view->touch($rawPath, $timestamp); + $this->view->touch($relPath, $timestamp); + + $this->view->chroot($fakeRoot); // Add the file to the cache \OC\Files\Filesystem::putFileInfo($relPath, array( @@ -839,7 +839,7 @@ class Util { $rawPath = '/' . $this->userId . '/files/' . $plainFile['path']; // keep timestamp - $timestamp = $this->view->filemtime($rawPath); + $timestamp = $fileInfo['mtime']; // Open plain file handle for binary reading $plainHandle = $this->view->fopen($rawPath, 'rb'); @@ -858,10 +858,10 @@ class Util { $this->view->rename($relPath . '.part', $relPath); - $this->view->chroot($fakeRoot); - // set timestamp - $this->view->touch($rawPath, $timestamp); + $this->view->touch($relPath, $timestamp); + + $this->view->chroot($fakeRoot); // Add the file to the cache \OC\Files\Filesystem::putFileInfo($relPath, array( From e2fb8d7128d0022406d3cc22cda0439acb599f09 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Nov 2013 15:55:59 +0100 Subject: [PATCH 2/5] only update file cache if the file is already indexed to avoid that we create a incomplete file cache entry --- apps/files_encryption/lib/stream.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index 5ce5caf80ce..6e2b2ceb27b 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -67,6 +67,7 @@ class Stream { * @var \OC\Files\View */ private $rootView; // a fsview object set to '/' + /** * @var \OCA\Encryption\Session */ @@ -528,20 +529,22 @@ class Stream { \OC_FileProxy::$enabled = $proxyStatus; } + // we need to update the file info for the real file, not for the + // part file. + $path = Helper::fixPartialFilePath($this->rawPath); + // get file info - $fileInfo = $this->rootView->getFileInfo($this->rawPath); - if (!is_array($fileInfo)) { - $fileInfo = array(); + $fileInfo = $this->rootView->getFileInfo($path); + if (is_array($fileInfo)) { + // set encryption data + $fileInfo['encrypted'] = true; + $fileInfo['size'] = $this->size; + $fileInfo['unencrypted_size'] = $this->unencryptedSize; + + // set fileinfo + $this->rootView->putFileInfo($path, $fileInfo); } - // set encryption data - $fileInfo['encrypted'] = true; - $fileInfo['size'] = $this->size; - $fileInfo['unencrypted_size'] = $this->unencryptedSize; - - // set fileinfo - $this->rootView->putFileInfo($this->rawPath, $fileInfo); - } return fclose($this->handle); From dd0ebcede2ae163fda19996311cd643631ca3637 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Nov 2013 18:48:31 +0100 Subject: [PATCH 3/5] fixPartialFilePath() is now stripPartialFileExtension() --- apps/files_encryption/lib/stream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index 6e2b2ceb27b..069d9145a28 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -531,7 +531,7 @@ class Stream { // we need to update the file info for the real file, not for the // part file. - $path = Helper::fixPartialFilePath($this->rawPath); + $path = Helper::stripPartialFileExtension($this->rawPath); // get file info $fileInfo = $this->rootView->getFileInfo($path); From 2df376367582edfbbfb98f8fe9837c5b57e5ad0c Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 14 Nov 2013 12:11:21 +0100 Subject: [PATCH 4/5] test encryptAll() and decryptAll() to make sure that mtime and etag stay the same --- apps/files_encryption/tests/util.php | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index 1b93bc36c8e..0f21ed96e45 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -281,6 +281,70 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->util->isSharedPath($path)); } + function testEncryptAll() { + + $filename = "/encryptAll" . time() . ".txt"; + $util = new Encryption\Util($this->view, $this->userId); + + // disable encryption to upload a unencrypted file + \OC_App::disable('files_encryption'); + + $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort); + + $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); + + $this->assertTrue(is_array($fileInfoUnencrypted)); + + // enable file encryption again + \OC_App::enable('files_encryption'); + + // encrypt all unencrypted files + $util->encryptAll('/' . $this->userId . '/' . 'files'); + + $fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); + + $this->assertTrue(is_array($fileInfoEncrypted)); + + // check if mtime and etags unchanged + $this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']); + $this->assertEquals($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']); + + $this->view->unlink($this->userId . '/files/' . $filename); + } + + + function testDecryptAll() { + + $filename = "/decryptAll" . time() . ".txt"; + $util = new Encryption\Util($this->view, $this->userId); + + $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort); + + $fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); + + print("\n File Info Encrypted\n"); + print_r($fileInfoEncrypted); + + $this->assertTrue(is_array($fileInfoEncrypted)); + + // encrypt all unencrypted files + $util->decryptAll('/' . $this->userId . '/' . 'files'); + + $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); + + print("\n File Info Unencrypted\n"); + print_r($fileInfoUnencrypted); + + $this->assertTrue(is_array($fileInfoUnencrypted)); + + // check if mtime and etags unchanged + $this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']); + $this->assertEquals($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']); + + $this->view->unlink($this->userId . '/files/' . $filename); + + } + /** * @large */ From 2624a6cb328102c4029b91dcc7617fa953ac454d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 14 Nov 2013 12:13:22 +0100 Subject: [PATCH 5/5] remove debug output --- apps/files_encryption/tests/util.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index 0f21ed96e45..e8dfb74f3f3 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -322,9 +322,6 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - print("\n File Info Encrypted\n"); - print_r($fileInfoEncrypted); - $this->assertTrue(is_array($fileInfoEncrypted)); // encrypt all unencrypted files @@ -332,9 +329,6 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - print("\n File Info Unencrypted\n"); - print_r($fileInfoUnencrypted); - $this->assertTrue(is_array($fileInfoUnencrypted)); // check if mtime and etags unchanged