diff --git a/lib/ExportDestination.php b/lib/ExportDestination.php index 8a0774ce..051b5297 100644 --- a/lib/ExportDestination.php +++ b/lib/ExportDestination.php @@ -56,26 +56,24 @@ public function __construct($r) { /** * {@inheritDoc} */ - public function addFileContents(string $path, string $content): bool { + public function addFileContents(string $path, string $content): void { $stream = fopen('php://temp', 'r+'); fwrite($stream, $content); rewind($stream); $this->streamer->addFileFromStream($stream, $path); - return true; } /** * {@inheritDoc} */ - public function addFileAsStream(string $path, $stream): bool { + public function addFileAsStream(string $path, $stream): void { $this->streamer->addFileFromStream($stream, $path); - return true; } /** * {@inheritDoc} */ - public function copyFolder(Folder $folder, string $destinationPath): bool { + public function copyFolder(Folder $folder, string $destinationPath): void { $this->streamer->addEmptyDir($destinationPath, [ 'timestamp' => $folder->getMTime(), ]); @@ -92,22 +90,19 @@ public function copyFolder(Folder $folder, string $destinationPath): bool { 'timestamp' => $node->getMTime(), ]); } elseif ($node instanceof Folder) { - $success = $this->copyFolder($node, $destinationPath.'/'.$node->getName()); - if ($success === false) { - return false; - } + $this->copyFolder($node, $destinationPath.'/'.$node->getName()); } else { - return false; + // ignore unknown node type, shouldn't happen + continue; } } - return true; } /** * {@inheritDoc} */ - public function setMigratorVersions(array $versions): bool { - return $this->addFileContents("migrator_versions.json", json_encode($versions)); + public function setMigratorVersions(array $versions): void { + $this->addFileContents("migrator_versions.json", json_encode($versions)); } /** diff --git a/lib/ImportSource.php b/lib/ImportSource.php index 0fb44550..5b047744 100644 --- a/lib/ImportSource.php +++ b/lib/ImportSource.php @@ -90,7 +90,7 @@ public function pathExists(string $path): bool { /** * {@inheritDoc} */ - public function copyToFolder(Folder $destination, string $sourcePath): bool { + public function copyToFolder(Folder $destination, string $sourcePath): void { // TODO log errors to ease debugging $sourcePath = rtrim($sourcePath, '/').'/'; $files = $this->archive->getFolder($sourcePath); @@ -99,9 +99,7 @@ public function copyToFolder(Folder $destination, string $sourcePath): bool { foreach ($files as $path) { $stat = $this->archive->getStat($sourcePath . $path); if ($stat === null) { - // TODO: use exception - echo "Stat information not found for " . $sourcePath . $path . "\n"; - return false; + throw new UserMigrationException("Failed to get stat information from archive for " . $sourcePath . $path . "\""); } if (str_ends_with($path, '/')) { try { @@ -113,16 +111,12 @@ public function copyToFolder(Folder $destination, string $sourcePath): bool { } catch (NotFoundException $e) { $folder = $destination->newFolder($path); } - if ($this->copyToFolder($folder, $sourcePath.$path) === false) { - // TODO: use exception - echo "copy to $sourcePath.$path failed\n"; - return false; - } + $this->copyToFolder($folder, $sourcePath.$path); $folder->touch($stat['mtime']); } else { $stream = $this->archive->getStream($sourcePath.$path, 'r'); if ($stream === false) { - return false; + throw new UserMigrationException("Failed to get " . $sourcePath . $path . " from archive\""); } try { $file = $destination->get($path); @@ -139,9 +133,8 @@ public function copyToFolder(Folder $destination, string $sourcePath): bool { } } } catch (NotPermittedException $e) { - return false; + throw new UserMigrationException("Could not import files due to permission issue", 0, $e); } - return true; } /** diff --git a/lib/Migrator/FilesMigrator.php b/lib/Migrator/FilesMigrator.php index 61df862c..1044fccc 100644 --- a/lib/Migrator/FilesMigrator.php +++ b/lib/Migrator/FilesMigrator.php @@ -99,15 +99,19 @@ public function export( $uid = $user->getUID(); $userFolder = $this->root->getUserFolder($uid); - if ($exportDestination->copyFolder($userFolder, static::PATH_FILES) === false) { - throw new UserMigrationException("Could not export files."); + try { + $exportDestination->copyFolder($userFolder, static::PATH_FILES); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export files.", 0, $e); } try { $versionsFolder = $this->root->get('/'.$uid.'/'.FilesVersionsStorage::VERSIONS_ROOT); $output->writeln("Exporting file versions…"); - if ($exportDestination->copyFolder($versionsFolder, static::PATH_VERSIONS) === false) { - throw new UserMigrationException("Could not export files versions."); + try { + $exportDestination->copyFolder($versionsFolder, static::PATH_VERSIONS); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export files versions.", 0, $e); } } catch (NotFoundException $e) { $output->writeln("No file versions to export…"); @@ -121,8 +125,10 @@ public function export( $tagger = $this->tagManager->load(Application::APP_ID, [], false, $uid); $tags = $tagger->getTagsForObjects(array_values($objectIds)); $taggedFiles = array_filter(array_map(fn ($id) => $tags[$id] ?? [], $objectIds)); - if ($exportDestination->addFileContents(static::PATH_TAGS, json_encode($taggedFiles)) === false) { - throw new UserMigrationException("Could not export tagged files information."); + try { + $exportDestination->addFileContents(static::PATH_TAGS, json_encode($taggedFiles)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export tagged files information.", 0, $e); } $output->writeln("Exporting file systemtags…"); @@ -136,8 +142,10 @@ public function export( $systemTags ); $systemTaggedFiles = array_filter(array_map(fn ($id) => $systemTags[$id] ?? [], $objectIds)); - if ($exportDestination->addFileContents(static::PATH_SYSTEMTAGS, json_encode($systemTaggedFiles)) === false) { - throw new UserMigrationException("Could not export systemtagged files information."); + try { + $exportDestination->addFileContents(static::PATH_SYSTEMTAGS, json_encode($systemTaggedFiles)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export systemtagged files information.", 0, $e); } $output->writeln("Exporting file comments…"); @@ -160,8 +168,10 @@ function (IComment $comment): array { ); } } - if ($exportDestination->addFileContents(static::PATH_COMMENTS, json_encode($comments)) === false) { - throw new UserMigrationException("Could not export file comments."); + try { + $exportDestination->addFileContents(static::PATH_COMMENTS, json_encode($comments)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export file comments.", 0, $e); } // TODO other files metadata should be exported as well if relevant. @@ -197,8 +207,10 @@ public function import( $uid = $user->getUID(); - if ($importSource->copyToFolder($this->root->getUserFolder($uid), static::PATH_FILES) === false) { - throw new UserMigrationException("Could not import files."); + try { + $importSource->copyToFolder($this->root->getUserFolder($uid), static::PATH_FILES); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not import files.", 0, $e); } $userFolder = $this->root->getUserFolder($uid); @@ -210,8 +222,10 @@ public function import( $versionsFolder = $this->root->newFolder('/'.$uid.'/'.FilesVersionsStorage::VERSIONS_ROOT); } $output->writeln("Importing file versions…"); - if ($importSource->copyToFolder($versionsFolder, static::PATH_VERSIONS) === false) { - throw new UserMigrationException("Could not import files versions."); + try { + $importSource->copyToFolder($versionsFolder, static::PATH_VERSIONS); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not import files versions.", 0, $e); } } else { $output->writeln("No file versions to import…"); diff --git a/lib/Service/UserMigrationService.php b/lib/Service/UserMigrationService.php index 1106cc2a..f3b6c5f0 100644 --- a/lib/Service/UserMigrationService.php +++ b/lib/Service/UserMigrationService.php @@ -111,8 +111,10 @@ public function export(IExportDestination $exportDestination, IUser $user, ?arra $migrator->export($user, $exportDestination, $output); $migratorVersions[$migrator->getId()] = $migrator->getVersion(); } - if ($exportDestination->setMigratorVersions($migratorVersions) === false) { - throw new UserMigrationException("Could not export user information."); + try { + $exportDestination->setMigratorVersions($migratorVersions); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export user information.", 0, $e); } $exportDestination->close(); @@ -169,8 +171,10 @@ protected function exportUserInformation(IUser $user, 'enabled' => $user->isEnabled(), ]; - if ($exportDestination->addFileContents(IImportSource::PATH_USER, json_encode($userinfo)) === false) { - throw new UserMigrationException("Could not export user information."); + try { + $exportDestination->addFileContents(IImportSource::PATH_USER, json_encode($userinfo)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export user information.", 0, $e); } } @@ -214,8 +218,10 @@ protected function exportVersions(string $uid, \OC_App::getAppVersions() ); - if ($exportDestination->addFileContents("versions.json", json_encode($versions)) === false) { - throw new UserMigrationException("Could not export versions."); + try { + $exportDestination->addFileContents("versions.json", json_encode($versions)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export versions.", 0, $e); } } @@ -229,8 +235,10 @@ protected function exportAppsSettings(string $uid, $data = $this->config->getAllUserValues($uid); - if ($exportDestination->addFileContents("settings.json", json_encode($data)) === false) { - throw new UserMigrationException("Could not export settings."); + try { + $exportDestination->addFileContents("settings.json", json_encode($data)); + } catch (\Throwable $e) { + throw new UserMigrationException("Could not export settings.", 0, $e); } }