Skip to content

Commit

Permalink
Use SR.Format instead of string.Format in Tar (#77528)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Oct 27, 2022
1 parent 0ba9b15 commit 060a196
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 50 deletions.
24 changes: 12 additions & 12 deletions src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void ExtractToFile(string destinationFileName, bool overwrite)
ArgumentException.ThrowIfNullOrEmpty(destinationFileName);
if (EntryType is TarEntryType.SymbolicLink or TarEntryType.HardLink or TarEntryType.GlobalExtendedAttributes)
{
throw new InvalidOperationException(string.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType));
throw new InvalidOperationException(SR.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType));
}
ExtractToFileInternal(destinationFileName, linkTargetPath: null, overwrite);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ public Task ExtractToFileAsync(string destinationFileName, bool overwrite, Cance
ArgumentException.ThrowIfNullOrEmpty(destinationFileName);
if (EntryType is TarEntryType.SymbolicLink or TarEntryType.HardLink or TarEntryType.GlobalExtendedAttributes)
{
return Task.FromException(new InvalidOperationException(string.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType)));
return Task.FromException(new InvalidOperationException(SR.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType)));
}
return ExtractToFileInternalAsync(destinationFileName, linkTargetPath: null, overwrite, cancellationToken);
}
Expand All @@ -254,7 +254,7 @@ public Stream? DataStream
{
if (!IsDataStreamSetterSupported())
{
throw new InvalidOperationException(string.Format(SR.TarEntryDoesNotSupportDataStream, Name, EntryType));
throw new InvalidOperationException(SR.Format(SR.TarEntryDoesNotSupportDataStream, Name, EntryType));
}

if (value != null && !value.CanRead)
Expand Down Expand Up @@ -338,7 +338,7 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b
string? fileDestinationPath = GetSanitizedFullPath(destinationDirectoryPath, Name);
if (fileDestinationPath == null)
{
throw new IOException(string.Format(SR.TarExtractingResultsFileOutside, Name, destinationDirectoryPath));
throw new IOException(SR.Format(SR.TarExtractingResultsFileOutside, Name, destinationDirectoryPath));
}

string? linkTargetPath = null;
Expand All @@ -352,7 +352,7 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b
linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, LinkName);
if (linkTargetPath == null)
{
throw new IOException(string.Format(SR.TarExtractingResultsLinkOutside, LinkName, destinationDirectoryPath));
throw new IOException(SR.Format(SR.TarExtractingResultsLinkOutside, LinkName, destinationDirectoryPath));
}
}

Expand Down Expand Up @@ -461,7 +461,7 @@ private void CreateNonRegularFile(string filePath, string? linkTargetPath)
case TarEntryType.SparseFile:
case TarEntryType.TapeVolume:
default:
throw new InvalidOperationException(string.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType));
throw new InvalidOperationException(SR.Format(SR.TarEntryTypeNotSupportedForExtracting, EntryType));
}
}

Expand All @@ -472,7 +472,7 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo
// If the destination contains a directory segment, need to check that it exists
if (!string.IsNullOrEmpty(directoryPath) && !Path.Exists(directoryPath))
{
throw new IOException(string.Format(SR.IO_PathNotFound_Path, filePath));
throw new IOException(SR.Format(SR.IO_PathNotFound_Path, filePath));
}

if (!Path.Exists(filePath))
Expand All @@ -483,13 +483,13 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo
// We never want to overwrite a directory, so we always throw
if (Directory.Exists(filePath))
{
throw new IOException(string.Format(SR.IO_AlreadyExists_Name, filePath));
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, filePath));
}

// A file exists at this point
if (!overwrite)
{
throw new IOException(string.Format(SR.IO_AlreadyExists_Name, filePath));
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, filePath));
}
File.Delete(filePath);

Expand All @@ -501,18 +501,18 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo
// If the destination target contains a directory segment, need to check that it exists
if (!string.IsNullOrEmpty(targetDirectoryPath) && !Path.Exists(targetDirectoryPath))
{
throw new IOException(string.Format(SR.TarSymbolicLinkTargetNotExists, filePath, linkTargetPath));
throw new IOException(SR.Format(SR.TarSymbolicLinkTargetNotExists, filePath, linkTargetPath));
}

