dir_view/
dir_entry_utf8.rs

1use crate::{DirViewUtf8, ViewKind};
2use cap_std::fs_utf8::{DirEntry, File, FileType, Metadata, OpenOptions};
3#[cfg(not(windows))]
4use rustix::fs::DirEntryExt;
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 DirEntryViewUtf8 {
22    pub(crate) entry: DirEntry,
23    pub(crate) view_kind: ViewKind,
24}
25
26impl DirEntryViewUtf8 {
27    /// Open the file for reading.
28    #[inline]
29    pub fn open(&self) -> io::Result<File> {
30        self.entry.open()
31    }
32
33    /// Open the file with the given options.
34    #[inline]
35    pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
36        // Override any flag that allows writing.
37        let mut options = options.clone();
38        match self.view_kind {
39            ViewKind::Full => {}
40            ViewKind::Readonly => {
41                // Override any flag that allows writing.
42                options.append(false);
43                options.truncate(false);
44                options.write(false);
45                options.create(false);
46                options.create_new(false);
47            }
48        }
49        self.entry.open_with(&options)
50    }
51
52    /// Open the entry as a directory.
53    #[inline]
54    pub fn open_dir(&self) -> io::Result<DirViewUtf8> {
55        Ok(DirViewUtf8 {
56            dir: self.entry.open_dir()?,
57            view_kind: self.view_kind,
58        })
59    }
60
61    /// Removes the file from its filesystem.
62    #[inline]
63    pub fn remove_file(&self) -> io::Result<()> {
64        self.check_mutation()?;
65        self.entry.remove_file()
66    }
67
68    /// Removes the directory from its filesystem.
69    #[inline]
70    pub fn remove_dir(&self) -> io::Result<()> {
71        self.check_mutation()?;
72        self.entry.remove_dir()
73    }
74
75    /// Returns the metadata for the file that this entry points at.
76    ///
77    /// This corresponds to [`std::fs::DirEntry::metadata`].
78    #[inline]
79    pub fn metadata(&self) -> io::Result<Metadata> {
80        self.entry.metadata()
81    }
82
83    /// Returns the file type for the file that this entry points at.
84    ///
85    /// This corresponds to [`std::fs::DirEntry::file_type`].
86    #[inline]
87    pub fn file_type(&self) -> io::Result<FileType> {
88        self.entry.file_type()
89    }
90
91    /// Returns the bare file name of this directory entry without any other
92    /// leading path component.
93    ///
94    /// This corresponds to [`std::fs::DirEntry::file_name`].
95    #[inline]
96    pub fn file_name(&self) -> io::Result<String> {
97        self.entry.file_name()
98    }
99
100    fn check_mutation(&self) -> io::Result<()> {
101        match self.view_kind {
102            ViewKind::Full => Ok(()),
103            ViewKind::Readonly => Err(Self::readonly()),
104        }
105    }
106
107    fn readonly() -> io::Error {
108        io::Error::new(
109            io::ErrorKind::PermissionDenied,
110            "attempt to modify a directory tree through a read-only `DirViewUtf8`",
111        )
112    }
113}
114
115#[cfg(not(windows))]
116impl DirEntryExt for DirEntryViewUtf8 {
117    #[inline]
118    fn ino(&self) -> u64 {
119        self.entry.ino()
120    }
121}
122
123#[cfg(windows)]
124#[doc(hidden)]
125impl cap_primitives::fs::_WindowsDirEntryExt for DirEntryViewUtf8 {
126    #[inline]
127    fn full_metadata(&self) -> io::Result<Metadata> {
128        cap_primitives::fs::_WindowsDirEntryExt::full_metadata(&self.entry)
129    }
130}
131
132impl fmt::Debug for DirEntryViewUtf8 {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        self.entry.fmt(f)
135    }
136}
137
138#[cfg(feature = "cap-fs-ext")]
139impl cap_fs_ext::DirEntryExt for DirEntryViewUtf8 {
140    fn full_metadata(&self) -> io::Result<Metadata> {
141        cap_fs_ext::DirEntryExt::full_metadata(&self.entry)
142    }
143}