Skip to content

Commit d4509c5

Browse files
committed
src: Fix bugs found with the tests that check if async APIs are not calling sync APIs and viceversa.
1 parent f3566a6 commit d4509c5

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.Async.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ public static async Task<ZipArchive> CreateAsync(Stream stream, ZipArchiveMode m
103103
{
104104
await zipArchive.ReadEndOfCentralDirectoryAsync(cancellationToken).ConfigureAwait(false);
105105
await zipArchive.EnsureCentralDirectoryReadAsync(cancellationToken).ConfigureAwait(false);
106-
zipArchive.CheckIfEntriesAreOpenable();
106+
107+
foreach (ZipArchiveEntry entry in zipArchive._entries)
108+
{
109+
await entry.ThrowIfNotOpenableAsync(needToUncompress: false, needToLoadIntoMemory: true, cancellationToken).ConfigureAwait(false);
110+
}
107111
}
108112
break;
109113
}
110114

111115
return zipArchive;
112-
113116
}
114-
catch
117+
catch (Exception)
115118
{
116119
if (extraTempStream != null)
117120
{

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,16 @@ public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding?
158158
{
159159
ReadEndOfCentralDirectory();
160160
EnsureCentralDirectoryRead();
161-
CheckIfEntriesAreOpenable();
161+
162+
foreach (ZipArchiveEntry entry in _entries)
163+
{
164+
entry.ThrowIfNotOpenable(needToUncompress: false, needToLoadIntoMemory: true);
165+
}
162166
}
163167
break;
164168
}
165169
}
166-
catch
170+
catch (Exception)
167171
{
168172
extraTempStream?.Dispose();
169173

@@ -920,14 +924,6 @@ private static Stream DecideArchiveStream(ZipArchiveMode mode, Stream stream)
920924
stream;
921925
}
922926

923-
private void CheckIfEntriesAreOpenable()
924-
{
925-
foreach (ZipArchiveEntry entry in _entries)
926-
{
927-
entry.ThrowIfNotOpenable(needToUncompress: false, needToLoadIntoMemory: true);
928-
}
929-
}
930-
931927

932928
[Flags]
933929
internal enum ChangeState

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.Async.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal async Task LoadLocalHeaderExtraFieldIfNeededAsync(CancellationToken can
148148
{
149149
cancellationToken.ThrowIfCancellationRequested();
150150
// we should have made this exact call in _archive.Init through ThrowIfOpenable
151-
Debug.Assert(IsOpenable(false, true, out _));
151+
Debug.Assert(await GetIsOpenableAsync(false, true, cancellationToken).ConfigureAwait(false));
152152

153153
// load local header's extra fields. it will be null if we couldn't read for some reason
154154
if (_originallyInArchive)
@@ -162,7 +162,7 @@ internal async Task LoadCompressedBytesIfNeededAsync(CancellationToken cancellat
162162
{
163163
cancellationToken.ThrowIfCancellationRequested();
164164
// we should have made this exact call in _archive.Init through ThrowIfOpenable
165-
Debug.Assert(IsOpenable(false, true, out _));
165+
Debug.Assert(await GetIsOpenableAsync(false, true, cancellationToken).ConfigureAwait(false));
166166

167167
if (!_everOpenedForWrite && _originallyInArchive)
168168
{
@@ -178,6 +178,13 @@ internal async Task LoadCompressedBytesIfNeededAsync(CancellationToken cancellat
178178
}
179179
}
180180

181+
private async Task<bool> GetIsOpenableAsync(bool needToUncompress, bool needToLoadIntoMemory, CancellationToken cancellationToken)
182+
{
183+
cancellationToken.ThrowIfCancellationRequested();
184+
(bool result, _) = await IsOpenableAsync(needToUncompress, needToLoadIntoMemory, cancellationToken).ConfigureAwait(false);
185+
return result;
186+
}
187+
181188
internal async Task ThrowIfNotOpenableAsync(bool needToUncompress, bool needToLoadIntoMemory, CancellationToken cancellationToken)
182189
{
183190
cancellationToken.ThrowIfCancellationRequested();
@@ -288,9 +295,8 @@ private async Task WriteLocalFileHeaderAndDataIfNeededAsync(bool forceWrite, Can
288295

289296
//The compressor fills in CRC and sizes
290297
//The DirectToArchiveWriterStream writes headers and such
291-
using (Stream entryWriter = new DirectToArchiveWriterStream(
292-
GetDataCompressor(_archive.ArchiveStream, true, null),
293-
this))
298+
DirectToArchiveWriterStream entryWriter = new(GetDataCompressor(_archive.ArchiveStream, true, null), this);
299+
await using (entryWriter)
294300
{
295301
_storedUncompressedData.Seek(0, SeekOrigin.Begin);
296302
await _storedUncompressedData.CopyToAsync(entryWriter, cancellationToken).ConfigureAwait(false);

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ private void WriteLocalFileHeaderAndDataIfNeeded(bool forceWrite)
11041104

11051105
//The compressor fills in CRC and sizes
11061106
//The DirectToArchiveWriterStream writes headers and such
1107-
using (Stream entryWriter = new DirectToArchiveWriterStream(
1107+
using (DirectToArchiveWriterStream entryWriter = new(
11081108
GetDataCompressor(_archive.ArchiveStream, true, null),
11091109
this))
11101110
{

0 commit comments

Comments
 (0)