if (EntryType is TarEntryType.HardLink)
{
if (!Path.Exists(linkTargetPath))
{
throw new IOException(string.Format(SR.TarHardLinkTargetNotExists, filePath, linkTargetPath));
throw new IOException(SR.Format(SR.TarHardLinkTargetNotExists, filePath, linkTargetPath));
}
else if (Directory.Exists(linkTargetPath))
{
throw new IOException(string.Format(SR.TarHardLinkToDirectoryNotAllowed, filePath, linkTargetPath));
throw new IOException(SR.Format(SR.TarHardLinkToDirectoryNotAllowed, filePath, linkTargetPath));
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void CreateFromDirectory(string sourceDirectoryName, Stream destin

if (!Directory.Exists(sourceDirectoryName))
{
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, sourceDirectoryName));
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceDirectoryName));
}

// Rely on Path.GetFullPath for validation of paths
Expand Down Expand Up @@ -79,7 +79,7 @@ public static Task CreateFromDirectoryAsync(string sourceDirectoryName, Stream d

if (!Directory.Exists(sourceDirectoryName))
{
return Task.FromException(new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, sourceDirectoryName)));
return Task.FromException(new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceDirectoryName)));
}

// Rely on Path.GetFullPath for validation of paths
Expand Down Expand Up @@ -109,7 +109,7 @@ public static void CreateFromDirectory(string sourceDirectoryName, string destin

if (!Directory.Exists(sourceDirectoryName))
{
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, sourceDirectoryName));
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceDirectoryName));
}

// Throws if the destination file exists
Expand Down Expand Up @@ -145,7 +145,7 @@ public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string d

if (!Directory.Exists(sourceDirectoryName))
{
return Task.FromException(new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, sourceDirectoryName)));
return Task.FromException(new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceDirectoryName)));
}

return CreateFromDirectoryInternalAsync(sourceDirectoryName, destinationFileName, includeBaseDirectory, cancellationToken);
Expand Down Expand Up @@ -180,7 +180,7 @@ public static void ExtractToDirectory(Stream source, string destinationDirectory

if (!Directory.Exists(destinationDirectoryName))
{
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, destinationDirectoryName));
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destinationDirectoryName));
}

// Rely on Path.GetFullPath for validation of paths
Expand Down Expand Up @@ -224,7 +224,7 @@ public static Task ExtractToDirectoryAsync(Stream source, string destinationDire

if (!Directory.Exists(destinationDirectoryName))
{
return Task.FromException(new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, destinationDirectoryName)));
return Task.FromException(new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destinationDirectoryName)));
}

// Rely on Path.GetFullPath for validation of paths
Expand Down Expand Up @@ -260,12 +260,12 @@ public static void ExtractToDirectory(string sourceFileName, string destinationD

if (!File.Exists(sourceFileName))
{
throw new FileNotFoundException(string.Format(SR.IO_FileNotFound_FileName, sourceFileName));
throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, sourceFileName));
}

if (!Directory.Exists(destinationDirectoryName))
{
throw new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, destinationDirectoryName));
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destinationDirectoryName));
}

using FileStream archive = File.OpenRead(sourceFileName);
Expand Down Expand Up @@ -306,12 +306,12 @@ public static Task ExtractToDirectoryAsync(string sourceFileName, string destina

if (!File.Exists(sourceFileName))
{
return Task.FromException(new FileNotFoundException(string.Format(SR.IO_FileNotFound_FileName, sourceFileName)));
return Task.FromException(new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, sourceFileName)));
}

if (!Directory.Exists(destinationDirectoryName))
{
return Task.FromException(new DirectoryNotFoundException(string.Format(SR.IO_PathNotFound_Path, destinationDirectoryName)));
return Task.FromException(new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destinationDirectoryName)));
}

