Skip to content

Commit 82adf77

Browse files
authored
Apply offline feedback on how to correctly use TarWriter (#15292)
1 parent 81a7583 commit 82adf77

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

src/Microsoft.DotNet.SignTool/src/ZipData.cs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -326,57 +326,48 @@ private void RepackTarGZip(TaskLoggingHelper log, string tempDir, string tarTool
326326
#else
327327
private void RepackTarGZip(TaskLoggingHelper log, string tempDir, string tarToolPath)
328328
{
329-
using MemoryStream outputStream = new();
330-
331-
using (GZipStream gzipStream = new(outputStream, CompressionMode.Compress, leaveOpen: true))
332-
using (TarWriter writer = new(gzipStream))
329+
using MemoryStream streamToCompress = new();
330+
using (TarWriter writer = new(streamToCompress, leaveOpen: true))
333331
{
334332
foreach (TarEntry entry in ReadTarGZipEntries(FileSignInfo.FullPath))
335333
{
336334
if (entry.DataStream != null)
337335
{
338-
Stream dataStream;
339-
340336
string relativeName = entry.Name;
341337
ZipPart? signedPart = FindNestedPart(relativeName);
342338

343339
if (signedPart.HasValue)
344340
{
345-
dataStream = File.OpenRead(signedPart.Value.FileSignInfo.FullPath);
341+
using FileStream signedStream = File.OpenRead(signedPart.Value.FileSignInfo.FullPath);
342+
entry.DataStream = signedStream;
343+
entry.DataStream.Position = 0;
344+
writer.WriteEntry(entry);
345+
346346
log.LogMessage(MessageImportance.Low, $"Copying signed stream from {signedPart.Value.FileSignInfo.FullPath} to {FileSignInfo.FullPath} -> {relativeName}.");
347-
}
348-
else
349-
{
350-
dataStream = new MemoryStream();
351-
entry.DataStream.CopyTo(dataStream);
352-
dataStream.Position = 0;
353-
log.LogMessage(MessageImportance.Low, $"Didn't find signed part for nested file: {FileSignInfo.FullPath} -> {relativeName}");
347+
continue;
354348
}
355349

356-
entry.DataStream = dataStream;
357-
writer.WriteEntry(entry);
358-
dataStream.Dispose();
359-
}
360-
else
361-
{
362-
writer.WriteEntry(entry);
350+
log.LogMessage(MessageImportance.Low, $"Didn't find signed part for nested file: {FileSignInfo.FullPath} -> {relativeName}");
363351
}
352+
353+
writer.WriteEntry(entry);
364354
}
365355
}
366356

367-
outputStream.Position = 0;
368-
369-
using var outputFile = File.Open(FileSignInfo.FullPath, FileMode.Truncate, FileAccess.Write);
370-
outputStream.CopyTo(outputFile);
357+
streamToCompress.Position = 0;
358+
using (FileStream outputStream = File.Open(FileSignInfo.FullPath, FileMode.Truncate, FileAccess.Write))
359+
{
360+
using GZipStream compressor = new(outputStream, CompressionMode.Compress);
361+
streamToCompress.CopyTo(compressor);
362+
}
371363
}
372364

373365
private static IEnumerable<TarEntry> ReadTarGZipEntries(string path)
374366
{
375-
using var gzipStream = File.Open(path, FileMode.Open);
376-
using var tar = new GZipStream(gzipStream, CompressionMode.Decompress);
377-
using var reader = new TarReader(tar);
378-
379-
while (reader.GetNextEntry() is TarEntry entry)
367+
using FileStream streamToDecompress = File.OpenRead(path);
368+
using GZipStream decompressor = new(streamToDecompress, CompressionMode.Decompress);
369+
using TarReader tarReader = new(decompressor);
370+
while (tarReader.GetNextEntry() is TarEntry entry)
380371
{
381372
yield return entry;
382373
}

0 commit comments

Comments
 (0)