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

Add Directory.CreateTempSubdirectory #73408

Merged
merged 7 commits into from
Aug 10, 2022

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Aug 4, 2022

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.

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
@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

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.

@ghost ghost assigned eerhardt Aug 4, 2022
@ghost
Copy link

ghost commented Aug 4, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: eerhardt
Assignees: -
Labels:

area-System.IO, new-api-needs-documentation

Milestone: -

while (attempts < MaxAttempts)
{
Interop.GetRandomBytes(pKey, RandomKeyLength);
Path.Populate83FileNameFromRandomBytes(pKey, RandomKeyLength, builder.RawChars.Slice(builder.Length, RandomFileNameLength));
Copy link
Member

@stephentoub stephentoub Aug 5, 2022

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?

Copy link
Member Author

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

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.

Copy link
Member Author

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?

Copy link

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

Copy link
Member Author

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.

Copy link
Member

@jozkee jozkee left a 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.

- Rewrite CreateTempSubdirectory and GetTempFileName on Unix to minimize allocations.
@eerhardt
Copy link
Member Author

eerhardt commented Aug 5, 2022

I have responded to all existing feedback and pushed new changes. PTAL.


namespace System.IO.Tests
{
public class Directory_CreateTempSubdirectory : FileSystemTest
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@adamsitnik adamsitnik left a 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 !

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@eerhardt eerhardt merged commit 9fd407a into dotnet:main Aug 10, 2022
@eerhardt eerhardt deleted the AddCreateTempDirectory branch August 10, 2022 12:43
eerhardt added a commit to eerhardt/runtime that referenced this pull request Aug 12, 2022
…tests

These tests had 3 methods that do the same thing. Consolidating down to one.

Follow up to dotnet#73408
@ghost ghost locked as resolved and limited conversation to collaborators Sep 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants