@@ -54,269 +54,269 @@ |
||
| 54 | 54 | |
| 55 | 55 | class OwnershipTransferService { |
| 56 | 56 | |
| 57 | - /** @var IEncryptionManager */ |
|
| 58 | - private $encryptionManager; |
|
| 59 | - |
|
| 60 | - /** @var IShareManager */ |
|
| 61 | - private $shareManager; |
|
| 62 | - |
|
| 63 | - /** @var IMountManager */ |
|
| 64 | - private $mountManager; |
|
| 65 | - |
|
| 66 | - public function __construct(IEncryptionManager $manager, |
|
| 67 | - IShareManager $shareManager, |
|
| 68 | - IMountManager $mountManager) { |
|
| 69 | - $this->encryptionManager = $manager; |
|
| 70 | - $this->shareManager = $shareManager; |
|
| 71 | - $this->mountManager = $mountManager; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param IUser $sourceUser |
|
| 76 | - * @param IUser $destinationUser |
|
| 77 | - * @param string $path |
|
| 78 | - * |
|
| 79 | - * @param OutputInterface|null $output |
|
| 80 | - * @param bool $move |
|
| 81 | - * @throws TransferOwnershipException |
|
| 82 | - * @throws \OC\User\NoUserException |
|
| 83 | - */ |
|
| 84 | - public function transfer(IUser $sourceUser, |
|
| 85 | - IUser $destinationUser, |
|
| 86 | - string $path, |
|
| 87 | - ?OutputInterface $output = null, |
|
| 88 | - bool $move = false, |
|
| 89 | - bool $firstLogin = false): void { |
|
| 90 | - $output = $output ?? new NullOutput(); |
|
| 91 | - $sourceUid = $sourceUser->getUID(); |
|
| 92 | - $destinationUid = $destinationUser->getUID(); |
|
| 93 | - $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 94 | - |
|
| 95 | - // target user has to be ready |
|
| 96 | - if ($destinationUser->getLastLogin() === 0 || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
|
| 97 | - throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // setup filesystem |
|
| 101 | - Filesystem::initMountPoints($sourceUid); |
|
| 102 | - Filesystem::initMountPoints($destinationUid); |
|
| 103 | - |
|
| 104 | - $view = new View(); |
|
| 105 | - |
|
| 106 | - if ($move) { |
|
| 107 | - $finalTarget = "$destinationUid/files/"; |
|
| 108 | - } else { |
|
| 109 | - $date = date('Y-m-d H-i-s'); |
|
| 110 | - |
|
| 111 | - // Remove some characters which are prone to cause errors |
|
| 112 | - $cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName()); |
|
| 113 | - // Replace multiple dashes with one dash |
|
| 114 | - $cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName); |
|
| 115 | - $cleanUserName = $cleanUserName ?: $sourceUid; |
|
| 116 | - |
|
| 117 | - $finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date"; |
|
| 118 | - try { |
|
| 119 | - $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
|
| 120 | - } catch (InvalidPathException $e) { |
|
| 121 | - $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { |
|
| 126 | - throw new TransferOwnershipException("Unknown path provided: $path", 1); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - if ($move && ( |
|
| 130 | - !$view->is_dir($finalTarget) || ( |
|
| 131 | - !$firstLogin && |
|
| 132 | - count($view->getDirectoryContent($finalTarget)) > 0 |
|
| 133 | - ) |
|
| 134 | - ) |
|
| 135 | - ) { |
|
| 136 | - throw new TransferOwnershipException("Destination path does not exists or is not empty", 1); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - // analyse source folder |
|
| 141 | - $this->analyse( |
|
| 142 | - $sourceUid, |
|
| 143 | - $destinationUid, |
|
| 144 | - $sourcePath, |
|
| 145 | - $view, |
|
| 146 | - $output |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - // collect all the shares |
|
| 150 | - $shares = $this->collectUsersShares( |
|
| 151 | - $sourceUid, |
|
| 152 | - $output |
|
| 153 | - ); |
|
| 154 | - |
|
| 155 | - // transfer the files |
|
| 156 | - $this->transferFiles( |
|
| 157 | - $sourceUid, |
|
| 158 | - $sourcePath, |
|
| 159 | - $finalTarget, |
|
| 160 | - $view, |
|
| 161 | - $output |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - // restore the shares |
|
| 165 | - $this->restoreShares( |
|
| 166 | - $sourceUid, |
|
| 167 | - $destinationUid, |
|
| 168 | - $shares, |
|
| 169 | - $output |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - private function walkFiles(View $view, $path, Closure $callBack) { |
|
| 174 | - foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 175 | - if (!$callBack($fileInfo)) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 179 | - $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @param OutputInterface $output |
|
| 186 | - * |
|
| 187 | - * @throws \Exception |
|
| 188 | - */ |
|
| 189 | - protected function analyse(string $sourceUid, |
|
| 190 | - string $destinationUid, |
|
| 191 | - string $sourcePath, |
|
| 192 | - View $view, |
|
| 193 | - OutputInterface $output): void { |
|
| 194 | - $output->writeln('Validating quota'); |
|
| 195 | - $size = $view->getFileInfo($sourcePath, false)->getSize(false); |
|
| 196 | - $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 197 | - if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
|
| 198 | - $output->writeln('<error>Target user does not have enough free space available.</error>'); |
|
| 199 | - throw new \Exception('Execution terminated.'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $output->writeln("Analysing files of $sourceUid ..."); |
|
| 203 | - $progress = new ProgressBar($output); |
|
| 204 | - $progress->start(); |
|
| 205 | - |
|
| 206 | - $encryptedFiles = []; |
|
| 207 | - $this->walkFiles($view, $sourcePath, |
|
| 208 | - function (FileInfo $fileInfo) use ($progress) { |
|
| 209 | - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 210 | - // only analyze into folders from main storage, |
|
| 211 | - if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - return true; |
|
| 215 | - } |
|
| 216 | - $progress->advance(); |
|
| 217 | - if ($fileInfo->isEncrypted()) { |
|
| 218 | - $encryptedFiles[] = $fileInfo; |
|
| 219 | - } |
|
| 220 | - return true; |
|
| 221 | - }); |
|
| 222 | - $progress->finish(); |
|
| 223 | - $output->writeln(''); |
|
| 224 | - |
|
| 225 | - // no file is allowed to be encrypted |
|
| 226 | - if (!empty($encryptedFiles)) { |
|
| 227 | - $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>"); |
|
| 228 | - foreach ($encryptedFiles as $encryptedFile) { |
|
| 229 | - /** @var FileInfo $encryptedFile */ |
|
| 230 | - $output->writeln(" " . $encryptedFile->getPath()); |
|
| 231 | - } |
|
| 232 | - throw new \Exception('Execution terminated.'); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - private function collectUsersShares(string $sourceUid, |
|
| 237 | - OutputInterface $output): array { |
|
| 238 | - $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); |
|
| 239 | - |
|
| 240 | - $shares = []; |
|
| 241 | - $progress = new ProgressBar($output); |
|
| 242 | - foreach ([IShare::TYPE_GROUP, IShare::TYPE_USER, IShare::TYPE_LINK, IShare::TYPE_REMOTE, IShare::TYPE_ROOM] as $shareType) { |
|
| 243 | - $offset = 0; |
|
| 244 | - while (true) { |
|
| 245 | - $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset); |
|
| 246 | - $progress->advance(count($sharePage)); |
|
| 247 | - if (empty($sharePage)) { |
|
| 248 | - break; |
|
| 249 | - } |
|
| 250 | - $shares = array_merge($shares, $sharePage); |
|
| 251 | - $offset += 50; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $progress->finish(); |
|
| 256 | - $output->writeln(''); |
|
| 257 | - return $shares; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @throws TransferOwnershipException |
|
| 262 | - */ |
|
| 263 | - protected function transferFiles(string $sourceUid, |
|
| 264 | - string $sourcePath, |
|
| 265 | - string $finalTarget, |
|
| 266 | - View $view, |
|
| 267 | - OutputInterface $output): void { |
|
| 268 | - $output->writeln("Transferring files to $finalTarget ..."); |
|
| 269 | - |
|
| 270 | - // This change will help user to transfer the folder specified using --path option. |
|
| 271 | - // Else only the content inside folder is transferred which is not correct. |
|
| 272 | - if ($sourcePath !== "$sourceUid/files") { |
|
| 273 | - $view->mkdir($finalTarget); |
|
| 274 | - $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 275 | - } |
|
| 276 | - if ($view->rename($sourcePath, $finalTarget) === false) { |
|
| 277 | - throw new TransferOwnershipException("Could not transfer files.", 1); |
|
| 278 | - } |
|
| 279 | - if (!is_dir("$sourceUid/files")) { |
|
| 280 | - // because the files folder is moved away we need to recreate it |
|
| 281 | - $view->mkdir("$sourceUid/files"); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - private function restoreShares(string $sourceUid, |
|
| 286 | - string $destinationUid, |
|
| 287 | - array $shares, |
|
| 288 | - OutputInterface $output) { |
|
| 289 | - $output->writeln("Restoring shares ..."); |
|
| 290 | - $progress = new ProgressBar($output, count($shares)); |
|
| 291 | - |
|
| 292 | - foreach ($shares as $share) { |
|
| 293 | - try { |
|
| 294 | - if ($share->getShareType() === IShare::TYPE_USER && |
|
| 295 | - $share->getSharedWith() === $destinationUid) { |
|
| 296 | - // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 297 | - $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 298 | - if ($shareMountPoint) { |
|
| 299 | - $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 300 | - } |
|
| 301 | - $this->shareManager->deleteShare($share); |
|
| 302 | - } else { |
|
| 303 | - if ($share->getShareOwner() === $sourceUid) { |
|
| 304 | - $share->setShareOwner($destinationUid); |
|
| 305 | - } |
|
| 306 | - if ($share->getSharedBy() === $sourceUid) { |
|
| 307 | - $share->setSharedBy($destinationUid); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $this->shareManager->updateShare($share); |
|
| 311 | - } |
|
| 312 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 313 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 314 | - } catch (\Throwable $e) { |
|
| 315 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 316 | - } |
|
| 317 | - $progress->advance(); |
|
| 318 | - } |
|
| 319 | - $progress->finish(); |
|
| 320 | - $output->writeln(''); |
|
| 321 | - } |
|
| 57 | + /** @var IEncryptionManager */ |
|
| 58 | + private $encryptionManager; |
|
| 59 | + |
|
| 60 | + /** @var IShareManager */ |
|
| 61 | + private $shareManager; |
|
| 62 | + |
|
| 63 | + /** @var IMountManager */ |
|
| 64 | + private $mountManager; |
|
| 65 | + |
|
| 66 | + public function __construct(IEncryptionManager $manager, |
|
| 67 | + IShareManager $shareManager, |
|
| 68 | + IMountManager $mountManager) { |
|
| 69 | + $this->encryptionManager = $manager; |
|
| 70 | + $this->shareManager = $shareManager; |
|
| 71 | + $this->mountManager = $mountManager; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param IUser $sourceUser |
|
| 76 | + * @param IUser $destinationUser |
|
| 77 | + * @param string $path |
|
| 78 | + * |
|
| 79 | + * @param OutputInterface|null $output |
|
| 80 | + * @param bool $move |
|
| 81 | + * @throws TransferOwnershipException |
|
| 82 | + * @throws \OC\User\NoUserException |
|
| 83 | + */ |
|
| 84 | + public function transfer(IUser $sourceUser, |
|
| 85 | + IUser $destinationUser, |
|
| 86 | + string $path, |
|
| 87 | + ?OutputInterface $output = null, |
|
| 88 | + bool $move = false, |
|
| 89 | + bool $firstLogin = false): void { |
|
| 90 | + $output = $output ?? new NullOutput(); |
|
| 91 | + $sourceUid = $sourceUser->getUID(); |
|
| 92 | + $destinationUid = $destinationUser->getUID(); |
|
| 93 | + $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 94 | + |
|
| 95 | + // target user has to be ready |
|
| 96 | + if ($destinationUser->getLastLogin() === 0 || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
|
| 97 | + throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // setup filesystem |
|
| 101 | + Filesystem::initMountPoints($sourceUid); |
|
| 102 | + Filesystem::initMountPoints($destinationUid); |
|
| 103 | + |
|
| 104 | + $view = new View(); |
|
| 105 | + |
|
| 106 | + if ($move) { |
|
| 107 | + $finalTarget = "$destinationUid/files/"; |
|
| 108 | + } else { |
|
| 109 | + $date = date('Y-m-d H-i-s'); |
|
| 110 | + |
|
| 111 | + // Remove some characters which are prone to cause errors |
|
| 112 | + $cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName()); |
|
| 113 | + // Replace multiple dashes with one dash |
|
| 114 | + $cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName); |
|
| 115 | + $cleanUserName = $cleanUserName ?: $sourceUid; |
|
| 116 | + |
|
| 117 | + $finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date"; |
|
| 118 | + try { |
|
| 119 | + $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
|
| 120 | + } catch (InvalidPathException $e) { |
|
| 121 | + $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { |
|
| 126 | + throw new TransferOwnershipException("Unknown path provided: $path", 1); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + if ($move && ( |
|
| 130 | + !$view->is_dir($finalTarget) || ( |
|
| 131 | + !$firstLogin && |
|
| 132 | + count($view->getDirectoryContent($finalTarget)) > 0 |
|
| 133 | + ) |
|
| 134 | + ) |
|
| 135 | + ) { |
|
| 136 | + throw new TransferOwnershipException("Destination path does not exists or is not empty", 1); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + // analyse source folder |
|
| 141 | + $this->analyse( |
|
| 142 | + $sourceUid, |
|
| 143 | + $destinationUid, |
|
| 144 | + $sourcePath, |
|
| 145 | + $view, |
|
| 146 | + $output |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + // collect all the shares |
|
| 150 | + $shares = $this->collectUsersShares( |
|
| 151 | + $sourceUid, |
|
| 152 | + $output |
|
| 153 | + ); |
|
| 154 | + |
|
| 155 | + // transfer the files |
|
| 156 | + $this->transferFiles( |
|
| 157 | + $sourceUid, |
|
| 158 | + $sourcePath, |
|
| 159 | + $finalTarget, |
|
| 160 | + $view, |
|
| 161 | + $output |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + // restore the shares |
|
| 165 | + $this->restoreShares( |
|
| 166 | + $sourceUid, |
|
| 167 | + $destinationUid, |
|
| 168 | + $shares, |
|
| 169 | + $output |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + private function walkFiles(View $view, $path, Closure $callBack) { |
|
| 174 | + foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
| 175 | + if (!$callBack($fileInfo)) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 179 | + $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @param OutputInterface $output |
|
| 186 | + * |
|
| 187 | + * @throws \Exception |
|
| 188 | + */ |
|
| 189 | + protected function analyse(string $sourceUid, |
|
| 190 | + string $destinationUid, |
|
| 191 | + string $sourcePath, |
|
| 192 | + View $view, |
|
| 193 | + OutputInterface $output): void { |
|
| 194 | + $output->writeln('Validating quota'); |
|
| 195 | + $size = $view->getFileInfo($sourcePath, false)->getSize(false); |
|
| 196 | + $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 197 | + if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
|
| 198 | + $output->writeln('<error>Target user does not have enough free space available.</error>'); |
|
| 199 | + throw new \Exception('Execution terminated.'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $output->writeln("Analysing files of $sourceUid ..."); |
|
| 203 | + $progress = new ProgressBar($output); |
|
| 204 | + $progress->start(); |
|
| 205 | + |
|
| 206 | + $encryptedFiles = []; |
|
| 207 | + $this->walkFiles($view, $sourcePath, |
|
| 208 | + function (FileInfo $fileInfo) use ($progress) { |
|
| 209 | + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
| 210 | + // only analyze into folders from main storage, |
|
| 211 | + if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + return true; |
|
| 215 | + } |
|
| 216 | + $progress->advance(); |
|
| 217 | + if ($fileInfo->isEncrypted()) { |
|
| 218 | + $encryptedFiles[] = $fileInfo; |
|
| 219 | + } |
|
| 220 | + return true; |
|
| 221 | + }); |
|
| 222 | + $progress->finish(); |
|
| 223 | + $output->writeln(''); |
|
| 224 | + |
|
| 225 | + // no file is allowed to be encrypted |
|
| 226 | + if (!empty($encryptedFiles)) { |
|
| 227 | + $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>"); |
|
| 228 | + foreach ($encryptedFiles as $encryptedFile) { |
|
| 229 | + /** @var FileInfo $encryptedFile */ |
|
| 230 | + $output->writeln(" " . $encryptedFile->getPath()); |
|
| 231 | + } |
|
| 232 | + throw new \Exception('Execution terminated.'); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + private function collectUsersShares(string $sourceUid, |
|
| 237 | + OutputInterface $output): array { |
|
| 238 | + $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); |
|
| 239 | + |
|
| 240 | + $shares = []; |
|
| 241 | + $progress = new ProgressBar($output); |
|
| 242 | + foreach ([IShare::TYPE_GROUP, IShare::TYPE_USER, IShare::TYPE_LINK, IShare::TYPE_REMOTE, IShare::TYPE_ROOM] as $shareType) { |
|
| 243 | + $offset = 0; |
|
| 244 | + while (true) { |
|
| 245 | + $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset); |
|
| 246 | + $progress->advance(count($sharePage)); |
|
| 247 | + if (empty($sharePage)) { |
|
| 248 | + break; |
|
| 249 | + } |
|
| 250 | + $shares = array_merge($shares, $sharePage); |
|
| 251 | + $offset += 50; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $progress->finish(); |
|
| 256 | + $output->writeln(''); |
|
| 257 | + return $shares; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @throws TransferOwnershipException |
|
| 262 | + */ |
|
| 263 | + protected function transferFiles(string $sourceUid, |
|
| 264 | + string $sourcePath, |
|
| 265 | + string $finalTarget, |
|
| 266 | + View $view, |
|
| 267 | + OutputInterface $output): void { |
|
| 268 | + $output->writeln("Transferring files to $finalTarget ..."); |
|
| 269 | + |
|
| 270 | + // This change will help user to transfer the folder specified using --path option. |
|
| 271 | + // Else only the content inside folder is transferred which is not correct. |
|
| 272 | + if ($sourcePath !== "$sourceUid/files") { |
|
| 273 | + $view->mkdir($finalTarget); |
|
| 274 | + $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 275 | + } |
|
| 276 | + if ($view->rename($sourcePath, $finalTarget) === false) { |
|
| 277 | + throw new TransferOwnershipException("Could not transfer files.", 1); |
|
| 278 | + } |
|
| 279 | + if (!is_dir("$sourceUid/files")) { |
|
| 280 | + // because the files folder is moved away we need to recreate it |
|
| 281 | + $view->mkdir("$sourceUid/files"); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + private function restoreShares(string $sourceUid, |
|
| 286 | + string $destinationUid, |
|
| 287 | + array $shares, |
|
| 288 | + OutputInterface $output) { |
|
| 289 | + $output->writeln("Restoring shares ..."); |
|
| 290 | + $progress = new ProgressBar($output, count($shares)); |
|
| 291 | + |
|
| 292 | + foreach ($shares as $share) { |
|
| 293 | + try { |
|
| 294 | + if ($share->getShareType() === IShare::TYPE_USER && |
|
| 295 | + $share->getSharedWith() === $destinationUid) { |
|
| 296 | + // Unmount the shares before deleting, so we don't try to get the storage later on. |
|
| 297 | + $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 298 | + if ($shareMountPoint) { |
|
| 299 | + $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
| 300 | + } |
|
| 301 | + $this->shareManager->deleteShare($share); |
|
| 302 | + } else { |
|
| 303 | + if ($share->getShareOwner() === $sourceUid) { |
|
| 304 | + $share->setShareOwner($destinationUid); |
|
| 305 | + } |
|
| 306 | + if ($share->getSharedBy() === $sourceUid) { |
|
| 307 | + $share->setSharedBy($destinationUid); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $this->shareManager->updateShare($share); |
|
| 311 | + } |
|
| 312 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 313 | + $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 314 | + } catch (\Throwable $e) { |
|
| 315 | + $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 316 | + } |
|
| 317 | + $progress->advance(); |
|
| 318 | + } |
|
| 319 | + $progress->finish(); |
|
| 320 | + $output->writeln(''); |
|
| 321 | + } |
|
| 322 | 322 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $output = $output ?? new NullOutput(); |
| 91 | 91 | $sourceUid = $sourceUser->getUID(); |
| 92 | 92 | $destinationUid = $destinationUser->getUID(); |
| 93 | - $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
|
| 93 | + $sourcePath = rtrim($sourceUid.'/files/'.$path, '/'); |
|
| 94 | 94 | |
| 95 | 95 | // target user has to be ready |
| 96 | 96 | if ($destinationUser->getLastLogin() === 0 || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | OutputInterface $output): void { |
| 194 | 194 | $output->writeln('Validating quota'); |
| 195 | 195 | $size = $view->getFileInfo($sourcePath, false)->getSize(false); |
| 196 | - $freeSpace = $view->free_space($destinationUid . '/files/'); |
|
| 196 | + $freeSpace = $view->free_space($destinationUid.'/files/'); |
|
| 197 | 197 | if ($size > $freeSpace && $freeSpace !== FileInfo::SPACE_UNKNOWN) { |
| 198 | 198 | $output->writeln('<error>Target user does not have enough free space available.</error>'); |
| 199 | 199 | throw new \Exception('Execution terminated.'); |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | |
| 206 | 206 | $encryptedFiles = []; |
| 207 | 207 | $this->walkFiles($view, $sourcePath, |
| 208 | - function (FileInfo $fileInfo) use ($progress) { |
|
| 208 | + function(FileInfo $fileInfo) use ($progress) { |
|
| 209 | 209 | if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
| 210 | 210 | // only analyze into folders from main storage, |
| 211 | 211 | if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>"); |
| 228 | 228 | foreach ($encryptedFiles as $encryptedFile) { |
| 229 | 229 | /** @var FileInfo $encryptedFile */ |
| 230 | - $output->writeln(" " . $encryptedFile->getPath()); |
|
| 230 | + $output->writeln(" ".$encryptedFile->getPath()); |
|
| 231 | 231 | } |
| 232 | 232 | throw new \Exception('Execution terminated.'); |
| 233 | 233 | } |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | // Else only the content inside folder is transferred which is not correct. |
| 272 | 272 | if ($sourcePath !== "$sourceUid/files") { |
| 273 | 273 | $view->mkdir($finalTarget); |
| 274 | - $finalTarget = $finalTarget . '/' . basename($sourcePath); |
|
| 274 | + $finalTarget = $finalTarget.'/'.basename($sourcePath); |
|
| 275 | 275 | } |
| 276 | 276 | if ($view->rename($sourcePath, $finalTarget) === false) { |
| 277 | 277 | throw new TransferOwnershipException("Could not transfer files.", 1); |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | if ($share->getShareType() === IShare::TYPE_USER && |
| 295 | 295 | $share->getSharedWith() === $destinationUid) { |
| 296 | 296 | // Unmount the shares before deleting, so we don't try to get the storage later on. |
| 297 | - $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); |
|
| 297 | + $shareMountPoint = $this->mountManager->find('/'.$destinationUid.'/files'.$share->getTarget()); |
|
| 298 | 298 | if ($shareMountPoint) { |
| 299 | 299 | $this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
| 300 | 300 | } |
@@ -310,9 +310,9 @@ discard block |
||
| 310 | 310 | $this->shareManager->updateShare($share); |
| 311 | 311 | } |
| 312 | 312 | } catch (\OCP\Files\NotFoundException $e) { |
| 313 | - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
| 313 | + $output->writeln('<error>Share with id '.$share->getId().' points at deleted file, skipping</error>'); |
|
| 314 | 314 | } catch (\Throwable $e) { |
| 315 | - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
| 315 | + $output->writeln('<error>Could not restore share with id '.$share->getId().':'.$e->getTraceAsString().'</error>'); |
|
| 316 | 316 | } |
| 317 | 317 | $progress->advance(); |
| 318 | 318 | } |