Skip to content

Commit

Permalink
PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Jul 29, 2022
1 parent 2440cc3 commit 9a9f251
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ private static UnixFileMode DetermineUMask()
private sealed class ReverseStringComparer : IComparer<string>
{
public int Compare (string? x, string? y)
=> StringComparer.InvariantCulture.Compare(y, x);
=> StringComparer.Ordinal.Compare(y, x);
}

private static readonly ReverseStringComparer s_reverseStringComparer = new();

internal static UnixFileMode UMask => s_umask.Value;
private static UnixFileMode UMask => s_umask.Value;

/*
Tar files are usually ordered: parent directories come before their child entries.
Expand All @@ -74,7 +74,6 @@ internal static void CreateDirectory(string fullPath, UnixFileMode? mode, bool o
// Restrictive mask for creating the missing parent directories while extracting.
const UnixFileMode ExtractPermissions = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute;

Debug.Assert(!OperatingSystem.IsWindows());
Debug.Assert(pendingModes is not null);

if (Directory.Exists(fullPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static partial class TarHelpers
internal static SortedDictionary<string, UnixFileMode>? CreatePendingModesDictionary()
=> null;

internal static void CreateDirectory(string fullPath, UnixFileMode? mode, SortedDictionary<string, UnixFileMode>? pendingModes)
internal static void CreateDirectory(string fullPath, UnixFileMode? mode, bool overwriteMetadata, SortedDictionary<string, UnixFileMode>? pendingModes)
=> Directory.CreateDirectory(fullPath);

internal static void SetPendingModes(SortedDictionary<string, UnixFileMode>? pendingModes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,17 @@ public void ExtractToFile_Link_Throws(TarEntryFormat format, TarEntryType entryT
Assert.Equal(0, Directory.GetFileSystemEntries(root.Path).Count());
}

public static IEnumerable<object[]> GetFormatsAndFiles()
{
foreach (TarEntryFormat format in new[] { TarEntryFormat.Ustar, TarEntryFormat.Pax, TarEntryFormat.Gnu })
{
foreach (TarEntryType entryType in new[] { TarEntryType.RegularFile, TarEntryType.Directory })
{
yield return new object[] { format, entryType };
}
}
}

[Theory]
[MemberData(nameof(GetFormatsAndFiles))]
public void Extract(TarEntryFormat format, TarEntryType entryType)
{
using TempDirectory root = new TempDirectory();

(string entryName, string destination, PosixTarEntry entry) = Prepare_Extract(root, format, entryType);
(string entryName, string destination, TarEntry entry) = Prepare_Extract(root, format, entryType);

entry.ExtractToFile(destination, overwrite: true);

Verify_Extract(destination, entry, entryType);
}

[Theory]
[MemberData(nameof(GetFormatsAndFiles))]
public async Task Extract_Async(TarEntryFormat format, TarEntryType entryType)
{
using TempDirectory root = new TempDirectory();

(string entryName, string destination, PosixTarEntry entry) = Prepare_Extract(root, format, entryType);

await entry.ExtractToFileAsync(destination, overwrite: true);

Verify_Extract(destination, entry, entryType);
}

private (string, string, PosixTarEntry) Prepare_Extract(TempDirectory root, TarEntryFormat format, TarEntryType entryType)
{
string entryName = entryType.ToString();
string destination = Path.Join(root.Path, entryName);

PosixTarEntry entry = InvokeTarEntryCreationConstructor(format, entryType, entryName) as PosixTarEntry;
Assert.NotNull(entry);
entry.Mode = TestPermission1;

return (entryName, destination, entry);
}

private void Verify_Extract(string destination, PosixTarEntry entry, TarEntryType entryType)
{
if (entryType is TarEntryType.RegularFile)
{
Assert.True(File.Exists(destination));
}
else if (entryType is TarEntryType.Directory)
{
Assert.True(Directory.Exists(destination));
}

AssertFileModeEquals(destination, TestPermission1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,18 @@ public async Task ExtractToFile_Link_Throws_Async(TarEntryFormat format, TarEntr
Assert.Equal(0, Directory.GetFileSystemEntries(root.Path).Count());
}
}

[Theory]
[MemberData(nameof(GetFormatsAndFiles))]
public async Task Extract_Async(TarEntryFormat format, TarEntryType entryType)
{
using TempDirectory root = new TempDirectory();

(string entryName, string destination, TarEntry entry) = Prepare_Extract(root, format, entryType);

await entry.ExtractToFileAsync(destination, overwrite: true);

Verify_Extract(destination, entry, entryType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public async Task ExtractArchiveWithEntriesThatStartWithSlashDotPrefix_Async()
}

[Fact]
public async Task UnixFileModes()
public async Task UnixFileModes_Async()
{
using TempDirectory source = new TempDirectory();
using TempDirectory destination = new TempDirectory();
Expand Down Expand Up @@ -249,7 +249,7 @@ public async Task UnixFileModes()
}

[Fact]
public async Task UnixFileModes_RestrictiveParentDir()
public async Task UnixFileModes_RestrictiveParentDir_Async()
{
using TempDirectory source = new TempDirectory();
using TempDirectory destination = new TempDirectory();
Expand Down
45 changes: 45 additions & 0 deletions src/libraries/System.Formats.Tar/tests/TarTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,21 @@ public static IEnumerable<object[]> GetFormatsAndLinks()
}
}

public static IEnumerable<object[]> GetFormatsAndFiles()
{
foreach (TarEntryType entryType in new[] { TarEntryType.V7RegularFile, TarEntryType.Directory })
{
yield return new object[] { TarEntryFormat.V7, entryType };
}
foreach (TarEntryFormat format in new[] { TarEntryFormat.Ustar, TarEntryFormat.Pax, TarEntryFormat.Gnu })
{
foreach (TarEntryType entryType in new[] { TarEntryType.RegularFile, TarEntryType.Directory })
{
yield return new object[] { format, entryType };
}
}
}

protected static void SetUnixFileMode(string path, UnixFileMode mode)
{
if (!PlatformDetection.IsWindows)
Expand Down Expand Up @@ -398,5 +413,35 @@ protected static void AssertFileModeEquals(string path, UnixFileMode mode)
Assert.Equal(mode, File.GetUnixFileMode(path));
}
}

protected (string, string, TarEntry) Prepare_Extract(TempDirectory root, TarEntryFormat format, TarEntryType entryType)
{
string entryName = entryType.ToString();
string destination = Path.Join(root.Path, entryName);

TarEntry entry = InvokeTarEntryCreationConstructor(format, entryType, entryName);
Assert.NotNull(entry);
entry.Mode = TestPermission1;

return (entryName, destination, entry);
}

protected void Verify_Extract(string destination, TarEntry entry, TarEntryType entryType)
{
if (entryType is TarEntryType.RegularFile or TarEntryType.V7RegularFile)
{
Assert.True(File.Exists(destination));
}
else if (entryType is TarEntryType.Directory)
{
Assert.True(Directory.Exists(destination));
}
else
{
Assert.True(false, "Unchecked entry type.");
}

AssertFileModeEquals(destination, TestPermission1);
}
}
}

0 comments on commit 9a9f251

Please sign in to comment.