Skip to content

Commit ddfa3a8

Browse files
committed
Make metadata a FileDescription operation
1 parent 7f317ca commit ddfa3a8

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/shims/files.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::any::Any;
22
use std::collections::BTreeMap;
3-
use std::io;
43
use std::io::{IsTerminal, Read, SeekFrom, Write};
54
use std::ops::Deref;
65
use std::rc::{Rc, Weak};
6+
use std::{fs, io};
77

88
use rustc_abi::Size;
99

@@ -62,6 +62,10 @@ pub trait FileDescription: std::fmt::Debug + Any {
6262
throw_unsup_format!("cannot close {}", self.name());
6363
}
6464

65+
fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result<fs::Metadata>> {
66+
throw_unsup_format!("obtaining metadata is only supported on file-backed file descriptors");
67+
}
68+
6569
fn is_tty(&self, _communicate_allowed: bool) -> bool {
6670
// Most FDs are not tty's and the consequence of a wrong `false` are minor,
6771
// so we use a default impl here.
@@ -216,7 +220,7 @@ impl Deref for FileDescriptionRef {
216220
}
217221

218222
impl FileDescriptionRef {
219-
fn new(fd: impl FileDescription, id: FdId) -> Self {
223+
pub fn new(fd: impl FileDescription, id: FdId) -> Self {
220224
FileDescriptionRef(Rc::new(FileDescWithId { id, file_description: Box::new(fd) }))
221225
}
222226

src/shims/unix/fs.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use std::borrow::Cow;
44
use std::fs::{
5-
DirBuilder, File, FileType, OpenOptions, ReadDir, read_dir, remove_dir, remove_file, rename,
5+
DirBuilder, File, FileType, Metadata, OpenOptions, ReadDir, read_dir, remove_dir, remove_file,
6+
rename,
67
};
78
use std::io::{self, ErrorKind, IsTerminal, Read, Seek, SeekFrom, Write};
89
use std::path::{Path, PathBuf};
@@ -100,6 +101,10 @@ impl FileDescription for FileHandle {
100101
}
101102
}
102103

104+
fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result<Metadata>> {
105+
interp_ok(self.file.metadata())
106+
}
107+
103108
fn is_tty(&self, communicate_allowed: bool) -> bool {
104109
communicate_allowed && self.file.is_terminal()
105110
}
@@ -1680,16 +1685,7 @@ impl FileMetadata {
16801685
return interp_ok(Err(LibcError("EBADF")));
16811686
};
16821687

1683-
let file = &fd
1684-
.downcast::<FileHandle>()
1685-
.ok_or_else(|| {
1686-
err_unsup_format!(
1687-
"obtaining metadata is only supported on file-backed file descriptors"
1688-
)
1689-
})?
1690-
.file;
1691-
1692-
let metadata = file.metadata();
1688+
let metadata = fd.metadata()?;
16931689
drop(fd);
16941690
FileMetadata::from_meta(ecx, metadata)
16951691
}

0 commit comments

Comments
 (0)