Skip to content

Commit 1bc0627

Browse files
author
Federico Ponzi
committed
Add as_flag function to the OpenOptionsExt struct
1 parent 27c90b8 commit 1bc0627

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

library/std/src/sys/unix/ext/fs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,33 @@ pub trait OpenOptionsExt {
345345
/// ```
346346
#[stable(feature = "open_options_ext", since = "1.10.0")]
347347
fn custom_flags(&mut self, flags: i32) -> &mut Self;
348+
349+
/// Get the flags of this OpenOptions as libc::c_int.
350+
///
351+
/// This method allows the reuse of the OpenOptions as flags argument for `libc::open()`.
352+
///
353+
/// # Examples
354+
///
355+
/// ```no_run
356+
/// # #![feature(rustc_private)]
357+
/// extern crate libc;
358+
/// use std::ffi::CString;
359+
/// use std::fs::OpenOptions;
360+
/// use std::os::unix::fs::OpenOptionsExt;
361+
///
362+
/// # fn main() {
363+
/// let mut options = OpenOptions::new();
364+
/// options.write(true).read(true);
365+
/// if cfg!(unix) {
366+
/// options.custom_flags(libc::O_NOFOLLOW);
367+
/// }
368+
/// let file_name = CString::new("foo.txt").unwrap();
369+
/// let file = unsafe{ libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) };
370+
///
371+
/// # }
372+
/// ```
373+
#[stable(feature = "open_options_ext_as_flags", since = "1.47.0")]
374+
fn as_flags(&self) -> io::Result<libc::c_int>;
348375
}
349376

350377
#[stable(feature = "fs_ext", since = "1.1.0")]
@@ -358,6 +385,9 @@ impl OpenOptionsExt for OpenOptions {
358385
self.as_inner_mut().custom_flags(flags);
359386
self
360387
}
388+
fn as_flags(&self) -> io::Result<libc::c_int> {
389+
self.as_inner().as_flags()
390+
}
361391
}
362392

363393
/// Unix-specific extensions to [`fs::Metadata`].

library/std/src/sys/unix/fs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,11 @@ impl OpenOptions {
655655
pub fn mode(&mut self, mode: u32) {
656656
self.mode = mode as mode_t;
657657
}
658-
658+
pub fn as_flags(&self) -> io::Result<c_int> {
659+
let access_mode = self.get_access_mode()?;
660+
let creation_mode = self.get_creation_mode()?;
661+
Ok(creation_mode | access_mode | self.custom_flags)
662+
}
659663
fn get_access_mode(&self) -> io::Result<c_int> {
660664
match (self.read, self.write, self.append) {
661665
(true, false, false) => Ok(libc::O_RDONLY),
@@ -692,7 +696,6 @@ impl OpenOptions {
692696
}
693697
}
694698

695-
696699
impl File {
697700
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
698701
let path = cstr(path)?;
@@ -963,11 +966,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
963966
Ok(())
964967
}
965968

966-
pub fn get_openopetions_as_cint(from: OpenOptions) -> io::Result<libc::c_int> {
967-
let access_mode = from.get_access_mode()?;
968-
let creation_mode = from.get_creation_mode()?;
969-
Ok(creation_mode | access_mode)
970-
}
971969

972970
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
973971
let p = cstr(p)?;

0 commit comments

Comments
 (0)