1
- using System . Diagnostics ;
2
1
using System . Formats . Tar ;
3
- using SafeFileHandle = Microsoft . Win32 . SafeHandles . SafeFileHandle ;
4
2
5
3
namespace DotNext . Net . Cluster . Consensus . Raft ;
6
4
7
5
public partial class PersistentState
8
6
{
9
7
private readonly TarEntryFormat backupFormat ;
10
8
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
-
51
9
/// <summary>
52
10
/// Creates backup of this audit trail in TAR format.
53
11
/// </summary>
@@ -72,17 +30,7 @@ public async Task CreateBackupAsync(Stream output, CancellationToken token = def
72
30
archive = new ( output , backupFormat , leaveOpen : true ) ;
73
31
foreach ( var file in Location . EnumerateFiles ( ) )
74
32
{
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 ) ;
86
34
}
87
35
88
36
await output . FlushAsync ( token ) . ConfigureAwait ( false ) ;
@@ -119,25 +67,8 @@ public static async Task RestoreFromBackupAsync(Stream backup, DirectoryInfo des
119
67
{
120
68
while ( await archive . GetNextEntryAsync ( copyData : false , token ) . ConfigureAwait ( false ) is { } entry )
121
69
{
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 ) ;
141
72
}
142
73
}
143
74
finally
0 commit comments