Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tar] Fill in the file size even if the file is empty. #106409

Merged
merged 4 commits into from
Aug 23, 2024
Merged
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
@@ -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));
}
Original file line number Diff line number Diff line change
@@ -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()
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