Skip to content

[release/7.0] Fix prefix writing on TarHeaderWrite #75373

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

Merged
merged 7 commits into from
Sep 14, 2022
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 @@ -374,12 +374,9 @@ private int WritePosixName(Span<byte> buffer)

if (_name.Length > FieldLengths.Name)
{
int prefixBytesLength = Math.Min(_name.Length - FieldLengths.Name, FieldLengths.Name);
Span<byte> remaining = prefixBytesLength <= 256 ?
stackalloc byte[prefixBytesLength] :
new byte[prefixBytesLength];

int encoded = Encoding.ASCII.GetBytes(_name.AsSpan(FieldLengths.Name), remaining);
int prefixBytesLength = Math.Min(_name.Length - FieldLengths.Name, FieldLengths.Prefix);
Span<byte> remaining = stackalloc byte[prefixBytesLength];
int encoded = Encoding.ASCII.GetBytes(_name.AsSpan(FieldLengths.Name, prefixBytesLength), remaining);
Debug.Assert(encoded == remaining.Length);

checksum += WriteLeftAlignedBytesAndGetChecksum(remaining, buffer.Slice(FieldLocations.Prefix, FieldLengths.Prefix));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;

namespace System.Formats.Tar.Tests
Expand Down Expand Up @@ -299,5 +300,57 @@ public void WriteTimestampsBeyondOctalLimit(TarEntryFormat format)
}
}
}

[Theory]
[InlineData(TarEntryFormat.V7)]
// [InlineData(TarEntryFormat.Ustar)] https://github.com/dotnet/runtime/issues/75360
[InlineData(TarEntryFormat.Pax)]
[InlineData(TarEntryFormat.Gnu)]
public void WriteLongName(TarEntryFormat format)
{
var r = new Random();
Copy link
Member

Choose a reason for hiding this comment

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

Ideally this would have a seed. That way the test is reproducible.

Suggested change
var r = new Random();
var r = new Random(42);

Copy link
Contributor

Choose a reason for hiding this comment

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

Glad you used 42. It's the answer to life, the universe, and everything. 😃

foreach (int length in new[] { 99, 100, 101, 199, 200, 201, 254, 255, 256 })
{
string name = string.Concat(Enumerable.Range(0, length).Select(_ => (char)('a' + r.Next(26))));
WriteLongNameCore(format, name);
}
}

private void WriteLongNameCore(TarEntryFormat format, string maxPathComponent)
{
TarEntry entry;
MemoryStream ms = new();
using (TarWriter writer = new(ms, true))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
using (TarWriter writer = new(ms, true))
using (TarWriter writer = new(ms, leaveOpen: true))

{
TarEntryType entryType = format == TarEntryFormat.V7 ? TarEntryType.V7RegularFile : TarEntryType.RegularFile;
entry = InvokeTarEntryCreationConstructor(format, entryType, maxPathComponent);
writer.WriteEntry(entry);

entry = InvokeTarEntryCreationConstructor(format, entryType, Path.Join(maxPathComponent, maxPathComponent));
writer.WriteEntry(entry);
}

ms.Position = 0;
using TarReader reader = new(ms);

entry = reader.GetNextEntry();
string expectedName = GetExpectedNameForFormat(format, maxPathComponent);
Assert.Equal(expectedName, entry.Name);

entry = reader.GetNextEntry();
expectedName = GetExpectedNameForFormat(format, Path.Join(maxPathComponent, maxPathComponent));
Assert.Equal(expectedName, entry.Name);

Assert.Null(reader.GetNextEntry());

string GetExpectedNameForFormat(TarEntryFormat format, string expectedName)
{
if (format is TarEntryFormat.V7 && expectedName.Length > 100) // V7 truncates names at 100 characters.
{
return expectedName.Substring(0, 100);
}
return expectedName;
}
}
}
}