]> BookStack Code Mirror - system-cli/commitdiff
Updated move to be a recursive copy+delete
authorDan Brown <redacted>
Sat, 6 May 2023 15:01:24 +0000 (16:01 +0100)
committerDan Brown <redacted>
Sat, 6 May 2023 15:01:24 +0000 (16:01 +0100)
Added via new Directory helper class, which the existing recursive
delete function has been added to.
Done since rename would not work across filesystem boundaries.
Tested via a linuxserver BS setup.

Closes #6

src/Commands/RestoreCommand.php
src/Services/Directories.php [new file with mode: 0644]

index 04ea1bb9ebc655ae675835b0739dfa87552b9ecb..6324e2fa6c65282b9461dc39e4f17539509888a3 100644 (file)
@@ -5,6 +5,7 @@ namespace Cli\Commands;
 use Cli\Services\AppLocator;
 use Cli\Services\ArtisanRunner;
 use Cli\Services\BackupZip;
+use Cli\Services\Directories;
 use Cli\Services\EnvironmentLoader;
 use Cli\Services\InteractiveConsole;
 use Cli\Services\MySqlRunner;
@@ -118,7 +119,7 @@ class RestoreCommand extends Command
         $artisan->run(['view:clear']);
 
         $output->writeln("<info>Cleaning up extract directory...</info>");
-        $this->deleteDirectoryAndContents($extractDir);
+        Directories::delete($extractDir);
 
         $output->writeln("<success>\nRestore operation complete!</success>");
         $output->writeln("<info>You may need to fix file/folder permissions so that the webserver has</info>");
@@ -180,23 +181,8 @@ class RestoreCommand extends Command
     protected function restoreFolder(string $folderSubPath, string $appDir, string $extractDir): void
     {
         $fullAppFolderPath = Paths::real(Paths::join($appDir, $folderSubPath));
-        $this->deleteDirectoryAndContents($fullAppFolderPath);
-        rename(Paths::join($extractDir, $folderSubPath), $fullAppFolderPath);
-    }
-
-    protected function deleteDirectoryAndContents(string $dir): void
-    {
-        $files = new RecursiveIteratorIterator(
-            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
-            RecursiveIteratorIterator::CHILD_FIRST
-        );
-
-        foreach ($files as $fileinfo) {
-            $path = $fileinfo->getRealPath();
-            $fileinfo->isDir() ? rmdir($path) : unlink($path);
-        }
-
-        rmdir($dir);
+        Directories::delete($fullAppFolderPath);
+        Directories::move(Paths::join($extractDir, $folderSubPath), $fullAppFolderPath);
     }
 
     protected function restoreDatabase(string $appDir, string $extractDir): void
diff --git a/src/Services/Directories.php b/src/Services/Directories.php
new file mode 100644 (file)
index 0000000..6418235
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+namespace Cli\Services;
+use RecursiveDirectoryIterator;
+use RecursiveIteratorIterator;
+
+class Directories
+{
+
+    public static function move(string $src, string $target): void
+    {
+        static::copy($src, $target);
+        static::delete($src);
+    }
+
+    public static function copy(string $src, string $target): void
+    {
+        mkdir($target);
+
+        /** @var RecursiveDirectoryIterator $files */
+        $files = new RecursiveIteratorIterator(
+            new RecursiveDirectoryIterator($src, RecursiveDirectoryIterator::SKIP_DOTS),
+            RecursiveIteratorIterator::SELF_FIRST
+        );
+
+        /** @var \SplFileInfo $fileinfo */
+        foreach ($files as $fileinfo) {
+            $srcPath = $fileinfo->getRealPath();
+            $subPath = $files->getSubPathName();
+            $destPath = Paths::join($target, $subPath);
+            if ($fileinfo->isDir()) {
+                $result = mkdir($destPath);
+            } else {
+                $result = copy($srcPath, $destPath);
+            }
+
+            if ($result === false) {
+                throw new \Exception("Failed to copy file or directory from {$srcPath} to {$destPath}");
+            }
+        }
+    }
+
+    public static function delete(string $dir): void
+    {
+        $files = new RecursiveIteratorIterator(
+            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
+            RecursiveIteratorIterator::CHILD_FIRST
+        );
+
+        foreach ($files as $fileinfo) {
+            $path = $fileinfo->getRealPath();
+            if ($fileinfo->isDir()) {
+                $result = rmdir($path);
+            } else {
+                $result = unlink($path);
+            }
+
+            if ($result === false) {
+                throw new \Exception("Failed to delete file or directory at {$path}");
+            }
+        }
+
+        rmdir($dir);
+    }
+}
\ No newline at end of file