Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move exists method from Path into Filesystem #72

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Refactor error types:
- Change `Error` enum to a struct with associated constants.
- Remove `Error::Success` and enforce negative values for `Error`.
- Replace `Path::exists` with `Filesystem::exists`

### Removed

Expand Down
10 changes: 9 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
{
use crate::path;

if !path.exists(self) {
if !self.exists(path) {
debug_now!("no such directory {}, early return", path);
return Ok(RemoveDirAllProgress {
files_removed: 0,
Expand Down Expand Up @@ -400,6 +400,14 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
io::result_from((), return_code)
}

/// Check whether a file or directory exists at a path.
///
/// This is equivalent to calling [`Filesystem::metadata`][] and checking for an `Ok` return
/// value.
pub fn exists(&self, path: &Path) -> bool {
self.metadata(path).is_ok()
}

/// Given a path, query the filesystem to get information about a file or directory.
///
/// To read user attributes, use
Expand Down
13 changes: 5 additions & 8 deletions src/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ impl dyn DynFile + '_ {

/// Object-safe trait for [`Filesystem`][].
///
/// It contains these additional methods from [`Path`][]:
/// - [`DynFilesystem::exists`][]
///
/// The following methods are implemented in [`DynStorage`][] instead:
/// - [`DynStorage::format`][]
/// - [`DynStorage::is_mountable`][]
Expand Down Expand Up @@ -100,6 +97,7 @@ pub trait DynFilesystem {
#[cfg(feature = "dir-entry-path")]
fn remove_dir_all_where(&self, path: &Path, predicate: Predicate<'_>) -> Result<usize>;
fn rename(&self, from: &Path, to: &Path) -> Result<()>;
fn exists(&self, path: &Path) -> bool;
fn metadata(&self, path: &Path) -> Result<Metadata>;
fn create_file_and_then_unit(&self, path: &Path, f: FileCallback<'_>) -> Result<()>;
fn open_file_and_then_unit(&self, path: &Path, f: FileCallback<'_>) -> Result<()>;
Expand All @@ -117,7 +115,6 @@ pub trait DynFilesystem {
fn create_dir_all(&self, path: &Path) -> Result<()>;
fn write(&self, path: &Path, contents: &[u8]) -> Result<()>;
fn write_chunk(&self, path: &Path, contents: &[u8], pos: OpenSeekFrom) -> Result<()>;
fn exists(&self, path: &Path) -> bool;
}

impl<S: Storage> DynFilesystem for Filesystem<'_, S> {
Expand Down Expand Up @@ -159,6 +156,10 @@ impl<S: Storage> DynFilesystem for Filesystem<'_, S> {
Filesystem::rename(self, from, to)
}

fn exists(&self, path: &Path) -> bool {
Filesystem::exists(self, path)
}

fn metadata(&self, path: &Path) -> Result<Metadata> {
Filesystem::metadata(self, path)
}
Expand Down Expand Up @@ -211,10 +212,6 @@ impl<S: Storage> DynFilesystem for Filesystem<'_, S> {
fn write_chunk(&self, path: &Path, contents: &[u8], pos: OpenSeekFrom) -> Result<()> {
Filesystem::write_chunk(self, path, contents, pos)
}

fn exists(&self, path: &Path) -> bool {
path.exists(self)
}
}

impl dyn DynFilesystem + '_ {
Expand Down
4 changes: 0 additions & 4 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ impl Path {
p
}

pub fn exists<S: crate::driver::Storage>(&self, fs: &crate::fs::Filesystem<S>) -> bool {
fs.metadata(self).is_ok()
}

// helpful for debugging wither the trailing nul is indeed a trailing nul.
pub const fn as_str_ref_with_trailing_nul(&self) -> &str {
// SAFETY: ASCII is valid UTF-8
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ fn test_create() {
assert_eq!(fs.available_blocks().unwrap(), 512 - 2);
assert_eq!(fs.available_space().unwrap(), 130_560);

assert!(!path!("/test_open.txt").exists(fs));
assert!(!fs.exists(path!("/test_open.txt")));
assert_eq!(
File::open_and_then(fs, b"/test_open.txt\0".try_into().unwrap(), |_| { Ok(()) })
.map(drop)
.unwrap_err(), // "real" contains_err is experimental
Error::NO_SUCH_ENTRY
);
assert!(!path!("/test_open.txt").exists(fs));
assert!(!fs.exists(path!("/test_open.txt")));

fs.create_dir(b"/tmp\0".try_into().unwrap()).unwrap();
assert_eq!(fs.available_blocks().unwrap(), 512 - 2 - 2);

// can create new files
assert!(!path!("/tmp/test_open.txt").exists(fs));
assert!(!fs.exists(path!("/tmp/test_open.txt")));
fs.create_file_and_then(b"/tmp/test_open.txt\0".try_into().unwrap(), |file| {
// can write to files
assert!(file.write(&[0u8, 1, 2]).unwrap() == 3);
Expand All @@ -219,7 +219,7 @@ fn test_create() {
// file.close()?;
Ok(())
})?;
assert!(path!("/tmp/test_open.txt").exists(fs));
assert!(fs.exists(path!("/tmp/test_open.txt")));

// // cannot remove non-empty directories
assert_eq!(
Expand Down
Loading