Skip to content

Commit

Permalink
refactor: refact code structure for platform specific behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzzzzzt committed Jun 29, 2024
1 parent 0feec6f commit 0fd60df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
35 changes: 7 additions & 28 deletions monoio/src/fs/dir_builder.rs → monoio/src/fs/dir_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -24,7 +29,7 @@ impl DirBuilder {
pub fn new() -> Self {
Self {
recursive: false,
inner: fs_impl::BuilderInner::new(),
inner: sys::BuilderInner::new(),
}
}

Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions monoio/src/fs/dir_builder/unix.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0fd60df

Please sign in to comment.