Skip to content

Commit

Permalink
Add exists method on Dir
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelj committed Jul 26, 2024
1 parent 46a39fd commit 797e2c1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions embedded-fatfs/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
}
}

/// Check to see if a file or directory with the given name exists
pub async fn exists(&self, mut path: &str, is_dir: Option<bool>) -> Result<bool, Error<IO::Error>> {
let mut dir = self.clone();
// traverse path
while let (name, Some(rest)) = split_path(path) {
dir = dir.find_entry(name, Some(true), None).await?.to_dir();
path = rest;
}
let (name, _rest) = split_path(path);

match self.find_entry(name, is_dir, None).await {
Ok(_) => Ok(true),
Err(e) => match e {
Error::NotFound => Ok(false),
_ => Err(e),
},
}
}

/// Opens existing subdirectory.
///
/// `path` is a '/' separated directory path relative to self directory.
Expand Down

0 comments on commit 797e2c1

Please sign in to comment.