Skip to content

Commit 36a6526

Browse files
committed
Simplified extraction of archive
1 parent d2e8fb8 commit 36a6526

File tree

1 file changed

+3
-72
lines changed

1 file changed

+3
-72
lines changed

src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/PersistentState.Backup.cs

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,11 @@
1-
using System.Diagnostics;
21
using System.Formats.Tar;
3-
using SafeFileHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;
42

53
namespace DotNext.Net.Cluster.Consensus.Raft;
64

75
public partial class PersistentState
86
{
97
private readonly TarEntryFormat backupFormat;
108

11-
private TarEntry CreateTarEntry(FileInfo source)
12-
{
13-
TarEntry destination = backupFormat switch
14-
{
15-
TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, source.Name) { AccessTime = source.LastAccessTime, ChangeTime = source.LastWriteTime, UserName = Environment.UserName },
16-
TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, source.Name) { UserName = Environment.UserName },
17-
TarEntryFormat.V7 => new V7TarEntry(TarEntryType.RegularFile, source.Name),
18-
_ => new PaxTarEntry(TarEntryType.RegularFile, source.Name) { UserName = Environment.UserName },
19-
};
20-
21-
destination.ModificationTime = source.LastWriteTime;
22-
23-
if (Environment.OSVersion is { Platform: PlatformID.Unix })
24-
{
25-
destination.Mode = source.UnixFileMode;
26-
}
27-
28-
return destination;
29-
}
30-
31-
private static void ImportAttributes(SafeFileHandle handle, TarEntry entry)
32-
{
33-
switch (entry)
34-
{
35-
case GnuTarEntry gnu:
36-
File.SetLastAccessTimeUtc(handle, gnu.AccessTime.UtcDateTime);
37-
goto default;
38-
default:
39-
File.SetLastWriteTimeUtc(handle, entry.ModificationTime.UtcDateTime);
40-
break;
41-
}
42-
43-
if (Environment.OSVersion is { Platform: PlatformID.Unix })
44-
{
45-
Debug.Assert(!OperatingSystem.IsWindows());
46-
47-
File.SetUnixFileMode(handle, entry.Mode);
48-
}
49-
}
50-
519
/// <summary>
5210
/// Creates backup of this audit trail in TAR format.
5311
/// </summary>
@@ -72,17 +30,7 @@ public async Task CreateBackupAsync(Stream output, CancellationToken token = def
7230
archive = new(output, backupFormat, leaveOpen: true);
7331
foreach (var file in Location.EnumerateFiles())
7432
{
75-
var destination = CreateTarEntry(file);
76-
var source = file.Open(options);
77-
try
78-
{
79-
destination.DataStream = source;
80-
await archive.WriteEntryAsync(destination, token).ConfigureAwait(false);
81-
}
82-
finally
83-
{
84-
await source.DisposeAsync().ConfigureAwait(false);
85-
}
33+
await archive.WriteEntryAsync(file.FullName, entryName: null, token).ConfigureAwait(false);
8634
}
8735

8836
await output.FlushAsync(token).ConfigureAwait(false);
@@ -119,25 +67,8 @@ public static async Task RestoreFromBackupAsync(Stream backup, DirectoryInfo des
11967
{
12068
while (await archive.GetNextEntryAsync(copyData: false, token).ConfigureAwait(false) is { } entry)
12169
{
122-
var sourceStream = entry.DataStream;
123-
var destinationStream = new FileStream(Path.Combine(destination.FullName, entry.Name), FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
124-
try
125-
{
126-
ImportAttributes(destinationStream.SafeFileHandle, entry);
127-
128-
if (sourceStream is not null)
129-
{
130-
await sourceStream.CopyToAsync(destinationStream, token).ConfigureAwait(false);
131-
await destinationStream.FlushAsync(token).ConfigureAwait(false);
132-
}
133-
}
134-
finally
135-
{
136-
if (sourceStream is not null)
137-
await sourceStream.DisposeAsync().ConfigureAwait(false);
138-
139-
await destinationStream.DisposeAsync().ConfigureAwait(false);
140-
}
70+
var destinationFileName = Path.Combine(destination.FullName, entry.Name);
71+
await entry.ExtractToFileAsync(destinationFileName, overwrite: false, token).ConfigureAwait(false);
14172
}
14273
}
14374
finally

0 commit comments

Comments
 (0)