Skip to content

Commit 8cd8cf8

Browse files
committed
Improve chdir
1 parent 588d350 commit 8cd8cf8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/dir.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,33 @@ 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+
let e = dir.find_entry(name, Some(true), None)?.to_dir();
223+
match rest_opt {
224+
Some(rest) => {
225+
dir = e;
226+
path = rest;
227+
}
228+
None => return Ok(e),
229+
}
217230
}
218231
}
219232

0 commit comments

Comments
 (0)