diff --git a/src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs b/src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs index 642433227b1cd2..2ee80959376357 100644 --- a/src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs +++ b/src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs @@ -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 { } } @@ -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? GlobalExtendedAttributes { get { throw null; } } public void Dispose() { } public System.Formats.Tar.TarEntry? GetNextEntry(bool copyData = false) { throw null; } diff --git a/src/libraries/System.Formats.Tar/src/Resources/Strings.resx b/src/libraries/System.Formats.Tar/src/Resources/Strings.resx index 3140ec6831a833..879e4d9f511c9f 100644 --- a/src/libraries/System.Formats.Tar/src/Resources/Strings.resx +++ b/src/libraries/System.Formats.Tar/src/Resources/Strings.resx @@ -189,9 +189,6 @@ The entry '{0}' has a duplicate extended attribute. - - An entry in '{0}' format was found in an archive where other entries of format '{1}' have been found. - Cannot set the 'DeviceMajor' or 'DeviceMinor' fields on an entry that does not represent a block or character device. diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs index 0800f39ecf0285..1f06a29b1c89a5 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs @@ -36,6 +36,7 @@ internal TarEntry(TarEntryType entryType, string entryName, TarFormat format) _readerOfOrigin = null; _header = default; + _header._format = format; _header._extendedAttributes = new Dictionary(); @@ -63,6 +64,10 @@ internal TarEntry(TarEntryType entryType, string entryName, TarFormat format) /// public TarEntryType EntryType => _header._typeFlag; + /// + /// The format of the entry. + /// + public TarFormat Format => _header._format; /// /// The ID of the group that owns the file represented by this entry. /// diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs index 0c2cf88c9c7e87..d182a2f0c43694 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs @@ -33,6 +33,7 @@ internal partial struct TarHeader internal static void WriteGlobalExtendedAttributesHeader(Stream archiveStream, Span buffer, IEnumerable> globalExtendedAttributes) { TarHeader geaHeader = default; + geaHeader._format = TarFormat.Pax; geaHeader._name = GenerateGlobalExtendedAttributeName(); geaHeader._mode = (int)TarHelpers.DefaultMode; geaHeader._typeFlag = TarEntryType.GlobalExtendedAttributes; @@ -88,6 +89,7 @@ internal void WriteAsPax(Stream archiveStream, Span 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 @@ -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; diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs index 2aa5b3875d848e..4a5e09378a45ef 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs @@ -41,17 +41,11 @@ public TarReader(Stream archiveStream, bool leaveOpen = false) _previouslyReadEntry = null; GlobalExtendedAttributes = null; - Format = TarFormat.Unknown; _isDisposed = false; _readFirstEntry = false; _reachedEndMarkers = false; } - /// - /// The format of the archive. It is initially . The archive format is detected after the first call to . - /// - public TarFormat Format { get; private set; } - /// /// If the archive format is , returns a read-only dictionary containing the string key-value pairs of the Global Extended Attributes in the first entry of the archive. /// If there is no Global Extended Attributes entry at the beginning of the archive, this returns an empty read-only dictionary. @@ -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), @@ -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)) { @@ -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) @@ -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; diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs index fa9ea86ed9d43f..cd6d6e4b835e15 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs @@ -31,11 +31,8 @@ public void Read_Archive_File(TarFormat format, TestTarFormat testFormat) Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry file = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -68,11 +65,8 @@ public void Read_Archive_File_HardLink(TarFormat format, TestTarFormat testForma // The GEA are collected after reading the first entry, not on the constructor Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry file = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -110,11 +104,8 @@ public void Read_Archive_File_SymbolicLink(TarFormat format, TestTarFormat testF Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry file = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -127,7 +118,7 @@ public void Read_Archive_File_SymbolicLink(TarFormat format, TestTarFormat testF Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "file.txt", $"Hello {testCaseName}"); TarEntry symbolicLink = reader.GetNextEntry(); - Verify_Archive_SymbolicLink(symbolicLink, reader.GlobalExtendedAttributes, "link.txt", "file.txt"); + Verify_Archive_SymbolicLink(symbolicLink, format, reader.GlobalExtendedAttributes, "link.txt", "file.txt"); Assert.Null(reader.GetNextEntry()); } @@ -152,11 +143,8 @@ public void Read_Archive_Folder_File(TarFormat format, TestTarFormat testFormat) Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry directory = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -165,7 +153,7 @@ public void Read_Archive_Folder_File(TarFormat format, TestTarFormat testFormat) Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(directory, reader.GlobalExtendedAttributes, "folder/"); + Verify_Archive_Directory(directory, format, reader.GlobalExtendedAttributes, "folder/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "folder/file.txt", $"Hello {testCaseName}"); @@ -192,11 +180,8 @@ public void Read_Archive_Folder_File_Utf8(TarFormat format, TestTarFormat testFo Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry directory = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -205,7 +190,7 @@ public void Read_Archive_Folder_File_Utf8(TarFormat format, TestTarFormat testFo Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(directory, reader.GlobalExtendedAttributes, "földër/"); + Verify_Archive_Directory(directory, format, reader.GlobalExtendedAttributes, "földër/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "földër/áöñ.txt", $"Hello {testCaseName}"); @@ -232,11 +217,8 @@ public void Read_Archive_Folder_Subfolder_File(TarFormat format, TestTarFormat t Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry parent = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -245,10 +227,10 @@ public void Read_Archive_Folder_Subfolder_File(TarFormat format, TestTarFormat t Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(parent, reader.GlobalExtendedAttributes, "parent/"); + Verify_Archive_Directory(parent, format, reader.GlobalExtendedAttributes, "parent/"); TarEntry child = reader.GetNextEntry(); - Verify_Archive_Directory(child, reader.GlobalExtendedAttributes, "parent/child/"); + Verify_Archive_Directory(child, format, reader.GlobalExtendedAttributes, "parent/child/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "parent/child/file.txt", $"Hello {testCaseName}"); @@ -275,11 +257,8 @@ public void Read_Archive_FolderSymbolicLink_Folder_Subfolder_File(TarFormat form Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry childlink = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -288,13 +267,13 @@ public void Read_Archive_FolderSymbolicLink_Folder_Subfolder_File(TarFormat form Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_SymbolicLink(childlink, reader.GlobalExtendedAttributes, "childlink", "parent/child"); + Verify_Archive_SymbolicLink(childlink, format, reader.GlobalExtendedAttributes, "childlink", "parent/child"); TarEntry parent = reader.GetNextEntry(); - Verify_Archive_Directory(parent, reader.GlobalExtendedAttributes, "parent/"); + Verify_Archive_Directory(parent, format, reader.GlobalExtendedAttributes, "parent/"); TarEntry child = reader.GetNextEntry(); - Verify_Archive_Directory(child, reader.GlobalExtendedAttributes, "parent/child/"); + Verify_Archive_Directory(child, format, reader.GlobalExtendedAttributes, "parent/child/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "parent/child/file.txt", $"Hello {testCaseName}"); @@ -321,17 +300,14 @@ public void Read_Archive_Many_Small_Files(TarFormat format, TestTarFormat testFo Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); - List entries = new List(); TarEntry entry; bool isFirstEntry = true; while ((entry = reader.GetNextEntry()) != null) { + Assert.Equal(format, entry.Format); if (isFirstEntry) { - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -375,11 +351,8 @@ public void Read_Archive_LongPath_Splitable_Under255(TarFormat format, TestTarFo Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry directory = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -388,7 +361,7 @@ public void Read_Archive_LongPath_Splitable_Under255(TarFormat format, TestTarFo Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(directory, reader.GlobalExtendedAttributes, "00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999/"); + Verify_Archive_Directory(directory, format, reader.GlobalExtendedAttributes, "00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, $"00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999/00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999.txt", $"Hello {testCaseName}"); @@ -415,11 +388,8 @@ public void Read_Archive_SpecialFiles(TarFormat format, TestTarFormat testFormat Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); PosixTarEntry blockDevice = reader.GetNextEntry() as PosixTarEntry; - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -428,13 +398,13 @@ public void Read_Archive_SpecialFiles(TarFormat format, TestTarFormat testFormat Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_BlockDevice(blockDevice, reader.GlobalExtendedAttributes, AssetBlockDeviceFileName); + Verify_Archive_BlockDevice(blockDevice, format, reader.GlobalExtendedAttributes, AssetBlockDeviceFileName); PosixTarEntry characterDevice = reader.GetNextEntry() as PosixTarEntry; - Verify_Archive_CharacterDevice(characterDevice, reader.GlobalExtendedAttributes, AssetCharacterDeviceFileName); + Verify_Archive_CharacterDevice(characterDevice, format, reader.GlobalExtendedAttributes, AssetCharacterDeviceFileName); PosixTarEntry fifo = reader.GetNextEntry() as PosixTarEntry; - Verify_Archive_Fifo(fifo, reader.GlobalExtendedAttributes, "fifofile"); + Verify_Archive_Fifo(fifo, format, reader.GlobalExtendedAttributes, "fifofile"); Assert.Null(reader.GetNextEntry()); } @@ -457,11 +427,8 @@ public void Read_Archive_File_LongSymbolicLink(TarFormat format, TestTarFormat t Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry directory = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -470,13 +437,13 @@ public void Read_Archive_File_LongSymbolicLink(TarFormat format, TestTarFormat t Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(directory, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/"); + Verify_Archive_Directory(directory, format, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222333333333344444444445.txt", $"Hello {testCaseName}"); TarEntry symbolicLink = reader.GetNextEntry(); - Verify_Archive_SymbolicLink(symbolicLink, reader.GlobalExtendedAttributes, "link.txt", "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222333333333344444444445.txt"); + Verify_Archive_SymbolicLink(symbolicLink, format, reader.GlobalExtendedAttributes, "link.txt", "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222333333333344444444445.txt"); Assert.Null(reader.GetNextEntry()); } @@ -499,11 +466,8 @@ public void Read_Archive_LongFileName_Over100_Under255(TarFormat format, TestTar Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry file = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -535,11 +499,8 @@ public void Read_Archive_LongPath_Over255(TarFormat format, TestTarFormat testFo Assert.Null(reader.GlobalExtendedAttributes); } - // Format is determined after reading the first entry, not on the constructor - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry directory = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); if (testFormat == TestTarFormat.pax_gea) { Assert.NotNull(reader.GlobalExtendedAttributes); @@ -548,7 +509,7 @@ public void Read_Archive_LongPath_Over255(TarFormat format, TestTarFormat testFo Assert.Equal(AssetPaxGeaValue, reader.GlobalExtendedAttributes[AssetPaxGeaKey]); } - Verify_Archive_Directory(directory, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/"); + Verify_Archive_Directory(directory, format, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/"); TarEntry file = reader.GetNextEntry(); Verify_Archive_RegularFile(file, format, reader.GlobalExtendedAttributes, "000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555/00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222333333333344444444445.txt", $"Hello {testCaseName}"); @@ -559,6 +520,7 @@ public void Read_Archive_LongPath_Over255(TarFormat format, TestTarFormat testFo private void Verify_Archive_RegularFile(TarEntry file, TarFormat format, IReadOnlyDictionary gea, string expectedFileName, string expectedContents) { Assert.NotNull(file); + Assert.Equal(format, file.Format); Assert.True(file.Checksum > 0); Assert.NotNull(file.DataStream); @@ -626,9 +588,10 @@ private void VerifyAssetExtendedAttributes(PaxTarEntry pax, IReadOnlyDictionary< Assert.True(ctimeSecondsSinceEpoch > 0); } - private void Verify_Archive_SymbolicLink(TarEntry symbolicLink, IReadOnlyDictionary gea, string expectedFileName, string expectedTargetName) + private void Verify_Archive_SymbolicLink(TarEntry symbolicLink, TarFormat format, IReadOnlyDictionary gea, string expectedFileName, string expectedTargetName) { Assert.NotNull(symbolicLink); + Assert.Equal(format, symbolicLink.Format); Assert.True(symbolicLink.Checksum > 0); Assert.Null(symbolicLink.DataStream); @@ -662,9 +625,10 @@ private void Verify_Archive_SymbolicLink(TarEntry symbolicLink, IReadOnlyDiction } } - private void Verify_Archive_Directory(TarEntry directory, IReadOnlyDictionary gea, string expectedFileName) + private void Verify_Archive_Directory(TarEntry directory, TarFormat format, IReadOnlyDictionary gea, string expectedFileName) { Assert.NotNull(directory); + Assert.Equal(format, directory.Format); Assert.True(directory.Checksum > 0); Assert.Null(directory.DataStream); @@ -698,10 +662,11 @@ private void Verify_Archive_Directory(TarEntry directory, IReadOnlyDictionary gea, string expectedFileName) + private void Verify_Archive_BlockDevice(PosixTarEntry blockDevice, TarFormat format, IReadOnlyDictionary gea, string expectedFileName) { Assert.NotNull(blockDevice); Assert.Equal(TarEntryType.BlockDevice, blockDevice.EntryType); + Assert.Equal(format, blockDevice.Format); Assert.True(blockDevice.Checksum > 0); Assert.Null(blockDevice.DataStream); @@ -734,10 +699,11 @@ private void Verify_Archive_BlockDevice(PosixTarEntry blockDevice, IReadOnlyDict } } - private void Verify_Archive_CharacterDevice(PosixTarEntry characterDevice, IReadOnlyDictionary gea, string expectedFileName) + private void Verify_Archive_CharacterDevice(PosixTarEntry characterDevice, TarFormat format, IReadOnlyDictionary gea, string expectedFileName) { Assert.NotNull(characterDevice); Assert.Equal(TarEntryType.CharacterDevice, characterDevice.EntryType); + Assert.Equal(format, characterDevice.Format); Assert.True(characterDevice.Checksum > 0); Assert.Null(characterDevice.DataStream); @@ -770,9 +736,10 @@ private void Verify_Archive_CharacterDevice(PosixTarEntry characterDevice, IRead } } - private void Verify_Archive_Fifo(PosixTarEntry fifo, IReadOnlyDictionary gea, string expectedFileName) + private void Verify_Archive_Fifo(PosixTarEntry fifo, TarFormat format, IReadOnlyDictionary gea, string expectedFileName) { Assert.NotNull(fifo); + Assert.Equal(format, fifo.Format); Assert.True(fifo.Checksum > 0); Assert.Null(fifo.DataStream); diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.Unix.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.Unix.cs index 2c57ef21f9b162..8c600f8ba7d8fa 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.Unix.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.Unix.cs @@ -36,9 +36,8 @@ public void Add_Fifo(TarFormat format) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); PosixTarEntry entry = reader.GetNextEntry() as PosixTarEntry; - Assert.Equal(expectedFormat, reader.Format); + Assert.Equal(expectedFormat, entry.Format); Assert.NotNull(entry); Assert.Equal(fifoName, entry.Name); @@ -79,9 +78,8 @@ public void Add_BlockDevice(TarFormat format) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); PosixTarEntry entry = reader.GetNextEntry() as PosixTarEntry; - Assert.Equal(expectedFormat, reader.Format); + Assert.Equal(expectedFormat, entry.Format); Assert.NotNull(entry); Assert.Equal(AssetBlockDeviceFileName, entry.Name); @@ -126,9 +124,8 @@ public void Add_CharacterDevice(TarFormat format) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); PosixTarEntry entry = reader.GetNextEntry() as PosixTarEntry; - Assert.Equal(expectedFormat, reader.Format); + Assert.Equal(expectedFormat, entry.Format); Assert.NotNull(entry); Assert.Equal(AssetCharacterDeviceFileName, entry.Name); diff --git a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs index 298509f1c92a48..894fae8b23c290 100644 --- a/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.File.Tests.cs @@ -91,10 +91,9 @@ public void Add_File(TarFormat format) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry entry = reader.GetNextEntry(); Assert.NotNull(entry); - Assert.Equal(format, reader.Format); + Assert.Equal(format, entry.Format); Assert.Equal(fileName, entry.Name); TarEntryType expectedEntryType = format is TarFormat.V7 ? TarEntryType.V7RegularFile : TarEntryType.RegularFile; Assert.Equal(expectedEntryType, entry.EntryType); @@ -145,9 +144,8 @@ public void Add_Directory(TarFormat format, bool withContents) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry entry = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); + Assert.Equal(format, entry.Format); Assert.NotNull(entry); Assert.Equal(dirName, entry.Name); @@ -194,9 +192,8 @@ public void Add_SymbolicLink(TarFormat format, bool createTarget) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - Assert.Equal(TarFormat.Unknown, reader.Format); TarEntry entry = reader.GetNextEntry(); - Assert.Equal(format, reader.Format); + Assert.Equal(format, entry.Format); Assert.NotNull(entry); Assert.Equal(linkName, entry.Name); @@ -231,13 +228,10 @@ public void Add_PaxGlobalExtendedAttributes_NoEntries(bool withAttributes) archive.Seek(0, SeekOrigin.Begin); using (TarReader reader = new TarReader(archive)) { - // Unknown until reading first entry - Assert.Equal(TarFormat.Unknown, reader.Format); Assert.Null(reader.GlobalExtendedAttributes); Assert.Null(reader.GetNextEntry()); - Assert.Equal(TarFormat.Pax, reader.Format); Assert.NotNull(reader.GlobalExtendedAttributes); int expectedCount = withAttributes ? 1 : 0;