-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add Directory.CreateTempSubdirectory #73408
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
Changes from all commits
9f3ffa3
692c536
5b86fd3
d744ac9
dc910fd
5fd8d41
e9f0a01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Sys | ||
| { | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_MkdTemp", SetLastError = true)] | ||
| internal static unsafe partial byte* MkdTemp(byte* template); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.DotNet.RemoteExecutor; | ||
| using Xunit; | ||
|
|
||
| namespace System.IO.Tests | ||
| { | ||
| public class Directory_CreateTempSubdirectory : FileSystemTest | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe verify that the filemode of the created directory is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: is it on Windows also guaranteed the resulting directory is only accessible to the current user?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Done.
No, on Windows there is no guarantee on the permissions of the directory. Just that it is empty, and it is in the TEMP directory. |
||
| { | ||
| public static TheoryData<string> CreateTempSubdirectoryData | ||
| { | ||
| get | ||
| { | ||
| var result = new TheoryData<string>() { null, "", "myDir", "my.Dir", "H\u00EBllo" }; | ||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| // ensure we can use backslashes on Unix since that isn't a directory separator | ||
| result.Add(@"my\File"); | ||
| result.Add(@"\"); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(CreateTempSubdirectoryData))] | ||
| public void CreateTempSubdirectory(string prefix) | ||
| { | ||
| DirectoryInfo tmpDir = Directory.CreateTempSubdirectory(prefix); | ||
| try | ||
| { | ||
| Assert.True(tmpDir.Exists); | ||
| Assert.Equal(-1, tmpDir.FullName.IndexOfAny(Path.GetInvalidPathChars())); | ||
| Assert.Empty(Directory.GetFileSystemEntries(tmpDir.FullName)); | ||
| Assert.Equal(Path.TrimEndingDirectorySeparator(Path.GetTempPath()), tmpDir.Parent.FullName); | ||
|
|
||
| if (!string.IsNullOrEmpty(prefix)) | ||
| { | ||
| Assert.StartsWith(prefix, tmpDir.Name); | ||
| int expectedNameLength = prefix.Length + (OperatingSystem.IsWindows() ? 12 : 6); | ||
| Assert.Equal(expectedNameLength, tmpDir.Name.Length); | ||
| } | ||
|
|
||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| UnixFileMode userRWX = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute; | ||
| Assert.Equal(userRWX, tmpDir.UnixFileMode); | ||
| } | ||
eerhardt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Ensure a file can be written to the directory | ||
| string tempFile = Path.Combine(tmpDir.FullName, "newFile"); | ||
| using (FileStream fs = File.Create(tempFile, bufferSize: 1024, FileOptions.DeleteOnClose)) | ||
| { | ||
| Assert.Equal(0, fs.Length); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| tmpDir.Delete(recursive: true); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
| public void CreateTempSubdirectoryTempUnicode() | ||
| { | ||
| RemoteExecutor.Invoke(() => | ||
| { | ||
| DirectoryInfo tempPathWithUnicode = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + "\u00F6")); | ||
| tempPathWithUnicode.Create(); | ||
|
|
||
| string tempEnvVar = OperatingSystem.IsWindows() ? "TMP" : "TMPDIR"; | ||
| Environment.SetEnvironmentVariable(tempEnvVar, tempPathWithUnicode.FullName); | ||
|
|
||
| try | ||
| { | ||
| DirectoryInfo tmpDir = Directory.CreateTempSubdirectory(); | ||
| Assert.True(tmpDir.Exists); | ||
| Assert.Equal(tempPathWithUnicode.FullName, tmpDir.Parent.FullName); | ||
|
|
||
| Environment.SetEnvironmentVariable(tempEnvVar, tempPathWithUnicode.Parent.FullName); | ||
| } | ||
| finally | ||
| { | ||
| tempPathWithUnicode.Delete(recursive: true); | ||
| } | ||
| }).Dispose(); | ||
| } | ||
|
|
||
| public static TheoryData<string> InvalidPrefixData | ||
| { | ||
| get | ||
| { | ||
| var result = new TheoryData<string>() { "/", "myDir/", "my/Dir" }; | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| result.Add(@"\"); | ||
| result.Add(@"myDir\"); | ||
| result.Add(@"my\Dir"); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(InvalidPrefixData))] | ||
| public void CreateTempSubdirectoryThrowsWithPrefixContainingDirectorySeparator(string prefix) | ||
| { | ||
| AssertExtensions.Throws<ArgumentException>("prefix", () => Directory.CreateTempSubdirectory(prefix)); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.