@@ -17,25 +17,16 @@ use crate::shims::os_str::bytes_to_os_str;
1717use crate :: * ;
1818use shims:: os_str:: os_str_to_bytes;
1919use shims:: time:: system_time_to_duration;
20- use shims:: unix:: linux:: fd:: epoll:: Epoll ;
2120
2221#[ derive( Debug ) ]
2322pub struct FileHandle {
2423 file : File ,
2524 writable : bool ,
2625}
2726
28- pub trait FileDescriptor : std:: fmt:: Debug {
27+ pub trait FileDescriptor : std:: fmt:: Debug + helpers :: AsAny {
2928 fn name ( & self ) -> & ' static str ;
3029
31- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
32- throw_unsup_format ! ( "{} cannot be used as FileHandle" , self . name( ) ) ;
33- }
34-
35- fn as_epoll_handle < ' tcx > ( & mut self ) -> InterpResult < ' tcx , & mut Epoll > {
36- throw_unsup_format ! ( "not an epoll file descriptor" ) ;
37- }
38-
3930 fn read < ' tcx > (
4031 & mut self ,
4132 _communicate_allowed : bool ,
@@ -69,7 +60,9 @@ pub trait FileDescriptor: std::fmt::Debug {
6960
7061 fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > ;
7162
72- fn is_tty ( & self ) -> bool ;
63+ fn is_tty ( & self ) -> bool {
64+ false
65+ }
7366
7467 #[ cfg( unix) ]
7568 fn as_unix_host_fd ( & self ) -> Option < i32 > {
@@ -82,10 +75,6 @@ impl FileDescriptor for FileHandle {
8275 "FILE"
8376 }
8477
85- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
86- Ok ( self )
87- }
88-
8978 fn read < ' tcx > (
9079 & mut self ,
9180 communicate_allowed : bool ,
@@ -271,10 +260,6 @@ impl FileDescriptor for NullOutput {
271260 fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
272261 Ok ( Box :: new ( NullOutput ) )
273262 }
274-
275- fn is_tty ( & self ) -> bool {
276- false
277- }
278263}
279264
280265#[ derive( Debug ) ]
@@ -694,7 +679,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
694679 } else if this. tcx . sess . target . os == "macos" && cmd == this. eval_libc_i32 ( "F_FULLFSYNC" ) {
695680 if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
696681 // FIXME: Support fullfsync for all FDs
697- let FileHandle { file, writable } = file_descriptor. as_file_handle ( ) ?;
682+ let FileHandle { file, writable } =
683+ file_descriptor. as_any ( ) . downcast_ref :: < FileHandle > ( ) . ok_or_else ( || {
684+ err_unsup_format ! (
685+ "`F_FULLFSYNC` is only supported on file-backed file descriptors"
686+ )
687+ } ) ?;
698688 let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
699689 this. try_unwrap_io_result ( io_result)
700690 } else {
@@ -1530,7 +1520,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15301520 Ok ( Scalar :: from_i32 (
15311521 if let Some ( file_descriptor) = this. machine . file_handler . handles . get_mut ( & fd) {
15321522 // FIXME: Support ftruncate64 for all FDs
1533- let FileHandle { file, writable } = file_descriptor. as_file_handle ( ) ?;
1523+ let FileHandle { file, writable } =
1524+ file_descriptor. as_any ( ) . downcast_ref :: < FileHandle > ( ) . ok_or_else ( || {
1525+ err_unsup_format ! (
1526+ "`ftruncate64` is only supported on file-backed file descriptors"
1527+ )
1528+ } ) ?;
15341529 if * writable {
15351530 if let Ok ( length) = length. try_into ( ) {
15361531 let result = file. set_len ( length) ;
@@ -1571,7 +1566,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15711566
15721567 if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
15731568 // FIXME: Support fsync for all FDs
1574- let FileHandle { file, writable } = file_descriptor. as_file_handle ( ) ?;
1569+ let FileHandle { file, writable } =
1570+ file_descriptor. as_any ( ) . downcast_ref :: < FileHandle > ( ) . ok_or_else ( || {
1571+ err_unsup_format ! ( "`fsync` is only supported on file-backed file descriptors" )
1572+ } ) ?;
15751573 let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
15761574 this. try_unwrap_io_result ( io_result)
15771575 } else {
@@ -1593,7 +1591,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15931591
15941592 if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
15951593 // FIXME: Support fdatasync for all FDs
1596- let FileHandle { file, writable } = file_descriptor. as_file_handle ( ) ?;
1594+ let FileHandle { file, writable } =
1595+ file_descriptor. as_any ( ) . downcast_ref :: < FileHandle > ( ) . ok_or_else ( || {
1596+ err_unsup_format ! (
1597+ "`fdatasync` is only supported on file-backed file descriptors"
1598+ )
1599+ } ) ?;
15971600 let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
15981601 this. try_unwrap_io_result ( io_result)
15991602 } else {
@@ -1638,7 +1641,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
16381641
16391642 if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
16401643 // FIXME: Support sync_data_range for all FDs
1641- let FileHandle { file, writable } = file_descriptor. as_file_handle ( ) ?;
1644+ let FileHandle { file, writable } =
1645+ file_descriptor. as_any ( ) . downcast_ref :: < FileHandle > ( ) . ok_or_else ( || {
1646+ err_unsup_format ! (
1647+ "`sync_data_range` is only supported on file-backed file descriptors"
1648+ )
1649+ } ) ?;
16421650 let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
16431651 Ok ( Scalar :: from_i32 ( this. try_unwrap_io_result ( io_result) ?) )
16441652 } else {
@@ -1942,7 +1950,16 @@ impl FileMetadata {
19421950 ) -> InterpResult < ' tcx , Option < FileMetadata > > {
19431951 let option = ecx. machine . file_handler . handles . get ( & fd) ;
19441952 let file = match option {
1945- Some ( file_descriptor) => & file_descriptor. as_file_handle ( ) ?. file ,
1953+ Some ( file_descriptor) =>
1954+ & file_descriptor
1955+ . as_any ( )
1956+ . downcast_ref :: < FileHandle > ( )
1957+ . ok_or_else ( || {
1958+ err_unsup_format ! (
1959+ "obtaining metadata is only supported on file-backed file descriptors"
1960+ )
1961+ } ) ?
1962+ . file ,
19461963 None => return ecx. handle_not_found ( ) . map ( |_: i32 | None ) ,
19471964 } ;
19481965 let metadata = file. metadata ( ) ;
0 commit comments