From 165542f27dbd0b3bf05eb714111ff81f1fe79fea Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Dec 2013 17:29:38 +0100 Subject: fix rename of shared files --- apps/files_encryption/hooks/hooks.php | 56 +++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'apps/files_encryption/hooks') diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 7b13ae2a1d0..3e453e210f3 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -30,6 +30,9 @@ use OC\Files\Filesystem; */ class Hooks { + // file for which we want to rename the keys after the rename operation was successful + private static $renamedFiles = array(); + /** * @brief Startup encryption backend upon user login * @note This method should never be called for users using client side encryption @@ -479,6 +482,18 @@ class Hooks { } } + /** + * @brief mark file as renamed so that we know the original source after the file was renamed + * @param string $path + */ + public static function preRename($params) { + $util = new Util(new \OC_FilesystemView('/'), \OCP\User::getUser()); + list($ownerOld, $pathOld) = $util->getUidAndFilename($params['oldpath']); + self::$renamedFiles[$params['oldpath']] = array( + 'uid' => $ownerOld, + 'path' => $pathOld); + } + /** * @brief after a file is renamed, rename its keyfile and share-keys also fix the file size and fix also the sharing * @param array with oldpath and newpath @@ -501,19 +516,25 @@ class Hooks { $userId = \OCP\User::getUser(); $util = new Util($view, $userId); + $ownerOld = self::$renamedFiles[$params['oldpath']]['uid']; + $pathOld = self::$renamedFiles[$params['oldpath']]['path']; + list($ownerNew, $pathNew) = $util->getUidAndFilename($params['newpath']); + // Format paths to be relative to user files dir - if ($util->isSystemWideMountPoint($params['oldpath'])) { - $baseDir = 'files_encryption/'; - $oldKeyfilePath = $baseDir . 'keyfiles/' . $params['oldpath']; + if ($util->isSystemWideMountPoint($pathOld)) { + $oldKeyfilePath = 'files_encryption/keyfiles/' . $pathOld; + $oldShareKeyPath = 'files_encryption/share-keys/' . $pathOld; } else { - $baseDir = $userId . '/' . 'files_encryption/'; - $oldKeyfilePath = $baseDir . 'keyfiles/' . $params['oldpath']; + $oldKeyfilePath = $ownerOld . '/' . 'files_encryption/keyfiles/' . $pathOld; + $oldShareKeyPath = $ownerOld . '/' . 'files_encryption/share-keys/' . $pathOld; } - if ($util->isSystemWideMountPoint($params['newpath'])) { - $newKeyfilePath = $baseDir . 'keyfiles/' . $params['newpath']; + if ($util->isSystemWideMountPoint($pathNew)) { + $newKeyfilePath = 'files_encryption/keyfiles/' . $pathNew; + $newShareKeyPath = 'files_encryption/share-keys/' . $pathNew; } else { - $newKeyfilePath = $baseDir . 'keyfiles/' . $params['newpath']; + $newKeyfilePath = $ownerNew . '/files_encryption/keyfiles/' . $pathNew; + $newShareKeyPath = $ownerNew . '/files_encryption/share-keys/' . $pathNew; } // add key ext if this is not an folder @@ -522,11 +543,11 @@ class Hooks { $newKeyfilePath .= '.key'; // handle share-keys - $localKeyPath = $view->getLocalFile($baseDir . 'share-keys/' . $params['oldpath']); + $localKeyPath = $view->getLocalFile($oldShareKeyPath); $escapedPath = Helper::escapeGlobPattern($localKeyPath); $matches = glob($escapedPath . '*.shareKey'); foreach ($matches as $src) { - $dst = \OC\Files\Filesystem::normalizePath(str_replace($params['oldpath'], $params['newpath'], $src)); + $dst = \OC\Files\Filesystem::normalizePath(str_replace($pathOld, $pathNew, $src)); // create destination folder if not exists if (!file_exists(dirname($dst))) { @@ -538,15 +559,13 @@ class Hooks { } else { // handle share-keys folders - $oldShareKeyfilePath = $baseDir . 'share-keys/' . $params['oldpath']; - $newShareKeyfilePath = $baseDir . 'share-keys/' . $params['newpath']; // create destination folder if not exists - if (!$view->file_exists(dirname($newShareKeyfilePath))) { - $view->mkdir(dirname($newShareKeyfilePath), 0750, true); + if (!$view->file_exists(dirname($newShareKeyPath))) { + $view->mkdir(dirname($newShareKeyPath), 0750, true); } - $view->rename($oldShareKeyfilePath, $newShareKeyfilePath); + $view->rename($oldShareKeyPath, $newShareKeyPath); } // Rename keyfile so it isn't orphaned @@ -561,18 +580,17 @@ class Hooks { } // build the path to the file - $newPath = '/' . $userId . '/files' . $params['newpath']; - $newPathRelative = $params['newpath']; + $newPath = '/' . $ownerNew . '/files' . $pathNew; if ($util->fixFileSize($newPath)) { // get sharing app state $sharingEnabled = \OCP\Share::isEnabled(); // get users - $usersSharing = $util->getSharingUsersArray($sharingEnabled, $newPathRelative); + $usersSharing = $util->getSharingUsersArray($sharingEnabled, $pathNew); // update sharing-keys - $util->setSharedFileKeyfiles($session, $usersSharing, $newPathRelative); + $util->setSharedFileKeyfiles($session, $usersSharing, $pathNew); } \OC_FileProxy::$enabled = $proxyStatus; -- cgit v1.2.3 From 552d0a6feb137ad1144312c6e6eef1c295e41572 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 19 Dec 2013 18:40:40 +0100 Subject: check if the old owner and the old path is available --- apps/files_encryption/hooks/hooks.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'apps/files_encryption/hooks') diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 3e453e210f3..09d5687e226 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -484,7 +484,7 @@ class Hooks { /** * @brief mark file as renamed so that we know the original source after the file was renamed - * @param string $path + * @param array $params with the old path and the new path */ public static function preRename($params) { $util = new Util(new \OC_FilesystemView('/'), \OCP\User::getUser()); @@ -516,8 +516,15 @@ class Hooks { $userId = \OCP\User::getUser(); $util = new Util($view, $userId); - $ownerOld = self::$renamedFiles[$params['oldpath']]['uid']; - $pathOld = self::$renamedFiles[$params['oldpath']]['path']; + if (isset(self::$renamedFiles[$params['oldpath']]['uid']) && + isset(self::$renamedFiles[$params['oldpath']]['path'])) { + $ownerOld = self::$renamedFiles[$params['oldpath']]['uid']; + $pathOld = self::$renamedFiles[$params['oldpath']]['path']; + } else { + \OCP\Util::writeLog('Encryption library', "can't get path and owner from the file before it was renamed", \OCP\Util::ERROR); + return false; + } + list($ownerNew, $pathNew) = $util->getUidAndFilename($params['newpath']); // Format paths to be relative to user files dir -- cgit v1.2.3