From d0c5f6bb3e6c869b9ffcecce6b36e9960d8aec62 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 28 Nov 2024 17:18:42 -0800 Subject: [PATCH] Make metadata a FileDescription operation --- src/shims/files.rs | 8 ++++++-- src/shims/unix/fs.rs | 18 +++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/shims/files.rs b/src/shims/files.rs index 85aeaf6207..1d533fffad 100644 --- a/src/shims/files.rs +++ b/src/shims/files.rs @@ -1,9 +1,9 @@ use std::any::Any; use std::collections::BTreeMap; -use std::io; use std::io::{IsTerminal, Read, SeekFrom, Write}; use std::ops::Deref; use std::rc::{Rc, Weak}; +use std::{fs, io}; use rustc_abi::Size; @@ -62,6 +62,10 @@ pub trait FileDescription: std::fmt::Debug + Any { throw_unsup_format!("cannot close {}", self.name()); } + fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result> { + throw_unsup_format!("obtaining metadata is only supported on file-backed file descriptors"); + } + fn is_tty(&self, _communicate_allowed: bool) -> bool { // Most FDs are not tty's and the consequence of a wrong `false` are minor, // so we use a default impl here. @@ -216,7 +220,7 @@ impl Deref for FileDescriptionRef { } impl FileDescriptionRef { - fn new(fd: impl FileDescription, id: FdId) -> Self { + pub fn new(fd: impl FileDescription, id: FdId) -> Self { FileDescriptionRef(Rc::new(FileDescWithId { id, file_description: Box::new(fd) })) } diff --git a/src/shims/unix/fs.rs b/src/shims/unix/fs.rs index 9e7e4345af..7eef65d83f 100644 --- a/src/shims/unix/fs.rs +++ b/src/shims/unix/fs.rs @@ -2,7 +2,8 @@ use std::borrow::Cow; use std::fs::{ - DirBuilder, File, FileType, OpenOptions, ReadDir, read_dir, remove_dir, remove_file, rename, + DirBuilder, File, FileType, Metadata, OpenOptions, ReadDir, read_dir, remove_dir, remove_file, + rename, }; use std::io::{self, ErrorKind, IsTerminal, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; @@ -100,6 +101,10 @@ impl FileDescription for FileHandle { } } + fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result> { + interp_ok(self.file.metadata()) + } + fn is_tty(&self, communicate_allowed: bool) -> bool { communicate_allowed && self.file.is_terminal() } @@ -1698,16 +1703,7 @@ impl FileMetadata { return interp_ok(Err(LibcError("EBADF"))); }; - let file = &fd - .downcast::() - .ok_or_else(|| { - err_unsup_format!( - "obtaining metadata is only supported on file-backed file descriptors" - ) - })? - .file; - - let metadata = file.metadata(); + let metadata = fd.metadata()?; drop(fd); FileMetadata::from_meta(ecx, metadata) }