Skip to content

Commit

Permalink
native: Always open a file with Open/Write modes
Browse files Browse the repository at this point in the history
Previously, windows was using the CREATE_NEW flag which fails if the file
previously existed, which differed from the unix semantics. This alters the
opening to use the OPEN_ALWAYS flag to mirror the unix semantics.

Closes rust-lang#13861
  • Loading branch information
alexcrichton committed Apr 30, 2014
1 parent 7e9f3ea commit 8375a22
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libnative/io/file_win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub fn open(path: &CString, fm: io::FileMode, fa: io::FileAccess)
(io::Truncate, io::Read) => libc::TRUNCATE_EXISTING,
(io::Truncate, _) => libc::CREATE_ALWAYS,
(io::Open, io::Read) => libc::OPEN_EXISTING,
(io::Open, _) => libc::CREATE_NEW,
(io::Open, _) => libc::OPEN_ALWAYS,
(io::Append, io::Read) => {
dwDesiredAccess |= libc::FILE_APPEND_DATA;
libc::OPEN_EXISTING
Expand Down
20 changes: 20 additions & 0 deletions src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,11 +1255,31 @@ mod test {
match File::open_mode(&tmpdir.join("a"), io::Open, io::Read) {
Ok(..) => fail!(), Err(..) => {}
}

// Perform each one twice to make sure that it succeeds the second time
// (where the file exists)
check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));
assert!(tmpdir.join("b").exists());
check!(File::open_mode(&tmpdir.join("b"), io::Open, io::Write));

check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));
assert!(tmpdir.join("c").exists());
check!(File::open_mode(&tmpdir.join("c"), io::Open, io::ReadWrite));

check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));
assert!(tmpdir.join("d").exists());
check!(File::open_mode(&tmpdir.join("d"), io::Append, io::Write));

check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));
assert!(tmpdir.join("e").exists());
check!(File::open_mode(&tmpdir.join("e"), io::Append, io::ReadWrite));

check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));
assert!(tmpdir.join("f").exists());
check!(File::open_mode(&tmpdir.join("f"), io::Truncate, io::Write));

check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));
assert!(tmpdir.join("g").exists());
check!(File::open_mode(&tmpdir.join("g"), io::Truncate, io::ReadWrite));

check!(File::create(&tmpdir.join("h")).write("foo".as_bytes()));
Expand Down

1 comment on commit 8375a22

@brson
Copy link

@brson brson commented on 8375a22 May 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.