Skip to content

Commit

Permalink
Add create_dir (#193)
Browse files Browse the repository at this point in the history
This adds a `create_dir` function call to `tokio_uring::fs` and
partially fixes #48.

I also renamed `tests/directory.rs` to `tests/fs_directory.rs` so the
name is in sync with that of `tests/fs_file.rs`.
  • Loading branch information
Kai Mast authored Dec 17, 2022
1 parent 3ba9bd8 commit d649652
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/fs/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ use crate::runtime::driver::op::Op;
use std::io;
use std::path::Path;

/// # Examples
///
/// ```no_run
/// use tokio_uring::fs::create_dir;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// tokio_uring::start(async {
/// create_dir("/some/dir").await?;
/// Ok::<(), std::io::Error>(())
/// })?;
/// Ok(())
/// }
/// ```
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
Op::make_dir(path.as_ref())?.await
}

/// Removes an empty directory.
///
/// # Examples
Expand Down
1 change: 1 addition & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Filesystem manipulation operations.
mod directory;
pub use directory::create_dir;
pub use directory::remove_dir;

mod file;
Expand Down
41 changes: 41 additions & 0 deletions src/io/mkdir_at.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;

use super::util::cstr;

use std::ffi::CString;
use std::io;
use std::path::Path;

/// Create a directory at path relative to the current working directory
/// of the caller's process.
pub(crate) struct Mkdir {
pub(crate) _path: CString,
}

impl Op<Mkdir> {
/// Submit a request to create a directory
pub(crate) fn make_dir(path: &Path) -> io::Result<Op<Mkdir>> {
use io_uring::{opcode, types};

let _path = cstr(path)?;

CONTEXT.with(|x| {
x.handle()
.expect("Not in a runtime context")
.submit_op(Mkdir { _path }, |mkdir| {
let p_ref = mkdir._path.as_c_str().as_ptr();

opcode::MkDirAt::new(types::Fd(libc::AT_FDCWD), p_ref).build()
})
})
}
}

impl Completable for Mkdir {
type Output = io::Result<()>;

fn complete(self, cqe: CqeResult) -> Self::Output {
cqe.result.map(|_| ())
}
}
2 changes: 2 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod connect;

mod fsync;

mod mkdir_at;

mod noop;
pub(crate) use noop::NoOp;

Expand Down
2 changes: 1 addition & 1 deletion src/io/unlink_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Op<Unlink> {
Self::unlink(path, 0)
}

/// Submit a request to unlink a specifed path with provided flags.
/// Submit a request to unlink a specified path with provided flags.
pub(crate) fn unlink(path: &Path, flags: i32) -> io::Result<Op<Unlink>> {
use io_uring::{opcode, types};

Expand Down
8 changes: 0 additions & 8 deletions tests/directory.rs

This file was deleted.

30 changes: 30 additions & 0 deletions tests/fs_directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[path = "../src/future.rs"]
#[allow(warnings)]
mod future;

use tokio_test::assert_ok;
use tokio_uring::fs;

use tempfile::tempdir;

#[test]
fn basic_create_dir() {
tokio_uring::start(async {
let base_dir = tempdir().unwrap();
let new_dir = base_dir.path().join("foo");
let new_dir_2 = new_dir.clone();

assert_ok!(fs::create_dir(new_dir).await);

assert!(new_dir_2.is_dir());
});
}

#[test]
fn basic_remove_dir() {
tokio_uring::start(async {
let temp_dir = tempfile::TempDir::new().unwrap();
tokio_uring::fs::remove_dir(temp_dir.path()).await.unwrap();
assert!(std::fs::metadata(temp_dir.path()).is_err());
});
}

0 comments on commit d649652

Please sign in to comment.