diff --git a/rust-bindings/rust/src/repo.rs b/rust-bindings/rust/src/repo.rs index ec38ce83..147199c9 100644 --- a/rust-bindings/rust/src/repo.rs +++ b/rust-bindings/rust/src/repo.rs @@ -145,6 +145,18 @@ impl Repo { } } + /// Borrow the directory file descriptor for this repository. + #[cfg(feature = "cap-std-apis")] + pub fn dfd_borrow<'a>(&'a self) -> io_lifetimes::BorrowedFd<'a> { + unsafe { io_lifetimes::BorrowedFd::borrow_raw_fd(self.dfd()) } + } + + /// Return a new `cap-std` directory reference for this repository. + #[cfg(feature = "cap-std-apis")] + pub fn dfd_as_dir(&self) -> std::io::Result { + cap_std::fs::Dir::reopen_dir(&self.dfd_borrow()) + } + /// Find all objects reachable from a commit. pub fn traverse_commit>( &self, diff --git a/rust-bindings/rust/tests/repo/mod.rs b/rust-bindings/rust/tests/repo/mod.rs index 2cfde3db..5d59aa52 100644 --- a/rust-bindings/rust/tests/repo/mod.rs +++ b/rust-bindings/rust/tests/repo/mod.rs @@ -31,6 +31,10 @@ fn should_commit_content_to_repo_and_list_refs_again() { fn cap_std_commit() { let test_repo = CapTestRepo::new(); + assert!(test_repo.dir.exists("config")); + // Also test re-acquiring a new dfd + assert!(test_repo.repo.dfd_as_dir().unwrap().exists("config")); + assert!(test_repo.repo.require_rev("nosuchrev").is_err()); let mtree = create_mtree(&test_repo.repo);