From 3244704ea2c0ed62075014216748b9c656e091a5 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Fri, 9 Feb 2024 23:25:20 -0800 Subject: [PATCH] fix: Fixes directory copying (#1668) --- Projects/Server/Utilities/PathUtility.cs | 65 +++++++++++++++---- Projects/Server/World/World.cs | 2 +- Projects/UOContent/Compression/TarArchive.cs | 2 +- Projects/UOContent/World Saves/AutoArchive.cs | 4 +- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/Projects/Server/Utilities/PathUtility.cs b/Projects/Server/Utilities/PathUtility.cs index 65064e616..11f5fa1af 100644 --- a/Projects/Server/Utilities/PathUtility.cs +++ b/Projects/Server/Utilities/PathUtility.cs @@ -64,22 +64,65 @@ public static class PathUtility return EnsureDirectory(Path.Combine(basePath, bytes.ToHexString())); } - public static void CopyDirectory(string sourcePath, string destinationPath, bool recursive = true) + public static void CopyDirectoryContents(string sourceDir, string destDir, bool recursive = true) { - var searchOptions = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - foreach (var file in Directory.EnumerateFiles(sourcePath, "*", searchOptions)) + var dir = new DirectoryInfo(sourceDir); + + if (!dir.Exists) { - var fi = new FileInfo(file); - var relativePath = Path.GetRelativePath(sourcePath, fi.DirectoryName!); - var destFolder = Path.Combine(destinationPath, relativePath); - EnsureDirectory(destFolder); - fi.CopyTo(Path.Combine(destFolder, fi.Name)); + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); + } + + Directory.CreateDirectory(destDir); + + foreach (FileInfo file in dir.GetFiles()) + { + string targetFilePath = Path.Combine(destDir, file.Name); + file.CopyTo(targetFilePath, true); + } + + if (recursive) + { + foreach (DirectoryInfo subdir in dir.GetDirectories()) + { + string destSubDir = Path.Combine(destDir, subdir.Name); + CopyDirectoryContents(subdir.FullName, destSubDir); + } } } - public static void MoveDirectory(string sourcePath, string destinationPath) + public static void MoveDirectoryContents(string sourceDir, string destDir, bool recursive = true) { - CopyDirectory(sourcePath, destinationPath); - Directory.Delete(sourcePath, true); + var dir = new DirectoryInfo(sourceDir); + + if (!dir.Exists) + { + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); + } + + Directory.CreateDirectory(destDir); + + foreach (FileInfo file in dir.GetFiles()) + { + file.MoveTo(Path.Combine(destDir, file.Name)); + } + + if (recursive) + { + foreach (DirectoryInfo subdir in dir.GetDirectories()) + { + string destSubDir = Path.Combine(destDir, subdir.Name); + MoveDirectoryContents(subdir.FullName, destSubDir); + } + } + + try + { + dir.Delete(true); + } + catch + { + // ignored + } } } diff --git a/Projects/Server/World/World.cs b/Projects/Server/World/World.cs index 2344baad4..c50b46764 100644 --- a/Projects/Server/World/World.cs +++ b/Projects/Server/World/World.cs @@ -230,7 +230,7 @@ public static class World try { EventSink.InvokeWorldSavePostSnapshot(SavePath, tempPath); - PathUtility.MoveDirectory(tempPath, SavePath); + PathUtility.MoveDirectoryContents(tempPath, SavePath); Directory.SetLastWriteTimeUtc(SavePath, Core.Now); } catch (Exception ex) diff --git a/Projects/UOContent/Compression/TarArchive.cs b/Projects/UOContent/Compression/TarArchive.cs index 4c5f8cd1a..db37a7de5 100755 --- a/Projects/UOContent/Compression/TarArchive.cs +++ b/Projects/UOContent/Compression/TarArchive.cs @@ -63,7 +63,7 @@ namespace Server.Compression ZipFile.ExtractToDirectory(libarchiveFile, tempDir); var libArchivePath = Path.Combine(tempDir, "libarchive"); - PathUtility.MoveDirectory(Path.Combine(libArchivePath, "bin"), Path.Combine(Core.BaseDirectory, "bsdtar")); + PathUtility.MoveDirectoryContents(Path.Combine(libArchivePath, "bin"), Path.Combine(Core.BaseDirectory, "bsdtar")); File.Delete(libarchiveFile); return Path.Combine(Core.BaseDirectory, "bsdtar/bsdtar.exe"); diff --git a/Projects/UOContent/World Saves/AutoArchive.cs b/Projects/UOContent/World Saves/AutoArchive.cs index 1ac2701b1..64a0a9a06 100755 --- a/Projects/UOContent/World Saves/AutoArchive.cs +++ b/Projects/UOContent/World Saves/AutoArchive.cs @@ -97,7 +97,7 @@ namespace Server.Saves Directory.CreateDirectory(AutomaticBackupPath); var backupPath = Path.Combine(AutomaticBackupPath, Utility.GetTimeStamp()); - PathUtility.MoveDirectory(args.OldSavePath, backupPath); + PathUtility.MoveDirectoryContents(args.OldSavePath, backupPath); logger.Information("Created backup at {Path}", backupPath); @@ -150,7 +150,7 @@ namespace Server.Saves Directory.Delete(savePath, true); var dirInfo = new DirectoryInfo(folder); logger.Information("Restoring backup {Directory}", dirInfo.Name); - PathUtility.MoveDirectory(folder, savePath); + PathUtility.MoveDirectoryContents(folder, savePath); break; }