return ExtractToDirectoryInternalAsync(sourceFileName, destinationDirectoryName, overwriteFiles, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace System.Formats.Tar
// Reads the header attributes from a tar archive entry.
internal sealed partial class TarHeader
{
private const string UstarPrefixFormat = "{0}/{1}"; // "prefix/name"

// Attempts to retrieve the next header from the specified tar archive stream.
// Throws if end of stream is reached or if any data type conversion fails.
// Returns a valid TarHeader object if the attributes were read successfully, null otherwise.
Expand Down Expand Up @@ -201,7 +199,7 @@ internal void ProcessDataBlock(Stream archiveStream, bool copyData)
// No data section
if (_size > 0)
{
throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
throw new InvalidDataException(SR.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
}
break;
case TarEntryType.RegularFile:
Expand Down Expand Up @@ -263,7 +261,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca
// No data section
if (_size > 0)
{
throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
throw new InvalidDataException(SR.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
}
break;
case TarEntryType.RegularFile:
Expand Down Expand Up @@ -380,7 +378,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca
Debug.Assert(size <= TarHelpers.MaxSizeLength, "size exceeded the max value possible with 11 octal digits. Actual size " + size);
if (size < 0)
{
throw new InvalidDataException(string.Format(SR.TarSizeFieldNegative));
throw new InvalidDataException(SR.Format(SR.TarSizeFieldNegative));
}

// Continue with the rest of the fields that require no special checks
Expand Down Expand Up @@ -414,7 +412,7 @@ TarEntryType.RenamedOrSymlinked or
// V7 is the only one that uses 'V7RegularFile'.
TarEntryType.V7RegularFile => TarEntryFormat.V7,

TarEntryType.SparseFile => throw new NotSupportedException(string.Format(SR.TarEntryTypeNotSupported, header._typeFlag)),
TarEntryType.SparseFile => throw new NotSupportedException(SR.Format(SR.TarEntryTypeNotSupported, header._typeFlag)),

// We can quickly determine the *minimum* possible format if the entry type
// is the POSIX 'RegularFile', although later we could upgrade it to PAX or GNU
Expand Down Expand Up @@ -478,7 +476,7 @@ private void ReadVersionAttribute(Span<byte> buffer)
// Check for gnu version header for mixed case
if (!version.SequenceEqual(GnuVersionBytes))
{
throw new InvalidDataException(string.Format(SR.TarPosixFormatExpected, _name));
throw new InvalidDataException(SR.Format(SR.TarPosixFormatExpected, _name));
}

_version = GnuVersion;
Expand All @@ -496,7 +494,7 @@ private void ReadVersionAttribute(Span<byte> buffer)
// Check for ustar or pax version header for mixed case
if (!version.SequenceEqual(UstarVersionBytes))
{
throw new InvalidDataException(string.Format(SR.TarGnuFormatExpected, _name));
throw new InvalidDataException(SR.Format(SR.TarGnuFormatExpected, _name));
}

_version = UstarVersion;
Expand Down Expand Up @@ -557,9 +555,9 @@ private void ReadUstarAttributes(Span<byte> buffer)
// Name, if the full path did not fit in the Name byte array.
if (!string.IsNullOrEmpty(_prefix))
{
// Prefix never has a leading separator, so we add it
// it should always be a forward slash for compatibility
_name = string.Format(UstarPrefixFormat, _prefix, _name);
// Prefix never has a leading separator, so we add it.
// It should always be a forward slash for compatibility
_name = $"{_prefix}/{_name}";
}
}

Expand Down Expand Up @@ -615,7 +613,7 @@ private void ValidateSize()

[DoesNotReturn]
void ThrowSizeFieldTooLarge() =>
throw new InvalidOperationException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag.ToString()));
throw new InvalidOperationException(SR.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag.ToString()));
}

// Returns a dictionary containing the extended attributes collected from the provided byte buffer.
Expand All @@ -627,7 +625,7 @@ private void ReadExtendedAttributesFromBuffer(ReadOnlySpan<byte> buffer, string
{
if (!ExtendedAttributes.TryAdd(key, value))
{
throw new InvalidDataException(string.Format(SR.TarDuplicateExtendedAttribute, name));
throw new InvalidDataException(SR.Format(SR.TarDuplicateExtendedAttribute, name));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ TarEntryType.RegularFile or

case TarEntryFormat.Unknown:
default:
throw new InvalidDataException(string.Format(SR.TarInvalidFormat, archiveFormat));
throw new InvalidDataException(SR.Format(SR.TarInvalidFormat, archiveFormat));
}

throw new ArgumentException(string.Format(SR.TarEntryTypeNotSupportedInFormat, entryType, archiveFormat), paramName);
throw new ArgumentException(SR.Format(SR.TarEntryTypeNotSupportedInFormat, entryType, archiveFormat), paramName);
}
}
}
Loading

0 comments on commit 060a196

Please sign in to comment.