From 7c250f6265d31aa7d191fdc4aff76cb85d015909 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 13 Nov 2024 18:51:02 +0100 Subject: [PATCH 1/9] fix: fix missing dot in unique name when restoring from trash Signed-off-by: Robin Appelman --- lib/Trash/TrashBackend.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 98a27a240..8e9fa9b59 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -138,7 +138,7 @@ public function restoreItem(ITrashItem $item) { $target .= ' (' . $i . ')'; if (isset($info['extension'])) { - $target .= $info['extension']; + $target .= '.' . $info['extension']; } return $target; From f506227aae01e41088fa53f0be54766741b5c1ce Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 13 Nov 2024 18:53:38 +0100 Subject: [PATCH 2/9] fix: remove manual un-jailing when moving to trash as the storage now handles this properly* *: expect for the mentioned bug Signed-off-by: Robin Appelman --- lib/Trash/TrashBackend.php | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 8e9fa9b59..969e6952f 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -6,7 +6,7 @@ namespace OCA\GroupFolders\Trash; -use OC\Files\Storage\Wrapper\Jail; +use OC\Files\Storage\Wrapper\Encryption; use OCA\Files_Trashbin\Expiration; use OCA\Files_Trashbin\Trash\ITrashBackend; use OCA\Files_Trashbin\Trash\ITrashItem; @@ -209,12 +209,17 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool { $trashStorage = $trashFolder->getStorage(); $time = time(); $trashName = $name . '.d' . $time; - [$unJailedStorage, $unJailedInternalPath] = $this->unwrapJails($storage, $internalPath); $targetInternalPath = $trashFolder->getInternalPath() . '/' . $trashName; - if ($trashStorage->moveFromStorage($unJailedStorage, $unJailedInternalPath, $targetInternalPath)) { + // until the fix from https://github.com/nextcloud/server/pull/49262 is in all versions we support we need to manually disable the optimization + if ($storage->instanceOfStorage(Encryption::class)) { + $result = $this->moveFromEncryptedStorage($storage, $trashStorage, $internalPath, $targetInternalPath); + } else { + $result = $trashStorage->moveFromStorage($storage, $internalPath, $targetInternalPath); + } + if ($result) { $this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId(), $this->userSession->getUser()->getUID()); if ($trashStorage->getCache()->getId($targetInternalPath) !== $fileEntry->getId()) { - $trashStorage->getCache()->moveFromCache($unJailedStorage->getCache(), $unJailedInternalPath, $targetInternalPath); + $trashStorage->getCache()->moveFromCache($storage->getCache(), $internalPath, $targetInternalPath); } } else { throw new \Exception("Failed to move groupfolder item to trash"); @@ -225,16 +230,36 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool { } } - private function unwrapJails(IStorage $storage, string $internalPath): array { - $unJailedInternalPath = $internalPath; - $unJailedStorage = $storage; - while ($unJailedStorage->instanceOfStorage(Jail::class)) { - $unJailedStorage = $unJailedStorage->getWrapperStorage(); - if ($unJailedStorage instanceof Jail) { - $unJailedInternalPath = $unJailedStorage->getUnjailedPath($unJailedInternalPath); + /** + * move from storage when we can't just move within the storage + * + * This is copied from the fallback implementation from Common::moveFromStorage + */ + private function moveFromEncryptedStorage(IStorage $sourceStorage, IStorage $targetStorage, string $sourceInternalPath, string $targetInternalPath): bool { + if (!$sourceStorage->isDeletable($sourceInternalPath)) { + return false; + } + + $result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); + if ($result) { + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(true); + } + try { + if ($sourceStorage->is_dir($sourceInternalPath)) { + $result = $sourceStorage->rmdir($sourceInternalPath); + } else { + $result = $sourceStorage->unlink($sourceInternalPath); + } + } finally { + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + /** @var ObjectStoreStorage $sourceStorage */ + $sourceStorage->setPreserveCacheOnDelete(false); + } } } - return [$unJailedStorage, $unJailedInternalPath]; + return $result; } private function userHasAccessToFolder(IUser $user, int $folderId): bool { From 377bab66195239e2eff07bb3daa758d7bb057cc8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 13 Nov 2024 19:01:57 +0100 Subject: [PATCH 3/9] fix: use "full" target folder from user when restoring trash items Signed-off-by: Robin Appelman --- lib/Trash/TrashBackend.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 969e6952f..e9a57dbdd 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -99,6 +99,7 @@ public function restoreItem(ITrashItem $item) { throw new \LogicException('Trying to restore normal trash item in group folder trash backend'); } $user = $item->getUser(); + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); [, $folderId] = explode('/', $item->getTrashPath()); $node = $this->getNodeForTrashItem($user, $item); if ($node === null) { @@ -114,7 +115,7 @@ public function restoreItem(ITrashItem $item) { $trashStorage = $node->getStorage(); /** @var Folder $targetFolder */ - $targetFolder = $this->mountProvider->getFolder((int)$folderId); + $targetFolder = $userFolder->get($item->getGroupFolderMountPoint()); $originalLocation = $item->getInternalOriginalLocation(); $parent = dirname($originalLocation); if ($parent === '.') { From a20f2655a29783d5ce0e41de56a7a9a14861aa51 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 18 Nov 2024 14:42:57 +0100 Subject: [PATCH 4/9] fix: make root cache entry optional for groupfolder storage Signed-off-by: Robin Appelman --- lib/Mount/GroupFolderStorage.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Mount/GroupFolderStorage.php b/lib/Mount/GroupFolderStorage.php index 0b0dbe37d..3230891bc 100644 --- a/lib/Mount/GroupFolderStorage.php +++ b/lib/Mount/GroupFolderStorage.php @@ -16,7 +16,7 @@ class GroupFolderStorage extends Quota { private int $folderId; - private ICacheEntry $rootEntry; + private ?ICacheEntry $rootEntry; private IUserSession $userSession; private ?IUser $mountOwner = null; /** @var RootEntryCache|null */ @@ -53,7 +53,11 @@ public function getCache($path = '', $storage = null) { $storage = $this; } - $this->cache = new RootEntryCache(parent::getCache($path, $storage), $this->rootEntry); + $cache = parent::getCache($path, $storage); + if ($this->rootEntry !== null) { + $cache = new RootEntryCache($cache, $this->rootEntry);} + $this->cache = $cache; + return $this->cache; } From 598b961f8dde27d36fc7612ae2983c082fe62ec8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 18 Nov 2024 14:43:56 +0100 Subject: [PATCH 5/9] fix: create a separate mount for trashbin to make encryption work Signed-off-by: Robin Appelman --- lib/Mount/MountProvider.php | 125 +++++++++++++++++++++++++---------- lib/Trash/GroupTrashItem.php | 13 +--- lib/Trash/TrashBackend.php | 38 ++++++++--- 3 files changed, 119 insertions(+), 57 deletions(-) diff --git a/lib/Mount/MountProvider.php b/lib/Mount/MountProvider.php index 49354b18a..7461865f7 100644 --- a/lib/Mount/MountProvider.php +++ b/lib/Mount/MountProvider.php @@ -128,7 +128,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) { $aclManager = $this->aclManagerFactory->getACLManager($user, $this->getRootStorageId()); $rootRules = $aclManager->getRelevantRulesForPath($aclRootPaths); - return array_values(array_filter(array_map(function ($folder) use ($user, $loader, $conflicts, $aclManager, $rootRules) { + return array_merge(...array_filter(array_map(function (array $folder) use ($user, $loader, $conflicts, $aclManager, $rootRules): ?array { // check for existing files in the user home and rename them if needed $originalFolderName = $folder['mount_point']; if (in_array($originalFolderName, $conflicts)) { @@ -147,7 +147,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) { $userStorage->getPropagator()->propagateChange("files/$folderName", time()); } - return $this->getMount( + $mount = $this->getMount( $folder['folder_id'], '/' . $user->getUID() . '/files/' . $folder['mount_point'], $folder['permissions'], @@ -159,6 +159,22 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) { $aclManager, $rootRules ); + if (!$mount) { + return null; + } + $trashMount = $this->getTrashMount( + $folder['folder_id'], + '/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folder['folder_id'], + $folder['quota'], + $loader, + $user + ); + + return [ + $mount, + $trashMount, + ]; + }, $folders))); } @@ -181,16 +197,16 @@ private function getCurrentUID(): ?string { } public function getMount( - int $id, - string $mountPoint, - int $permissions, - int $quota, - ?ICacheEntry $cacheEntry = null, + int $id, + string $mountPoint, + int $permissions, + int $quota, + ?ICacheEntry $cacheEntry = null, ?IStorageFactory $loader = null, - bool $acl = false, - ?IUser $user = null, - ?ACLManager $aclManager = null, - array $rootRules = [] + bool $acl = false, + ?IUser $user = null, + ?ACLManager $aclManager = null, + array $rootRules = [] ): ?IMountPoint { if (!$cacheEntry) { // trigger folder creation @@ -220,52 +236,91 @@ public function getMount( $cacheEntry['permissions'] &= $aclRootPermissions; } + $quotaStorage = $this->getGroupFolderStorage($id, $storage, $user, $rootPath, $quota, $cacheEntry); + + $maskedStore = new PermissionsMask([ + 'storage' => $quotaStorage, + 'mask' => $permissions, + ]); + + if (!$this->allowRootShare) { + $maskedStore = new RootPermissionsMask([ + 'storage' => $maskedStore, + 'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, + ]); + } + + return new GroupMountPoint( + $id, + $maskedStore, + $mountPoint, + null, + $loader + ); + } + + public function getTrashMount( + int $id, + string $mountPoint, + int $quota, + IStorageFactory $loader, + IUser $user, + ): IMountPoint { + + $storage = $this->getRootFolder()->getStorage(); + + $storage->setOwner($user?->getUID()); + + $trashPath = $this->getRootFolder()->getInternalPath() . '/trash/' . $id; + + $trashStorage = $this->getGroupFolderStorage($id, $storage, $user, $trashPath, $quota, null); + + return new GroupMountPoint( + $id, + $trashStorage, + $mountPoint, + null, + $loader + ); + } + + public function getGroupFolderStorage( + int $id, + IStorage $rootStorage, + ?IUser $user, + string $rootPath, + int $quota, + ?ICacheEntry $rootCacheEntry, + ): IStorage { if ($this->enableEncryption) { $baseStorage = new GroupFolderEncryptionJail([ - 'storage' => $storage, - 'root' => $rootPath + 'storage' => $rootStorage, + 'root' => $rootPath, ]); $quotaStorage = new GroupFolderStorage([ 'storage' => $baseStorage, 'quota' => $quota, 'folder_id' => $id, - 'rootCacheEntry' => $cacheEntry, + 'rootCacheEntry' => $rootCacheEntry, 'userSession' => $this->userSession, 'mountOwner' => $user, ]); } else { $baseStorage = new Jail([ - 'storage' => $storage, - 'root' => $rootPath + 'storage' => $rootStorage, + 'root' => $rootPath, ]); $quotaStorage = new GroupFolderNoEncryptionStorage([ 'storage' => $baseStorage, 'quota' => $quota, 'folder_id' => $id, - 'rootCacheEntry' => $cacheEntry, + 'rootCacheEntry' => $rootCacheEntry, 'userSession' => $this->userSession, 'mountOwner' => $user, ]); } - $maskedStore = new PermissionsMask([ - 'storage' => $quotaStorage, - 'mask' => $permissions - ]); - - if (!$this->allowRootShare) { - $maskedStore = new RootPermissionsMask([ - 'storage' => $maskedStore, - 'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, - ]); - } - return new GroupMountPoint( - $id, - $maskedStore, - $mountPoint, - null, - $loader - ); + return $quotaStorage; } public function getJailPath(int $folderId): string { diff --git a/lib/Trash/GroupTrashItem.php b/lib/Trash/GroupTrashItem.php index 819db698c..a10f8d5a2 100644 --- a/lib/Trash/GroupTrashItem.php +++ b/lib/Trash/GroupTrashItem.php @@ -45,18 +45,7 @@ public function getTitle(): string { return $this->getGroupFolderMountPoint() . '/' . $this->getOriginalLocation(); } - public function getStorage() { - // get the unjailed storage, since the trash item is outside the jail - // (the internal path is also unjailed) - $groupFolderStorage = parent::getStorage(); - if ($groupFolderStorage->instanceOfStorage(Jail::class)) { - /** @var Jail $groupFolderStorage */ - return $groupFolderStorage->getUnjailedStorage(); - } - return $groupFolderStorage; - } - - public function getMtime() { + public function getMtime(): int { // trashbin is currently (incorrectly) assuming these to be the same return $this->getDeletedTime(); } diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index e9a57dbdd..72665baa1 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -67,14 +67,16 @@ public function listTrashFolder(ITrashItem $trashItem): array { return []; } $user = $trashItem->getUser(); - $folder = $this->getNodeForTrashItem($user, $trashItem); - if (!$folder instanceof Folder) { + $folderNode = $this->getNodeForTrashItem($user, $trashItem); + if (!$folderNode instanceof Folder) { return []; } - $content = $folder->getDirectoryListing(); + + $content = $folderNode->getDirectoryListing(); $this->aclManagerFactory->getACLManager($user)->preloadRulesForFolder($trashItem->getPath()); - return array_values(array_filter(array_map(function (Node $node) use ($trashItem, $user) { - if (!$this->userHasAccessToPath($user, $trashItem->getPath() . '/' . $node->getName())) { + + return array_values(array_filter(array_map(function (Node $node) use ($trashItem, $user): ?GroupTrashItem { + if (!$this->userHasAccessToPath($user, $this->getUnJailedPath($node))) { return null; } return new GroupTrashItem( @@ -283,10 +285,11 @@ private function userHasAccessToPath( private function getNodeForTrashItem(IUser $user, ITrashItem $trashItem): ?Node { [, $folderId, $path] = explode('/', $trashItem->getTrashPath(), 3); + $folderId = (int)$folderId; $folders = $this->folderManager->getFoldersForUser($user); foreach ($folders as $groupFolder) { - if ($groupFolder['folder_id'] === (int)$folderId) { - $trashRoot = $this->getTrashFolder((int)$folderId); + if ($groupFolder['folder_id'] === $folderId) { + $trashRoot = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId); try { $node = $trashRoot->get($path); if (!$this->userHasAccessToPath($user, $trashItem->getPath())) { @@ -320,6 +323,17 @@ private function getTrashFolder(int $folderId): Folder { } } + private function getUnJailedPath(Node $node): string { + $storage = $node->getStorage(); + $path = $node->getInternalPath(); + while ($storage->instanceOfStorage(Jail::class)) { + /** @var Jail $storage */ + $path = $storage->getUnjailedPath($path); + $storage = $storage->getUnjailedStorage(); + } + return $path; + } + /** * @param list $folders * @return list @@ -341,10 +355,14 @@ private function getTrashForFolders(IUser $user, array $folders): array { $folderId = $folder['folder_id']; $folderHasAcl = $folder['acl']; $mountPoint = $folder['mount_point']; - $trashFolder = $this->getTrashFolder($folderId); + + // ensure the trash folder exists + $this->getTrashFolder($folderId); + + $trashFolder = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId); $content = $trashFolder->getDirectoryListing(); $userCanManageAcl = $this->folderManager->canManageACL($folderId, $user); - $this->aclManagerFactory->getACLManager($user)->preloadRulesForFolder($trashFolder->getPath()); + $this->aclManagerFactory->getACLManager($user)->preloadRulesForFolder($this->getUnJailedPath($trashFolder)); foreach ($content as $item) { /** @var \OC\Files\Node\Node $item */ $pathParts = pathinfo($item->getName()); @@ -360,7 +378,7 @@ private function getTrashForFolders(IUser $user, array $folders): array { if ($originalLocation === '' && !$userCanManageAcl) { continue; } - if (!$this->userHasAccessToPath($user, $item->getPath())) { + if (!$this->userHasAccessToPath($user, $this->getUnJailedPath($item))) { continue; } // if a parent of the original location has also been deleted, we also need to check it based on the now-deleted parent path From 16e4ea9e62b8f5fef66b110dd95261d41a898668 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 18 Nov 2024 14:44:07 +0100 Subject: [PATCH 6/9] fix: fix moving files to trash Signed-off-by: Robin Appelman --- lib/Trash/TrashBackend.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 72665baa1..461f7022d 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -8,6 +8,7 @@ use OC\Files\Storage\Wrapper\Encryption; use OCA\Files_Trashbin\Expiration; +use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trash\ITrashBackend; use OCA\Files_Trashbin\Trash\ITrashItem; use OCA\GroupFolders\ACL\ACLManagerFactory; @@ -208,7 +209,14 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool { $name = basename($internalPath); $fileEntry = $storage->getCache()->get($internalPath); $folderId = $storage->getFolderId(); - $trashFolder = $this->getTrashFolder($folderId); + $user = $this->userSession->getUser(); + if (!$user) { + throw new \Exception("file moved to trash with no user in context"); + } + // ensure the folder exists + $this->getTrashFolder($folderId); + + $trashFolder = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId); $trashStorage = $trashFolder->getStorage(); $time = time(); $trashName = $name . '.d' . $time; @@ -220,9 +228,16 @@ public function moveToTrash(IStorage $storage, string $internalPath): bool { $result = $trashStorage->moveFromStorage($storage, $internalPath, $targetInternalPath); } if ($result) { - $this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId(), $this->userSession->getUser()->getUID()); - if ($trashStorage->getCache()->getId($targetInternalPath) !== $fileEntry->getId()) { + $this->trashManager->addTrashItem($folderId, $name, $time, $internalPath, $fileEntry->getId(), $user->getUID()); + + // some storage backends (object/encryption) can either already move the cache item or cause the target to be scanned + // so we only conditionally do the cache move here + if (!$trashStorage->getCache()->inCache($targetInternalPath)) { + // doesn't exist in target yet, do the move $trashStorage->getCache()->moveFromCache($storage->getCache(), $internalPath, $targetInternalPath); + } elseif ($storage->getCache()->inCache($internalPath)) { + // exists in both source and target, cleanup source + $storage->getCache()->remove($internalPath); } } else { throw new \Exception("Failed to move groupfolder item to trash"); @@ -243,6 +258,11 @@ private function moveFromEncryptedStorage(IStorage $sourceStorage, IStorage $tar return false; } + // the trash should be the top wrapper, remove it to prevent recursive attempts to move to trash + if ($sourceStorage instanceof Storage) { + $sourceStorage = $sourceStorage->getWrapperStorage(); + } + $result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); if ($result) { if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { From aa0d1a24846097890c78ab687f628f6a8099142c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 27 Nov 2024 18:59:39 +0100 Subject: [PATCH 7/9] fix: add fallback behavior for pre-fix trashbin restore Signed-off-by: Robin Appelman --- lib/Trash/TrashBackend.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index 461f7022d..b2ff19344 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -6,7 +6,9 @@ namespace OCA\GroupFolders\Trash; +use OC\Encryption\Exceptions\DecryptionFailedException; use OC\Files\Storage\Wrapper\Encryption; +use OC\Files\Storage\Wrapper\Jail; use OCA\Files_Trashbin\Expiration; use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trash\ITrashBackend; @@ -155,8 +157,19 @@ public function restoreItem(ITrashItem $item) { } $targetLocation = $targetFolder->getInternalPath() . '/' . $originalLocation; - $targetFolder->getStorage()->moveFromStorage($trashStorage, $node->getInternalPath(), $targetLocation); - $targetFolder->getStorage()->getUpdater()->renameFromStorage($trashStorage, $node->getInternalPath(), $targetLocation); + $targetStorage = $targetFolder->getStorage(); + $trashLocation = $node->getInternalPath(); + try { + $targetStorage->moveFromStorage($trashStorage, $trashLocation, $targetLocation); + $targetStorage->getUpdater()->renameFromStorage($trashStorage, $trashLocation, $targetLocation); + } catch (DecryptionFailedException $e) { + // Before https://github.com/nextcloud/groupfolders/pull/3425 the key would be in the wrong place, leading to the decryption failure. + // for those we fall back to the old restore behavior + [$unwrappedTargetStorage, $unwrappedTargetLocation] = $this->unwrapJails($targetStorage, $targetLocation); + [$unwrappedTrashStorage, $unwrappedTrashLocation] = $this->unwrapJails($trashStorage, $trashLocation); + $unwrappedTargetStorage->moveFromStorage($unwrappedTrashStorage, $unwrappedTrashLocation, $unwrappedTargetLocation); + $unwrappedTargetStorage->getUpdater()->renameFromStorage($unwrappedTrashStorage, $unwrappedTrashLocation, $unwrappedTargetLocation); + } $this->trashManager->removeItem((int)$folderId, $item->getName(), $item->getDeletedTime()); \OCP\Util::emitHook( '\OCA\Files_Trashbin\Trashbin', @@ -168,6 +181,18 @@ public function restoreItem(ITrashItem $item) { ); } + private function unwrapJails(IStorage $storage, string $internalPath): array { + $unJailedInternalPath = $internalPath; + $unJailedStorage = $storage; + while ($unJailedStorage->instanceOfStorage(Jail::class)) { + $unJailedStorage = $unJailedStorage->getWrapperStorage(); + if ($unJailedStorage instanceof Jail) { + $unJailedInternalPath = $unJailedStorage->getUnjailedPath($unJailedInternalPath); + } + } + return [$unJailedStorage, $unJailedInternalPath]; + } + /** * @return void * @throw \LogicException From ba847e57c46d83701dac5ace90acb199eb633068 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 11 Dec 2024 18:08:51 +0100 Subject: [PATCH 8/9] chore: formatting Signed-off-by: Robin Appelman --- lib/Mount/GroupFolderStorage.php | 3 ++- lib/Trash/GroupTrashItem.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Mount/GroupFolderStorage.php b/lib/Mount/GroupFolderStorage.php index 3230891bc..4e59c88e2 100644 --- a/lib/Mount/GroupFolderStorage.php +++ b/lib/Mount/GroupFolderStorage.php @@ -55,7 +55,8 @@ public function getCache($path = '', $storage = null) { $cache = parent::getCache($path, $storage); if ($this->rootEntry !== null) { - $cache = new RootEntryCache($cache, $this->rootEntry);} + $cache = new RootEntryCache($cache, $this->rootEntry); + } $this->cache = $cache; return $this->cache; diff --git a/lib/Trash/GroupTrashItem.php b/lib/Trash/GroupTrashItem.php index a10f8d5a2..2ccdbcb76 100644 --- a/lib/Trash/GroupTrashItem.php +++ b/lib/Trash/GroupTrashItem.php @@ -6,7 +6,6 @@ namespace OCA\GroupFolders\Trash; -use OC\Files\Storage\Wrapper\Jail; use OCA\Files_Trashbin\Trash\ITrashBackend; use OCA\Files_Trashbin\Trash\TrashItem; use OCP\Files\FileInfo; From 61a181889729816fafe1a75f8a863fdb3fb6dc98 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 28 Nov 2024 15:05:06 +0100 Subject: [PATCH 9/9] chore: psalm fixes Signed-off-by: Robin Appelman --- lib/Mount/GroupFolderStorage.php | 5 +++-- lib/Mount/MountProvider.php | 2 +- lib/Trash/TrashBackend.php | 10 ++++++++-- tests/stub.phpstub | 15 ++++++++++++--- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/Mount/GroupFolderStorage.php b/lib/Mount/GroupFolderStorage.php index 4e59c88e2..94daba55e 100644 --- a/lib/Mount/GroupFolderStorage.php +++ b/lib/Mount/GroupFolderStorage.php @@ -10,6 +10,7 @@ use OC\Files\ObjectStore\ObjectStoreScanner; use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\Storage\Wrapper\Quota; +use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; use OCP\IUser; use OCP\IUserSession; @@ -18,8 +19,8 @@ class GroupFolderStorage extends Quota { private int $folderId; private ?ICacheEntry $rootEntry; private IUserSession $userSession; - private ?IUser $mountOwner = null; - /** @var RootEntryCache|null */ + private ?IUser $mountOwner; + /** @var ICache|null */ public $cache = null; public function __construct($parameters) { diff --git a/lib/Mount/MountProvider.php b/lib/Mount/MountProvider.php index 7461865f7..edada0aa9 100644 --- a/lib/Mount/MountProvider.php +++ b/lib/Mount/MountProvider.php @@ -269,7 +269,7 @@ public function getTrashMount( $storage = $this->getRootFolder()->getStorage(); - $storage->setOwner($user?->getUID()); + $storage->setOwner($user->getUID()); $trashPath = $this->getRootFolder()->getInternalPath() . '/trash/' . $id; diff --git a/lib/Trash/TrashBackend.php b/lib/Trash/TrashBackend.php index b2ff19344..7cfd19432 100644 --- a/lib/Trash/TrashBackend.php +++ b/lib/Trash/TrashBackend.php @@ -7,6 +7,7 @@ namespace OCA\GroupFolders\Trash; use OC\Encryption\Exceptions\DecryptionFailedException; +use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\Storage\Wrapper\Encryption; use OC\Files\Storage\Wrapper\Jail; use OCA\Files_Trashbin\Expiration; @@ -288,9 +289,11 @@ private function moveFromEncryptedStorage(IStorage $sourceStorage, IStorage $tar $sourceStorage = $sourceStorage->getWrapperStorage(); } + /** @psalm-suppress TooManyArguments */ $result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true); if ($result) { - if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + // hacky workaround to make sure we don't rely on a newer minor version + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class) && is_callable([$sourceStorage, 'setPreserveCacheOnDelete'])) { /** @var ObjectStoreStorage $sourceStorage */ $sourceStorage->setPreserveCacheOnDelete(true); } @@ -301,7 +304,7 @@ private function moveFromEncryptedStorage(IStorage $sourceStorage, IStorage $tar $result = $sourceStorage->unlink($sourceInternalPath); } } finally { - if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) { + if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class) && is_callable([$sourceStorage, 'setPreserveCacheOnDelete'])) { /** @var ObjectStoreStorage $sourceStorage */ $sourceStorage->setPreserveCacheOnDelete(false); } @@ -334,6 +337,7 @@ private function getNodeForTrashItem(IUser $user, ITrashItem $trashItem): ?Node $folders = $this->folderManager->getFoldersForUser($user); foreach ($folders as $groupFolder) { if ($groupFolder['folder_id'] === $folderId) { + /** @var Folder $trashRoot */ $trashRoot = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId); try { $node = $trashRoot->get($path); @@ -404,6 +408,8 @@ private function getTrashForFolders(IUser $user, array $folders): array { // ensure the trash folder exists $this->getTrashFolder($folderId); + + /** @var Folder $trashFolder */ $trashFolder = $this->rootFolder->get('/' . $user->getUID() . '/files_trashbin/groupfolders/' . $folderId); $content = $trashFolder->getDirectoryListing(); $userCanManageAcl = $this->folderManager->canManageACL($folderId, $user); diff --git a/tests/stub.phpstub b/tests/stub.phpstub index 75d09a397..5075330bf 100644 --- a/tests/stub.phpstub +++ b/tests/stub.phpstub @@ -359,6 +359,9 @@ namespace OCA\Files_Trashbin { { } } + + class Storage extends \OC\Files\Storage\Wrapper\Wrapper { + } } namespace OCA\Files_Versions\Versions { @@ -1560,7 +1563,7 @@ namespace OC\Files\Storage\Wrapper{ * @param string $targetInternalPath * @return bool */ - public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) + public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { } /** @@ -2010,7 +2013,7 @@ namespace OC\Files\Storage\Wrapper{ * @param string $targetInternalPath * @return bool */ - public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) + public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { } /** @@ -2125,7 +2128,7 @@ namespace OC\Files\Storage\Wrapper{ * @param string $targetInternalPath * @return bool */ - public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) + public function copyFromStorage(\OCP\Files\Storage\IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { } /** @@ -2228,6 +2231,8 @@ namespace OC\Files\Storage\Wrapper{ { } } + class Encryption extends \OC\Files\Storage\Wrapper\Wrapper { + } } namespace OC\Files\ObjectStore { @@ -2323,3 +2328,7 @@ namespace OCA\DAV\Connector\Sabre\Exception { public function serialize(\Sabre\DAV\Server $server, \DOMElement $errorNode) {} } } + +namespace OC\Encryption\Exceptions { + class DecryptionFailedException extends \Exception {} +}