about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/ext/fs.rs28
-rw-r--r--src/libstd/sys/unix/fs.rs4
-rw-r--r--src/libstd/sys/windows/ext/fs.rs26
3 files changed, 55 insertions, 3 deletions
diff --git a/src/libstd/sys/unix/ext/fs.rs b/src/libstd/sys/unix/ext/fs.rs
index 801b222b9a8..75ea5846fb3 100644
--- a/src/libstd/sys/unix/ext/fs.rs
+++ b/src/libstd/sys/unix/ext/fs.rs
@@ -104,6 +104,29 @@ pub trait OpenOptionsExt {
     /// the final permissions.
     #[stable(feature = "fs_ext", since = "1.1.0")]
     fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
+
+    /// Pass custom flags to the `flags` agument of `open`.
+    ///
+    /// The bits that define the access mode are masked out with `O_ACCMODE`, to ensure
+    /// they do not interfere with the access mode set by Rusts options.
+    ///
+    /// Custom flags can only set flags, not remove flags set by Rusts options.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// extern crate libc;
+    /// use std::fs::OpenOptions;
+    /// use std::os::unix::fs::OpenOptionsExt;
+    ///
+    /// let options = OpenOptions::new().write(true);
+    /// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
+    /// let file = options.open("foo.txt");
+    /// ```
+    #[unstable(feature = "expand_open_options",
+               reason = "recently added",
+               issue = "30014")]
+    fn custom_flags(&mut self, flags: i32) -> &mut Self;
 }
 
 #[stable(feature = "fs_ext", since = "1.1.0")]
@@ -111,6 +134,10 @@ impl OpenOptionsExt for OpenOptions {
     fn mode(&mut self, mode: raw::mode_t) -> &mut OpenOptions {
         self.as_inner_mut().mode(mode); self
     }
+
+    fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
+        self.as_inner_mut().custom_flags(flags); self
+    }
 }
 
 // Hm, why are there casts here to the returned type, shouldn't the types always
@@ -265,4 +292,3 @@ impl DirBuilderExt for fs::DirBuilder {
         self
     }
 }
-
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs
index c6a02f2b8b9..31ea6b7dd2e 100644
--- a/src/libstd/sys/unix/fs.rs
+++ b/src/libstd/sys/unix/fs.rs
@@ -58,7 +58,7 @@ pub struct OpenOptions {
     create: bool,
     create_new: bool,
     // system-specific
-    custom_flags: u32,
+    custom_flags: i32,
     mode: mode_t,
 }
 
@@ -259,7 +259,7 @@ impl OpenOptions {
     pub fn create(&mut self, create: bool) { self.create = create; }
     pub fn create_new(&mut self, create_new: bool) { self.create_new = create_new; }
 
-    pub fn custom_flags(&mut self, flags: u32) { self.custom_flags = flags; }
+    pub fn custom_flags(&mut self, flags: i32) { self.custom_flags = flags; }
     pub fn mode(&mut self, mode: raw::mode_t) { self.mode = mode as mode_t; }
 
     fn get_access_mode(&self) -> io::Result<c_int> {
diff --git a/src/libstd/sys/windows/ext/fs.rs b/src/libstd/sys/windows/ext/fs.rs
index fa7e0e564ac..04053b6cb6a 100644
--- a/src/libstd/sys/windows/ext/fs.rs
+++ b/src/libstd/sys/windows/ext/fs.rs
@@ -64,6 +64,28 @@ pub trait OpenOptionsExt {
     /// ```
     fn share_mode(&mut self, val: u32) -> &mut Self;
 
+    /// Sets extra flags for the `dwFileFlags` argument to the call to `CreateFile2`
+    /// (or combines it with `attributes` and `security_qos_flags` to set the
+    /// `dwFlagsAndAttributes` for `CreateFile`).
+    ///
+    /// Custom flags can only set flags, not remove flags set by Rusts options.
+    ///
+    /// # Examples
+    ///
+    /// ```rust,ignore
+    /// extern crate winapi;
+    /// use std::fs::OpenOptions;
+    /// use std::os::windows::fs::OpenOptionsExt;
+    ///
+    /// let options = OpenOptions::new().create(true).write(true);
+    /// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE); }
+    /// let file = options.open("foo.txt");
+    /// ```
+    #[unstable(feature = "expand_open_options",
+               reason = "recently added",
+               issue = "30014")]
+    fn custom_flags(&mut self, flags: u32) -> &mut Self;
+
     /// Sets the `dwFileAttributes` argument to the call to `CreateFile2` to
     /// the specified value (or combines it with `custom_flags` and
     /// `security_qos_flags` to set the `dwFlagsAndAttributes` for `CreateFile`).
@@ -114,6 +136,10 @@ impl OpenOptionsExt for OpenOptions {
         self.as_inner_mut().share_mode(share); self
     }
 
+    fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
+        self.as_inner_mut().custom_flags(flags); self
+    }
+
     fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
         self.as_inner_mut().attributes(attributes); self
     }