summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fs-err/src/dir.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/fs-err/src/dir.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/fs-err/src/dir.rs')
-rw-r--r--third_party/rust/fs-err/src/dir.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/third_party/rust/fs-err/src/dir.rs b/third_party/rust/fs-err/src/dir.rs
new file mode 100644
index 0000000000..6efa58da61
--- /dev/null
+++ b/third_party/rust/fs-err/src/dir.rs
@@ -0,0 +1,90 @@
+use std::ffi::OsString;
+use std::fs;
+use std::io;
+use std::path::PathBuf;
+
+use crate::errors::{Error, ErrorKind};
+
+/// Wrapper for [`fs::read_dir`](https://doc.rust-lang.org/stable/std/fs/fn.read_dir.html).
+pub fn read_dir<P: Into<PathBuf>>(path: P) -> io::Result<ReadDir> {
+ let path = path.into();
+
+ match fs::read_dir(&path) {
+ Ok(inner) => Ok(ReadDir { inner, path }),
+ Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path)),
+ }
+}
+
+/// Wrapper around [`std::fs::ReadDir`][std::fs::ReadDir] which adds more
+/// helpful information to all errors.
+///
+/// This struct is created via [`fs_err::read_dir`][fs_err::read_dir].
+///
+/// [std::fs::ReadDir]: https://doc.rust-lang.org/stable/std/fs/struct.ReadDir.html
+/// [fs_err::read_dir]: fn.read_dir.html
+#[derive(Debug)]
+pub struct ReadDir {
+ inner: fs::ReadDir,
+ path: PathBuf,
+}
+
+impl Iterator for ReadDir {
+ type Item = io::Result<DirEntry>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ Some(
+ self.inner
+ .next()?
+ .map_err(|source| Error::build(source, ErrorKind::ReadDir, &self.path))
+ .map(|inner| DirEntry { inner }),
+ )
+ }
+}
+
+/// Wrapper around [`std::fs::DirEntry`][std::fs::DirEntry] which adds more
+/// helpful information to all errors.
+///
+/// [std::fs::DirEntry]: https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html
+#[derive(Debug)]
+pub struct DirEntry {
+ inner: fs::DirEntry,
+}
+
+impl DirEntry {
+ /// Wrapper for [`DirEntry::path`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.path).
+ pub fn path(&self) -> PathBuf {
+ self.inner.path()
+ }
+
+ /// Wrapper for [`DirEntry::metadata`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.metadata).
+ pub fn metadata(&self) -> io::Result<fs::Metadata> {
+ self.inner
+ .metadata()
+ .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
+ }
+
+ /// Wrapper for [`DirEntry::file_type`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.file_type).
+ pub fn file_type(&self) -> io::Result<fs::FileType> {
+ self.inner
+ .file_type()
+ .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
+ }
+
+ /// Wrapper for [`DirEntry::file_name`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.file_name).
+ pub fn file_name(&self) -> OsString {
+ self.inner.file_name()
+ }
+}
+
+#[cfg(unix)]
+mod unix {
+ use std::os::unix::fs::DirEntryExt;
+
+ use super::*;
+
+ impl DirEntryExt for DirEntry {
+ fn ino(&self) -> u64 {
+ self.inner.ino()
+ }
+ }
+}