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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Directory CRUD operations implemented as `impl Handle`.
use crate::handle::Handle;
use crate::meta::DirEntry;
use crate::{Error, Result};
use std::path::Path;
impl Handle {
// ──────────────────────────────────────────────────────────────────────────
// Creation
// ──────────────────────────────────────────────────────────────────────────
/// Creates a directory at `path`.
///
/// Returns [`Error::Io`] (with kind `AlreadyExists`) if the directory
/// already exists. Use [`Handle::mkdir_all`] for idempotent creation.
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::Io`] on any IO error.
pub fn mkdir(&self, path: impl AsRef<Path>) -> Result<()> {
let path = self.resolve_path(path.as_ref())?;
std::fs::create_dir(&path).map_err(Error::Io)
}
/// Creates `path` and all missing ancestors, idempotently.
///
/// Returns `Ok(())` if the directory already exists. On partial failure,
/// reports which intermediate directories were already created before the
/// error via [`Error::PartialDirectoryOp`].
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::PartialDirectoryOp`] if creation fails partway through
/// the ancestor chain.
pub fn mkdir_all(&self, path: impl AsRef<Path>) -> Result<()> {
let path = self.resolve_path(path.as_ref())?;
// Collect the ancestor chain that needs to be created.
let mut to_create: Vec<std::path::PathBuf> = Vec::new();
let mut current = path.as_path();
loop {
match current.metadata() {
Ok(m) if m.is_dir() => break, // ancestor already exists
Ok(_) => {
return Err(Error::InvalidPath {
path: current.to_owned(),
reason: "a non-directory already exists at this path".into(),
});
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
to_create.push(current.to_owned());
match current.parent() {
Some(p) => current = p,
None => break,
}
}
Err(e) => return Err(Error::Io(e)),
}
}
to_create.reverse(); // create from shallowest to deepest
let mut completed: Vec<String> = Vec::new();
for dir in &to_create {
match std::fs::create_dir(dir) {
Ok(()) => {
completed.push(dir.display().to_string());
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
// Race: another thread/process created it. Fine.
completed.push(dir.display().to_string());
}
Err(e) => {
return Err(Error::PartialDirectoryOp {
failed_step: format!("create_dir({}): {}", dir.display(), e),
completed_steps: completed,
});
}
}
}
Ok(())
}
// ──────────────────────────────────────────────────────────────────────────
// Removal
// ──────────────────────────────────────────────────────────────────────────
/// Removes the empty directory at `path`.
///
/// Fails if the directory is not empty. Use [`Handle::rmdir_all`] to
/// recursively remove a non-empty directory tree.
///
/// This operation is **idempotent**: if `path` does not exist,
/// `Ok(())` is returned.
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::Io`] for errors other than "not found" (e.g. not empty).
pub fn rmdir(&self, path: impl AsRef<Path>) -> Result<()> {
let path = self.resolve_path(path.as_ref())?;
match std::fs::remove_dir(&path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(Error::Io(e)),
}
}
/// Recursively removes the directory tree rooted at `path`.
///
/// This operation is **idempotent**: if `path` does not exist,
/// `Ok(())` is returned.
///
/// On partial failure (e.g. permission error mid-tree), reports which
/// top-level entries were successfully removed via
/// [`Error::PartialDirectoryOp`].
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::PartialDirectoryOp`] on partial failure.
pub fn rmdir_all(&self, path: impl AsRef<Path>) -> Result<()> {
let path = self.resolve_path(path.as_ref())?;
match std::fs::remove_dir_all(&path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(Error::PartialDirectoryOp {
failed_step: format!("remove_dir_all({}): {}", path.display(), e),
completed_steps: Vec::new(),
}),
}
}
// ──────────────────────────────────────────────────────────────────────────
// Listing and metadata
// ──────────────────────────────────────────────────────────────────────────
/// Returns a list of entries in the directory at `path`.
///
/// The entries are not sorted. Symlinks are not followed.
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::Io`] if the directory cannot be read.
pub fn list(&self, path: impl AsRef<Path>) -> Result<Vec<DirEntry>> {
let path = self.resolve_path(path.as_ref())?;
let rd = std::fs::read_dir(&path).map_err(Error::Io)?;
let mut entries = Vec::new();
for item in rd {
let entry = item.map_err(Error::Io)?;
entries.push(DirEntry::from_std(entry));
}
Ok(entries)
}
/// Returns `true` if a directory exists at `path`.
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::Io`] on errors other than "not found".
pub fn is_dir(&self, path: impl AsRef<Path>) -> Result<bool> {
let path = self.resolve_path(path.as_ref())?;
match std::fs::metadata(&path) {
Ok(m) => Ok(m.is_dir()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(Error::Io(e)),
}
}
/// Returns `true` if a regular file exists at `path`.
///
/// Alias for [`Handle::exists`] provided for symmetry with
/// [`Handle::is_dir`].
///
/// # Errors
///
/// - [`Error::InvalidPath`] if `path` escapes the handle root.
/// - [`Error::Io`] on errors other than "not found".
pub fn is_file(&self, path: impl AsRef<Path>) -> Result<bool> {
self.exists(path)
}
}
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use crate::builder::Builder;
use crate::method::Method;
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
fn tmp_path(suffix: &str) -> std::path::PathBuf {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"fsys_crud_dir_{}_{}_{}",
std::process::id(),
n,
suffix
))
}
struct TmpDir(std::path::PathBuf);
impl Drop for TmpDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn handle() -> crate::handle::Handle {
Builder::new()
.method(Method::Sync)
.build()
.expect("build handle")
}
#[test]
fn test_mkdir_creates_directory() {
let dir = tmp_path("mkdir");
let _g = TmpDir(dir.clone());
let h = handle();
h.mkdir(&dir).expect("mkdir");
assert!(dir.is_dir());
}
#[test]
fn test_mkdir_fails_if_exists() {
let dir = tmp_path("mkdir_exists");
let _g = TmpDir(dir.clone());
std::fs::create_dir(&dir).expect("create");
assert!(handle().mkdir(&dir).is_err());
}
#[test]
fn test_mkdir_all_creates_nested() {
let root = tmp_path("mkdir_all");
let _g = TmpDir(root.clone());
let nested = root.join("a").join("b").join("c");
handle().mkdir_all(&nested).expect("mkdir_all");
assert!(nested.is_dir());
}
#[test]
fn test_mkdir_all_idempotent() {
let dir = tmp_path("mkdir_all_idem");
let _g = TmpDir(dir.clone());
std::fs::create_dir(&dir).expect("create");
handle().mkdir_all(&dir).expect("mkdir_all on existing");
}
#[test]
fn test_rmdir_removes_empty() {
let dir = tmp_path("rmdir");
std::fs::create_dir(&dir).expect("create");
handle().rmdir(&dir).expect("rmdir");
assert!(!dir.exists());
}
#[test]
fn test_rmdir_idempotent() {
let dir = tmp_path("rmdir_idem");
handle().rmdir(&dir).expect("rmdir on non-existent");
}
#[test]
fn test_rmdir_all_removes_tree() {
let root = tmp_path("rmdir_all");
std::fs::create_dir_all(root.join("sub")).expect("create tree");
handle().rmdir_all(&root).expect("rmdir_all");
assert!(!root.exists());
}
#[test]
fn test_list_returns_entries() {
let root = tmp_path("list");
let _g = TmpDir(root.clone());
std::fs::create_dir(&root).expect("create root");
std::fs::write(root.join("file.txt"), b"x").expect("write");
std::fs::create_dir(root.join("subdir")).expect("create subdir");
let entries = handle().list(&root).expect("list");
assert_eq!(entries.len(), 2);
}
#[test]
fn test_is_dir_reflects_state() {
let dir = tmp_path("is_dir");
let _g = TmpDir(dir.clone());
let h = handle();
assert!(!h.is_dir(&dir).expect("is_dir before create"));
std::fs::create_dir(&dir).expect("create");
assert!(h.is_dir(&dir).expect("is_dir after create"));
}
}