cap_async_std/fs/
dir_entry.rs

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