@@ -3,12 +3,14 @@ use crate::fs_utf8::{from_utf8, to_utf8, DirBuilder, File, Metadata, ReadDir};
33use async_std:: { fs, io} ;
44use camino:: { Utf8Path , Utf8PathBuf } ;
55use cap_primitives:: AmbientAuthority ;
6+ use io_lifetimes:: raw:: { AsRawFilelike , FromRawFilelike } ;
67use io_lifetimes:: AsFilelike ;
78#[ cfg( not( windows) ) ]
8- use io_lifetimes:: { AsFd , BorrowedFd , FromFd , IntoFd , OwnedFd } ;
9+ use io_lifetimes:: { AsFd , BorrowedFd , OwnedFd } ;
910#[ cfg( windows) ]
1011use io_lifetimes:: { AsHandle , BorrowedHandle , OwnedHandle } ;
1112use std:: fmt;
13+ use std:: mem:: ManuallyDrop ;
1214#[ cfg( unix) ]
1315use {
1416 crate :: os:: unix:: net:: { UnixDatagram , UnixListener , UnixStream } ,
@@ -170,8 +172,8 @@ impl Dir {
170172 to_dir : & Self ,
171173 to : Q ,
172174 ) -> io:: Result < u64 > {
173- let from = from_utf8 ( from) ?;
174- let to = from_utf8 ( to) ?;
175+ let from = from_utf8 ( from. as_ref ( ) ) ?;
176+ let to = from_utf8 ( to. as_ref ( ) ) ?;
175177 self . cap_std . copy ( from, & to_dir. cap_std , to) . await
176178 }
177179
@@ -186,8 +188,8 @@ impl Dir {
186188 dst_dir : & Self ,
187189 dst : Q ,
188190 ) -> io:: Result < ( ) > {
189- let src = from_utf8 ( src) ?;
190- let dst = from_utf8 ( dst) ?;
191+ let src = from_utf8 ( src. as_ref ( ) ) ?;
192+ let dst = from_utf8 ( dst. as_ref ( ) ) ?;
191193 self . cap_std . hard_link ( src, & dst_dir. cap_std , dst) . await
192194 }
193195
@@ -321,8 +323,8 @@ impl Dir {
321323 to_dir : & Self ,
322324 to : Q ,
323325 ) -> io:: Result < ( ) > {
324- let from = from_utf8 ( from) ?;
325- let to = from_utf8 ( to) ?;
326+ let from = from_utf8 ( from. as_ref ( ) ) ?;
327+ let to = from_utf8 ( to. as_ref ( ) ) ?;
326328 self . cap_std . rename ( from, & to_dir. cap_std , to) . await
327329 }
328330
@@ -388,8 +390,8 @@ impl Dir {
388390 original : P ,
389391 link : Q ,
390392 ) -> io:: Result < ( ) > {
391- let original = from_utf8 ( original) ?;
392- let link = from_utf8 ( link) ?;
393+ let original = from_utf8 ( original. as_ref ( ) ) ?;
394+ let link = from_utf8 ( link. as_ref ( ) ) ?;
393395 self . cap_std . symlink ( original, link) . await
394396 }
395397
@@ -416,8 +418,8 @@ impl Dir {
416418 original : P ,
417419 link : Q ,
418420 ) -> io:: Result < ( ) > {
419- let original = from_utf8 ( original) ?;
420- let link = from_utf8 ( link) ?;
421+ let original = from_utf8 ( original. as_ref ( ) ) ?;
422+ let link = from_utf8 ( link. as_ref ( ) ) ?;
421423 self . cap_std . symlink_file ( original, link) . await
422424 }
423425
@@ -444,8 +446,8 @@ impl Dir {
444446 original : P ,
445447 link : Q ,
446448 ) -> io:: Result < ( ) > {
447- let original = from_utf8 ( original) ?;
448- let link = from_utf8 ( link) ?;
449+ let original = from_utf8 ( original. as_ref ( ) ) ?;
450+ let link = from_utf8 ( link. as_ref ( ) ) ?;
449451 self . cap_std . symlink_dir ( original, link) . await
450452 }
451453
@@ -655,15 +657,17 @@ impl Dir {
655657 /// This can be useful when interacting with other libraries and or C/C++
656658 /// code which has invoked `openat(..., O_DIRECTORY)` external to this
657659 /// crate.
658- pub fn reopen_dir < Filelike : AsFilelike > ( dir : & Filelike ) -> io:: Result < Self > {
659- spawn_blocking ( move || {
660- cap_primitives:: fs:: open_dir (
661- & dir. as_filelike_view :: < std:: fs:: File > ( ) ,
662- std:: path:: Component :: CurDir . as_ref ( ) ,
663- )
664- } )
665- . await
666- . map ( Self :: from_std_file)
660+ pub async fn reopen_dir < Filelike : AsFilelike > ( dir : & Filelike ) -> io:: Result < Self > {
661+ // Our public API has a `&Filelike` here, which prevents us from doing
662+ // a `clone` as we usually do. So instead, we use the raw fd, which we
663+ // can clone and depend on it remaining open until we return.
664+ let raw_filelike = dir. as_filelike_view :: < std:: fs:: File > ( ) . as_raw_filelike ( ) ;
665+ // SAFETY: `raw_filelike` remains open for the duration of the `reopen_dir`
666+ // call.
667+ let file = ManuallyDrop :: new ( unsafe { std:: fs:: File :: from_raw_filelike ( raw_filelike) } ) ;
668+ crate :: fs:: Dir :: reopen_dir ( & * file)
669+ . await
670+ . map ( Self :: from_cap_std)
667671 }
668672}
669673
0 commit comments