Skip to content

Commit

Permalink
Make metadata a FileDescription operation
Browse files Browse the repository at this point in the history
  • Loading branch information
CraftSpider committed Dec 1, 2024
1 parent b41d968 commit 9fd1e13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/shims/files.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<fs::Metadata>> {
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.
Expand Down Expand Up @@ -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) }))
}

Expand Down Expand Up @@ -273,8 +277,8 @@ impl VisitProvenance for WeakFileDescriptionRef {

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

Expand Down
18 changes: 7 additions & 11 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -100,6 +101,10 @@ impl FileDescription for FileHandle {
}
}

fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result<Metadata>> {
interp_ok(self.file.metadata())
}

fn is_tty(&self, communicate_allowed: bool) -> bool {
communicate_allowed && self.file.is_terminal()
}
Expand Down Expand Up @@ -1698,16 +1703,7 @@ impl FileMetadata {
return interp_ok(Err(LibcError("EBADF")));
};

let file = &fd
.downcast::<FileHandle>()
.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)
}
Expand Down

0 comments on commit 9fd1e13

Please sign in to comment.