@@ -17,25 +17,16 @@ use crate::shims::os_str::bytes_to_os_str;
17
17
use crate :: * ;
18
18
use shims:: os_str:: os_str_to_bytes;
19
19
use shims:: time:: system_time_to_duration;
20
- use shims:: unix:: linux:: fd:: epoll:: Epoll ;
21
20
22
21
#[ derive( Debug ) ]
23
22
pub struct FileHandle {
24
23
file : File ,
25
24
writable : bool ,
26
25
}
27
26
28
- pub trait FileDescriptor : std:: fmt:: Debug {
27
+ pub trait FileDescriptor : std:: fmt:: Debug + helpers :: AsAny {
29
28
fn name ( & self ) -> & ' static str ;
30
29
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
-
39
30
fn read < ' tcx > (
40
31
& mut self ,
41
32
_communicate_allowed : bool ,
@@ -69,7 +60,9 @@ pub trait FileDescriptor: std::fmt::Debug {
69
60
70
61
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > ;
71
62
72
- fn is_tty ( & self ) -> bool ;
63
+ fn is_tty ( & self ) -> bool {
64
+ false
65
+ }
73
66
74
67
#[ cfg( unix) ]
75
68
fn as_unix_host_fd ( & self ) -> Option < i32 > {
@@ -82,10 +75,6 @@ impl FileDescriptor for FileHandle {
82
75
"FILE"
83
76
}
84
77
85
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
86
- Ok ( self )
87
- }
88
-
89
78
fn read < ' tcx > (
90
79
& mut self ,
91
80
communicate_allowed : bool ,
@@ -271,10 +260,6 @@ impl FileDescriptor for NullOutput {
271
260
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
272
261
Ok ( Box :: new ( NullOutput ) )
273
262
}
274
-
275
- fn is_tty ( & self ) -> bool {
276
- false
277
- }
278
263
}
279
264
280
265
#[ derive( Debug ) ]
@@ -694,7 +679,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
694
679
} else if this. tcx . sess . target . os == "macos" && cmd == this. eval_libc_i32 ( "F_FULLFSYNC" ) {
695
680
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
696
681
// 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
+ } ) ?;
698
688
let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
699
689
this. try_unwrap_io_result ( io_result)
700
690
} else {
@@ -1530,7 +1520,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1530
1520
Ok ( Scalar :: from_i32 (
1531
1521
if let Some ( file_descriptor) = this. machine . file_handler . handles . get_mut ( & fd) {
1532
1522
// 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
+ } ) ?;
1534
1529
if * writable {
1535
1530
if let Ok ( length) = length. try_into ( ) {
1536
1531
let result = file. set_len ( length) ;
@@ -1571,7 +1566,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1571
1566
1572
1567
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1573
1568
// 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
+ } ) ?;
1575
1573
let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
1576
1574
this. try_unwrap_io_result ( io_result)
1577
1575
} else {
@@ -1593,7 +1591,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1593
1591
1594
1592
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1595
1593
// 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
+ } ) ?;
1597
1600
let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
1598
1601
this. try_unwrap_io_result ( io_result)
1599
1602
} else {
@@ -1638,7 +1641,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1638
1641
1639
1642
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1640
1643
// 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
+ } ) ?;
1642
1650
let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
1643
1651
Ok ( Scalar :: from_i32 ( this. try_unwrap_io_result ( io_result) ?) )
1644
1652
} else {
@@ -1942,7 +1950,16 @@ impl FileMetadata {
1942
1950
) -> InterpResult < ' tcx , Option < FileMetadata > > {
1943
1951
let option = ecx. machine . file_handler . handles . get ( & fd) ;
1944
1952
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 ,
1946
1963
None => return ecx. handle_not_found ( ) . map ( |_: i32 | None ) ,
1947
1964
} ;
1948
1965
let metadata = file. metadata ( ) ;
0 commit comments