Skip to content

Commit 72fcf2e

Browse files
[release/7.0] backport Tar fixes (#76322)
* Use UTF8 encoding on Tar string fields * Slice destination on Checksum * Use Encoding.GetByteCount as fast path * Use escape sequences on hardcoded UTF8 characters * Fix ustar prefix logic and throw if name would be truncated * Address feedback * Fix truncation and prefix logic * Fix nits * Add async tests * Add tests for unseekable streams * Address feedback * Tar: Use indexer setter instead of Add on ExtendedAttributes dictionary (#76404) * Use indexer setter instead of Add on ExtendedAttributes dictionary * Add roundtrip tests * Fix TryAddStringField and always set mtime Co-authored-by: David Cantu <[email protected]>
1 parent 6bcb45f commit 72fcf2e

13 files changed

+1092
-103
lines changed

src/libraries/Common/src/System/IO/PathInternal.Unix.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static partial class PathInternal
1717
internal const string DirectorySeparatorCharAsString = "/";
1818
internal const string ParentDirectoryPrefix = @"../";
1919
internal const string DirectorySeparators = DirectorySeparatorCharAsString;
20+
internal static ReadOnlySpan<byte> Utf8DirectorySeparators => "/"u8;
2021

2122
internal static int GetRootLength(ReadOnlySpan<char> path)
2223
{

src/libraries/Common/src/System/IO/PathInternal.Windows.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ internal static partial class PathInternal
5555
internal const string DevicePathPrefix = @"\\.\";
5656
internal const string ParentDirectoryPrefix = @"..\";
5757
internal const string DirectorySeparators = @"\/";
58+
internal static ReadOnlySpan<byte> Utf8DirectorySeparators => @"\/"u8;
5859

5960
internal const int MaxShortPath = 260;
6061
internal const int MaxShortDirectoryPath = 248;

src/libraries/System.Formats.Tar/src/Resources/Strings.resx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
3+
<!--
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -261,4 +261,7 @@
261261
<data name="TarInvalidNumber" xml:space="preserve">
262262
<value>Unable to parse number.</value>
263263
</data>
264-
</root>
264+
<data name="TarEntryFieldExceedsMaxLength" xml:space="preserve">
265+
<value>The field '{0}' exceeds the maximum allowed length for this format.</value>
266+
</data>
267+
</root>

src/libraries/System.Formats.Tar/src/System.Formats.Tar.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.MkFifo.cs" Link="Common\Interop\Unix\System.Native\Interop.MkFifo.cs" />
6666
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Stat.cs" Link="Common\Interop\Unix\Interop.Stat.cs" />
6767
<Compile Include="$(CommonPath)System\IO\Archiving.Utils.Unix.cs" Link="Common\System\IO\Archiving.Utils.Unix.cs" />
68+
<Compile Include="$(CommonPath)System\IO\PathInternal.Unix.cs" Link="Common\System\IO\PathInternal.Unix.cs" />
6869
</ItemGroup>
6970
<ItemGroup>
7071
<Reference Include="System.Collections" />

src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ private void ReadVersionAttribute(Span<byte> buffer)
517517
private void ReadPosixAndGnuSharedAttributes(Span<byte> buffer)
518518
{
519519
// Convert the byte arrays
520-
_uName = TarHelpers.GetTrimmedAsciiString(buffer.Slice(FieldLocations.UName, FieldLengths.UName));
521-
_gName = TarHelpers.GetTrimmedAsciiString(buffer.Slice(FieldLocations.GName, FieldLengths.GName));
520+
_uName = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.UName, FieldLengths.UName));
521+
_gName = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.GName, FieldLengths.GName));
522522

523523
// DevMajor and DevMinor only have values with character devices and block devices.
524524
// For all other typeflags, the values in these fields are irrelevant.

0 commit comments

Comments
 (0)