Skip to content
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

Function to convert OpenOptions to c_int #76110

Merged
merged 8 commits into from
Sep 22, 2020
29 changes: 29 additions & 0 deletions library/std/src/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,31 @@ 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`].
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
/// With: [`libc::open`]
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
///
/// This method allows the reuse of the OpenOptions as flags argument for [`fs::OpenOptions`].
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`libc::c_int`]: https://docs.rs/libc/*/libc/type.c_int.html
/// [`libc::open`]: https://docs.rs/libc/*/libc/fn.open.html
///
/// # Examples
///
/// ```no_run
/// # #![feature(rustc_private)]
/// extern crate libc;
/// use std::ffi::CString;
/// use std::fs::OpenOptions;
/// use std::os::unix::fs::OpenOptionsExt;
///
/// let mut options = OpenOptions::new();
/// options.write(true).read(true);
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
/// 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")]
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
fn as_flags(&self) -> io::Result<libc::c_int>;
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
}

#[stable(feature = "fs_ext", since = "1.1.0")]
Expand All @@ -358,6 +383,10 @@ impl OpenOptionsExt for OpenOptions {
self.as_inner_mut().custom_flags(flags);
self
}
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved

fn as_flags(&self) -> io::Result<libc::c_int> {
self.as_inner().as_flags()
}
}

/// Unix-specific extensions to [`fs::Metadata`].
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ impl OpenOptions {
pub fn mode(&mut self, mode: u32) {
self.mode = mode as mode_t;
}
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
pub fn as_flags(&self) -> io::Result<c_int> {
let access_mode = self.get_access_mode()?;
let creation_mode = self.get_creation_mode()?;
Ok(creation_mode | access_mode | self.custom_flags)
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved
}
FedericoPonzi marked this conversation as resolved.
Show resolved Hide resolved

fn get_access_mode(&self) -> io::Result<c_int> {
match (self.read, self.write, self.append) {
Expand Down