-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
docs: fs: stat.isDirectory: added clarification #27413
Conversation
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.
Sorry, this documentation isn't correct.
I'm not clear why you think it's not valid when using lstat()
. lstat gets information for the link itself, and the link is not a directory (it's a link). From your code, I could equally (wrongly) say that isDirectory() is invalid when using stat()
, because when you call stat('123')
, 123 is not a directory, its a link, so isDirectory() should be false (but I wouldn't say that! it ignores the stat docs, which says it follows links).
You are also missing a test in the code you posted: call lstat() on /, and then call isDirectory, it will return true, which is valid, in contradiction of the sentence you added.
Is it possible you are just confused about the difference between stat and lstat? Perhaps some additional text is needed there? Or did you miss this sentence?
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
The thing is I'm working on a file manager, and use readify to get file list with attributes, such as size, type, mode, date. I have stepped on this rake a lot times, so I propose to clarify this thing in documentation because it is not obvious. I understand the difference, but I don't know why do I should always remember this :). If everyone else always remember the difference, we can close the issue, I just thought that it's a good idea for improvement and that it can help people who see this Also superstat can help not think at all about this. |
Sorry, still not quite understanding.
Most users don't want to know a path is a symlink, they want whatever they do to happen to the thing pointed to. In other words, whether they open (get stat, whatever) usgin a path to a file or to a symlink to a file, they don't want to know. If you are writing code that actually needs to know both that a path is a symlink, and what the link points to, then you need to make two stat calls. Always use If I understand you correctly, you are hoping for some kind of "fs.Stats" structure to be returned that tells you both about the entity pointed to (its size, its inode, the device it is on, its creation time, owner, etc), and whether the path used to get that info went through symlink resolution. That's just not possible. Not structurally, because each property can have a different value for the symlink vs the entity it points to, so the structure doesn't work. Also, not possible because the POSIX system call to do a one-shot "get info on two things" don't exist. In terms of naming, "lstat" gets the stat for the link itself ("l"), "stat" gets the stat for what the link points to, as most people want. I'm not going to say the names couldn't be more memorable, but they exactly mirror the UNIX system API names, so using the traditional name seems better than inventing a new one, and is consistent with the rest of the fs API. I'm also not going to say the docs couldn't get better, I'm sure they can, but this particular change is inaccurate. The lstat vs stat distinction applies to ALL of fs.Stat, but you have singled out isDirectory as if its somehow special (it is not). Perhaps a note right at the top of the fs.Stat class docs that refers to the two APIs that return it (stat and lstat), and mention that depending on the API used, Stat will either refer to the link itself, or the entity it points to, would be a more helpful change. |
@sam-github I updated commit in accordance to changes you requested. |
Your text is more clear, thank you, but the above problem is not fixed. I suggest moving the text, as I requested:
|
What are the next steps on this PR ? and this needs a rebase. |
Rebase is done :). |
8ae28ff
to
2935f72
Compare
@coderaiser ... the "merge commit" needs to be dropped. We do not use merge commits. Instead we use git rebase instead. Let us know if you need some help with that. |
@jasnell rebase is done, now without merge commit :) |
Ping @sam-github ... does this look good to you now? |
Co-authored-by: Antoine du Hamel <[email protected]>
PR-URL: #27413 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
Landed in 6c3326c |
PR-URL: #27413 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #27413 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #27413 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #27413 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAdded clarification to documentation, similar to one describes stat.isSymbolicLink which tells that
This method is only valid when using
fs.lstat()
.When use
fs.lstat
for a symbolic link to a directory it will returnstat
whichisDirectory
will always befalse
. This is not really obvious, and because of this both methodsstat
andlstat
should be called to get true information about symbolic link to a directory.Let's create symbolic link to root directory.
And then run in
REPL
: