fix: Cleans up file system paths and archiving (#815)
* Makes EnsureDirectory properly work for relative and absolute paths * Adds a `PathUtility.GetFullPath` which returns full paths for relative paths to `Core.BaseDirectory`. If the path is absolute, it will return as-is. * Moves EnsureDirectory to `PathUtility`. So `ScriptsHandler.EnsureDirectory` and `AssemblyHandler.EnsureDirectory` are now `PathUtility.EnsureDirectory` * Fixes crash guard so that it copies accounts properly. * Changes world save and auto archive to use a random folder name inside of the temp folder.
This commit is contained in:
parent
da31153b20
commit
a55e271a69
17 changed files with 447 additions and 341 deletions
|
|
@ -50,8 +50,7 @@ namespace Server.Compression
|
|||
|
||||
private static string DownloadTarForWindows()
|
||||
{
|
||||
var tempDir = Path.Combine(Core.BaseDirectory, "temp");
|
||||
AssemblyHandler.EnsureDirectory("temp");
|
||||
var tempDir = PathUtility.EnsureRandomPath(Path.GetTempPath());
|
||||
|
||||
var libarchiveFile = Path.Combine(tempDir, "libarchive.zip");
|
||||
using WebClient wc = new WebClient();
|
||||
|
|
@ -82,29 +81,28 @@ namespace Server.Compression
|
|||
}
|
||||
|
||||
public static bool CreateFromPaths(
|
||||
List<string> paths,
|
||||
IEnumerable<string> paths,
|
||||
string destinationArchiveFileName,
|
||||
string relativeTo,
|
||||
string compressCommand = null,
|
||||
string compressionProgramPath = null
|
||||
)
|
||||
{
|
||||
_pathToTar ??= GetPathToTar();
|
||||
|
||||
AssemblyHandler.EnsureDirectory(new FileInfo(destinationArchiveFileName));
|
||||
var di = new DirectoryInfo(paths[0]);
|
||||
var directory = di.Parent!.FullName;
|
||||
new FileInfo(destinationArchiveFileName).EnsureDirectory();
|
||||
|
||||
using var builder = new ValueStringBuilder();
|
||||
for (var i = 0; i < paths.Count; i++)
|
||||
var i = 0;
|
||||
foreach (var path in paths)
|
||||
{
|
||||
var path = paths[i];
|
||||
builder.Append($"{(i > 0 ? " " : "")}\"{Path.GetRelativePath(directory, path)}\"");
|
||||
builder.Append($"{(i++ > 0 ? " " : "")}\"{Path.GetRelativePath(relativeTo, path)}\"");
|
||||
}
|
||||
var pathsToCompress = builder.ToString();
|
||||
|
||||
var tarFlags = compressCommand == null ? "-acf" : "-cf";
|
||||
var useExternalCompression = compressCommand != null ? $"--use-compress-program \"{compressCommand}\" " : "";
|
||||
var arguments = $"{useExternalCompression}{tarFlags} \"{destinationArchiveFileName}\" -C \"{directory}\" {pathsToCompress}";
|
||||
var arguments = $"{useExternalCompression}{tarFlags} \"{destinationArchiveFileName}\" -C \"{relativeTo}\" {pathsToCompress}";
|
||||
|
||||
return RunTar(arguments, compressionProgramPath) == 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,17 +42,15 @@ namespace Server.Compression
|
|||
}
|
||||
}
|
||||
|
||||
return TarArchive.ExtractToDirectory(fileNamePath, outputDirectory, "zstd -d", _pathToZstd);
|
||||
return TarArchive.ExtractToDirectory(fileNamePath, outputDirectory, "zstd -q -d", _pathToZstd);
|
||||
}
|
||||
|
||||
public static bool CreateFromPaths(
|
||||
List<string> paths,
|
||||
IEnumerable<string> paths,
|
||||
string destinationArchiveFileName,
|
||||
int compressionLevel = 10
|
||||
string relativeTo
|
||||
)
|
||||
{
|
||||
Debug.Assert(compressionLevel is >= 1 and <= 22, $"{nameof(compressionLevel)} must be between 1 and 22");
|
||||
|
||||
// bsdtar has a bug and hangs, so we are doing it in two steps.
|
||||
if (Core.IsWindows)
|
||||
{
|
||||
|
|
@ -60,7 +58,7 @@ namespace Server.Compression
|
|||
|
||||
try
|
||||
{
|
||||
if (!TarArchive.CreateFromPaths(paths, tempTarArchive))
|
||||
if (!TarArchive.CreateFromPaths(paths, relativeTo, tempTarArchive))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
@ -70,7 +68,7 @@ namespace Server.Compression
|
|||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Path.Combine(_pathToZstd, "zstd.exe"),
|
||||
Arguments = $"-q -10 \"{tempTarArchive}\" -o \"{destinationArchiveFileName}\""
|
||||
Arguments = $"-q \"{tempTarArchive}\" -o \"{destinationArchiveFileName}\""
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -89,7 +87,7 @@ namespace Server.Compression
|
|||
}
|
||||
}
|
||||
|
||||
return TarArchive.CreateFromPaths(paths, destinationArchiveFileName, "zstd -10", _pathToZstd);
|
||||
return TarArchive.CreateFromPaths(paths, destinationArchiveFileName, relativeTo, "zstd -q", _pathToZstd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue