Skip to content

Commit e3d6bb1

Browse files
committed
Rollup merge of #34270 - gkoz:error_file_exists, r=alexcrichton
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
2 parents 2675a55 + 552afd3 commit e3d6bb1

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/libstd/fs.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl OpenOptions {
512512
/// No file is allowed to exist at the target location, also no (dangling)
513513
/// symlink.
514514
///
515-
/// This option is useful because it as atomic. Otherwise between checking
515+
/// This option is useful because it is atomic. Otherwise between checking
516516
/// whether a file exists and creating a new one, the file may have been
517517
/// created by another process (a TOCTOU race condition / attack).
518518
///
@@ -1770,6 +1770,15 @@ mod tests {
17701770
check!(fs::remove_dir(dir));
17711771
}
17721772

1773+
#[test]
1774+
fn file_create_new_already_exists_error() {
1775+
let tmpdir = tmpdir();
1776+
let file = &tmpdir.join("file_create_new_error_exists");
1777+
check!(fs::File::create(file));
1778+
let e = fs::OpenOptions::new().write(true).create_new(true).open(file).unwrap_err();
1779+
assert_eq!(e.kind(), ErrorKind::AlreadyExists);
1780+
}
1781+
17731782
#[test]
17741783
fn mkdir_path_already_exists_error() {
17751784
let tmpdir = tmpdir();

src/libstd/sys/windows/c.rs

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub const ERROR_ACCESS_DENIED: DWORD = 5;
181181
pub const ERROR_INVALID_HANDLE: DWORD = 6;
182182
pub const ERROR_NO_MORE_FILES: DWORD = 18;
183183
pub const ERROR_HANDLE_EOF: DWORD = 38;
184+
pub const ERROR_FILE_EXISTS: DWORD = 80;
184185
pub const ERROR_BROKEN_PIPE: DWORD = 109;
185186
pub const ERROR_CALL_NOT_IMPLEMENTED: DWORD = 120;
186187
pub const ERROR_INSUFFICIENT_BUFFER: DWORD = 122;

src/libstd/sys/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
6868
match errno as c::DWORD {
6969
c::ERROR_ACCESS_DENIED => return ErrorKind::PermissionDenied,
7070
c::ERROR_ALREADY_EXISTS => return ErrorKind::AlreadyExists,
71+
c::ERROR_FILE_EXISTS => return ErrorKind::AlreadyExists,
7172
c::ERROR_BROKEN_PIPE => return ErrorKind::BrokenPipe,
7273
c::ERROR_FILE_NOT_FOUND => return ErrorKind::NotFound,
7374
c::ERROR_PATH_NOT_FOUND => return ErrorKind::NotFound,

0 commit comments

Comments
 (0)