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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::fs::{self, FileType};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::{ClientState, Error, ReadDirSpec, Result};
pub struct DirEntry<C: ClientState> {
pub depth: usize,
pub file_name: OsString,
pub file_type: FileType,
pub client_state: C::DirEntryState,
pub parent_path: Arc<Path>,
pub read_children_path: Option<Arc<Path>>,
pub read_children_error: Option<Error>,
follow_link: bool,
follow_link_ancestors: Arc<Vec<Arc<Path>>>,
}
impl<C: ClientState> DirEntry<C> {
pub(crate) fn from_entry(
depth: usize,
parent_path: Arc<Path>,
fs_dir_entry: &fs::DirEntry,
follow_link_ancestors: Arc<Vec<Arc<Path>>>,
) -> Result<Self> {
let file_type = fs_dir_entry
.file_type()
.map_err(|err| Error::from_path(depth, fs_dir_entry.path(), err))?;
let file_name = fs_dir_entry.file_name();
let read_children_path: Option<Arc<Path>> = if file_type.is_dir() {
Some(Arc::from(parent_path.join(&file_name)))
} else {
None
};
Ok(DirEntry {
depth,
file_name,
file_type,
parent_path,
read_children_path,
read_children_error: None,
client_state: C::DirEntryState::default(),
follow_link: false,
follow_link_ancestors,
})
}
pub(crate) fn from_path(
depth: usize,
path: &Path,
follow_link: bool,
follow_link_ancestors: Arc<Vec<Arc<Path>>>,
) -> Result<Self> {
let metadata = if follow_link {
fs::metadata(&path).map_err(|err| Error::from_path(depth, path.to_owned(), err))?
} else {
fs::symlink_metadata(&path)
.map_err(|err| Error::from_path(depth, path.to_owned(), err))?
};
let root_name = path.file_name().unwrap_or_else(|| path.as_os_str());
let read_children_path: Option<Arc<Path>> = if metadata.file_type().is_dir() {
Some(Arc::from(path))
} else {
None
};
Ok(DirEntry {
depth,
file_name: root_name.to_owned(),
file_type: metadata.file_type(),
parent_path: Arc::from(path.parent().map(Path::to_path_buf).unwrap_or_default()),
read_children_path,
read_children_error: None,
client_state: C::DirEntryState::default(),
follow_link,
follow_link_ancestors,
})
}
pub fn file_type(&self) -> fs::FileType {
self.file_type
}
pub fn file_name(&self) -> &OsStr {
&self.file_name
}
pub fn depth(&self) -> usize {
self.depth
}
pub fn path(&self) -> PathBuf {
self.parent_path.join(&self.file_name)
}
pub fn path_is_symlink(&self) -> bool {
self.file_type.is_symlink() || self.follow_link
}
pub fn metadata(&self) -> Result<fs::Metadata> {
if self.follow_link {
fs::metadata(&self.path())
} else {
fs::symlink_metadata(&self.path())
}
.map_err(|err| Error::from_entry(self, err))
}
pub fn parent_path(&self) -> &Path {
&self.parent_path
}
pub(crate) fn read_children_spec(
&self,
client_read_state: C::ReadDirState,
) -> Option<ReadDirSpec<C>> {
if let Some(read_children_path) = self.read_children_path.as_ref() {
Some(ReadDirSpec {
depth: self.depth,
client_read_state,
path: read_children_path.clone(),
follow_link_ancestors: self.follow_link_ancestors.clone(),
})
} else {
None
}
}
pub(crate) fn follow_symlink(&self) -> Result<Self> {
let path = self.path();
let origins = self.follow_link_ancestors.clone();
let dir_entry = DirEntry::from_path(self.depth, &path, true, origins)?;
if dir_entry.file_type.is_dir() {
let target = std::fs::read_link(&path).unwrap();
for ancestor in self.follow_link_ancestors.iter().rev() {
if target.as_path() == ancestor.as_ref() {
return Err(Error::from_loop(
self.depth,
ancestor.as_ref(),
path.as_ref(),
));
}
}
}
Ok(dir_entry)
}
}
impl<C: ClientState> fmt::Debug for DirEntry<C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DirEntry({:?})", self.path())
}
}