-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add fs::symlink * chore: add fs::symlink * remove println * detect uring add symlinkat * rename symlink structure field * assert file is symlink * cargo fmt
- Loading branch information
1 parent
c3cb780
commit d8855e9
Showing
7 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{ffi::CString, io, path::Path}; | ||
|
||
use super::{Op, OpAble}; | ||
use crate::driver::util::cstr; | ||
|
||
pub(crate) struct Symlink { | ||
pub(crate) from: CString, | ||
pub(crate) to: CString, | ||
} | ||
impl Op<Symlink> { | ||
pub(crate) fn symlink<P: AsRef<Path>, Q: AsRef<Path>>( | ||
from: P, | ||
to: Q, | ||
) -> io::Result<Op<Symlink>> { | ||
let from = cstr(from.as_ref())?; | ||
let to = cstr(to.as_ref())?; | ||
Op::submit_with(Symlink { from, to }) | ||
} | ||
} | ||
|
||
impl OpAble for Symlink { | ||
#[cfg(all(target_os = "linux", feature = "iouring"))] | ||
fn uring_op(&mut self) -> io_uring::squeue::Entry { | ||
use io_uring::{opcode, types}; | ||
let from_ref = self.from.as_c_str().as_ptr(); | ||
let to_ref = self.to.as_c_str().as_ptr(); | ||
opcode::SymlinkAt::new(types::Fd(libc::AT_FDCWD), from_ref, to_ref).build() | ||
} | ||
|
||
#[cfg(any(feature = "legacy", feature = "poll-io"))] | ||
fn legacy_interest(&self) -> Option<(crate::driver::ready::Direction, usize)> { | ||
None | ||
} | ||
|
||
#[cfg(any(feature = "legacy", feature = "poll-io"))] | ||
fn legacy_call(&mut self) -> std::io::Result<u32> { | ||
use crate::syscall_u32; | ||
syscall_u32!(symlink( | ||
self.from.as_c_str().as_ptr(), | ||
self.to.as_c_str().as_ptr() | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::{io, path::Path}; | ||
|
||
use crate::driver::op::Op; | ||
|
||
/// Creates a new symbolic link on the filesystem. | ||
/// The dst path will be a symbolic link pointing to the src path. | ||
/// This is an async version of std::os::unix::fs::symlink. | ||
pub async fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> { | ||
Op::symlink(src, dst)?.await.meta.result?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![cfg(all(unix, feature = "symlinkat"))] | ||
|
||
use std::{io, path::PathBuf}; | ||
|
||
use monoio::fs::File; | ||
use tempfile::tempdir; | ||
|
||
const TEST_PAYLOAD: &[u8] = b"I am data in the source file"; | ||
|
||
async fn create_file(path: &PathBuf) -> io::Result<File> { | ||
File::create(path).await | ||
} | ||
|
||
#[monoio::test_all] | ||
async fn create_symlink() { | ||
let tmpdir = tempdir().unwrap(); | ||
let src_file_path = tmpdir.path().join("src"); | ||
let dst_file_path = tmpdir.path().join("dst"); | ||
let src_file = create_file(&src_file_path).await.unwrap(); | ||
src_file.write_all_at(TEST_PAYLOAD, 0).await.0.unwrap(); | ||
monoio::fs::symlink(src_file_path.as_path(), dst_file_path.as_path()) | ||
.await | ||
.unwrap(); | ||
|
||
let content = monoio::fs::read(dst_file_path).await.unwrap(); | ||
assert_eq!(content, TEST_PAYLOAD); | ||
assert(dst_file_path.is_symlink()); | ||
} |