cap_std/fs_utf8/dir_entry.rs
1use crate::fs::{FileType, Metadata, OpenOptions};
2use crate::fs_utf8::{to_utf8, Dir, File};
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 way
20/// to construct a `DirEntry` without opening directories by ambient paths.
21pub struct DirEntry {
22 cap_std: crate::fs::DirEntry,
23}
24
25impl DirEntry {
26 /// Constructs a new instance of `Self` from the given
27 /// `cap_std::fs::DirEntry`.
28 #[inline]
29 pub fn from_cap_std(cap_std: crate::fs::DirEntry) -> Self {
30 Self { cap_std }
31 }
32
33 /// Open the file for reading.
34 #[inline]
35 pub fn open(&self) -> io::Result<File> {
36 self.cap_std.open().map(File::from_cap_std)
37 }
38
39 /// Open the file with the given options.
40 #[inline]
41 pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
42 self.cap_std.open_with(options).map(File::from_cap_std)
43 }
44
45 /// Open the entry as a directory.
46 #[inline]
47 pub fn open_dir(&self) -> io::Result<Dir> {
48 self.cap_std.open_dir().map(Dir::from_cap_std)
49 }
50
51 /// Removes the file from its filesystem.
52 #[inline]
53 pub fn remove_file(&self) -> io::Result<()> {
54 self.cap_std.remove_file()
55 }
56
57 /// Removes the directory from its filesystem.
58 #[inline]
59 pub fn remove_dir(&self) -> io::Result<()> {
60 self.cap_std.remove_dir()
61 }
62
63 /// Returns the metadata for the file that this entry points at.
64 ///
65 /// This corresponds to [`std::fs::DirEntry::metadata`].
66 #[inline]
67 pub fn metadata(&self) -> io::Result<Metadata> {
68 self.cap_std.metadata()
69 }
70
71 /// Returns the file type for the file that this entry points at.
72 ///
73 /// This corresponds to [`std::fs::DirEntry::file_type`].
74 #[inline]
75 pub fn file_type(&self) -> io::Result<FileType> {
76 self.cap_std.file_type()
77 }
78
79 /// Returns the bare file name of this directory entry without any other
80 /// leading path component.
81 ///
82 /// This function returns an `Err` in the case that the file name isn't
83 /// encodable as UTF-8.
84 ///
85 /// If the `arf_strings` feature is enabled, unencodable names are
86 /// translated to UTF-8 using `arf-strings`.
87 ///
88 /// This corresponds to [`std::fs::DirEntry::file_name`].
89 #[inline]
90 pub fn file_name(&self) -> io::Result<String> {
91 Ok(to_utf8(self.cap_std.file_name())?.into())
92 }
93}
94
95#[cfg(not(windows))]
96impl DirEntryExt for DirEntry {
97 #[inline]
98 fn ino(&self) -> u64 {
99 self.cap_std.ino()
100 }
101}
102
103#[cfg(windows)]
104#[doc(hidden)]
105impl cap_primitives::fs::_WindowsDirEntryExt for DirEntry {
106 #[inline]
107 fn full_metadata(&self) -> io::Result<Metadata> {
108 self.cap_std.full_metadata()
109 }
110}
111
112impl fmt::Debug for DirEntry {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 self.cap_std.fmt(f)
115 }
116}