-
Notifications
You must be signed in to change notification settings - Fork 13.4k
libstd/io/fs.rs: avoid reading directories as files on *BSD #19303
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
Conversation
unreachable!() | ||
|
||
let bytes = File::open(path).and_then(|f| | ||
if cfg!(any(target_os = "freebsd", target_os = "dragonfly")) { |
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.
We typically try to not house logic such as this in the compiler itself but rather put it in the standard library. As std::io::File
is a cross-platform abstraction and we generally try to enforce the same behavior across platforms. If read_to_end()
is only available on freebsd/dragonfly, I'd personally be a little more in favor of putting the error all the way over into std::io
.
Exposing the platform-specific behavior will be done through std::sys
in time as we expose the raw underlying system primitives for this sort of operation.
5fe5c79
to
5d1865d
Compare
@alexcrichton I restricted the change to |
}, | ||
Err(e) => Err(e) | ||
} | ||
) |
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 to be somewhat nitpickity, but could you move this to std::io
instead? We're generally trying to keep the std::sys
bindings a low-level as possible (no worries about cross-platform errors like this) while pushing the compatibility layer to std::io
instead.
5d1865d
to
6080b7d
Compare
@alexcrichton The check logic was moved to |
8c1b2fb
to
e7e2d7a
Compare
I fixed |
The same goes for sys::fs::FileDesc::fstat() on Windows. Signed-off-by: NODA, Kai <nodakai@gmail.com>
On *BSD systems, we can open(2) a directory and directly read(2) from it due to an old tradition. We should avoid doing so by explicitly calling fstat(2) to check the type of the opened file. Opening a directory as a module file can't always be avoided. Even when there's no "path" attribute trick involved, there can always be a *directory* named "my_module.rs". Fix rust-lang#12460 Signed-off-by: NODA, Kai <nodakai@gmail.com>
e7e2d7a
to
3980cde
Compare
A unit test on OS X failed because it expected an error message modified by my patch. I updated the unit test as well. |
@cmr Please note that I updated my commit after @alexcrichton reviewed it. Update: oops, it was a classical case of race between two threads |
…hton On *BSD systems, we can `open(2)` a directory and directly `read(2)` from it due to an old tradition. We should avoid doing so by explicitly calling `fstat(2)` to check the type of the opened file. Opening a directory as a module file can't always be avoided. Even when there's no "path" attribute trick involved, there can always be a *directory* named `my_module.rs`. Incidentally, remove unnecessary mutability of `&self` from `io::fs::File::stat()`.
On *BSD systems, we can
open(2)
a directory and directlyread(2)
from it due to an old tradition. We should avoid doing so by explicitly callingfstat(2)
to check the type of the opened file.Opening a directory as a module file can't always be avoided. Even when there's no "path" attribute trick involved, there can always be a directory named
my_module.rs
.Incidentally, remove unnecessary mutability of
&self
fromio::fs::File::stat()
.