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
Show file tree
Hide file tree
Changes from 3 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) :)

{
MemoryStream emptyData = new(0);
MemoryStream output = new();
TarWriter archive = new(output, TarEntryFormat.Pax);
sobczyk marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Did you only see the problem in Pax, or is this the only format that you tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The one I tested, but the problem would apply equally to all, since small sizes are encoded the same way.

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