From 6713babcedbaac73d28e1b32b43b1e80361d2a0f Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Wed, 14 Aug 2024 20:55:24 +0200 Subject: [PATCH 1/4] [Tar] Fill in the file size even if the file is empty. This matches standard behavior. --- .../src/System/Formats/Tar/TarHeader.Write.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs index 81d90e1d7be213..103a756069a0ca 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs @@ -629,7 +629,7 @@ private int WriteCommonFields(Span buffer, TarEntryType actualEntryType) checksum += FormatNumeric(_gid, buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)); } - if (_size > 0) + if (_size >= 0) { checksum += FormatNumeric(_size, buffer.Slice(FieldLocations.Size, FieldLengths.Size)); } From 9f8b4c15d78ed974b4182f0d43dc6db93d65af45 Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Thu, 15 Aug 2024 17:37:40 +0200 Subject: [PATCH 2/4] fix tests, set size only when datastream is not null --- .../src/System/Formats/Tar/TarHeader.Write.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs index 103a756069a0ca..fcc5d22cde60eb 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs @@ -629,7 +629,7 @@ private int WriteCommonFields(Span buffer, TarEntryType actualEntryType) checksum += FormatNumeric(_gid, buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)); } - if (_size >= 0) + if (_dataStream != null && _size >= 0) { checksum += FormatNumeric(_size, buffer.Slice(FieldLocations.Size, FieldLengths.Size)); } From 94f32512a130717a8ac161faa8c453cdfaea835c Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Wed, 21 Aug 2024 22:16:06 +0200 Subject: [PATCH 3/4] Add unit tests for size header of an empty file --- .../tests/TarWriter/TarWriter.Tests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs index b46816844b44cb..de5b8451ac5864 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.IO; +using System.Linq; using Xunit; namespace System.Formats.Tar.Tests @@ -350,5 +351,20 @@ private int GetChecksumForFormatSpecificFields(TarEntry entry, TarEntryFormat fo // Gnu BlockDevice: 623 + 1004 + 686 + 1142 = 3455 return checksum; } + + [Fact] + void Verify_Size_RegularFile_Empty() + { + MemoryStream emptyData = new(0); + MemoryStream output = new(); + TarWriter archive = new(output, TarEntryFormat.Pax); + PaxTarEntry te = new(TarEntryType.RegularFile, "zero_size") + { DataStream = emptyData }; + archive.WriteEntry(te); + var sizeBuffer = output.GetBuffer()[1148..(1148 + 12)]; + // we expect ocal zeros + byte[] expected = [0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0]; + Assert.True(sizeBuffer.SequenceEqual(expected)); + } } } From d3f301b71c49df1306e651828cdf00e17c251b68 Mon Sep 17 00:00:00 2001 From: Szymon Sobik Date: Wed, 21 Aug 2024 22:31:13 +0200 Subject: [PATCH 4/4] Scope disposables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carlos Sánchez López <1175054+carlossanlop@users.noreply.github.com> --- .../System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs index de5b8451ac5864..cb65c0e6d163e0 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.Tests.cs @@ -355,9 +355,9 @@ private int GetChecksumForFormatSpecificFields(TarEntry entry, TarEntryFormat fo [Fact] void Verify_Size_RegularFile_Empty() { - MemoryStream emptyData = new(0); - MemoryStream output = new(); - TarWriter archive = new(output, TarEntryFormat.Pax); + using MemoryStream emptyData = new(0); + using MemoryStream output = new(); + using TarWriter archive = new(output, TarEntryFormat.Pax); PaxTarEntry te = new(TarEntryType.RegularFile, "zero_size") { DataStream = emptyData }; archive.WriteEntry(te);