From ee7688fa5c327e7e61652da5e3faa0eb6e2c4460 Mon Sep 17 00:00:00 2001 From: nathan hsiao Date: Mon, 9 Sep 2024 11:50:57 -0700 Subject: [PATCH] Use `Path::get()` in `chdir` function to support `..` path nav (#1104) --- kernel/environment/src/lib.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/kernel/environment/src/lib.rs b/kernel/environment/src/lib.rs index 1dcf6f78d5..9c5a43252b 100644 --- a/kernel/environment/src/lib.rs +++ b/kernel/environment/src/lib.rs @@ -31,17 +31,14 @@ impl Environment { /// Changes the current working directory. #[doc(alias("change"))] pub fn chdir(&mut self, path: &Path) -> Result<()> { - for component in path.components() { - let new = self.working_dir.lock().get(component.as_ref()); - match new { - Some(FileOrDir::Dir(dir)) => { - self.working_dir = dir; - } - Some(FileOrDir::File(_)) => return Err(Error::NotADirectory), - None => return Err(Error::NotFound), + match path.get(&self.working_dir) { + Some(FileOrDir::Dir(dir)) => { + self.working_dir = dir; + Ok(()) } + Some(FileOrDir::File(_)) => Err(Error::NotADirectory), + None => Err(Error::NotFound), } - Ok(()) } /// Returns the value of the environment variable with the given `key`.