fix: Fixes directory copying (#1668)
This commit is contained in:
parent
00332e542d
commit
3244704ea2
4 changed files with 58 additions and 15 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue