Skip to content

Commit 70cf817

Browse files
authored
Implement Tar async APIs (#70574)
* ref: TarEntry.ExtractToFileAsync * src: Implement TarEntry.ExtractToFileAsync * ref: Add TarReader.GetNextEntryAsync * src: Implement TarReader.GetNextEntryAsync * ref: Add TarWriter.WriteEntryAsync * src: Implement TarWriter.WriteEntryAsync * ref: Add TarFile.CreateFromDirectoryAsync * src: Implement TarFile.CreateFromDirectoryAsync * ref: Add TarFile.ExtractToDirectoryAsync * src: Implement TarFile.ExtractToDirectoryAsync * ref: Add TarWriter.DisposeAsync * src: Implement TarWriter.DisposeAsync * ref: Add TarReader.DisposeAsync * src: Implement TarReader.DisposeAsync * tests: Async tests, analogous to the sync ones.
1 parent 945e7eb commit 70cf817

File tree

64 files changed

+6088
-982
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6088
-982
lines changed

src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal TarEntry() { }
4848
public string Name { get { throw null; } set { } }
4949
public int Uid { get { throw null; } set { } }
5050
public void ExtractToFile(string destinationFileName, bool overwrite) { }
51+
public System.Threading.Tasks.Task ExtractToFileAsync(string destinationFileName, bool overwrite, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
5152
public override string ToString() { throw null; }
5253
}
5354
public enum TarEntryFormat
@@ -83,24 +84,33 @@ public static partial class TarFile
8384
{
8485
public static void CreateFromDirectory(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory) { }
8586
public static void CreateFromDirectory(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory) { }
87+
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
88+
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8689
public static void ExtractToDirectory(System.IO.Stream source, string destinationDirectoryName, bool overwriteFiles) { }
8790
public static void ExtractToDirectory(string sourceFileName, string destinationDirectoryName, bool overwriteFiles) { }
91+
public static System.Threading.Tasks.Task ExtractToDirectoryAsync(System.IO.Stream source, string destinationDirectoryName, bool overwriteFiles, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
92+
public static System.Threading.Tasks.Task ExtractToDirectoryAsync(string sourceFileName, string destinationDirectoryName, bool overwriteFiles, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8893
}
89-
public sealed partial class TarReader : System.IDisposable
94+
public sealed partial class TarReader : System.IAsyncDisposable, System.IDisposable
9095
{
9196
public TarReader(System.IO.Stream archiveStream, bool leaveOpen = false) { }
9297
public void Dispose() { }
98+
public System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
9399
public System.Formats.Tar.TarEntry? GetNextEntry(bool copyData = false) { throw null; }
100+
public System.Threading.Tasks.ValueTask<System.Formats.Tar.TarEntry?> GetNextEntryAsync(bool copyData = false, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
94101
}
95-
public sealed partial class TarWriter : System.IDisposable
102+
public sealed partial class TarWriter : System.IAsyncDisposable, System.IDisposable
96103
{
97104
public TarWriter(System.IO.Stream archiveStream) { }
98105
public TarWriter(System.IO.Stream archiveStream, bool leaveOpen = false) { }
99106
public TarWriter(System.IO.Stream archiveStream, System.Formats.Tar.TarEntryFormat format = System.Formats.Tar.TarEntryFormat.Pax, bool leaveOpen = false) { }
100107
public System.Formats.Tar.TarEntryFormat Format { get { throw null; } }
101108
public void Dispose() { }
109+
public System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
102110
public void WriteEntry(System.Formats.Tar.TarEntry entry) { }
103111
public void WriteEntry(string fileName, string? entryName) { }
112+
public System.Threading.Tasks.Task WriteEntryAsync(System.Formats.Tar.TarEntry entry, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
113+
public System.Threading.Tasks.Task WriteEntryAsync(string fileName, string? entryName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
104114
}
105115
public sealed partial class UstarTarEntry : System.Formats.Tar.PosixTarEntry
106116
{

src/libraries/System.Formats.Tar/src/Resources/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
<data name="TarSizeFieldNegative" xml:space="preserve">
235235
<value>The size field is negative in the tar entry '{0}'.</value>
236236
</data>
237-
<data name="TarSizeFieldTooLargeForExtendedAttribute" xml:space="preserve">
237+
<data name="TarSizeFieldTooLargeForEntryType" xml:space="preserve">
238238
<value>The value of the size field for the current entry of type '{0}' is beyond the expected length.</value>
239239
</data>
240240
<data name="TarSymbolicLinkTargetNotExists" xml:space="preserve">

src/libraries/System.Formats.Tar/src/System/Formats/Tar/SeekableSubReadStream.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public override int Read(Span<byte> destination)
7171

7272
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
7373
{
74+
if (cancellationToken.IsCancellationRequested)
75+
{
76+
return ValueTask.FromCanceled<int>(cancellationToken);
77+
}
7478
ThrowIfDisposed();
7579
VerifyPositionInSuperStream();
7680
return ReadAsyncCore(buffer, cancellationToken);

src/libraries/System.Formats.Tar/src/System/Formats/Tar/SubReadStream.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,20 @@ public override int ReadByte()
132132

133133
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
134134
{
135+
if (cancellationToken.IsCancellationRequested)
136+
{
137+
return Task.FromCanceled<int>(cancellationToken);
138+
}
135139
ValidateBufferArguments(buffer, offset, count);
136140
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
137141
}
138142

139143
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
140144
{
145+
if (cancellationToken.IsCancellationRequested)
146+
{
147+
return ValueTask.FromCanceled<int>(cancellationToken);
148+
}
141149
ThrowIfDisposed();
142150
ThrowIfBeyondEndOfStream();
143151
return ReadAsyncCore(buffer, cancellationToken);
@@ -147,6 +155,8 @@ protected async ValueTask<int> ReadAsyncCore(Memory<byte> buffer, CancellationTo
147155
{
148156
Debug.Assert(!_hasReachedEnd);
149157

158+
cancellationToken.ThrowIfCancellationRequested();
159+
150160
if (_positionInSuperStream > _endInSuperStream - buffer.Length)
151161
{
152162
buffer = buffer.Slice(0, (int)(_endInSuperStream - _positionInSuperStream));

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.Unix.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ namespace System.Formats.Tar
1111
public abstract partial class TarEntry
1212
{
1313
// Unix specific implementation of the method that extracts the current entry as a block device.
14-
partial void ExtractAsBlockDevice(string destinationFileName)
14+
private void ExtractAsBlockDevice(string destinationFileName)
1515
{
1616
Debug.Assert(EntryType is TarEntryType.BlockDevice);
1717
Interop.CheckIo(Interop.Sys.CreateBlockDevice(destinationFileName, (uint)Mode, (uint)_header._devMajor, (uint)_header._devMinor), destinationFileName);
1818
}
1919

2020
// Unix specific implementation of the method that extracts the current entry as a character device.
21-
partial void ExtractAsCharacterDevice(string destinationFileName)
21+
private void ExtractAsCharacterDevice(string destinationFileName)
2222
{
2323
Debug.Assert(EntryType is TarEntryType.CharacterDevice);
2424
Interop.CheckIo(Interop.Sys.CreateCharacterDevice(destinationFileName, (uint)Mode, (uint)_header._devMajor, (uint)_header._devMinor), destinationFileName);
2525
}
2626

2727
// Unix specific implementation of the method that extracts the current entry as a fifo file.
28-
partial void ExtractAsFifo(string destinationFileName)
28+
private void ExtractAsFifo(string destinationFileName)
2929
{
3030
Debug.Assert(EntryType is TarEntryType.Fifo);
3131
Interop.CheckIo(Interop.Sys.MkFifo(destinationFileName, (uint)Mode), destinationFileName);
3232
}
3333

3434
// Unix specific implementation of the method that extracts the current entry as a hard link.
35-
partial void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
35+
private void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
3636
{
3737
Debug.Assert(EntryType is TarEntryType.HardLink);
3838
Debug.Assert(!string.IsNullOrEmpty(targetFilePath));
@@ -41,7 +41,7 @@ partial void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
4141
}
4242

4343
// Unix specific implementation of the method that specifies the file permissions of the extracted file.
44-
partial void SetModeOnFile(SafeFileHandle handle, string destinationFileName)
44+
private void SetModeOnFile(SafeFileHandle handle)
4545
{
4646
// Only extract USR, GRP, and OTH file permissions, and ignore
4747
// S_ISUID, S_ISGID, and S_ISVTX bits.

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.Windows.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ namespace System.Formats.Tar
1010
public abstract partial class TarEntry
1111
{
1212
// Throws on Windows. Block devices are not supported on this platform.
13-
partial void ExtractAsBlockDevice(string destinationFileName)
13+
private void ExtractAsBlockDevice(string destinationFileName)
1414
{
1515
Debug.Assert(EntryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice);
1616
throw new InvalidOperationException(SR.IO_DeviceFiles_NotSupported);
1717
}
1818

1919
// Throws on Windows. Character devices are not supported on this platform.
20-
partial void ExtractAsCharacterDevice(string destinationFileName)
20+
private void ExtractAsCharacterDevice(string destinationFileName)
2121
{
2222
Debug.Assert(EntryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice);
2323
throw new InvalidOperationException(SR.IO_DeviceFiles_NotSupported);
2424
}
2525

2626
// Throws on Windows. Fifo files are not supported on this platform.
27-
partial void ExtractAsFifo(string destinationFileName)
27+
private void ExtractAsFifo(string destinationFileName)
2828
{
2929
Debug.Assert(EntryType is TarEntryType.Fifo);
3030
throw new InvalidOperationException(SR.IO_FifoFiles_NotSupported);
3131
}
3232

3333
// Windows specific implementation of the method that extracts the current entry as a hard link.
34-
partial void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
34+
private void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
3535
{
3636
Debug.Assert(EntryType is TarEntryType.HardLink);
3737
Debug.Assert(!string.IsNullOrEmpty(targetFilePath));
@@ -41,7 +41,7 @@ partial void ExtractAsHardLink(string targetFilePath, string hardLinkFilePath)
4141

4242
// Mode is not used on Windows.
4343
#pragma warning disable CA1822 // Member 'SetModeOnFile' does not access instance data and can be marked as static
44-
partial void SetModeOnFile(SafeFileHandle handle, string destinationFileName)
44+
private void SetModeOnFile(SafeFileHandle handle)
4545
#pragma warning restore CA1822
4646
{
4747
// TODO: Verify that executables get their 'executable' permission applied on Windows when extracted, if applicable. https://github.com/dotnet/runtime/issues/68230

0 commit comments

Comments
 (0)