Skip to content

Commit 173a252

Browse files
refactor(API): make VfsPath::exists() return VfsResult<bool> instead of plain bool (fixes #17)
1 parent 7788984 commit 173a252

File tree

8 files changed

+88
-77
lines changed

8 files changed

+88
-77
lines changed

src/filesystem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub trait FileSystem: Debug + Sync + Send + 'static {
2121
/// Returns the file metadata for the file at this path
2222
fn metadata(&self, path: &str) -> VfsResult<VfsMetadata>;
2323
/// Returns true if a file or directory at path exists, false otherwise
24-
fn exists(&self, path: &str) -> bool;
24+
fn exists(&self, path: &str) -> VfsResult<bool>;
2525
/// Removes the file at this path
2626
fn remove_file(&self, path: &str) -> VfsResult<()>;
2727
/// Removes the directory at this path

src/impls/altroot.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ impl FileSystem for AltrootFS {
6262
self.path(path)?.metadata()
6363
}
6464

65-
fn exists(&self, path: &str) -> bool {
66-
self.path(path).map(|path| path.exists()).unwrap_or(false)
65+
fn exists(&self, path: &str) -> VfsResult<bool> {
66+
self.path(path)
67+
.map(|path| path.exists())
68+
.unwrap_or(Ok(false))
6769
}
6870

6971
fn remove_file(&self, path: &str) -> VfsResult<()> {

src/impls/memory.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl MemoryFS {
3535
fn ensure_has_parent(&self, path: &str) -> VfsResult<()> {
3636
let separator = path.rfind('/');
3737
if let Some(index) = separator {
38-
if self.exists(&path[..index]) {
38+
if self.exists(&path[..index])? {
3939
return Ok(());
4040
}
4141
}
@@ -219,8 +219,8 @@ impl FileSystem for MemoryFS {
219219
})
220220
}
221221

222-
fn exists(&self, path: &str) -> bool {
223-
self.handle.read().unwrap().files.contains_key(path)
222+
fn exists(&self, path: &str) -> VfsResult<bool> {
223+
Ok(self.handle.read().unwrap().files.contains_key(path))
224224
}
225225

226226
fn remove_file(&self, path: &str) -> VfsResult<()> {
@@ -283,7 +283,7 @@ mod tests {
283283
test_vfs!(MemoryFS::new());
284284

285285
#[test]
286-
fn write_and_read_file() {
286+
fn write_and_read_file() -> VfsResult<()> {
287287
let root = VfsPath::new(MemoryFS::new());
288288
let path = root.join("foobar.txt").unwrap();
289289
let _send = &path as &dyn Send;
@@ -298,11 +298,12 @@ mod tests {
298298
file.read_to_string(&mut string).unwrap();
299299
assert_eq!(string, "Hello world!");
300300
}
301-
assert!(path.exists());
302-
assert!(!root.join("foo").unwrap().exists());
301+
assert!(path.exists()?);
302+
assert!(!root.join("foo").unwrap().exists()?);
303303
let metadata = path.metadata().unwrap();
304304
assert_eq!(metadata.len, 12);
305305
assert_eq!(metadata.file_type, VfsFileType::File);
306+
Ok(())
306307
}
307308

308309
#[test]

src/impls/overlay.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ impl OverlayFS {
3434
if path.is_empty() {
3535
return Ok(self.layers[0].clone());
3636
}
37-
if self.whiteout_path(path)?.exists() {
37+
if self.whiteout_path(path)?.exists()? {
3838
return Err(VfsError::FileNotFound {
3939
path: path.to_string(),
4040
});
4141
}
4242
for layer in &self.layers {
4343
let layer_path = layer.join(&path[1..])?;
44-
if layer_path.exists() {
44+
if layer_path.exists()? {
4545
return Ok(layer_path);
4646
}
4747
}
4848
let read_path = self.write_layer().join(&path[1..])?;
49-
if !read_path.exists() {
49+
if !read_path.exists()? {
5050
return Err(VfsError::FileNotFound {
5151
path: path.to_string(),
5252
});
@@ -73,7 +73,7 @@ impl OverlayFS {
7373
let separator = path.rfind('/');
7474
if let Some(index) = separator {
7575
let parent_path = &path[..index];
76-
if self.exists(parent_path) {
76+
if self.exists(parent_path)? {
7777
self.write_path(parent_path)?.create_dir_all()?;
7878
return Ok(());
7979
}
@@ -87,23 +87,23 @@ impl OverlayFS {
8787
impl FileSystem for OverlayFS {
8888
fn read_dir(&self, path: &str) -> VfsResult<Box<dyn Iterator<Item = String>>> {
8989
let actual_path = if !path.is_empty() { &path[1..] } else { path };
90-
if !self.read_path(path)?.exists() {
90+
if !self.read_path(path)?.exists()? {
9191
return Err(VfsError::FileNotFound {
9292
path: path.to_string(),
9393
});
9494
}
9595
let mut entries = HashSet::<String>::new();
9696
for layer in &self.layers {
9797
let layer_path = layer.join(actual_path)?;
98-
if layer_path.exists() {
98+
if layer_path.exists()? {
9999
for path in layer_path.read_dir()? {
100100
entries.insert(path.filename());
101101
}
102102
}
103103
}
104104
// remove whiteout entries that have been removed
105105
let whiteout_path = self.write_layer().join(&format!(".whiteout{}", path))?;
106-
if whiteout_path.exists() {
106+
if whiteout_path.exists()? {
107107
for path in whiteout_path.read_dir()? {
108108
let filename = path.filename();
109109
if filename.ends_with("_wo") {
@@ -118,7 +118,7 @@ impl FileSystem for OverlayFS {
118118
self.ensure_has_parent(path)?;
119119
self.write_path(path)?.create_dir()?;
120120
let whiteout_path = self.whiteout_path(path)?;
121-
if whiteout_path.exists() {
121+
if whiteout_path.exists()? {
122122
whiteout_path.remove_file()?;
123123
}
124124
Ok(())
@@ -132,15 +132,15 @@ impl FileSystem for OverlayFS {
132132
self.ensure_has_parent(path)?;
133133
let result = self.write_path(path)?.create_file()?;
134134
let whiteout_path = self.whiteout_path(path)?;
135-
if whiteout_path.exists() {
135+
if whiteout_path.exists()? {
136136
whiteout_path.remove_file()?;
137137
}
138138
Ok(result)
139139
}
140140

141141
fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write>> {
142142
let write_path = self.write_path(path)?;
143-
if !write_path.exists() {
143+
if !write_path.exists()? {
144144
self.ensure_has_parent(path)?;
145145
self.read_path(path)?.copy_file(&write_path)?;
146146
}
@@ -151,20 +151,20 @@ impl FileSystem for OverlayFS {
151151
self.read_path(path)?.metadata()
152152
}
153153

154-
fn exists(&self, path: &str) -> bool {
155-
if self.whiteout_path(path).expect("whiteout_path").exists() {
156-
return false;
154+
fn exists(&self, path: &str) -> VfsResult<bool> {
155+
if self.whiteout_path(path).expect("whiteout_path").exists()? {
156+
return Ok(false);
157157
}
158158
self.read_path(path)
159159
.map(|path| path.exists())
160-
.unwrap_or(false)
160+
.unwrap_or(Ok(false))
161161
}
162162

163163
fn remove_file(&self, path: &str) -> VfsResult<()> {
164164
// Ensure path exists
165165
self.read_path(path)?;
166166
let write_path = self.write_path(path)?;
167-
if write_path.exists() {
167+
if write_path.exists()? {
168168
write_path.remove_file()?;
169169
}
170170
let whiteout_path = self.whiteout_path(path)?;
@@ -177,7 +177,7 @@ impl FileSystem for OverlayFS {
177177
// Ensure path exists
178178
self.read_path(path)?;
179179
let write_path = self.write_path(path)?;
180-
if write_path.exists() {
180+
if write_path.exists()? {
181181
write_path.remove_dir()?;
182182
}
183183
let whiteout_path = self.whiteout_path(path)?;
@@ -218,7 +218,7 @@ mod tests {
218218
lower_path.remove_file()?;
219219
assert_eq!(&overlay_path.read_to_string()?, "Hello Upper");
220220
upper_path.remove_file()?;
221-
assert!(!overlay_path.exists(), "File should not exist anymore");
221+
assert!(!overlay_path.exists()?, "File should not exist anymore");
222222
Ok(())
223223
}
224224

@@ -254,19 +254,19 @@ mod tests {
254254
fn create_dir() -> VfsResult<()> {
255255
let (lower_root, _upper_root, overlay_root) = create_roots();
256256
lower_root.join("foo")?.create_dir_all()?;
257-
assert!(overlay_root.join("foo")?.exists(), "dir should exist");
257+
assert!(overlay_root.join("foo")?.exists()?, "dir should exist");
258258
overlay_root.join("foo/bar")?.create_dir()?;
259-
assert!(overlay_root.join("foo/bar")?.exists(), "dir should exist");
259+
assert!(overlay_root.join("foo/bar")?.exists()?, "dir should exist");
260260
Ok(())
261261
}
262262

263263
#[test]
264264
fn create_file() -> VfsResult<()> {
265265
let (lower_root, _upper_root, overlay_root) = create_roots();
266266
lower_root.join("foo")?.create_dir_all()?;
267-
assert!(overlay_root.join("foo")?.exists(), "dir should exist");
267+
assert!(overlay_root.join("foo")?.exists()?, "dir should exist");
268268
overlay_root.join("foo/bar")?.create_file()?;
269-
assert!(overlay_root.join("foo/bar")?.exists(), "file should exist");
269+
assert!(overlay_root.join("foo/bar")?.exists()?, "file should exist");
270270
Ok(())
271271
}
272272

@@ -298,13 +298,13 @@ mod tests {
298298
.create_file()?
299299
.write_all(b"Hello Lower\n")?;
300300
assert!(
301-
overlay_root.join("foo/bar.txt")?.exists(),
301+
overlay_root.join("foo/bar.txt")?.exists()?,
302302
"file should exist"
303303
);
304304

305305
overlay_root.join("foo/bar.txt")?.remove_file()?;
306306
assert!(
307-
!overlay_root.join("foo/bar.txt")?.exists(),
307+
!overlay_root.join("foo/bar.txt")?.exists()?,
308308
"file should not exist anymore"
309309
);
310310

@@ -313,7 +313,7 @@ mod tests {
313313
.create_file()?
314314
.write_all(b"Hello Overlay\n")?;
315315
assert!(
316-
overlay_root.join("foo/bar.txt")?.exists(),
316+
overlay_root.join("foo/bar.txt")?.exists()?,
317317
"file should exist"
318318
);
319319
assert_eq!(
@@ -328,16 +328,16 @@ mod tests {
328328
let (lower_root, _upper_root, overlay_root) = create_roots();
329329
lower_root.join("foo")?.create_dir_all()?;
330330
lower_root.join("foo/bar")?.create_dir_all()?;
331-
assert!(overlay_root.join("foo/bar")?.exists(), "dir should exist");
331+
assert!(overlay_root.join("foo/bar")?.exists()?, "dir should exist");
332332

333333
overlay_root.join("foo/bar")?.remove_dir()?;
334334
assert!(
335-
!overlay_root.join("foo/bar")?.exists(),
335+
!overlay_root.join("foo/bar")?.exists()?,
336336
"dir should not exist anymore"
337337
);
338338

339339
overlay_root.join("foo/bar")?.create_dir()?;
340-
assert!(overlay_root.join("foo/bar")?.exists(), "dir should exist");
340+
assert!(overlay_root.join("foo/bar")?.exists()?, "dir should exist");
341341
Ok(())
342342
}
343343

src/impls/physical.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl FileSystem for PhysicalFS {
7474
})
7575
}
7676

77-
fn exists(&self, path: &str) -> bool {
78-
self.get_path(path).exists()
77+
fn exists(&self, path: &str) -> VfsResult<bool> {
78+
Ok(self.get_path(path).exists())
7979
}
8080

8181
fn remove_file(&self, path: &str) -> VfsResult<()> {

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//!
2121
//! # fn main() -> vfs::VfsResult<()> {
2222
//! let root: VfsPath = PhysicalFS::new(std::env::current_dir().unwrap()).into();
23-
//! assert!(root.exists());
23+
//! assert!(root.exists()?);
2424
//!
2525
//! let mut content = String::new();
2626
//! root.join("README.md")?.open_file()?.read_to_string(&mut content)?;
@@ -35,10 +35,10 @@
3535
//! # fn main() -> vfs::VfsResult<()> {
3636
//! let root: VfsPath = MemoryFS::new().into();
3737
//! let path = root.join("test.txt")?;
38-
//! assert!(!path.exists());
38+
//! assert!(!path.exists()?);
3939
//!
4040
//! path.create_file()?.write_all(b"Hello world")?;
41-
//! assert!(path.exists());
41+
//! assert!(path.exists()?);
4242
//! let mut content = String::new();
4343
//! path.open_file()?.read_to_string(&mut content)?;
4444
//! assert_eq!(content, "Hello world");

src/path.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl VfsPath {
150150
.map(|it| it + pos)
151151
.unwrap_or_else(|| path.len());
152152
let directory = &path[..end];
153-
if !self.fs.fs.exists(directory) {
153+
if !self.fs.fs.exists(directory)? {
154154
self.fs.fs.create_dir(directory)?;
155155
}
156156
if end == path.len() {
@@ -190,7 +190,7 @@ impl VfsPath {
190190
.into());
191191
}
192192
Some(directory) => {
193-
if !directory.exists() {
193+
if !directory.exists()? {
194194
return Err(format!(
195195
"Could not {} at '{}', parent directory does not exist",
196196
action, &self.path
@@ -240,7 +240,7 @@ impl VfsPath {
240240
///
241241
/// Returns successfully if directory does not exist
242242
pub fn remove_dir_all(&self) -> VfsResult<()> {
243-
if !self.exists() {
243+
if !self.exists()? {
244244
return Ok(());
245245
}
246246
for child in self.read_dir()? {
@@ -263,7 +263,7 @@ impl VfsPath {
263263
}
264264

265265
/// Returns true if a file or directory exists at this path, false otherwise
266-
pub fn exists(&self) -> bool {
266+
pub fn exists(&self) -> VfsResult<bool> {
267267
self.fs.fs.exists(&self.path)
268268
}
269269

@@ -334,7 +334,7 @@ impl VfsPath {
334334
/// The destination must not exist, but its parent directory must
335335
pub fn copy_file(&self, destination: &VfsPath) -> VfsResult<()> {
336336
|| -> VfsResult<()> {
337-
if destination.exists() {
337+
if destination.exists()? {
338338
return Err("Destination exists already".to_string().into());
339339
}
340340
if Arc::ptr_eq(&self.fs, &destination.fs) {
@@ -365,7 +365,7 @@ impl VfsPath {
365365
/// The destination must not exist, but its parent directory must
366366
pub fn move_file(&self, destination: &VfsPath) -> VfsResult<()> {
367367
|| -> VfsResult<()> {
368-
if destination.exists() {
368+
if destination.exists()? {
369369
return Err("Destination exists already".to_string().into());
370370
}
371371
if Arc::ptr_eq(&self.fs, &destination.fs) {
@@ -400,7 +400,7 @@ impl VfsPath {
400400
pub fn copy_dir(&self, destination: &VfsPath) -> VfsResult<u64> {
401401
let mut files_copied = 0u64;
402402
|| -> VfsResult<()> {
403-
if destination.exists() {
403+
if destination.exists()? {
404404
return Err("Destination exists already".to_string().into());
405405
}
406406
destination.create_dir()?;
@@ -432,7 +432,7 @@ impl VfsPath {
432432
/// The destination must not exist, but its parent directory must
433433
pub fn move_dir(&self, destination: &VfsPath) -> VfsResult<()> {
434434
|| -> VfsResult<()> {
435-
if destination.exists() {
435+
if destination.exists()? {
436436
return Err("Destination exists already".to_string().into());
437437
}
438438
if Arc::ptr_eq(&self.fs, &destination.fs) {

0 commit comments

Comments
 (0)