Skip to content

Commit 9fd1e13

Browse files
committed
Make metadata a FileDescription operation
1 parent b41d968 commit 9fd1e13

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/shims/files.rs

Lines changed: 8 additions & 4 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

@@ -273,8 +277,8 @@ impl VisitProvenance for WeakFileDescriptionRef {
273277

274278
/// A unique id for file descriptions. While we could use the address, considering that
275279
/// is definitely unique, the address would expose interpreter internal state when used
276-
/// for sorting things. So instead we generate a unique id per file description that stays
277-
/// the same even if a file descriptor is duplicated and gets a new integer file descriptor.
280+
/// for sorting things. So instead we generate a unique id per file description is the name
281+
/// for all `dup`licates and is never reused.
278282
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
279283
pub struct FdId(usize);
280284

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
}
@@ -1698,16 +1703,7 @@ impl FileMetadata {
16981703
return interp_ok(Err(LibcError("EBADF")));
16991704
};
17001705

1701-
let file = &fd
1702-
.downcast::<FileHandle>()
1703-
.ok_or_else(|| {
1704-
err_unsup_format!(
1705-
"obtaining metadata is only supported on file-backed file descriptors"
1706-
)
1707-
})?
1708-
.file;
1709-
1710-
let metadata = file.metadata();
1706+
let metadata = fd.metadata()?;
17111707
drop(fd);
17121708
FileMetadata::from_meta(ecx, metadata)
17131709
}

0 commit comments

Comments
 (0)