Skip to content

Commit ac5e22c

Browse files
committed
Port reopen_dir to fs_utf8 and cap-async-std.
1 parent a3b1763 commit ac5e22c

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

cap-async-std/src/fs/dir.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,23 @@ impl Dir {
871871
let path = path.as_ref().to_path_buf();
872872
fs::create_dir_all(path).await
873873
}
874+
875+
/// Construct a new instance of `Self` from existing directory file
876+
/// descriptor.
877+
///
878+
/// This can be useful when interacting with other libraries and or C/C++
879+
/// code which has invoked `openat(..., O_DIRECTORY)` external to this
880+
/// crate.
881+
pub fn reopen_dir<Filelike: AsFilelike>(dir: &Filelike) -> io::Result<Self> {
882+
spawn_blocking(move || {
883+
cap_primitives::fs::open_dir(
884+
&dir.as_filelike_view::<std::fs::File>(),
885+
std::path::Component::CurDir.as_ref(),
886+
)
887+
})
888+
.await
889+
.map(Self::from_std_file)
890+
}
874891
}
875892

876893
#[cfg(not(target_os = "wasi"))]

cap-async-std/src/fs_utf8/dir.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::fs_utf8::{from_utf8, to_utf8, DirBuilder, File, Metadata, ReadDir};
33
use async_std::{fs, io};
44
use camino::{Utf8Path, Utf8PathBuf};
55
use cap_primitives::AmbientAuthority;
6+
use io_lifetimes::AsFilelike;
67
#[cfg(not(windows))]
78
use io_lifetimes::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
89
#[cfg(windows)]
@@ -642,6 +643,23 @@ impl Dir {
642643
let path = from_utf8(path.as_ref())?;
643644
fs::create_dir_all(path).await
644645
}
646+
647+
/// Construct a new instance of `Self` from existing directory file
648+
/// descriptor.
649+
///
650+
/// This can be useful when interacting with other libraries and or C/C++
651+
/// code which has invoked `openat(..., O_DIRECTORY)` external to this
652+
/// crate.
653+
pub fn reopen_dir<Filelike: AsFilelike>(dir: &Filelike) -> io::Result<Self> {
654+
spawn_blocking(move || {
655+
cap_primitives::fs::open_dir(
656+
&dir.as_filelike_view::<std::fs::File>(),
657+
std::path::Component::CurDir.as_ref(),
658+
)
659+
})
660+
.await
661+
.map(Self::from_std_file)
662+
}
645663
}
646664

647665
// Safety: `FilelikeViewType` is implemented for `std::fs::File`.

cap-std/src/fs_utf8/dir.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use camino::{Utf8Path, Utf8PathBuf};
66
use cap_primitives::AmbientAuthority;
77
#[cfg(not(windows))]
88
use io_extras::os::rustix::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
9+
use io_lifetimes::AsFilelike;
910
#[cfg(not(windows))]
1011
use io_lifetimes::{AsFd, BorrowedFd, OwnedFd};
1112
#[cfg(windows)]
@@ -626,6 +627,20 @@ impl Dir {
626627
let path = from_utf8(path.as_ref())?;
627628
fs::create_dir_all(path)
628629
}
630+
631+
/// Construct a new instance of `Self` from existing directory file
632+
/// descriptor.
633+
///
634+
/// This can be useful when interacting with other libraries and or C/C++
635+
/// code which has invoked `openat(..., O_DIRECTORY)` external to this
636+
/// crate.
637+
pub fn reopen_dir<Filelike: AsFilelike>(dir: &Filelike) -> io::Result<Self> {
638+
cap_primitives::fs::open_dir(
639+
&dir.as_filelike_view::<std::fs::File>(),
640+
std::path::Component::CurDir.as_ref(),
641+
)
642+
.map(Self::from_std_file)
643+
}
629644
}
630645

631646
#[cfg(not(windows))]

0 commit comments

Comments
 (0)