fs_err/
dir.rs

1use std::ffi::OsString;
2use std::fs;
3use std::io;
4use std::path::PathBuf;
5
6use crate::errors::{Error, ErrorKind};
7
8/// Returns an iterator over the entries within a directory.
9///
10/// Wrapper for [`fs::read_dir`](https://doc.rust-lang.org/stable/std/fs/fn.read_dir.html).
11pub fn read_dir<P: Into<PathBuf>>(path: P) -> io::Result<ReadDir> {
12    let path = path.into();
13
14    match fs::read_dir(&path) {
15        Ok(inner) => Ok(ReadDir { inner, path }),
16        Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path)),
17    }
18}
19
20/// Wrapper around [`std::fs::ReadDir`][std::fs::ReadDir] which adds more
21/// helpful information to all errors.
22///
23/// This struct is created via [`fs_err::read_dir`][fs_err::read_dir].
24///
25/// [std::fs::ReadDir]: https://doc.rust-lang.org/stable/std/fs/struct.ReadDir.html
26/// [fs_err::read_dir]: fn.read_dir.html
27#[derive(Debug)]
28pub struct ReadDir {
29    inner: fs::ReadDir,
30    path: PathBuf,
31}
32
33impl Iterator for ReadDir {
34    type Item = io::Result<DirEntry>;
35
36    fn next(&mut self) -> Option<Self::Item> {
37        Some(
38            self.inner
39                .next()?
40                .map_err(|source| Error::build(source, ErrorKind::ReadDir, &self.path))
41                .map(|inner| DirEntry { inner }),
42        )
43    }
44}
45
46/// Wrapper around [`std::fs::DirEntry`][std::fs::DirEntry] which adds more
47/// helpful information to all errors.
48///
49/// [std::fs::DirEntry]: https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html
50#[derive(Debug)]
51pub struct DirEntry {
52    inner: fs::DirEntry,
53}
54
55impl DirEntry {
56    /// Returns the full path to the file that this entry represents.
57    ///
58    /// Wrapper for [`DirEntry::path`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.path).
59    pub fn path(&self) -> PathBuf {
60        self.inner.path()
61    }
62
63    /// Returns the metadata for the file that this entry points at.
64    ///
65    /// Wrapper for [`DirEntry::metadata`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.metadata).
66    pub fn metadata(&self) -> io::Result<fs::Metadata> {
67        self.inner
68            .metadata()
69            .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
70    }
71
72    /// Returns the file type for the file that this entry points at.
73    ///
74    /// Wrapper for [`DirEntry::file_type`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.file_type).
75    pub fn file_type(&self) -> io::Result<fs::FileType> {
76        self.inner
77            .file_type()
78            .map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
79    }
80
81    /// Returns the file name of this directory entry without any leading path component(s).
82    ///
83    /// Wrapper for [`DirEntry::file_name`](https://doc.rust-lang.org/stable/std/fs/struct.DirEntry.html#method.file_name).
84    pub fn file_name(&self) -> OsString {
85        self.inner.file_name()
86    }
87}
88
89#[cfg(unix)]
90mod unix {
91    use std::os::unix::fs::DirEntryExt;
92
93    use super::*;
94
95    impl DirEntryExt for DirEntry {
96        fn ino(&self) -> u64 {
97            self.inner.ino()
98        }
99    }
100}