diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/PersistentState.Backup.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/PersistentState.Backup.cs index e1680a636..f17d0d4a8 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/PersistentState.Backup.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/PersistentState.Backup.cs @@ -1,6 +1,4 @@ -using System.Diagnostics; using System.Formats.Tar; -using SafeFileHandle = Microsoft.Win32.SafeHandles.SafeFileHandle; namespace DotNext.Net.Cluster.Consensus.Raft; @@ -8,46 +6,6 @@ public partial class PersistentState { private readonly TarEntryFormat backupFormat; - private TarEntry CreateTarEntry(FileInfo source) - { - TarEntry destination = backupFormat switch - { - TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, source.Name) { AccessTime = source.LastAccessTime, ChangeTime = source.LastWriteTime, UserName = Environment.UserName }, - TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, source.Name) { UserName = Environment.UserName }, - TarEntryFormat.V7 => new V7TarEntry(TarEntryType.RegularFile, source.Name), - _ => new PaxTarEntry(TarEntryType.RegularFile, source.Name) { UserName = Environment.UserName }, - }; - - destination.ModificationTime = source.LastWriteTime; - - if (Environment.OSVersion is { Platform: PlatformID.Unix }) - { - destination.Mode = source.UnixFileMode; - } - - return destination; - } - - private static void ImportAttributes(SafeFileHandle handle, TarEntry entry) - { - switch (entry) - { - case GnuTarEntry gnu: - File.SetLastAccessTimeUtc(handle, gnu.AccessTime.UtcDateTime); - goto default; - default: - File.SetLastWriteTimeUtc(handle, entry.ModificationTime.UtcDateTime); - break; - } - - if (Environment.OSVersion is { Platform: PlatformID.Unix }) - { - Debug.Assert(!OperatingSystem.IsWindows()); - - File.SetUnixFileMode(handle, entry.Mode); - } - } - /// /// Creates backup of this audit trail in TAR format. /// @@ -72,17 +30,7 @@ public async Task CreateBackupAsync(Stream output, CancellationToken token = def archive = new(output, backupFormat, leaveOpen: true); foreach (var file in Location.EnumerateFiles()) { - var destination = CreateTarEntry(file); - var source = file.Open(options); - try - { - destination.DataStream = source; - await archive.WriteEntryAsync(destination, token).ConfigureAwait(false); - } - finally - { - await source.DisposeAsync().ConfigureAwait(false); - } + await archive.WriteEntryAsync(file.FullName, entryName: null, token).ConfigureAwait(false); } await output.FlushAsync(token).ConfigureAwait(false); @@ -119,25 +67,8 @@ public static async Task RestoreFromBackupAsync(Stream backup, DirectoryInfo des { while (await archive.GetNextEntryAsync(copyData: false, token).ConfigureAwait(false) is { } entry) { - var sourceStream = entry.DataStream; - var destinationStream = new FileStream(Path.Combine(destination.FullName, entry.Name), FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan); - try - { - ImportAttributes(destinationStream.SafeFileHandle, entry); - - if (sourceStream is not null) - { - await sourceStream.CopyToAsync(destinationStream, token).ConfigureAwait(false); - await destinationStream.FlushAsync(token).ConfigureAwait(false); - } - } - finally - { - if (sourceStream is not null) - await sourceStream.DisposeAsync().ConfigureAwait(false); - - await destinationStream.DisposeAsync().ConfigureAwait(false); - } + var destinationFileName = Path.Combine(destination.FullName, entry.Name); + await entry.ExtractToFileAsync(destinationFileName, overwrite: false, token).ConfigureAwait(false); } } finally