Merge pull request #5833 from owncloud/encryption_fixes

[encryption] preserve timestamps and etags during encryption/decryption
remotes/origin/stable6
Björn Schießle 13 years ago
commit 3ad546002f
  1. 15
      apps/files_encryption/lib/stream.php
  2. 22
      apps/files_encryption/lib/util.php
  3. 58
      apps/files_encryption/tests/util.php

@ -67,6 +67,7 @@ class Stream {
* @var \OC\Files\View * @var \OC\Files\View
*/ */
private $rootView; // a fsview object set to '/' private $rootView; // a fsview object set to '/'
/** /**
* @var \OCA\Encryption\Session * @var \OCA\Encryption\Session
*/ */
@ -528,19 +529,21 @@ class Stream {
\OC_FileProxy::$enabled = $proxyStatus; \OC_FileProxy::$enabled = $proxyStatus;
} }
// get file info // we need to update the file info for the real file, not for the
$fileInfo = $this->rootView->getFileInfo($this->rawPath); // part file.
if (!is_array($fileInfo)) { $path = Helper::stripPartialFileExtension($this->rawPath);
$fileInfo = array();
}
// get file info
$fileInfo = $this->rootView->getFileInfo($path);
if (is_array($fileInfo)) {
// set encryption data // set encryption data
$fileInfo['encrypted'] = true; $fileInfo['encrypted'] = true;
$fileInfo['size'] = $this->size; $fileInfo['size'] = $this->size;
$fileInfo['unencrypted_size'] = $this->unencryptedSize; $fileInfo['unencrypted_size'] = $this->unencryptedSize;
// set fileinfo // set fileinfo
$this->rootView->putFileInfo($this->rawPath, $fileInfo); $this->rootView->putFileInfo($path, $fileInfo);
}
} }

@ -717,17 +717,17 @@ class Util {
// Encrypt unencrypted files // Encrypt unencrypted files
foreach ($found['encrypted'] as $encryptedFile) { foreach ($found['encrypted'] as $encryptedFile) {
//get file info
$fileInfo = \OC\Files\Filesystem::getFileInfo($encryptedFile['path']);
//relative to data/<user>/file //relative to data/<user>/file
$relPath = Helper::stripUserFilesPath($encryptedFile['path']); $relPath = Helper::stripUserFilesPath($encryptedFile['path']);
//get file info
$fileInfo = \OC\Files\Filesystem::getFileInfo($relPath);
//relative to /data //relative to /data
$rawPath = $encryptedFile['path']; $rawPath = $encryptedFile['path'];
//get timestamp //get timestamp
$timestamp = $this->view->filemtime($rawPath); $timestamp = $fileInfo['mtime'];
//enable proxy to use OC\Files\View to access the original file //enable proxy to use OC\Files\View to access the original file
\OC_FileProxy::$enabled = true; \OC_FileProxy::$enabled = true;
@ -768,10 +768,10 @@ class Util {
$this->view->rename($relPath . '.part', $relPath); $this->view->rename($relPath . '.part', $relPath);
$this->view->chroot($fakeRoot);
//set timestamp //set timestamp
$this->view->touch($rawPath, $timestamp); $this->view->touch($relPath, $timestamp);
$this->view->chroot($fakeRoot);
// Add the file to the cache // Add the file to the cache
\OC\Files\Filesystem::putFileInfo($relPath, array( \OC\Files\Filesystem::putFileInfo($relPath, array(
@ -839,7 +839,7 @@ class Util {
$rawPath = '/' . $this->userId . '/files/' . $plainFile['path']; $rawPath = '/' . $this->userId . '/files/' . $plainFile['path'];
// keep timestamp // keep timestamp
$timestamp = $this->view->filemtime($rawPath); $timestamp = $fileInfo['mtime'];
// Open plain file handle for binary reading // Open plain file handle for binary reading
$plainHandle = $this->view->fopen($rawPath, 'rb'); $plainHandle = $this->view->fopen($rawPath, 'rb');
@ -858,10 +858,10 @@ class Util {
$this->view->rename($relPath . '.part', $relPath); $this->view->rename($relPath . '.part', $relPath);
$this->view->chroot($fakeRoot);
// set timestamp // set timestamp
$this->view->touch($rawPath, $timestamp); $this->view->touch($relPath, $timestamp);
$this->view->chroot($fakeRoot);
// Add the file to the cache // Add the file to the cache
\OC\Files\Filesystem::putFileInfo($relPath, array( \OC\Files\Filesystem::putFileInfo($relPath, array(

@ -281,6 +281,64 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$this->assertFalse($this->util->isSharedPath($path)); $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);
$this->assertTrue(is_array($fileInfoEncrypted));
// encrypt all unencrypted files
$util->decryptAll('/' . $this->userId . '/' . 'files');
$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
$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 * @large
*/ */

Loading…
Cancel
Save