Skip to content

Commit d649652

Browse files
author
Kai Mast
authored
Add create_dir (tokio-rs#193)
This adds a `create_dir` function call to `tokio_uring::fs` and partially fixes tokio-rs#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`.
1 parent 3ba9bd8 commit d649652

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

src/fs/directory.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ use crate::runtime::driver::op::Op;
22
use std::io;
33
use std::path::Path;
44

5+
/// # Examples
6+
///
7+
/// ```no_run
8+
/// use tokio_uring::fs::create_dir;
9+
///
10+
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
11+
/// tokio_uring::start(async {
12+
/// create_dir("/some/dir").await?;
13+
/// Ok::<(), std::io::Error>(())
14+
/// })?;
15+
/// Ok(())
16+
/// }
17+
/// ```
18+
pub async fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
19+
Op::make_dir(path.as_ref())?.await
20+
}
21+
522
/// Removes an empty directory.
623
///
724
/// # Examples

src/fs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Filesystem manipulation operations.
22
33
mod directory;
4+
pub use directory::create_dir;
45
pub use directory::remove_dir;
56

67
mod file;

src/io/mkdir_at.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::runtime::driver::op::{Completable, CqeResult, Op};
2+
use crate::runtime::CONTEXT;
3+
4+
use super::util::cstr;
5+
6+
use std::ffi::CString;
7+
use std::io;
8+
use std::path::Path;
9+
10+
/// Create a directory at path relative to the current working directory
11+
/// of the caller's process.
12+
pub(crate) struct Mkdir {
13+
pub(crate) _path: CString,
14+
}
15+
16+
impl Op<Mkdir> {
17+
/// Submit a request to create a directory
18+
pub(crate) fn make_dir(path: &Path) -> io::Result<Op<Mkdir>> {
19+
use io_uring::{opcode, types};
20+
21+
let _path = cstr(path)?;
22+
23+
CONTEXT.with(|x| {
24+
x.handle()
25+
.expect("Not in a runtime context")
26+
.submit_op(Mkdir { _path }, |mkdir| {
27+
let p_ref = mkdir._path.as_c_str().as_ptr();
28+
29+
opcode::MkDirAt::new(types::Fd(libc::AT_FDCWD), p_ref).build()
30+
})
31+
})
32+
}
33+
}
34+
35+
impl Completable for Mkdir {
36+
type Output = io::Result<()>;
37+
38+
fn complete(self, cqe: CqeResult) -> Self::Output {
39+
cqe.result.map(|_| ())
40+
}
41+
}

src/io/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod connect;
77

88
mod fsync;
99

10+
mod mkdir_at;
11+
1012
mod noop;
1113
pub(crate) use noop::NoOp;
1214

src/io/unlink_at.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Op<Unlink> {
2020
Self::unlink(path, 0)
2121
}
2222

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

tests/directory.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/fs_directory.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#[path = "../src/future.rs"]
2+
#[allow(warnings)]
3+
mod future;
4+
5+
use tokio_test::assert_ok;
6+
use tokio_uring::fs;
7+
8+
use tempfile::tempdir;
9+
10+
#[test]
11+
fn basic_create_dir() {
12+
tokio_uring::start(async {
13+
let base_dir = tempdir().unwrap();
14+
let new_dir = base_dir.path().join("foo");
15+
let new_dir_2 = new_dir.clone();
16+
17+
assert_ok!(fs::create_dir(new_dir).await);
18+
19+
assert!(new_dir_2.is_dir());
20+
});
21+
}
22+
23+
#[test]
24+
fn basic_remove_dir() {
25+
tokio_uring::start(async {
26+
let temp_dir = tempfile::TempDir::new().unwrap();
27+
tokio_uring::fs::remove_dir(temp_dir.path()).await.unwrap();
28+
assert!(std::fs::metadata(temp_dir.path()).is_err());
29+
});
30+
}

0 commit comments

Comments
 (0)