Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal TarEntry() { }
public int Checksum { get { throw null; } }
public System.IO.Stream? DataStream { get { throw null; } set { } }
public System.Formats.Tar.TarEntryType EntryType { get { throw null; } }
public System.Formats.Tar.TarFormat Format { get { throw null; } }
public int Gid { get { throw null; } set { } }
public long Length { get { throw null; } }
public string LinkName { get { throw null; } set { } }
Expand Down Expand Up @@ -98,7 +99,6 @@ public enum TarFormat
public sealed partial class TarReader : System.IDisposable
{
public TarReader(System.IO.Stream archiveStream, bool leaveOpen = false) { }
public System.Formats.Tar.TarFormat Format { get { throw null; } }
public System.Collections.Generic.IReadOnlyDictionary<string, string>? GlobalExtendedAttributes { get { throw null; } }
public void Dispose() { }
public System.Formats.Tar.TarEntry? GetNextEntry(bool copyData = false) { throw null; }
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Formats.Tar/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@
<data name="TarDuplicateExtendedAttribute" xml:space="preserve">
<value>The entry '{0}' has a duplicate extended attribute.</value>
</data>
<data name="TarEntriesInDifferentFormats" xml:space="preserve">
<value>An entry in '{0}' format was found in an archive where other entries of format '{1}' have been found.</value>
</data>
<data name="TarEntryBlockOrCharacterExpected" xml:space="preserve">
<value>Cannot set the 'DeviceMajor' or 'DeviceMinor' fields on an entry that does not represent a block or character device.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal TarEntry(TarEntryType entryType, string entryName, TarFormat format)
_readerOfOrigin = null;

_header = default;
_header._format = format;

_header._extendedAttributes = new Dictionary<string, string>();

Expand Down Expand Up @@ -63,6 +64,10 @@ internal TarEntry(TarEntryType entryType, string entryName, TarFormat format)
/// </summary>
public TarEntryType EntryType => _header._typeFlag;

/// <summary>
/// The format of the entry.
/// </summary>
public TarFormat Format => _header._format;
/// <summary>
/// The ID of the group that owns the file represented by this entry.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal partial struct TarHeader
internal static void WriteGlobalExtendedAttributesHeader(Stream archiveStream, Span<byte> buffer, IEnumerable<KeyValuePair<string, string>> globalExtendedAttributes)
{
TarHeader geaHeader = default;
geaHeader._format = TarFormat.Pax;
geaHeader._name = GenerateGlobalExtendedAttributeName();
geaHeader._mode = (int)TarHelpers.DefaultMode;
geaHeader._typeFlag = TarEntryType.GlobalExtendedAttributes;
Expand Down Expand Up @@ -88,6 +89,7 @@ internal void WriteAsPax(Stream archiveStream, Span<byte> buffer)
{
// First, we write the preceding extended attributes header
TarHeader extendedAttributesHeader = default;
extendedAttributesHeader._format = TarFormat.Pax;
// Fill the current header's dict
CollectExtendedAttributesFromStandardFieldsIfNeeded();
// And pass them to the extended attributes header for writing
Expand Down Expand Up @@ -129,6 +131,7 @@ private static TarHeader GetGnuLongMetadataHeader(TarEntryType entryType, string
(entryType is TarEntryType.LongLink && longText.Length > FieldLengths.LinkName));

TarHeader longMetadataHeader = default;
longMetadataHeader._format = TarFormat.Gnu;

longMetadataHeader._name = GnuLongMetadataName; // Same name for both longpath or longlink
longMetadataHeader._mode = (int)TarHelpers.DefaultMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ public TarReader(Stream archiveStream, bool leaveOpen = false)

_previouslyReadEntry = null;
GlobalExtendedAttributes = null;
Format = TarFormat.Unknown;
_isDisposed = false;
_readFirstEntry = false;
_reachedEndMarkers = false;
}

/// <summary>
/// The format of the archive. It is initially <see cref="TarFormat.Unknown"/>. The archive format is detected after the first call to <see cref="GetNextEntry(bool)"/>.
/// </summary>
public TarFormat Format { get; private set; }

/// <summary>
/// <para>If the archive format is <see cref="TarFormat.Pax"/>, returns a read-only dictionary containing the string key-value pairs of the Global Extended Attributes in the first entry of the archive.</para>
/// <para>If there is no Global Extended Attributes entry at the beginning of the archive, this returns an empty read-only dictionary.</para>
Expand Down Expand Up @@ -115,16 +109,10 @@ public void Dispose()
{
if (!_readFirstEntry)
{
Debug.Assert(Format == TarFormat.Unknown);
Format = header._format;
_readFirstEntry = true;
}
else if (header._format != Format)
{
throw new FormatException(string.Format(SR.TarEntriesInDifferentFormats, header._format, Format));
}

TarEntry entry = Format switch
TarEntry entry = header._format switch
{
TarFormat.Pax => new PaxTarEntry(header, this),
TarFormat.Gnu => new GnuTarEntry(header, this),
Expand Down Expand Up @@ -227,11 +215,7 @@ private bool TryGetNextEntryHeader(out TarHeader header, bool copyData)
Debug.Assert(!_reachedEndMarkers);

header = default;

// Set the initial format that is expected to be retrieved when calling TarHeader.TryReadAttributes.
// If the archive format is set to unknown here, it means this is the first entry we read and the value will be changed as fields get discovered.
// If the archive format is initially detected as pax, then any subsequent entries detected as ustar will be assumed to be pax.
header._format = Format;
header._format = TarFormat.Unknown;

if (!header.TryGetNextHeader(_archiveStream, copyData))
{
Expand Down Expand Up @@ -261,7 +245,6 @@ private bool TryGetNextEntryHeader(out TarHeader header, bool copyData)
catch (EndOfStreamException)
{
// Edge case: The only entry in the archive was a Global Extended Attributes entry
Format = TarFormat.Pax;
return false;
}
if (header._typeFlag == TarEntryType.GlobalExtendedAttributes)
Expand Down Expand Up @@ -344,6 +327,7 @@ private bool TryProcessExtendedAttributesHeader(TarHeader firstHeader, bool copy
private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out TarHeader finalHeader)
{
finalHeader = default;
finalHeader._format = TarFormat.Gnu;

TarHeader secondHeader = default;
secondHeader._format = TarFormat.Gnu;
Expand Down
Loading