From 0fd60df927f7b786fda7a29e9cc52e9a4f4f0f7d Mon Sep 17 00:00:00 2001 From: Lzzzt Date: Sat, 29 Jun 2024 21:40:52 +0800 Subject: [PATCH] refactor: refact code structure for platform specific behavior --- .../fs/{dir_builder.rs => dir_builder/mod.rs} | 35 ++++--------------- monoio/src/fs/dir_builder/unix.rs | 23 ++++++++++++ 2 files changed, 30 insertions(+), 28 deletions(-) rename monoio/src/fs/{dir_builder.rs => dir_builder/mod.rs} (86%) create mode 100644 monoio/src/fs/dir_builder/unix.rs diff --git a/monoio/src/fs/dir_builder.rs b/monoio/src/fs/dir_builder/mod.rs similarity index 86% rename from monoio/src/fs/dir_builder.rs rename to monoio/src/fs/dir_builder/mod.rs index 3e78cefb..b9efd11b 100644 --- a/monoio/src/fs/dir_builder.rs +++ b/monoio/src/fs/dir_builder/mod.rs @@ -1,11 +1,16 @@ +mod unix; + use std::{io, os::unix::fs::DirBuilderExt, path::Path}; +#[cfg(unix)] +use unix as sys; + /// A builder used to create directories in various manners. /// /// This builder also supports platform-specific options. pub struct DirBuilder { recursive: bool, - inner: fs_impl::BuilderInner, + inner: sys::BuilderInner, } impl DirBuilder { @@ -24,7 +29,7 @@ impl DirBuilder { pub fn new() -> Self { Self { recursive: false, - inner: fs_impl::BuilderInner::new(), + inner: sys::BuilderInner::new(), } } @@ -140,32 +145,6 @@ impl DirBuilderExt for DirBuilder { } } -mod fs_impl { - use std::path::Path; - - use libc::mode_t; - - use crate::driver::op::Op; - - pub(super) struct BuilderInner { - mode: libc::mode_t, - } - - impl BuilderInner { - pub(super) fn new() -> Self { - Self { mode: 0o777 } - } - - pub(super) async fn mkdir(&self, path: &Path) -> std::io::Result<()> { - Op::mkdir(path, self.mode)?.await.meta.result.map(|_| ()) - } - - pub(super) fn set_mode(&mut self, mode: u32) { - self.mode = mode as mode_t; - } - } -} - // currently, will use the std version of metadata, will change to use the io-uring version // when the statx is merge async fn is_dir(path: &Path) -> bool { diff --git a/monoio/src/fs/dir_builder/unix.rs b/monoio/src/fs/dir_builder/unix.rs new file mode 100644 index 00000000..ec5745a3 --- /dev/null +++ b/monoio/src/fs/dir_builder/unix.rs @@ -0,0 +1,23 @@ +use std::path::Path; + +use libc::mode_t; + +use crate::driver::op::Op; + +pub(super) struct BuilderInner { + mode: libc::mode_t, +} + +impl BuilderInner { + pub(super) fn new() -> Self { + Self { mode: 0o777 } + } + + pub(super) async fn mkdir(&self, path: &Path) -> std::io::Result<()> { + Op::mkdir(path, self.mode)?.await.meta.result.map(|_| ()) + } + + pub(super) fn set_mode(&mut self, mode: u32) { + self.mode = mode as mode_t; + } +}