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

File and Metadata should document reasons to prefer *not* to use is_file() #64170

Closed
kentfredric opened this issue Sep 5, 2019 · 2 comments · Fixed by #73243
Closed

File and Metadata should document reasons to prefer *not* to use is_file() #64170

kentfredric opened this issue Sep 5, 2019 · 2 comments · Fixed by #73243
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@kentfredric
Copy link

As per rust-lang/rust-clippy#4503, there's a bit of a trap you can fall into if you use is_file() as a predictor for "can I probably open and read bytes from this thing".

You generally don't want to assert is_file() for user specified paths, especially if they come from command line arguments, where they might be Unix FIFO File Descriptors.

If for example you were implementing cat in rust, using is_file() instead of !is_dir() would prevent you from reading block devices, character devices, pipes, and more:

           S_IFSOCK   0140000   socket
           S_IFLNK    0120000   symbolic link
           S_IFREG    0100000   regular file
           S_IFBLK    0060000   block device
           S_IFDIR    0040000   directory
           S_IFCHR    0020000   character device
           S_IFIFO    0010000   FIFO

And this would break your ability to be composed nicely in Unix flows where patterns like:

diff <( prog_a ) <( prog_b )

Would become broken if diff was a rust program that ensured all its arguments were is_file()

@GuillaumeGomez GuillaumeGomez added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Sep 6, 2019
@the8472
Copy link
Member

the8472 commented Sep 6, 2019

This isn't specific to is_file(). When you build tools that recurse into directories you also may or may not want to skip symlinks. If you write archives you may need special handling for non-regular files and so on. CLI tools often also probe whether a stdout is a TTY or something else to adjust their behavior for interactive or non-interactive mode or to avoid breaking the console with binary data interpreted as escape sequences.

I think it would be better to add a more general warning to the top-level documentation that symlink/file/directory is not exhaustive and one has to take the platform-extensions into account if one wants to deal with arbitrary user-provided files or directory-trees.

@kentfredric
Copy link
Author

This isn't specific to is_file(). When you build tools that recurse into directories you also may or may not want to skip symlinks. If you write archives you may need special handling for non-regular files and so on. CLI tools often also probe whether a stdout is a TTY or something else to adjust their behavior for interactive or non-interactive mode or to avoid breaking the console with binary data interpreted as escape sequences.

I think it would be better to add a more general warning to the top-level documentation that symlink/file/directory is not exhaustive and one has to take the platform-extensions into account if one wants to deal with arbitrary user-provided files or directory-trees.

Sure, as long as the documentation conveys "If you are using this for usecase X, then !is_dir() is probably more appropriate".

Just because the average novice programmer who first encounters the need to differentiate that a given thing is what they consider to be "a file", and they'll point is_file() at it, reducing their usefulness without realizing it.

( People have been making this mistake in many languages for a long time, and it would be nice if rust had some way to encourage better systems development in this regard, good docs and applicable lints would go far )

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 6, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 27, 2020
…Amanieu

Add documentation to point to `File::open` or `OpenOptions::open` instead of `is_file` to check read/write possibility

Fixes rust-lang#64170.

This adds documentation to point user towards `!is_dir` instead of `is_file` when all they want to is read from a source.

I ran `rg "fn is_file\("` to find all `is_file` methods, I hope I did not miss one.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 28, 2020
…Amanieu

Add documentation to point to `File::open` or `OpenOptions::open` instead of `is_file` to check read/write possibility

Fixes rust-lang#64170.

This adds documentation to point user towards `!is_dir` instead of `is_file` when all they want to is read from a source.

I ran `rg "fn is_file\("` to find all `is_file` methods, I hope I did not miss one.
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 28, 2020
…Amanieu

Add documentation to point to `File::open` or `OpenOptions::open` instead of `is_file` to check read/write possibility

Fixes rust-lang#64170.

This adds documentation to point user towards `!is_dir` instead of `is_file` when all they want to is read from a source.

I ran `rg "fn is_file\("` to find all `is_file` methods, I hope I did not miss one.
@bors bors closed this as completed in 6a944c1 Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
4 participants