cap_std/fs/
dir_entry.rs

1use crate::fs::{Dir, File, FileType, Metadata, OpenOptions};
2#[cfg(not(windows))]
3use rustix::fs::DirEntryExt;
4use std::ffi::OsString;
5use std::{fmt, io};
6
7/// Entries returned by the `ReadDir` iterator.
8///
9/// This corresponds to [`std::fs::DirEntry`].
10///
11/// Unlike `std::fs::DirEntry`, this API has no `DirEntry::path`, because
12/// absolute paths don't interoperate well with the capability model.
13///
14/// There is a `file_name` function, however there are also `open`,
15/// `open_with`, `open_dir`, `remove_file`, and `remove_dir` functions for
16/// opening or removing the entry directly, which can be more efficient and
17/// convenient.
18///
19/// There is no `from_std` method, as `std::fs::DirEntry` doesn't provide a
20/// way to construct a `DirEntry` without opening directories by ambient paths.
21pub struct DirEntry {
22    pub(crate) inner: cap_primitives::fs::DirEntry,
23}
24
25impl DirEntry {
26    /// Open the file for reading.
27    #[inline]
28    pub fn open(&self) -> io::Result<File> {
29        let file = self.inner.open()?;
30        Ok(File::from_std(file))
31    }
32
33    /// Open the file with the given options.
34    #[inline]
35    pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
36        let file = self.inner.open_with(options)?;
37        Ok(File::from_std(file))
38    }
39
40    /// Open the entry as a directory.
41    #[inline]
42    pub fn open_dir(&self) -> io::Result<Dir> {
43        let dir = self.inner.open_dir()?;
44        Ok(Dir::from_std_file(dir))
45    }
46
47    /// Removes the file from its filesystem.
48    #[inline]
49    pub fn remove_file(&self) -> io::Result<()> {
50        self.inner.remove_file()
51    }
52
53    /// Removes the directory from its filesystem.
54    #[inline]
55    pub fn remove_dir(&self) -> io::Result<()> {
56        self.inner.remove_dir()
57    }
58
59    /// Returns the metadata for the file that this entry points at.
60    ///
61    /// This corresponds to [`std::fs::DirEntry::metadata`].
62    #[inline]
63    pub fn metadata(&self) -> io::Result<Metadata> {
64        self.inner.metadata()
65    }
66
67    /// Returns the file type for the file that this entry points at.
68    ///
69    /// This corresponds to [`std::fs::DirEntry::file_type`].
70    #[inline]
71    pub fn file_type(&self) -> io::Result<FileType> {
72        self.inner.file_type()
73    }
74
75    /// Returns the bare file name of this directory entry without any other
76    /// leading path component.
77    ///
78    /// This corresponds to [`std::fs::DirEntry::file_name`].
79    #[inline]
80    pub fn file_name(&self) -> OsString {
81        self.inner.file_name()
82    }
83}
84
85#[cfg(not(windows))]
86impl DirEntryExt for DirEntry {
87    #[inline]
88    fn ino(&self) -> u64 {
89        self.inner.ino()
90    }
91}
92
93#[cfg(windows)]
94#[doc(hidden)]
95impl cap_primitives::fs::_WindowsDirEntryExt for DirEntry {
96    #[inline]
97    fn full_metadata(&self) -> io::Result<Metadata> {
98        cap_primitives::fs::_WindowsDirEntryExt::full_metadata(&self.inner)
99    }
100}
101
102impl fmt::Debug for DirEntry {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        self.inner.fmt(f)
105    }
106}