Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ private int WriteCommonFields(Span<byte> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the test methods should be public

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, for consistency and repo conventions. However, it is known to work with xunit: xunit/xunit#2438 (reply in thread) :)

{
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);
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));
}
}
}
Loading