@@ -7,17 +7,20 @@ use libc::{c_char, c_int};
77use sigsafe:: { AsRawFd as _, BorrowedFd , CWD } ;
88
99#[ cfg( target_os = "linux" ) ]
10- fn get_fd_path < A : Allocator > ( allocator : A , fd : BorrowedFd < ' _ > ) -> nix:: Result < Option < Vec < u8 , A > > > {
10+ fn get_fd_path < ' a , A : Allocator > (
11+ allocator : & ' a A ,
12+ fd : BorrowedFd < ' _ > ,
13+ ) -> nix:: Result < Option < & ' a BStr > > {
1114 if fd. as_raw_fd ( ) == CWD . as_raw_fd ( ) {
1215 let path = sigsafe_alloc:: fs:: getcwd ( allocator)
1316 . map_err ( |errno| nix:: errno:: Errno :: from_raw ( errno. raw_os_error ( ) ) ) ?
1417 . into_bytes ( ) ;
15- return Ok ( Some ( path) ) ;
18+ return Ok ( Some ( path. leak ( ) . as_bstr ( ) ) ) ;
1619 }
1720 let mut path = [ 0 ; PROC_FD_PATH_CAPACITY ] ;
1821 let path = proc_fd_path ( fd, & mut path) ;
1922 match sigsafe_alloc:: fs:: readlinkat ( allocator, CWD , path) {
20- Ok ( path) => Ok ( Some ( path) ) ,
23+ Ok ( path) => Ok ( Some ( path. leak ( ) . as_bstr ( ) ) ) ,
2124 Err ( sigsafe:: Errno :: BADF | sigsafe:: Errno :: NOENT ) => Ok ( None ) ,
2225 Err ( errno) => Err ( nix:: errno:: Errno :: from_raw ( errno. raw_os_error ( ) ) ) ,
2326 }
@@ -46,40 +49,40 @@ fn proc_fd_path<'buf>(
4649}
4750
4851#[ cfg( target_os = "macos" ) ]
49- fn get_fd_path < A : Allocator > ( allocator : A , fd : BorrowedFd < ' _ > ) -> nix:: Result < Option < Vec < u8 , A > > > {
52+ fn get_fd_path < ' a , A : Allocator > (
53+ allocator : & ' a A ,
54+ fd : BorrowedFd < ' _ > ,
55+ ) -> nix:: Result < Option < & ' a BStr > > {
5056 if fd. as_raw_fd ( ) == CWD . as_raw_fd ( ) {
5157 let path = sigsafe_alloc:: fs:: getcwd ( allocator)
5258 . map_err ( |errno| nix:: errno:: Errno :: from_raw ( errno. raw_os_error ( ) ) ) ?
5359 . into_bytes ( ) ;
54- return Ok ( Some ( path) ) ;
60+ return Ok ( Some ( path. leak ( ) . as_bstr ( ) ) ) ;
5561 }
5662
5763 match sigsafe_alloc:: fs:: fcntl_getpath ( allocator, fd) {
5864 Ok ( path) => {
5965 // `F_GETPATH` does not return a length. Count at this caller before
6066 // converting its allocation into the returned path.
61- Ok ( Some ( path. count ( ) . into_bytes ( ) ) )
67+ Ok ( Some ( path. count ( ) . into_bytes ( ) . leak ( ) . as_bstr ( ) ) )
6268 }
6369 Err ( sigsafe:: Errno :: BADF | sigsafe:: Errno :: NOENT ) => Ok ( None ) ,
6470 Err ( errno) => Err ( nix:: errno:: Errno :: from_raw ( errno. raw_os_error ( ) ) ) ,
6571 }
6672}
6773
6874pub trait ToAbsolutePath {
69- fn to_absolute_path < R , F : FnOnce ( Option < & BStr > ) -> nix:: Result < R > > (
70- self ,
71- f : F ,
72- ) -> nix:: Result < R > ;
75+ fn to_absolute_path < ' a , A : Allocator > ( self , allocator : & ' a A ) -> nix:: Result < Option < & ' a BStr > >
76+ where
77+ Self : ' a ;
7378}
7479
7580impl ToAbsolutePath for BorrowedFd < ' _ > {
76- fn to_absolute_path < R , F : FnOnce ( Option < & BStr > ) -> nix:: Result < R > > (
77- self ,
78- f : F ,
79- ) -> nix:: Result < R > {
80- let arena = sigsafe_alloc:: arena ( ) ;
81- let path = get_fd_path ( & arena, self ) ?;
82- f ( path. as_ref ( ) . map ( |path| path. as_slice ( ) . as_bstr ( ) ) )
81+ fn to_absolute_path < ' a , A : Allocator > ( self , allocator : & ' a A ) -> nix:: Result < Option < & ' a BStr > >
82+ where
83+ Self : ' a ,
84+ {
85+ get_fd_path ( allocator, self )
8386 }
8487}
8588
@@ -99,46 +102,43 @@ impl PathAt<'_, '_> {
99102}
100103
101104impl ToAbsolutePath for PathAt < ' _ , ' _ > {
102- fn to_absolute_path < R , F : FnOnce ( Option < & BStr > ) -> nix:: Result < R > > (
103- self ,
104- f : F ,
105- ) -> nix :: Result < R > {
105+ fn to_absolute_path < ' a , A : Allocator > ( self , allocator : & ' a A ) -> nix:: Result < Option < & ' a BStr > >
106+ where
107+ Self : ' a ,
108+ {
106109 let pathname = self . 1 . count ( ) . as_bytes ( ) . as_bstr ( ) ;
107110
108111 if pathname. starts_with ( b"/" ) {
109- f ( Some ( pathname) )
112+ Ok ( Some ( pathname) )
110113 } else {
111- self . 0 . to_absolute_path ( |base| {
112- let Some ( base) = base else {
113- return f ( None ) ;
114- } ;
115- if pathname. is_empty ( ) {
116- return f ( Some ( base) ) ;
117- }
118-
119- let arena = sigsafe_alloc:: arena ( ) ;
120- let needs_separator = !base. ends_with ( b"/" ) ;
121- let mut abs_path = Vec :: with_capacity_in (
122- base. len ( ) + usize:: from ( needs_separator) + pathname. len ( ) ,
123- & arena,
124- ) ;
125- abs_path. extend_from_slice ( base) ;
126- if needs_separator {
127- abs_path. push ( b'/' ) ;
128- }
129- abs_path. extend_from_slice ( pathname) ;
130- f ( Some ( abs_path. as_slice ( ) . as_bstr ( ) ) )
131- } )
114+ let Some ( base) = self . 0 . to_absolute_path ( allocator) ? else {
115+ return Ok ( None ) ;
116+ } ;
117+ if pathname. is_empty ( ) {
118+ return Ok ( Some ( base) ) ;
119+ }
120+
121+ let needs_separator = !base. ends_with ( b"/" ) ;
122+ let mut abs_path = Vec :: with_capacity_in (
123+ base. len ( ) + usize:: from ( needs_separator) + pathname. len ( ) ,
124+ allocator,
125+ ) ;
126+ abs_path. extend_from_slice ( base) ;
127+ if needs_separator {
128+ abs_path. push ( b'/' ) ;
129+ }
130+ abs_path. extend_from_slice ( pathname) ;
131+ Ok ( Some ( abs_path. leak ( ) . as_bstr ( ) ) )
132132 }
133133 }
134134}
135135
136136impl ToAbsolutePath for sigsafe:: CStr < ' _ , sigsafe:: Thin > {
137- fn to_absolute_path < R , F : FnOnce ( Option < & BStr > ) -> nix:: Result < R > > (
138- self ,
139- f : F ,
140- ) -> nix :: Result < R > {
141- PathAt ( CWD , self ) . to_absolute_path ( f )
137+ fn to_absolute_path < ' a , A : Allocator > ( self , allocator : & ' a A ) -> nix:: Result < Option < & ' a BStr > >
138+ where
139+ Self : ' a ,
140+ {
141+ PathAt ( CWD , self ) . to_absolute_path ( allocator )
142142 }
143143}
144144
0 commit comments