From b286184e42c2f776054852064f8f59cdd54c2f1f Mon Sep 17 00:00:00 2001 From: Sujith H Date: Mon, 4 Sep 2017 17:02:29 +0530 Subject: [PATCH] [stable10] Fix error logs due to deletion of keys When files are deleted and moved to trashbin, the keys are deleted. This results in exception due to file not exist. And hence this change helps to fix the issue. The issue is caused when the version of the files are tried to retain in the trashbin. Signed-off-by: Sujith H --- apps/files_trashbin/lib/Storage.php | 22 +++++++++++++++++++ apps/files_trashbin/lib/Trashbin.php | 20 ++++++++++++++--- .../Files/Storage/Wrapper/Encryption.php | 12 ++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index dc97146e8ce2..7f3df9040a36 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -199,6 +199,28 @@ private function doDelete($path, $method) { return $result; } + /** + * Retain the encryption keys + * + * @param $filename + * @param $owner + * @param $ownerPath + * @param $timestamp + * @param $sourceStorage + * @return bool + */ + + public function retainKeys($filename, $owner, $ownerPath, $timestamp, $sourceStorage) { + if (\OC::$server->getEncryptionManager()->isEnabled()) { + if ($sourceStorage !== null) { + $sourcePath = '/' . $owner . '/files_trashbin/files/'. $filename . '.d' . $timestamp; + $targetPath = '/' . $owner . '/files/' . $ownerPath; + return $sourceStorage->copyKeys($sourcePath, $targetPath); + } + } + return false; + } + /** * Setup the storate wrapper callback */ diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index 8d4de6aa2afd..b7ba861473e2 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -208,7 +208,7 @@ public static function copyBackupForOwner($ownerPath, $owner, $timestamp) { $view = new View('/'); self::copy_recursive($source, $target, $view); - self::retainVersions($targetFilename, $owner, $ownerPath, $timestamp, true); + self::retainVersions($targetFilename, $owner, $ownerPath, $timestamp, null, true); if ($view->file_exists($target)) { self::insertTrashEntry($owner, $targetFilename, $targetLocation, $timestamp); @@ -304,7 +304,7 @@ public static function move2trash($file_path) { \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', ['filePath' => Filesystem::normalizePath($file_path), 'trashPath' => Filesystem::normalizePath($filename . '.d' . $timestamp)]); - self::retainVersions($filename, $owner, $ownerPath, $timestamp); + self::retainVersions($filename, $owner, $ownerPath, $timestamp, $sourceStorage); // if owner !== user we need to also add a copy to the owners trash if ($user !== $owner) { @@ -331,9 +331,19 @@ public static function move2trash($file_path) { * @param integer $timestamp when the file was deleted * @param bool $forceCopy true to only make a copy of the versions into the trashbin */ - private static function retainVersions($filename, $owner, $ownerPath, $timestamp, $forceCopy = false) { + private static function retainVersions($filename, $owner, $ownerPath, $timestamp, $sourceStorage = null, $forceCopy = false) { if (\OCP\App::isEnabled('files_versions') && !empty($ownerPath)) { + $copyKeysResult = false; + + /** + * In case if encryption is enabled then we need to retain the keys which were + * deleted due to move operation to trashbin. + */ + if ($sourceStorage !== null) { + $copyKeysResult = $sourceStorage->retainKeys($filename, $owner, $ownerPath, $timestamp, $sourceStorage); + } + $user = User::getUser(); $rootView = new View('/'); @@ -355,6 +365,10 @@ private static function retainVersions($filename, $owner, $ownerPath, $timestamp } } } + + if ($copyKeysResult === true) { + $sourceStorage->deleteAllFileKeys($filename); + } } } diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index eb5187d241e8..baa2b7f1618e 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -1028,6 +1028,18 @@ protected function copyKeys($source, $target) { return false; } + /** + * + * delete file keys of the file + * + * @param $path path of the file key to delete + * @return bool + */ + protected function deleteAllFileKeys($path) { + $fullPath = $this->getFullPath($path); + return $this->keyStorage->deleteAllFileKeys($fullPath); + } + /** * check if path points to a files version *