Skip to content
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

[release/7.0] Change some exception types thrown in Tar APIs #74893

Merged
merged 17 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
17fdb6e
Throw ArgumentException on unsupported tar entry type
carlossanlop Aug 30, 2022
151797a
Adjust tests
carlossanlop Aug 30, 2022
6b42246
Also change exception type for internal TarEntry conversion construct…
carlossanlop Aug 30, 2022
7b8a909
LinkName setter null check.
carlossanlop Aug 31, 2022
61010d8
Internal constructors SeekableSubReadStream and SubReadStream unseeka…
carlossanlop Aug 31, 2022
f51806e
DataStream setter for regular file should throw ArgumentException if …
carlossanlop Aug 31, 2022
0f9d96c
TarFile CreateFromDirectory unwritable destination change exception t…
carlossanlop Aug 31, 2022
5faad2c
Change to ArgumentException when ExtractToDirectory is an unreadable …
carlossanlop Aug 31, 2022
6094b2f
Add some missing exception docs for TarEntry.
carlossanlop Aug 31, 2022
2775c07
Change TarReader constructor exception if unreadable stream. Close te…
carlossanlop Aug 31, 2022
b116660
Change TarWriter exception for unwritable stream to ArgumentException…
carlossanlop Aug 31, 2022
18292b7
Add missing documentation for exceptions in constructors.
carlossanlop Aug 31, 2022
2f615e9
Change wording of conversion constructors comment when passing a Pax …
carlossanlop Aug 31, 2022
74747ea
Apply suggestions by Jozkee
carlossanlop Aug 31, 2022
3927c72
Add exception to LinkName if the entry type is hard/symlink and the u…
carlossanlop Aug 31, 2022
dd15f10
Convert all FormatException to InvalidDataException
carlossanlop Aug 31, 2022
eb4aebf
Address more suggestions
carlossanlop Aug 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ internal GnuTarEntry(TarHeader header, TarReader readerOfOrigin)
/// </summary>
/// <param name="entryType">The type of the entry.</param>
/// <param name="entryName">A string with the path and file name of this entry.</param>
/// <exception cref="ArgumentException"><paramref name="entryName"/> is null or empty.</exception>
/// <exception cref="InvalidOperationException">The entry type is not supported for creating an entry.</exception>
/// <remarks>When creating an instance using the <see cref="GnuTarEntry(TarEntryType, string)"/> constructor, only the following entry types are supported:
/// <list type="bullet">
/// <item>In all platforms: <see cref="TarEntryType.Directory"/>, <see cref="TarEntryType.HardLink"/>, <see cref="TarEntryType.SymbolicLink"/>, <see cref="TarEntryType.RegularFile"/>.</item>
/// <item>In Unix platforms only: <see cref="TarEntryType.BlockDevice"/>, <see cref="TarEntryType.CharacterDevice"/> and <see cref="TarEntryType.Fifo"/>.</item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="entryName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><para><paramref name="entryName"/> is empty.</para>
/// <para>-or-</para>
/// <para><paramref name="entryType"/> is not supported in the specified format.</para></exception>
public GnuTarEntry(TarEntryType entryType, string entryName)
: base(entryType, entryName, TarEntryFormat.Gnu, isGea: false)
{
Expand All @@ -38,6 +40,9 @@ public GnuTarEntry(TarEntryType entryType, string entryName)
/// <summary>
/// Initializes a new <see cref="GnuTarEntry"/> instance by converting the specified <paramref name="other"/> entry into the GNU format.
/// </summary>
/// <exception cref="ArgumentException"><para><paramref name="other"/> is a <see cref="PaxGlobalExtendedAttributesTarEntry"/> and cannot be converted.</para>
/// <para>-or-</para>
/// <para>The entry type of <paramref name="other"/> is not supported for conversion to the GNU format.</para></exception>
public GnuTarEntry(TarEntry other)
: base(other, TarEntryFormat.Gnu)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ internal PaxTarEntry(TarHeader header, TarReader readerOfOrigin)
/// </summary>
/// <param name="entryType">The type of the entry.</param>
/// <param name="entryName">A string with the path and file name of this entry.</param>
/// <exception cref="ArgumentException"><paramref name="entryName"/> is null or empty.</exception>
/// <exception cref="InvalidOperationException">The entry type is not supported for creating an entry.</exception>
/// <remarks><para>When creating an instance using the <see cref="PaxTarEntry(TarEntryType, string)"/> constructor, only the following entry types are supported:</para>
/// <list type="bullet">
/// <item>In all platforms: <see cref="TarEntryType.Directory"/>, <see cref="TarEntryType.HardLink"/>, <see cref="TarEntryType.SymbolicLink"/>, <see cref="TarEntryType.RegularFile"/>.</item>
Expand All @@ -47,6 +45,10 @@ internal PaxTarEntry(TarHeader header, TarReader readerOfOrigin)
/// <item>File length, under the name <c>size</c>, as an <see cref="int"/>, if the string representation of the number is larger than 12 bytes.</item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="entryName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><para><paramref name="entryName"/> is empty.</para>
/// <para>-or-</para>
/// <para><paramref name="entryType"/> is not supported in the specified format.</para></exception>
public PaxTarEntry(TarEntryType entryType, string entryName)
: base(entryType, entryName, TarEntryFormat.Pax, isGea: false)
{
Expand All @@ -62,9 +64,6 @@ public PaxTarEntry(TarEntryType entryType, string entryName)
/// <param name="entryType">The type of the entry.</param>
/// <param name="entryName">A string with the path and file name of this entry.</param>
/// <param name="extendedAttributes">An enumeration of string key-value pairs that represents the metadata to include in the Extended Attributes entry that precedes the current entry.</param>
/// <exception cref="ArgumentNullException"><paramref name="extendedAttributes"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="entryName"/> is null or empty.</exception>
/// <exception cref="InvalidOperationException">The entry type is not supported for creating an entry.</exception>
/// <remarks>When creating an instance using the <see cref="PaxTarEntry(TarEntryType, string)"/> constructor, only the following entry types are supported:
/// <list type="bullet">
/// <item>In all platforms: <see cref="TarEntryType.Directory"/>, <see cref="TarEntryType.HardLink"/>, <see cref="TarEntryType.SymbolicLink"/>, <see cref="TarEntryType.RegularFile"/>.</item>
Expand All @@ -85,6 +84,10 @@ public PaxTarEntry(TarEntryType entryType, string entryName)
/// <item>File length, under the name <c>size</c>, as an <see cref="int"/>, if the string representation of the number is larger than 12 bytes.</item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="extendedAttributes"/> or <paramref name="entryName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><para><paramref name="entryName"/> is empty.</para>
/// <para>-or-</para>
/// <para><paramref name="entryType"/> is not supported in the specified format.</para></exception>
public PaxTarEntry(TarEntryType entryType, string entryName, IEnumerable<KeyValuePair<string, string>> extendedAttributes)
: base(entryType, entryName, TarEntryFormat.Pax, isGea: false)
{
Expand All @@ -100,6 +103,9 @@ public PaxTarEntry(TarEntryType entryType, string entryName, IEnumerable<KeyValu
/// <summary>
/// Initializes a new <see cref="PaxTarEntry"/> instance by converting the specified <paramref name="other"/> entry into the PAX format.
/// </summary>
/// <exception cref="ArgumentException"><para><paramref name="other"/> is a <see cref="PaxGlobalExtendedAttributesTarEntry"/> and cannot be converted.</para>
/// <para>-or-</para>
/// <para>The entry type of <paramref name="other"/> is not supported for conversion to the PAX format.</para></exception>
public PaxTarEntry(TarEntry other)
: base(other, TarEntryFormat.Pax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SeekableSubReadStream(Stream superStream, long startPosition, long maxLen
{
if (!superStream.CanSeek)
{
throw new InvalidOperationException(SR.IO_NotSupported_UnseekableStream);
throw new ArgumentException(SR.IO_NotSupported_UnseekableStream, nameof(superStream));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SubReadStream(Stream superStream, long startPosition, long maxLength)
{
if (!superStream.CanRead)
{
throw new InvalidOperationException(SR.IO_NotSupported_UnreadableStream);
throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(superStream));
}
_startInSuperStream = startPosition;
_positionInSuperStream = startPosition;
Expand Down
27 changes: 16 additions & 11 deletions src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ internal TarEntry(TarEntry other, TarEntryFormat format)
{
if (other is PaxGlobalExtendedAttributesTarEntry)
{
throw new InvalidOperationException(SR.TarCannotConvertPaxGlobalExtendedAttributesEntry);
throw new ArgumentException(SR.TarCannotConvertPaxGlobalExtendedAttributesEntry, nameof(other));
}

TarEntryType compatibleEntryType = TarHelpers.GetCorrectTypeFlagForFormat(format, other.EntryType);

TarHelpers.ThrowIfEntryTypeNotSupported(compatibleEntryType, format);
TarHelpers.ThrowIfEntryTypeNotSupported(compatibleEntryType, format, nameof(other));

_readerOfOrigin = other._readerOfOrigin;

Expand Down Expand Up @@ -92,6 +92,7 @@ public int Gid
/// A timestamps that represents the last time the contents of the file represented by this entry were modified.
/// </summary>
/// <remarks>In Unix platforms, this timestamp is commonly known as <c>mtime</c>.</remarks>
/// <exception cref="ArgumentOutOfRangeException">The specified value is larger than <see cref="DateTimeOffset.UnixEpoch"/>.</exception>
public DateTimeOffset ModificationTime
{
get => _header._mTime;
Expand All @@ -114,7 +115,9 @@ public DateTimeOffset ModificationTime
/// <summary>
/// When the <see cref="EntryType"/> indicates a <see cref="TarEntryType.SymbolicLink"/> or a <see cref="TarEntryType.HardLink"/>, this property returns the link target path of such link.
/// </summary>
/// <exception cref="InvalidOperationException">Cannot set the link name if the entry type is not <see cref="TarEntryType.HardLink"/> or <see cref="TarEntryType.SymbolicLink"/>.</exception>
/// <exception cref="InvalidOperationException">The entry type is not <see cref="TarEntryType.HardLink"/> or <see cref="TarEntryType.SymbolicLink"/>.</exception>
/// <exception cref="ArgumentNullException">The specified value is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">The specified value is empty.</exception>
public string LinkName
{
get => _header._linkName ?? string.Empty;
Expand All @@ -124,6 +127,7 @@ public string LinkName
{
throw new InvalidOperationException(SR.TarEntryHardLinkOrSymLinkExpected);
}
ArgumentException.ThrowIfNullOrEmpty(value);
_header._linkName = value;
}
}
Expand Down Expand Up @@ -177,7 +181,8 @@ public int Uid
/// <para>Elevation is required to extract a <see cref="TarEntryType.BlockDevice"/> or <see cref="TarEntryType.CharacterDevice"/> to disk.</para>
/// <para>Symbolic links can be recreated using <see cref="File.CreateSymbolicLink(string, string)"/>, <see cref="Directory.CreateSymbolicLink(string, string)"/> or <see cref="FileSystemInfo.CreateAsSymbolicLink(string)"/>.</para>
/// <para>Hard links can only be extracted when using <see cref="TarFile.ExtractToDirectory(Stream, string, bool)"/> or <see cref="TarFile.ExtractToDirectory(string, string, bool)"/>.</para></remarks>
/// <exception cref="ArgumentException"><paramref name="destinationFileName"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="destinationFileName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="destinationFileName"/> is empty.</exception>
/// <exception cref="IOException"><para>The parent directory of <paramref name="destinationFileName"/> does not exist.</para>
/// <para>-or-</para>
/// <para><paramref name="overwrite"/> is <see langword="false"/> and a file already exists in <paramref name="destinationFileName"/>.</para>
Expand Down Expand Up @@ -206,7 +211,8 @@ public void ExtractToFile(string destinationFileName, bool overwrite)
/// <returns>A task that represents the asynchronous extraction operation.</returns>
/// <remarks><para>Files of type <see cref="TarEntryType.BlockDevice"/>, <see cref="TarEntryType.CharacterDevice"/> or <see cref="TarEntryType.Fifo"/> can only be extracted in Unix platforms.</para>
/// <para>Elevation is required to extract a <see cref="TarEntryType.BlockDevice"/> or <see cref="TarEntryType.CharacterDevice"/> to disk.</para></remarks>
/// <exception cref="ArgumentException"><paramref name="destinationFileName"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentNullException"><paramref name="destinationFileName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="destinationFileName"/> is empty.</exception>
/// <exception cref="IOException"><para>The parent directory of <paramref name="destinationFileName"/> does not exist.</para>
/// <para>-or-</para>
/// <para><paramref name="overwrite"/> is <see langword="false"/> and a file already exists in <paramref name="destinationFileName"/>.</para>
Expand Down Expand Up @@ -237,9 +243,8 @@ public Task ExtractToFileAsync(string destinationFileName, bool overwrite, Cance
/// <para>Sets a new stream that represents the data section, if it makes sense for the <see cref="EntryType"/> to contain data; if a stream already existed, the old stream gets disposed before substituting it with the new stream. Setting a <see langword="null"/> stream is allowed.</para></value>
/// <remarks>If you write data to this data stream, make sure to rewind it to the desired start position before writing this entry into an archive using <see cref="TarWriter.WriteEntry(TarEntry)"/>.</remarks>
/// <exception cref="InvalidOperationException">Setting a data section is not supported because the <see cref="EntryType"/> is not <see cref="TarEntryType.RegularFile"/> (or <see cref="TarEntryType.V7RegularFile"/> for an archive of <see cref="TarEntryFormat.V7"/> format).</exception>
/// <exception cref="IOException"><para>Cannot set an unreadable stream.</para>
/// <para>-or-</para>
/// <para>An I/O problem occurred.</para></exception>
/// <exception cref="ArgumentException">Cannot set an unreadable stream.</exception>
/// <exception cref="IOException">An I/O problem occurred.</exception>
public Stream? DataStream
{
get => _header._dataStream;
Expand All @@ -252,7 +257,7 @@ public Stream? DataStream

if (value != null && !value.CanRead)
{
throw new IOException(SR.IO_NotSupported_UnreadableStream);
throw new ArgumentException(SR.IO_NotSupported_UnreadableStream, nameof(value));
}

if (_readerOfOrigin != null)
Expand Down Expand Up @@ -339,7 +344,7 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b
{
if (string.IsNullOrEmpty(LinkName))
{
throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
}

linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, LinkName);
Expand Down Expand Up @@ -511,7 +516,7 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo
}
else
{
throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
}
}
}
Expand Down
Loading