-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add Directory.CreateTempSubdirectory #73408
Conversation
CreateTempSubdirectory will create a new uniquely named directory under the temp directory. This allows applications to write temporary files in a co-located directory. Contributes to dotnet#72881
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsCreateTempSubdirectory will create a new uniquely named directory under the temp directory. This allows applications to write temporary files in a co-located directory. Contributes to #72881 Note: I've decided not to implement the full approved API in #72881 because of the feedback on the File API. That will be added in a future PR.
|
src/libraries/System.Private.CoreLib/src/System/IO/Directory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Directory.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Directory.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Directory.Unix.cs
Outdated
Show resolved
Hide resolved
while (attempts < MaxAttempts) | ||
{ | ||
Interop.GetRandomBytes(pKey, RandomKeyLength); | ||
Path.Populate83FileNameFromRandomBytes(pKey, RandomKeyLength, builder.RawChars.Slice(builder.Length, RandomFileNameLength)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it important to use the 8.3 naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't important. But it is what GetRandomFileName uses
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs
Lines 281 to 289 in 916f1ad
public static unsafe string GetRandomFileName() | |
{ | |
byte* pKey = stackalloc byte[KeyLength]; | |
Interop.GetRandomBytes(pKey, KeyLength); | |
return string.Create( | |
12, (IntPtr)pKey, (span, key) => // 12 == 8 + 1 (for period) + 3 | |
Populate83FileNameFromRandomBytes((byte*)key, KeyLength, span)); | |
} |
I'm just simulating a call to GetRandomFileName
here, without allocating an intermediate string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a comment here, if that helps. Or did you have a different algorithm in mind for generating a folder name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO having a period in the directory name seems a bit strange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO having a period in the directory name seems a bit strange
Does it really matter though? Code shouldn't depend on these names - they even have a different pattern between Windows and Unix. Besides, there are plenty of cases where directories have periods in their names - versions, namespace-like, etc.
e.g. C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.7
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than existing comments and a few more I'm leaving, LGTM.
src/libraries/System.Runtime.Loader/tests/ResourceAssemblyLoadContext.cs
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem/tests/Directory/CreateTempSubdirectory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem/tests/Directory/CreateTempSubdirectory.cs
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem/tests/Directory/CreateTempSubdirectory.cs
Outdated
Show resolved
Hide resolved
- Rewrite CreateTempSubdirectory and GetTempFileName on Unix to minimize allocations.
I have responded to all existing feedback and pushed new changes. PTAL. |
|
||
namespace System.IO.Tests | ||
{ | ||
public class Directory_CreateTempSubdirectory : FileSystemTest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe verify that the filemode of the created directory is 700
on Unix. That is an important trait of this API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe verify that the filemode of the created directory is 700 on Unix.
Done.
Q: is it on Windows also guaranteed the resulting directory is only accessible to the current user?
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks a lot @eerhardt !
...sition/tests/System/ComponentModel/Composition/Hosting/DirectoryCatalogDebuggerProxyTests.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Directory.Unix.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…tests These tests had 3 methods that do the same thing. Consolidating down to one. Follow up to dotnet#73408
CreateTempSubdirectory will create a new uniquely named directory under the temp directory. This allows applications to write temporary files in a co-located directory.
Contributes to #72881
Note: I've decided not to implement the full approved API in #72881 because of the feedback on the File API. That will be added in a future PR.