Skip to content

Commit ee33c37

Browse files
committed
Improve open_dir
1 parent 588d350 commit ee33c37

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ New features:
4141
* Undeprecate `set_created`, `set_accessed`, `set_modified` methods on `File` - there are scenarios where those methods are needed
4242
* Relax BPB validation by allowing non-zero values in both `total_sectors_32` and `total_sectors_16`
4343
* Add `strict` field in `FsOptions`, which allows disabling validation of boot signature to improve compatibility with old FAT images
44+
* Improve `open_dir`
4445

4546
Bug fixes:
4647
* Fix formatting volumes with size in range 4096-4199 KB

src/dir.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,32 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
200200
///
201201
/// `path` is a '/' separated directory path relative to self directory.
202202
///
203+
/// An empty path returns the current directory.
204+
/// A trailing slash is ignored.
205+
/// (But the path '/' will not be found.)
206+
///
203207
/// # Errors
204208
///
205209
/// Errors that can be returned:
206210
///
207211
/// * `Error::NotFound` will be returned if `path` does not point to any existing directory entry.
208212
/// * `Error::InvalidInput` will be returned if `path` points to a file that is not a directory.
209213
/// * `Error::Io` will be returned if the underlying storage object returned an I/O error.
210-
pub fn open_dir(&self, path: &str) -> Result<Self, Error<IO::Error>> {
214+
pub fn open_dir(&self, mut path: &str) -> Result<Self, Error<IO::Error>> {
211215
trace!("Dir::open_dir {}", path);
212-
let (name, rest_opt) = split_path(path);
213-
let e = self.find_entry(name, Some(true), None)?;
214-
match rest_opt {
215-
Some(rest) => e.to_dir().open_dir(rest),
216-
None => Ok(e.to_dir()),
216+
let mut dir = self.clone();
217+
loop {
218+
if path.is_empty() {
219+
return Ok(dir);
220+
}
221+
let (name, rest_opt) = split_path(path);
222+
dir = dir.find_entry(name, Some(true), None)?.to_dir();
223+
match rest_opt {
224+
Some(rest) => {
225+
path = rest;
226+
}
227+
None => return Ok(dir),
228+
}
217229
}
218230
}
219231

0 commit comments

Comments
 (0)