mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 12:44:40 +00:00
Add as_flag function to the OpenOptionsExt struct
This commit is contained in:
parent
27c90b881d
commit
1bc0627607
@ -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<libc::c_int>;
|
||||
}
|
||||
|
||||
#[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<libc::c_int> {
|
||||
self.as_inner().as_flags()
|
||||
}
|
||||
}
|
||||
|
||||
/// Unix-specific extensions to [`fs::Metadata`].
|
||||
|
@ -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<c_int> {
|
||||
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<c_int> {
|
||||
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<File> {
|
||||
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<libc::c_int> {
|
||||
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)?;
|
||||
|
Loading…
Reference in New Issue
Block a user