From 27c90b881df93b53fd3f24dcbfed116379c2fc69 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Sun, 30 Aug 2020 16:21:41 +0200 Subject: [PATCH 1/7] initial implementation of OpenOptions to c_int --- library/std/src/sys/unix/fs.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index acb18e6d064e6..59dfd9f9dc424 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -692,6 +692,7 @@ impl OpenOptions { } } + impl File { pub fn open(path: &Path, opts: &OpenOptions) -> io::Result { let path = cstr(path)?; @@ -962,6 +963,12 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> { Ok(()) } +pub fn get_openopetions_as_cint(from: OpenOptions) -> io::Result { + let access_mode = from.get_access_mode()?; + let creation_mode = from.get_creation_mode()?; + Ok(creation_mode | access_mode) +} + pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { let p = cstr(p)?; cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) })?; From eb3906be4ad375cc6b83cd6a6e0116817db22575 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Sun, 30 Aug 2020 17:01:20 +0200 Subject: [PATCH 2/7] Fix typo get openoptions function name Co-authored-by: Ivan Tham --- library/std/src/sys/unix/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 59dfd9f9dc424..338ce17b06c38 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -963,7 +963,7 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> { Ok(()) } -pub fn get_openopetions_as_cint(from: OpenOptions) -> io::Result { +pub fn get_openoptions_as_cint(from: OpenOptions) -> io::Result { let access_mode = from.get_access_mode()?; let creation_mode = from.get_creation_mode()?; Ok(creation_mode | access_mode) From 1bc0627607262cc60a7692b16e205f30fc88b89f Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Mon, 31 Aug 2020 15:48:28 +0200 Subject: [PATCH 3/7] Add as_flag function to the OpenOptionsExt struct --- library/std/src/sys/unix/ext/fs.rs | 30 ++++++++++++++++++++++++++++++ library/std/src/sys/unix/fs.rs | 12 +++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/library/std/src/sys/unix/ext/fs.rs b/library/std/src/sys/unix/ext/fs.rs index b590a0280d138..0a00c64e6c545 100644 --- a/library/std/src/sys/unix/ext/fs.rs +++ b/library/std/src/sys/unix/ext/fs.rs @@ -345,6 +345,33 @@ pub trait OpenOptionsExt { /// ``` #[stable(feature = "open_options_ext", since = "1.10.0")] fn custom_flags(&mut self, flags: i32) -> &mut Self; + + /// Get the flags of this OpenOptions as libc::c_int. + /// + /// This method allows the reuse of the OpenOptions as flags argument for `libc::open()`. + /// + /// # Examples + /// + /// ```no_run + /// # #![feature(rustc_private)] + /// extern crate libc; + /// use std::ffi::CString; + /// use std::fs::OpenOptions; + /// use std::os::unix::fs::OpenOptionsExt; + /// + /// # fn main() { + /// let mut options = OpenOptions::new(); + /// options.write(true).read(true); + /// if cfg!(unix) { + /// options.custom_flags(libc::O_NOFOLLOW); + /// } + /// let file_name = CString::new("foo.txt").unwrap(); + /// let file = unsafe{ libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) }; + /// + /// # } + /// ``` + #[stable(feature = "open_options_ext_as_flags", since = "1.47.0")] + fn as_flags(&self) -> io::Result; } #[stable(feature = "fs_ext", since = "1.1.0")] @@ -358,6 +385,9 @@ impl OpenOptionsExt for OpenOptions { self.as_inner_mut().custom_flags(flags); self } + fn as_flags(&self) -> io::Result { + self.as_inner().as_flags() + } } /// Unix-specific extensions to [`fs::Metadata`]. diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 59dfd9f9dc424..f4d3ad2d2a484 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -655,7 +655,11 @@ impl OpenOptions { pub fn mode(&mut self, mode: u32) { self.mode = mode as mode_t; } - + pub fn as_flags(&self) -> io::Result { + let access_mode = self.get_access_mode()?; + let creation_mode = self.get_creation_mode()?; + Ok(creation_mode | access_mode | self.custom_flags) + } fn get_access_mode(&self) -> io::Result { match (self.read, self.write, self.append) { (true, false, false) => Ok(libc::O_RDONLY), @@ -692,7 +696,6 @@ impl OpenOptions { } } - impl File { pub fn open(path: &Path, opts: &OpenOptions) -> io::Result { let path = cstr(path)?; @@ -963,11 +966,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> { Ok(()) } -pub fn get_openopetions_as_cint(from: OpenOptions) -> io::Result { - let access_mode = from.get_access_mode()?; - let creation_mode = from.get_creation_mode()?; - Ok(creation_mode | access_mode) -} pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> { let p = cstr(p)?; From 7c1e5c1dcd25c945f619eda289f639dbe2b002da Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Mon, 31 Aug 2020 21:42:57 +0200 Subject: [PATCH 4/7] Update OpenOptions::as_flags docs, and minor styling --- library/std/src/sys/unix/ext/fs.rs | 17 ++++++++--------- library/std/src/sys/unix/fs.rs | 1 + 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/library/std/src/sys/unix/ext/fs.rs b/library/std/src/sys/unix/ext/fs.rs index 0a00c64e6c545..afda21d00e050 100644 --- a/library/std/src/sys/unix/ext/fs.rs +++ b/library/std/src/sys/unix/ext/fs.rs @@ -346,9 +346,13 @@ pub trait OpenOptionsExt { #[stable(feature = "open_options_ext", since = "1.10.0")] fn custom_flags(&mut self, flags: i32) -> &mut Self; - /// Get the flags of this OpenOptions as libc::c_int. + /// Get the flags of this OpenOptions as [`libc::c_int`]. + /// With: [`libc::open`] /// - /// This method allows the reuse of the OpenOptions as flags argument for `libc::open()`. + /// This method allows the reuse of the OpenOptions as flags argument for [`fs::OpenOptions`]. + /// + /// [`libc::c_int`]: https://docs.rs/libc/*/libc/type.c_int.html + /// [`libc::open`]: https://docs.rs/libc/*/libc/fn.open.html /// /// # Examples /// @@ -359,16 +363,10 @@ pub trait OpenOptionsExt { /// use std::fs::OpenOptions; /// use std::os::unix::fs::OpenOptionsExt; /// - /// # fn main() { /// let mut options = OpenOptions::new(); /// options.write(true).read(true); - /// if cfg!(unix) { - /// options.custom_flags(libc::O_NOFOLLOW); - /// } /// let file_name = CString::new("foo.txt").unwrap(); - /// let file = unsafe{ libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) }; - /// - /// # } + /// let file = unsafe { libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) }; /// ``` #[stable(feature = "open_options_ext_as_flags", since = "1.47.0")] fn as_flags(&self) -> io::Result; @@ -385,6 +383,7 @@ impl OpenOptionsExt for OpenOptions { self.as_inner_mut().custom_flags(flags); self } + fn as_flags(&self) -> io::Result { self.as_inner().as_flags() } diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 13d8d59b03457..1281fcc47bcb3 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -660,6 +660,7 @@ impl OpenOptions { let creation_mode = self.get_creation_mode()?; Ok(creation_mode | access_mode | self.custom_flags) } + fn get_access_mode(&self) -> io::Result { match (self.read, self.write, self.append) { (true, false, false) => Ok(libc::O_RDONLY), From 321b680fe66d1be04cd67fac75ff7f148fd117fe Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Wed, 2 Sep 2020 10:48:11 +0200 Subject: [PATCH 5/7] Update docs of OpenOptions::as_flags --- library/std/src/sys/unix/ext/fs.rs | 5 ++--- library/std/src/sys/unix/fs.rs | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/std/src/sys/unix/ext/fs.rs b/library/std/src/sys/unix/ext/fs.rs index afda21d00e050..16fe316110d94 100644 --- a/library/std/src/sys/unix/ext/fs.rs +++ b/library/std/src/sys/unix/ext/fs.rs @@ -346,10 +346,9 @@ pub trait OpenOptionsExt { #[stable(feature = "open_options_ext", since = "1.10.0")] fn custom_flags(&mut self, flags: i32) -> &mut Self; - /// Get the flags of this OpenOptions as [`libc::c_int`]. - /// With: [`libc::open`] + /// Get the flags as [`libc::c_int`]. /// - /// This method allows the reuse of the OpenOptions as flags argument for [`fs::OpenOptions`]. + /// This method allows the reuse of the OpenOptions as flags argument for [`libc::open`]. /// /// [`libc::c_int`]: https://docs.rs/libc/*/libc/type.c_int.html /// [`libc::open`]: https://docs.rs/libc/*/libc/fn.open.html diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 1281fcc47bcb3..6f1db6503f1c1 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -655,6 +655,7 @@ impl OpenOptions { pub fn mode(&mut self, mode: u32) { self.mode = mode as mode_t; } + pub fn as_flags(&self) -> io::Result { let access_mode = self.get_access_mode()?; let creation_mode = self.get_creation_mode()?; From 70292d45060ee2b8829e2af0be54d5a76696cd1d Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Wed, 16 Sep 2020 19:40:44 +0200 Subject: [PATCH 6/7] Sets as_flags as unstable --- library/std/src/sys/unix/ext/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/ext/fs.rs b/library/std/src/sys/unix/ext/fs.rs index 16fe316110d94..afcd80abb9d27 100644 --- a/library/std/src/sys/unix/ext/fs.rs +++ b/library/std/src/sys/unix/ext/fs.rs @@ -367,7 +367,7 @@ pub trait OpenOptionsExt { /// let file_name = CString::new("foo.txt").unwrap(); /// let file = unsafe { libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) }; /// ``` - #[stable(feature = "open_options_ext_as_flags", since = "1.47.0")] + #[unstable(feature = "open_options_ext_as_flags", issue = "76801")] fn as_flags(&self) -> io::Result; } From 2f5192280f57ab859a2414ff2e9b6f6398d3feb7 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Tue, 22 Sep 2020 09:54:36 +0100 Subject: [PATCH 7/7] enable unstable open_options_ext_as_flags feature in doc comments --- library/std/src/sys/unix/ext/fs.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/sys/unix/ext/fs.rs b/library/std/src/sys/unix/ext/fs.rs index afcd80abb9d27..15831c632ea3b 100644 --- a/library/std/src/sys/unix/ext/fs.rs +++ b/library/std/src/sys/unix/ext/fs.rs @@ -357,6 +357,7 @@ pub trait OpenOptionsExt { /// /// ```no_run /// # #![feature(rustc_private)] + /// #![feature(open_options_ext_as_flags)] /// extern crate libc; /// use std::ffi::CString; /// use std::fs::OpenOptions;