@@ -14,7 +14,7 @@ use uhyve_interface::{
1414use crate :: {
1515 isolation:: {
1616 fd:: { FdData , GuestFd , UhyveFileDescriptorLayer } ,
17- filemap:: UhyveFileMap ,
17+ filemap:: { UhyveFileMap , UhyveTreeFile } ,
1818 } ,
1919 mem:: MmapMemory ,
2020 params:: EnvVars ,
@@ -129,18 +129,27 @@ fn translate_last_errno() -> Option<i32> {
129129pub fn unlink ( mem : & MmapMemory , sysunlink : & mut UnlinkParams , file_map : & mut UhyveFileMap ) {
130130 let requested_path_ptr = mem. host_address ( sysunlink. name ) . unwrap ( ) as * const i8 ;
131131 let guest_path = unsafe { CStr :: from_ptr ( requested_path_ptr) } ;
132- sysunlink. ret = if let Some ( host_path) = file_map. get_host_path ( guest_path) {
133- // We can safely unwrap here, as host_path.as_bytes will never contain internal \0 bytes
134- // As host_path_c_string is a valid CString, this implementation is presumed to be safe.
135- let host_path_c_string = CString :: new ( host_path. as_bytes ( ) ) . unwrap ( ) ;
136- if unsafe { libc:: unlink ( host_path_c_string. as_c_str ( ) . as_ptr ( ) ) } < 0 {
137- -translate_last_errno ( ) . unwrap_or ( 1 )
138- } else {
132+ sysunlink. ret = match file_map. unlink ( guest_path) {
133+ Ok ( Some ( host_path) ) => {
134+ // We can safely unwrap here, as host_path.as_bytes will never contain internal \0 bytes
135+ // As host_path_c_string is a valid CString, this implementation is presumed to be safe.
136+ let host_path_c_string = CString :: new ( host_path. as_bytes ( ) ) . unwrap ( ) ;
137+ if unsafe { libc:: unlink ( host_path_c_string. as_c_str ( ) . as_ptr ( ) ) } < 0 {
138+ -translate_last_errno ( ) . unwrap_or ( 1 )
139+ } else {
140+ 0
141+ }
142+ }
143+ Ok ( None ) => {
144+ // Removed virtual entry
139145 0
140146 }
141- } else {
142- error ! ( "The kernel requested to unlink() an unknown path ({guest_path:?}): Rejecting..." ) ;
143- -ENOENT
147+ Err ( ( ) ) => {
148+ error ! (
149+ "The kernel requested to unlink() an unknown path ({guest_path:?}): Rejecting..."
150+ ) ;
151+ -ENOENT
152+ }
144153 } ;
145154}
146155
@@ -181,10 +190,25 @@ pub fn open(mem: &MmapMemory, sysopen: &mut OpenParams, file_map: &mut UhyveFile
181190
182191 sysopen. ret = if let Some ( host_path) = file_map. get_host_path ( guest_path) {
183192 debug ! ( "{guest_path:#?} found in file map." ) ;
184- // We can safely unwrap here, as host_path.as_bytes will never contain internal \0 bytes
185- // As host_path_c_string is a valid CString, this implementation is presumed to be safe.
186- let host_path_c_string = CString :: new ( host_path. as_bytes ( ) ) . unwrap ( ) ;
187- do_open ( & mut file_map. fdmap , host_path_c_string, flags, sysopen. mode )
193+ match host_path {
194+ UhyveTreeFile :: OnHost ( host_path) => {
195+ // We can safely unwrap here, as host_path.as_bytes will never contain internal \0 bytes
196+ // As host_path_c_string is a valid CString, this implementation is presumed to be safe.
197+ let host_path_c_string = CString :: new ( host_path. as_os_str ( ) . as_bytes ( ) ) . unwrap ( ) ;
198+ do_open ( & mut file_map. fdmap , host_path_c_string, flags, sysopen. mode )
199+ }
200+ UhyveTreeFile :: Virtual ( data) => {
201+ if let Some ( guest_fd) = file_map. fdmap . insert ( FdData :: Virtual {
202+ // The following only clones a pointer, and increases an `Arc` refcount.
203+ data : data. clone ( ) ,
204+ offset : 0 ,
205+ } ) {
206+ guest_fd. 0
207+ } else {
208+ -ENOENT
209+ }
210+ }
211+ }
188212 } else {
189213 debug ! ( "{guest_path:#?} not found in file map." ) ;
190214 if ( flags & O_CREAT ) == O_CREAT {
@@ -198,8 +222,15 @@ pub fn open(mem: &MmapMemory, sysopen: &mut OpenParams, file_map: &mut UhyveFile
198222 flags |= file_map. get_io_mode_flags ( ) ;
199223 }
200224
201- let host_path_c_string = file_map. create_temporary_file ( guest_path) ;
202- do_open ( & mut file_map. fdmap , host_path_c_string, flags, sysopen. mode )
225+ match file_map. create_temporary_file ( guest_path) {
226+ Some ( host_path_c_string) => {
227+ do_open ( & mut file_map. fdmap , host_path_c_string, flags, sysopen. mode )
228+ }
229+ None => {
230+ debug ! ( "Returning -EINVAL for {guest_path:#?}" ) ;
231+ -EINVAL
232+ }
233+ }
203234 } else {
204235 debug ! ( "Returning -ENOENT for {guest_path:#?}" ) ;
205236 -ENOENT
0 commit comments