Skip to content

Commit

Permalink
Simplified extraction of archive
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Apr 12, 2024
1 parent d2e8fb8 commit 36a6526
Showing 1 changed file with 3 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
using System.Diagnostics;
using System.Formats.Tar;
using SafeFileHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;

namespace DotNext.Net.Cluster.Consensus.Raft;

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);
}
}

/// <summary>
/// Creates backup of this audit trail in TAR format.
/// </summary>
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 36a6526

Please sign in to comment.