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

std::fs::File::create return a wrong error on Windows when the path points to a directory #134893

Open
Chen1Plus opened this issue Dec 29, 2024 · 3 comments
Labels
A-filesystem Area: `std::fs` A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@Chen1Plus
Copy link

std::fs::File::create return a wrong error on Windows.

I tried this code:

fn main() {
    // <path> points to an existing directory
    std::fs::File::create("<path>").unwrap();
}

I expected the ErrorKind is IsADirectory. Instead, the ErrorKind is PermissionDenied.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-msvc
release: 1.83.0
LLVM version: 19.1.1
Backtrace

thread 'main' panicked at src/main.rs:2:34:
called `Result::unwrap()` on an `Err` value: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }
stack backtrace:
   0:     0x7ff6b4e16e01 - std::backtrace_rs::backtrace::dbghelp64::trace
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
   1:     0x7ff6b4e16e01 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff6b4e16e01 - std::sys::backtrace::_print_fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:66
   3:     0x7ff6b4e16e01 - std::sys::backtrace::impl$0::print::impl$0::fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:39
   4:     0x7ff6b4e244ca - core::fmt::rt::Argument::fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\fmt\rt.rs:177
   5:     0x7ff6b4e244ca - core::fmt::write
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\fmt\mod.rs:1186
   6:     0x7ff6b4e152f7 - std::io::Write::write_fmt<std::sys::pal::windows::stdio::Stderr>
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\io\mod.rs:1839
   7:     0x7ff6b4e16c45 - std::sys::backtrace::BacktraceLock::print
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:42
   8:     0x7ff6b4e181d7 - std::panicking::default_hook::closure$1
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:268
   9:     0x7ff6b4e17fb7 - std::panicking::default_hook
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:295
  10:     0x7ff6b4e18863 - std::panicking::rust_panic_with_hook
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:801
  11:     0x7ff6b4e186e9 - std::panicking::begin_panic_handler::closure$0
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:674
  12:     0x7ff6b4e1754f - std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\sys\backtrace.rs:170
  13:     0x7ff6b4e182ee - std::panicking::begin_panic_handler
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:665
  14:     0x7ff6b4e29861 - core::panicking::panic_fmt
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\panicking.rs:74
  15:     0x7ff6b4e29bb0 - core::result::unwrap_failed
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/core\src\result.rs:1700
  16:     0x7ff6b4e11fa4 - enum2$<core::result::Result<std::fs::File,std::io::error::Error> >::unwrap
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:1104
  17:     0x7ff6b4e11fa4 - test_file_create::main
                               at C:\Users\1plus\Desktop\test-file-create\src\main.rs:2
  18:     0x7ff6b4e110bb - core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
  19:     0x7ff6b4e1151e - core::hint::black_box
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\hint.rs:389
  20:     0x7ff6b4e1151e - std::sys::backtrace::__rust_begin_short_backtrace<void (*)(),tuple$<> >
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys\backtrace.rs:154
  21:     0x7ff6b4e11591 - std::rt::lang_start::closure$0<tuple$<> >
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:195
  22:     0x7ff6b4e13cec - std::rt::lang_start_internal::closure$1
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\rt.rs:174
  23:     0x7ff6b4e13cec - std::panicking::try::do_call
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:557
  24:     0x7ff6b4e13cec - std::panicking::try
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panicking.rs:520
  25:     0x7ff6b4e13cec - std::panic::catch_unwind
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\panic.rs:358
  26:     0x7ff6b4e13cec - std::rt::lang_start_internal
                               at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\library/std\src\rt.rs:174
  27:     0x7ff6b4e1156a - std::rt::lang_start<tuple$<> >
                               at C:\Users\1plus\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:194
  28:     0x7ff6b4e12009 - main
  29:     0x7ff6b4e28090 - invoke_main
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  30:     0x7ff6b4e28090 - __scrt_common_main_seh
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  31:     0x7fff86cfe8d7 - BaseThreadInitThunk
  32:     0x7fff888bfbcc - RtlUserThreadStart
error: process didn't exit successfully: `target\debug\test-file-create.exe` (exit code: 101)

@Chen1Plus Chen1Plus added the C-bug Category: This is a bug. label Dec 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 29, 2024
@fmease fmease added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 29, 2024
@ChrisDenton
Copy link
Member

ChrisDenton commented Dec 29, 2024

That is unfortunately the error given to us by the OS.

The underlying NT status code is STATUS_FILE_IS_A_DIRECTORY but that gets mapped to the Win32 error ERROR_ACCESS_DENIED by CreateFileW.

@fmease fmease added O-windows Operating system: Windows C-bug Category: This is a bug. A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-filesystem Area: `std::fs` C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 29, 2024
@Chen1Plus
Copy link
Author

Should we consider adding an extra check? How does the standard library typically handle such cases?

@ChrisDenton
Copy link
Member

The standard library just passes on what the OS gives it. An external tool might decide to do an extra check but the standard library generally can't due to time-of-check to time-of-use (TOCTOU) concerns. That is to say a directory could be replaced by a file by the time we check (or vice versa),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-filesystem Area: `std::fs` A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants