@@ -27,10 +27,6 @@ pub struct FileHandle {
27
27
pub trait FileDescriptor : std:: fmt:: Debug + helpers:: AsAny {
28
28
fn name ( & self ) -> & ' static str ;
29
29
30
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
31
- throw_unsup_format ! ( "{} cannot be used as FileHandle" , self . name( ) ) ;
32
- }
33
-
34
30
fn read < ' tcx > (
35
31
& mut self ,
36
32
_communicate_allowed : bool ,
@@ -79,10 +75,6 @@ impl FileDescriptor for FileHandle {
79
75
"FILE"
80
76
}
81
77
82
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
83
- Ok ( self )
84
- }
85
-
86
78
fn read < ' tcx > (
87
79
& mut self ,
88
80
communicate_allowed : bool ,
@@ -687,7 +679,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
687
679
} else if this. tcx . sess . target . os == "macos" && cmd == this. eval_libc_i32 ( "F_FULLFSYNC" ) {
688
680
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
689
681
// FIXME: Support fullfsync for all FDs
690
- 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
+ } ) ?;
691
688
let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
692
689
this. try_unwrap_io_result ( io_result)
693
690
} else {
@@ -1523,7 +1520,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1523
1520
Ok ( Scalar :: from_i32 (
1524
1521
if let Some ( file_descriptor) = this. machine . file_handler . handles . get_mut ( & fd) {
1525
1522
// FIXME: Support ftruncate64 for all FDs
1526
- 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
+ } ) ?;
1527
1529
if * writable {
1528
1530
if let Ok ( length) = length. try_into ( ) {
1529
1531
let result = file. set_len ( length) ;
@@ -1564,7 +1566,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1564
1566
1565
1567
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1566
1568
// FIXME: Support fsync for all FDs
1567
- 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
+ } ) ?;
1568
1573
let io_result = maybe_sync_file ( file, * writable, File :: sync_all) ;
1569
1574
this. try_unwrap_io_result ( io_result)
1570
1575
} else {
@@ -1586,7 +1591,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1586
1591
1587
1592
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1588
1593
// FIXME: Support fdatasync for all FDs
1589
- 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
+ } ) ?;
1590
1600
let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
1591
1601
this. try_unwrap_io_result ( io_result)
1592
1602
} else {
@@ -1631,7 +1641,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
1631
1641
1632
1642
if let Some ( file_descriptor) = this. machine . file_handler . handles . get ( & fd) {
1633
1643
// FIXME: Support sync_data_range for all FDs
1634
- 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
+ } ) ?;
1635
1650
let io_result = maybe_sync_file ( file, * writable, File :: sync_data) ;
1636
1651
Ok ( Scalar :: from_i32 ( this. try_unwrap_io_result ( io_result) ?) )
1637
1652
} else {
@@ -1935,7 +1950,16 @@ impl FileMetadata {
1935
1950
) -> InterpResult < ' tcx , Option < FileMetadata > > {
1936
1951
let option = ecx. machine . file_handler . handles . get ( & fd) ;
1937
1952
let file = match option {
1938
- 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 ,
1939
1963
None => return ecx. handle_not_found ( ) . map ( |_: i32 | None ) ,
1940
1964
} ;
1941
1965
let metadata = file. metadata ( ) ;
0 commit comments