Skip to content

Conversation

@tmds
Copy link
Member

@tmds tmds commented Jul 6, 2021

When checking for a directory, we can use stat, there is no need to make an lstat call.

When checking for a file, use lstat which will give us what we need in most cases.
Only when it is a link, we need to call stat to check if the final target is not a directory.

@adamsitnik @stephentoub ptal.

…sting file/directory.

When checking for a directory, we can use `stat`, there is no need to make an `lstat` call.

When checking for a file, use `lstat` which will give us what we need in most cases.
Only when it is a link, we need to call `stat` to check if the final target is not a directory.
@ghost ghost added the area-System.IO label Jul 6, 2021
@ghost
Copy link

ghost commented Jul 6, 2021

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

Issue Details

When checking for a directory, we can use stat, there is no need to make an lstat call.

When checking for a file, use lstat which will give us what we need in most cases.
Only when it is a link, we need to call stat to check if the final target is not a directory.

@adamsitnik @stephentoub ptal.

Author: tmds
Assignees: -
Labels:

area-System.IO

Milestone: -

((fileinfo.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR);
if ((fileinfo.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFLNK)
{
if (Interop.Sys.Stat(fullPath, out fileinfo) < 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that, when the target is a symbolic link, we're now making two calls instead of one.
So, this is an improvement when the API is used more to check for non-existing files, than it is used against links.

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.

Otherwise; lgtm, thanks.

Comment on lines -52 to -54
// Something exists at this path. If the caller is asking for a directory, return true if it's
// a directory and false for everything else. If the caller is asking for a file, return false for
// a directory and true for everything else.
Copy link
Member

Choose a reason for hiding this comment

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

Can you please preserve this comment and extend it to reflect that symbolic links need to be stat'd in order to discard that they are not targeting a directory?

@jozkee jozkee merged commit b2bc284 into dotnet:main Jul 8, 2021
@adamsitnik adamsitnik added this to the 6.0.0 milestone Jul 8, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 7, 2021
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.

3 participants