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

simplify CreateTempSubdirectory #45633

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 5 additions & 15 deletions src/Cli/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,9 @@ public FilePath WithFile(string fileName)
return new FilePath(Path.Combine(Value, fileName));
}

public string ToQuotedString()
{
return $"\"{Value}\"";
}

public string ToXmlEncodeString()
{
return System.Net.WebUtility.HtmlEncode(Value);
}

public override string ToString()
{
return ToQuotedString();
return $"\"{Value}\"";
}

public DirectoryPath GetParentPath()
Expand All @@ -56,8 +46,8 @@ public DirectoryPath GetParentPath()

var directoryInfo = new DirectoryInfo(Value);

DirectoryInfo parentDirectory = directoryInfo.Parent;
if (directoryInfo.Parent is null)
DirectoryInfo? parentDirectory = directoryInfo.Parent;
if (parentDirectory is null)
{
throw new InvalidOperationException(Value + " does not have parent directory.");
}
Expand All @@ -69,8 +59,8 @@ public DirectoryPath GetParentPath()
{
var directoryInfo = new DirectoryInfo(Value);

DirectoryInfo parentDirectory = directoryInfo.Parent;
if (directoryInfo.Parent is null)
DirectoryInfo? parentDirectory = directoryInfo.Parent;
if (parentDirectory is null)
{
return null;
}
Expand Down

This file was deleted.

7 changes: 1 addition & 6 deletions src/Cli/Microsoft.DotNet.InternalAbstractions/FilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ public FilePath(string value)
Value = value;
}

public string ToQuotedString()
{
return $"\"{Value}\"";
}

public override string ToString()
{
return Value;
}

public DirectoryPath GetDirectoryPath()
{
return new DirectoryPath(Path.GetDirectoryName(Value));
return new DirectoryPath(Path.GetDirectoryName(Value)!);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Abstractions for making code that uses file system and environment testable.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StrongNameKeyId>MicrosoftAspNetCore</StrongNameKeyId>
Expand Down
40 changes: 8 additions & 32 deletions src/Common/PathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,17 @@ namespace Microsoft.DotNet;

static class PathUtilities
{
const int S_IRUSR = 256;
const int S_IWUSR = 128;
const int S_IXUSR = 64;
const int S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR; // 700 (octal) Permissions

const int MAX_NUM_DIRECTORY_CREATE_RETRIES = 2;

public static string CreateTempSubdirectory()
{
return CreateTempSubdirectoryRetry(0);
}

[DllImport("libc", SetLastError = true)]
private static extern int mkdir(string pathname, int mode);
private static string CreateTempSubdirectoryRetry(int attemptNo)
{
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
int mkdirStatusCode = mkdir(path, S_IRWXU);
if (mkdirStatusCode != 0)
{
int errno = Marshal.GetLastWin32Error();
if (Directory.Exists(path) && attemptNo < MAX_NUM_DIRECTORY_CREATE_RETRIES)
{
return CreateTempSubdirectoryRetry(attemptNo + 1);
}
else
throw new IOException($"Failed to create a temporary subdirectory {path} with mkdir, error code: {errno}");
}
}
else
{
Directory.CreateDirectory(path);
}
Directory.CreateDirectory(path);

#if NET
if (!OperatingSystem.IsWindows())
File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
// #else is only used by Microsoft.NET.TestFramework.csproj for netframework support. nothing to do on unix.
#endif

return path;
}
}
3 changes: 1 addition & 2 deletions test/dotnet.Tests/CommandTests/ToolRunCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.CommandFactory;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.ToolManifest;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools.Tool.Run;
Expand Down Expand Up @@ -39,7 +38,7 @@ public void WhenRunWithRollForwardOptionItShouldIncludeRollForwardInNativeHost()
{
CommandName = "dotnet-a",
CommandArguments = testForwardArgument
}, toolRunCommand._allowRollForward);
}, toolRunCommand._allowRollForward);

result.Should().NotBeNull();
result.Args.Should().Contain("--roll-forward", "Major", fakeExecutable.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Microsoft.Extensions.EnvironmentAbstractions;
using LocalizableStrings = Microsoft.DotNet.Tools.Tool.Update.LocalizableStrings;
using Parser = Microsoft.DotNet.Cli.Parser;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.DotNet.Tools.Tool.Uninstall;

namespace Microsoft.DotNet.Tests.Commands.Tool
Expand Down Expand Up @@ -383,7 +382,7 @@ public void GivenAnExistedLowerversionWhenReinstallThrowsItRollsBack()
_reporter.Lines.Clear();

ParseResult result = Parser.Instance.Parse("dotnet tool update " + $"-g {_packageId}");

var command = new ToolUpdateGlobalOrToolPathCommand(
result,
(location, forwardArguments, currentWorkingDirectory) => (_store, _store,
Expand Down Expand Up @@ -421,7 +420,7 @@ public void GivenPackagedShimIsProvidedWhenRunWithPackageIdItCreatesShimUsingPac

string options = $"-g {_packageId}";
ParseResult result = Parser.Instance.Parse("dotnet tool update " + options);

var command = new ToolUpdateGlobalOrToolPathCommand(
result,
(_, _, _) => (_store, _store, new ToolPackageDownloaderMock(
Expand Down
Loading