cap_primitives/rustix/fs/
dir_entry_inner.rs

1use crate::fs::{
2    FileType, FollowSymlinks, Metadata, MetadataExt, OpenOptions, ReadDir, ReadDirInner,
3};
4use rustix::fs::DirEntry;
5use std::ffi::{OsStr, OsString};
6#[cfg(unix)]
7use std::os::unix::ffi::OsStrExt;
8#[cfg(target_os = "wasi")]
9use std::os::wasi::ffi::OsStrExt;
10use std::{fmt, fs, io};
11
12pub(crate) struct DirEntryInner {
13    pub(super) rustix: DirEntry,
14    pub(super) read_dir: ReadDirInner,
15}
16
17impl DirEntryInner {
18    #[inline]
19    pub(crate) fn open(&self, options: &OpenOptions) -> io::Result<fs::File> {
20        self.read_dir.open(self.file_name_bytes(), options)
21    }
22
23    #[inline]
24    pub(crate) fn metadata(&self) -> io::Result<Metadata> {
25        self.read_dir.metadata(self.file_name_bytes())
26    }
27
28    #[inline]
29    pub(crate) fn remove_file(&self) -> io::Result<()> {
30        self.read_dir.remove_file(self.file_name_bytes())
31    }
32
33    #[inline]
34    pub(crate) fn read_dir(&self, follow: FollowSymlinks) -> io::Result<ReadDir> {
35        self.read_dir.read_dir(self.file_name_bytes(), follow)
36    }
37
38    #[inline]
39    pub(crate) fn remove_dir(&self) -> io::Result<()> {
40        self.read_dir.remove_dir(self.file_name_bytes())
41    }
42
43    #[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
44    #[inline]
45    #[allow(clippy::unnecessary_wraps)]
46    pub(crate) fn file_type(&self) -> io::Result<FileType> {
47        use crate::fs::ImplFileTypeExt;
48
49        Ok(match self.rustix.file_type() {
50            rustix::fs::FileType::Directory => FileType::dir(),
51            rustix::fs::FileType::RegularFile => FileType::file(),
52            rustix::fs::FileType::Symlink => FileType::ext(ImplFileTypeExt::symlink()),
53            #[cfg(not(target_os = "wasi"))]
54            rustix::fs::FileType::Fifo => FileType::ext(ImplFileTypeExt::fifo()),
55            #[cfg(not(target_os = "wasi"))]
56            rustix::fs::FileType::Socket => FileType::ext(ImplFileTypeExt::socket()),
57            rustix::fs::FileType::CharacterDevice => FileType::ext(ImplFileTypeExt::char_device()),
58            rustix::fs::FileType::BlockDevice => FileType::ext(ImplFileTypeExt::block_device()),
59            rustix::fs::FileType::Unknown => FileType::unknown(),
60        })
61    }
62
63    #[cfg(any(target_os = "illumos", target_os = "solaris"))]
64    #[inline]
65    pub(crate) fn file_type(&self) -> io::Result<FileType> {
66        // These platforms don't have the file type on the dirent, so we must lstat the file.
67        self.metadata().map(|m| m.file_type())
68    }
69
70    #[inline]
71    pub(crate) fn file_name(&self) -> OsString {
72        self.file_name_bytes().to_os_string()
73    }
74
75    #[inline]
76    pub(crate) fn ino(&self) -> u64 {
77        self.rustix.ino()
78    }
79
80    #[inline]
81    pub(crate) fn is_same_file(&self, metadata: &Metadata) -> io::Result<bool> {
82        let self_md = self.metadata()?;
83        Ok(self_md.ino() == metadata.ino() && self_md.dev() == metadata.dev())
84    }
85
86    fn file_name_bytes(&self) -> &OsStr {
87        OsStr::from_bytes(self.rustix.file_name().to_bytes())
88    }
89}
90
91impl fmt::Debug for DirEntryInner {
92    // Like libstd's version, but doesn't print the path.
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        f.debug_tuple("DirEntry").field(&self.file_name()).finish()
95    }
96}