Skip to content

Commit b01c655

Browse files
authored
simplify CreateTempSubdirectory (#45633)
2 parents 3a3a0b5 + db00256 commit b01c655

File tree

7 files changed

+15
-74
lines changed

7 files changed

+15
-74
lines changed

src/Cli/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,9 @@ public FilePath WithFile(string fileName)
3535
return new FilePath(Path.Combine(Value, fileName));
3636
}
3737

38-
public string ToQuotedString()
39-
{
40-
return $"\"{Value}\"";
41-
}
42-
43-
public string ToXmlEncodeString()
44-
{
45-
return System.Net.WebUtility.HtmlEncode(Value);
46-
}
47-
4838
public override string ToString()
4939
{
50-
return ToQuotedString();
40+
return $"\"{Value}\"";
5141
}
5242

5343
public DirectoryPath GetParentPath()
@@ -56,8 +46,8 @@ public DirectoryPath GetParentPath()
5646

5747
var directoryInfo = new DirectoryInfo(Value);
5848

59-
DirectoryInfo parentDirectory = directoryInfo.Parent;
60-
if (directoryInfo.Parent is null)
49+
DirectoryInfo? parentDirectory = directoryInfo.Parent;
50+
if (parentDirectory is null)
6151
{
6252
throw new InvalidOperationException(Value + " does not have parent directory.");
6353
}
@@ -69,8 +59,8 @@ public DirectoryPath GetParentPath()
6959
{
7060
var directoryInfo = new DirectoryInfo(Value);
7161

72-
DirectoryInfo parentDirectory = directoryInfo.Parent;
73-
if (directoryInfo.Parent is null)
62+
DirectoryInfo? parentDirectory = directoryInfo.Parent;
63+
if (parentDirectory is null)
7464
{
7565
return null;
7666
}

src/Cli/Microsoft.DotNet.InternalAbstractions/EnvironmentWrapper.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Cli/Microsoft.DotNet.InternalAbstractions/FilePath.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ public FilePath(string value)
2121
Value = value;
2222
}
2323

24-
public string ToQuotedString()
25-
{
26-
return $"\"{Value}\"";
27-
}
28-
2924
public override string ToString()
3025
{
3126
return Value;
3227
}
3328

3429
public DirectoryPath GetDirectoryPath()
3530
{
36-
return new DirectoryPath(Path.GetDirectoryName(Value));
31+
return new DirectoryPath(Path.GetDirectoryName(Value)!);
3732
}
3833
}
3934
}

src/Cli/Microsoft.DotNet.InternalAbstractions/Microsoft.DotNet.InternalAbstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Abstractions for making code that uses file system and environment testable.</Description>
5-
<TargetFramework>netstandard2.0</TargetFramework>
5+
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<StrongNameKeyId>MicrosoftAspNetCore</StrongNameKeyId>

src/Common/PathUtilities.cs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,14 @@ namespace Microsoft.DotNet;
55

66
static class PathUtilities
77
{
8-
const int S_IRUSR = 256;
9-
const int S_IWUSR = 128;
10-
const int S_IXUSR = 64;
11-
const int S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR; // 700 (octal) Permissions
12-
13-
const int MAX_NUM_DIRECTORY_CREATE_RETRIES = 2;
14-
158
public static string CreateTempSubdirectory()
169
{
17-
return CreateTempSubdirectoryRetry(0);
18-
}
19-
20-
[DllImport("libc", SetLastError = true)]
21-
private static extern int mkdir(string pathname, int mode);
22-
private static string CreateTempSubdirectoryRetry(int attemptNo)
23-
{
10+
#if NETFRAMEWORK
2411
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
25-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
26-
{
27-
int mkdirStatusCode = mkdir(path, S_IRWXU);
28-
if (mkdirStatusCode != 0)
29-
{
30-
int errno = Marshal.GetLastWin32Error();
31-
if (Directory.Exists(path) && attemptNo < MAX_NUM_DIRECTORY_CREATE_RETRIES)
32-
{
33-
return CreateTempSubdirectoryRetry(attemptNo + 1);
34-
}
35-
else
36-
throw new IOException($"Failed to create a temporary subdirectory {path} with mkdir, error code: {errno}");
37-
}
38-
}
39-
else
40-
{
41-
Directory.CreateDirectory(path);
42-
}
12+
Directory.CreateDirectory(path);
4313
return path;
14+
#else
15+
return Directory.CreateTempSubdirectory().FullName;
16+
#endif
4417
}
4518
}

test/dotnet.Tests/CommandTests/ToolRunCommandTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.DotNet.Cli.ToolManifest;
77
using Microsoft.DotNet.Cli.ToolPackage;
88
using Microsoft.DotNet.Cli.Utils;
9-
using Microsoft.DotNet.InternalAbstractions;
109
using Microsoft.DotNet.Tools.Tool.Run;
1110
using Microsoft.Extensions.DependencyModel.Tests;
1211
using Microsoft.Extensions.EnvironmentAbstractions;
@@ -39,7 +38,7 @@ public void WhenRunWithRollForwardOptionItShouldIncludeRollForwardInNativeHost()
3938
{
4039
CommandName = "dotnet-a",
4140
CommandArguments = testForwardArgument
42-
}, toolRunCommand._allowRollForward);
41+
}, toolRunCommand._allowRollForward);
4342

4443
result.Should().NotBeNull();
4544
result.Args.Should().Contain("--roll-forward", "Major", fakeExecutable.Value);

test/dotnet.Tests/CommandTests/ToolUpdateGlobalOrToolPathCommandTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Microsoft.Extensions.EnvironmentAbstractions;
1313
using LocalizableStrings = Microsoft.DotNet.Tools.Tool.Update.LocalizableStrings;
1414
using Parser = Microsoft.DotNet.Cli.Parser;
15-
using Microsoft.DotNet.InternalAbstractions;
1615
using Microsoft.DotNet.Tools.Tool.Uninstall;
1716
using Microsoft.DotNet.Cli.ShellShim;
1817
using Microsoft.DotNet.Cli.ToolPackage;
@@ -383,7 +382,7 @@ public void GivenAnExistedLowerversionWhenReinstallThrowsItRollsBack()
383382
_reporter.Lines.Clear();
384383

385384
ParseResult result = Parser.Instance.Parse("dotnet tool update " + $"-g {_packageId}");
386-
385+
387386
var command = new ToolUpdateGlobalOrToolPathCommand(
388387
result,
389388
(location, forwardArguments, currentWorkingDirectory) => (_store, _store,
@@ -421,7 +420,7 @@ public void GivenPackagedShimIsProvidedWhenRunWithPackageIdItCreatesShimUsingPac
421420

422421
string options = $"-g {_packageId}";
423422
ParseResult result = Parser.Instance.Parse("dotnet tool update " + options);
424-
423+
425424
var command = new ToolUpdateGlobalOrToolPathCommand(
426425
result,
427426
(_, _, _) => (_store, _store, new ToolPackageDownloaderMock(

0 commit comments

Comments
 (0)