From afba9eb3df2feaf5551ec9d3a4fa130449ac6540 Mon Sep 17 00:00:00 2001 From: Zhanhui Li Date: Tue, 31 Jan 2023 15:04:24 +0800 Subject: Implement OpenOptionsExt trait for OpenOptions (#139) * Implement OpenOptionsExt trait for OpenOptions Signed-off-by: Li Zhanhui * Initialize custom_flags if target_os is unix Signed-off-by: Li Zhanhui --------- Signed-off-by: Li Zhanhui --- monoio/src/driver/op/open.rs | 5 ++++- monoio/src/fs/open_options.rs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/monoio/src/driver/op/open.rs b/monoio/src/driver/op/open.rs index 1952154..a104678 100644 --- a/monoio/src/driver/op/open.rs +++ b/monoio/src/driver/op/open.rs @@ -22,7 +22,10 @@ impl Op { pub(crate) fn open>(path: P, options: &OpenOptions) -> io::Result> { // Here the path will be copied, so its safe. let path = cstr(path.as_ref())?; - let flags = libc::O_CLOEXEC | options.access_mode()? | options.creation_mode()?; + let flags = libc::O_CLOEXEC + | options.access_mode()? + | options.creation_mode()? + | (options.custom_flags & !libc::O_ACCMODE); let mode = options.mode; Op::submit_with(Open { path, flags, mode }) diff --git a/monoio/src/fs/open_options.rs b/monoio/src/fs/open_options.rs index e1cb199..913e88c 100644 --- a/monoio/src/fs/open_options.rs +++ b/monoio/src/fs/open_options.rs @@ -1,4 +1,4 @@ -use std::{io, path::Path}; +use std::{io, os::unix::prelude::OpenOptionsExt, path::Path}; use crate::{ driver::{op::Op, shared_fd::SharedFd}, @@ -59,6 +59,8 @@ pub struct OpenOptions { create_new: bool, #[cfg(unix)] pub(crate) mode: libc::mode_t, + #[cfg(unix)] + pub(crate) custom_flags: libc::c_int, } impl OpenOptions { @@ -89,6 +91,8 @@ impl OpenOptions { create_new: false, #[cfg(unix)] mode: 0o666, + #[cfg(unix)] + custom_flags: 0, } } @@ -351,3 +355,16 @@ impl OpenOptions { }) } } + +#[cfg(unix)] +impl OpenOptionsExt for OpenOptions { + fn mode(&mut self, mode: u32) -> &mut Self { + self.mode = mode as libc::mode_t; + self + } + + fn custom_flags(&mut self, flags: i32) -> &mut Self { + self.custom_flags = flags as libc::c_int; + self + } +} -- cgit v1.2.3