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