From 5db9424de3ce428704023acc580abdc24038b021 Mon Sep 17 00:00:00 2001 From: Gleb Kozyrev Date: Tue, 14 Jun 2016 12:04:24 +0300 Subject: [PATCH 1/3] Add ERROR_FILE_EXISTS to ErrorKind conversion on Windows Bug report: https://users.rust-lang.org/t/detecting-error-kind-for-opening-file/6215 Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx#error_file_exists --- src/libstd/sys/windows/c.rs | 1 + src/libstd/sys/windows/mod.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index 2acf6485eb3da..ce563dc7b16d3 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -181,6 +181,7 @@ pub const ERROR_ACCESS_DENIED: DWORD = 5; pub const ERROR_INVALID_HANDLE: DWORD = 6; pub const ERROR_NO_MORE_FILES: DWORD = 18; pub const ERROR_HANDLE_EOF: DWORD = 38; +pub const ERROR_FILE_EXISTS: DWORD = 80; pub const ERROR_BROKEN_PIPE: DWORD = 109; pub const ERROR_CALL_NOT_IMPLEMENTED: DWORD = 120; pub const ERROR_INSUFFICIENT_BUFFER: DWORD = 122; diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs index 384940e4dc446..6dd4f4c3e750e 100644 --- a/src/libstd/sys/windows/mod.rs +++ b/src/libstd/sys/windows/mod.rs @@ -68,6 +68,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind { match errno as c::DWORD { c::ERROR_ACCESS_DENIED => return ErrorKind::PermissionDenied, c::ERROR_ALREADY_EXISTS => return ErrorKind::AlreadyExists, + c::ERROR_FILE_EXISTS => return ErrorKind::AlreadyExists, c::ERROR_BROKEN_PIPE => return ErrorKind::BrokenPipe, c::ERROR_FILE_NOT_FOUND => return ErrorKind::NotFound, c::ERROR_PATH_NOT_FOUND => return ErrorKind::NotFound, From 1db97575cc679790b087e00beee6c399a0b213a5 Mon Sep 17 00:00:00 2001 From: Gleb Kozyrev Date: Tue, 14 Jun 2016 19:45:42 +0300 Subject: [PATCH 2/3] Test ErrorKind::AlreadyExists for files --- src/libstd/fs.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 0180c3118a586..cb8105d7dc1d7 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -1770,6 +1770,15 @@ mod tests { check!(fs::remove_dir(dir)); } + #[test] + fn file_create_new_already_exists_error() { + let tmpdir = tmpdir(); + let file = &tmpdir.join("file_create_new_error_exists"); + check!(fs::File::create(file)); + let e = fs::OpenOptions::new().write(true).create_new(true).open(file).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::AlreadyExists); + } + #[test] fn mkdir_path_already_exists_error() { let tmpdir = tmpdir(); From 552afd30d075f3c7ecc8918f088c3aa805f6cef7 Mon Sep 17 00:00:00 2001 From: Gleb Kozyrev Date: Tue, 14 Jun 2016 19:47:05 +0300 Subject: [PATCH 3/3] Fix a docs typo --- src/libstd/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index cb8105d7dc1d7..668fa1fb30360 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -512,7 +512,7 @@ impl OpenOptions { /// No file is allowed to exist at the target location, also no (dangling) /// symlink. /// - /// This option is useful because it as atomic. Otherwise between checking + /// This option is useful because it is atomic. Otherwise between checking /// whether a file exists and creating a new one, the file may have been /// created by another process (a TOCTOU race condition / attack). ///