Skip to content

native: Always open a file with Open/Write modes #13865

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

Merged
merged 1 commit into from
May 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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