Skip to content

Add OpenOptions::exclusive #27217

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,25 @@ impl OpenOptions {
self.0.create(create); self
}

/// Sets the option for exclusive creation mode.
///
/// If both `OpenOptions::create` and this are set, the file will only
/// be successfully opened if it does not already exist on the filesystem.
/// If this is set but `OpenOptions::create` is not, the result is
/// unspecified.
///
/// # Examples
///
/// ```no_run
/// use std::fs::OpenOptions;
///
/// let file = OpenOptions::new().create(true).exclusive(true).open("foo.txt");
/// ```
#[unstable(feature = "open_options_exclusive", reason = "recently added")]
pub fn exclusive(&mut self, exclusive: bool) -> &mut OpenOptions {
self.0.exclusive(exclusive); self
}

/// Opens a file at `path` with the options specified by `self`.
///
/// # Errors
Expand Down Expand Up @@ -2044,6 +2063,10 @@ mod tests {
check!(f.write("bar".as_bytes()));
}
assert_eq!(check!(fs::metadata(&tmpdir.join("h"))).len(), 3);

check!(c(&rw).create(true).exclusive(true).open(&tmpdir.join("i")));
assert!(tmpdir.join("i").exists());
error!(c(&rw).create(true).exclusive(true).open(&tmpdir.join("i")), "exists");
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ impl OpenOptions {
self.flag(libc::O_CREAT, create);
}

pub fn exclusive(&mut self, exclusive: bool) {
self.flag(libc::O_EXCL, exclusive);
}

pub fn mode(&mut self, mode: raw::mode_t) {
self.mode = mode as mode_t;
}
Expand Down
13 changes: 8 additions & 5 deletions src/libstd/sys/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct OpenOptions {
read: bool,
write: bool,
truncate: bool,
exclusive: bool,
desired_access: Option<libc::DWORD>,
share_mode: Option<libc::DWORD>,
creation_disposition: Option<libc::DWORD>,
Expand Down Expand Up @@ -158,6 +159,7 @@ impl OpenOptions {
pub fn append(&mut self, append: bool) { self.append = append; }
pub fn create(&mut self, create: bool) { self.create = create; }
pub fn truncate(&mut self, truncate: bool) { self.truncate = truncate; }
pub fn exclusive(&mut self, exclusive: bool) { self.exclusive = exclusive; }
pub fn creation_disposition(&mut self, val: u32) {
self.creation_disposition = Some(val);
}
Expand Down Expand Up @@ -197,11 +199,12 @@ impl OpenOptions {

fn get_creation_disposition(&self) -> libc::DWORD {
self.creation_disposition.unwrap_or({
match (self.create, self.truncate) {
(true, true) => libc::CREATE_ALWAYS,
(true, false) => libc::OPEN_ALWAYS,
(false, false) => libc::OPEN_EXISTING,
(false, true) => {
match (self.create, self.truncate, self.exclusive) {
(true, true, false) => libc::CREATE_ALWAYS,
(true, false, false) => libc::OPEN_ALWAYS,
(true, _, true) => libc::CREATE_NEW,
(false, false, _) => libc::OPEN_EXISTING,
(false, true, _) => {
if self.write && !self.append {
libc::CREATE_ALWAYS
} else {
Expand Down