1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::ffi::OsString;
use std::fs;
use std::io;
use std::path::PathBuf;
use crate::errors::{Error, ErrorKind};
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)),
}
}
#[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 }),
)
}
}
#[derive(Debug)]
pub struct DirEntry {
inner: fs::DirEntry,
}
impl DirEntry {
pub fn path(&self) -> PathBuf {
self.inner.path()
}
pub fn metadata(&self) -> io::Result<fs::Metadata> {
self.inner
.metadata()
.map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
}
pub fn file_type(&self) -> io::Result<fs::FileType> {
self.inner
.file_type()
.map_err(|source| Error::build(source, ErrorKind::Metadata, self.path()))
}
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()
}
}
}