11use std:: ffi:: { CStr , CString } ;
22use std:: fs:: { DirBuilder , File , Metadata , OpenOptions } ;
33use std:: io:: { self , Error , ErrorKind } ;
4- use std:: os:: fd:: { AsRawFd , FromRawFd } ;
4+ use std:: os:: fd:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , OwnedFd } ;
55use std:: os:: unix:: {
66 ffi:: OsStrExt ,
77 fs:: { DirBuilderExt , MetadataExt , PermissionsExt } ,
@@ -115,7 +115,7 @@ fn secure_open_impl(
115115}
116116
117117#[ cfg_attr( not( feature = "sudoedit" ) , allow( dead_code) ) ]
118- fn open_at ( parent : & File , file_name : & CStr , create : bool ) -> io:: Result < File > {
118+ fn open_at ( parent : BorrowedFd , file_name : & CStr , create : bool ) -> io:: Result < OwnedFd > {
119119 let flags = if create {
120120 libc:: O_NOFOLLOW | libc:: O_RDWR | libc:: O_CREAT
121121 } else {
@@ -135,14 +135,14 @@ fn open_at(parent: &File, file_name: &CStr, create: bool) -> io::Result<File> {
135135 mode,
136136 ) ) ?;
137137
138- Ok ( File :: from_raw_fd ( fd) )
138+ Ok ( OwnedFd :: from_raw_fd ( fd) )
139139 }
140140}
141141
142- #[ cfg_attr( not( feature = "sudoedit" ) , allow( dead_code) ) ]
143142/// This opens a file making sure that
144143/// - no directory leading up to the file is editable by the user
145144/// - no components are a symbolic link
145+ #[ cfg_attr( not( feature = "sudoedit" ) , allow( dead_code) ) ]
146146fn traversed_secure_open ( path : impl AsRef < Path > , user : & User ) -> io:: Result < File > {
147147 let path = path. as_ref ( ) ;
148148
@@ -191,11 +191,11 @@ fn traversed_secure_open(path: impl AsRef<Path>, user: &User) -> io::Result<File
191191 }
192192 } ;
193193
194- cur = open_at ( & cur, & dir, false ) ?;
194+ cur = open_at ( cur. as_fd ( ) , & dir, false ) ?. into ( ) ;
195195 user_cannot_write ( & cur) ?;
196196 }
197197
198- cur = open_at ( & cur, & CString :: new ( file_name. as_bytes ( ) ) ?, true ) ?;
198+ cur = open_at ( cur. as_fd ( ) , & CString :: new ( file_name. as_bytes ( ) ) ?, true ) ?. into ( ) ;
199199 user_cannot_write ( & cur) ?;
200200
201201 Ok ( cur)
0 commit comments