Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d52d836
Avoid unnecessary byte[] allocations
stephentoub Aug 19, 2022
1e5020e
Remove unnecessary use of FileStreamOptions
stephentoub Aug 19, 2022
6938c77
Clean up Dispose{Async} implementations
stephentoub Aug 19, 2022
5fa03e9
Clean up unnecessary consts
stephentoub Aug 19, 2022
8dd0ac1
Remove MemoryStream/Encoding.UTF8.GetBytes allocations, unnecessary a…
stephentoub Aug 19, 2022
5be57ad
Avoid string allocations in ReadMagicAttribute
stephentoub Aug 19, 2022
ab71e6c
Avoid allocation in WriteAsOctal
stephentoub Aug 19, 2022
df2d742
Improve handling of octal
stephentoub Aug 19, 2022
c6058bd
Avoid allocation for version string
stephentoub Aug 19, 2022
5756a8c
Removing boxing and char string allocation in GenerateExtendedAttribu…
stephentoub Aug 19, 2022
9539a4a
Fix a couple unnecessary dictionary lookups
stephentoub Aug 19, 2022
74bbc9c
Replace Enum.HasFlag usage
stephentoub Aug 19, 2022
46e0855
Remove allocations from Write{Posix}Name
stephentoub Aug 19, 2022
02ca7da
Replace ArrayPool use with string.Create
stephentoub Aug 19, 2022
f9eb99f
Replace more superfluous ArrayPool usage
stephentoub Aug 19, 2022
add6179
Remove ArrayPool use from System.IO.Compression.ZipFile
stephentoub Aug 20, 2022
6f8cb75
Fix inverted condition
stephentoub Aug 20, 2022
827a588
Use generic math to parse octal
stephentoub Aug 20, 2022
ae21478
Remove allocations from StringReader and string.Split
stephentoub Aug 20, 2022
d6b6727
Remove magic string allocation for Ustar when not V7
stephentoub Aug 20, 2022
480af5c
Remove file name and directory name allocation in GenerateExtendedAtt…
stephentoub Aug 20, 2022
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
Prev Previous commit
Next Next commit
Avoid allocation for version string
  • Loading branch information
stephentoub committed Aug 19, 2022
commit c6058bd1d9b39a443f6f84a27d0b8d05ad7e14af
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private void ReadMagicAttribute(Span<byte> buffer)
_magic = GnuMagic;
_format = TarEntryFormat.Gnu;
}
else if (_format == TarEntryFormat.V7 && magic.SequenceEqual(PaxMagicBytes))
else if (_format == TarEntryFormat.V7 && magic.SequenceEqual(UstarMagicBytes))
{
// Important: Only change to ustar if we had not changed the format to pax already
_magic = UstarMagic;
Expand All @@ -452,19 +452,29 @@ private void ReadVersionAttribute(Span<byte> buffer)
}

Span<byte> version = buffer.Slice(FieldLocations.Version, FieldLengths.Version);

_version = Encoding.ASCII.GetString(version);

// The POSIX formats have a 6 byte Magic "ustar\0", followed by a 2 byte Version "00"
if ((_format is TarEntryFormat.Ustar or TarEntryFormat.Pax) && _version != UstarVersion)
switch (_format)
{
throw new FormatException(string.Format(SR.TarPosixFormatExpected, _name));
}
case TarEntryFormat.Ustar or TarEntryFormat.Pax:
// The POSIX formats have a 6 byte Magic "ustar\0", followed by a 2 byte Version "00"
if (!version.SequenceEqual(UstarVersionBytes))
{
throw new FormatException(string.Format(SR.TarPosixFormatExpected, _name));
}
_version = UstarVersion;
break;

// The GNU format has a Magic+Version 8 byte string "ustar \0"
if (_format == TarEntryFormat.Gnu && _version != GnuVersion)
{
throw new FormatException(string.Format(SR.TarGnuFormatExpected, _name));
case TarEntryFormat.Gnu:
// The GNU format has a Magic+Version 8 byte string "ustar \0"
if (!version.SequenceEqual(GnuVersionBytes))
{
throw new FormatException(string.Format(SR.TarGnuFormatExpected, _name));
}
_version = GnuVersion;
break;

default:
_version = Encoding.ASCII.GetString(version);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace System.Formats.Tar
// Writes header attributes of a tar archive entry.
internal sealed partial class TarHeader
{
private static ReadOnlySpan<byte> PaxMagicBytes => "ustar\0"u8;
private static ReadOnlySpan<byte> PaxVersionBytes => "00"u8;
private static ReadOnlySpan<byte> UstarMagicBytes => "ustar\0"u8;
private static ReadOnlySpan<byte> UstarVersionBytes => "00"u8;

private static ReadOnlySpan<byte> GnuMagicBytes => "ustar "u8;
private static ReadOnlySpan<byte> GnuVersionBytes => " \0"u8;
Expand Down Expand Up @@ -442,8 +442,8 @@ private long GetTotalDataBytesToWrite()
// Writes the magic and version fields of a ustar or pax entry into the specified spans.
private static int WritePosixMagicAndVersion(Span<byte> buffer)
{
int checksum = WriteLeftAlignedBytesAndGetChecksum(PaxMagicBytes, buffer.Slice(FieldLocations.Magic, FieldLengths.Magic));
checksum += WriteLeftAlignedBytesAndGetChecksum(PaxVersionBytes, buffer.Slice(FieldLocations.Version, FieldLengths.Version));
int checksum = WriteLeftAlignedBytesAndGetChecksum(UstarMagicBytes, buffer.Slice(FieldLocations.Magic, FieldLengths.Magic));
checksum += WriteLeftAlignedBytesAndGetChecksum(UstarVersionBytes, buffer.Slice(FieldLocations.Version, FieldLengths.Version));
return checksum;
}

Expand Down