Skip to content

Commit

Permalink
chdir: handle paths with multiple components (#1080)
Browse files Browse the repository at this point in the history
* Adds support for changing multiple levels of directories at once,
  e.g., `cd ./task/2`.

* Also, since `components()` normalizes the trailing slash,
  this fixes the bug which prevented changing into a directory
  followed by a trailing slash, e.g., `cd namespaces/`.

* This is indicative of the larger issue that each `FsNode` implementation
  needs its own path parsing algorithm. For example, the command
  `cd task && cd ..` does not work because `TaskFs` doesn't support
  the notion of a parent directory (`..`). A fix for this is coming soon.

Signed-off-by: Klimenty Tsoutsman <klim@tsoutsman.com>
  • Loading branch information
tsoutsman authored Dec 12, 2023
1 parent 92295d7 commit 3a678a9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions kernel/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ impl Environment {
/// Changes the current working directory.
#[doc(alias("change"))]
pub fn chdir(&mut self, path: &Path) -> Result<()> {
let new_dir = self.working_dir.lock().get(path.as_ref());
match new_dir {
Some(FileOrDir::Dir(dir_ref)) => {
self.working_dir = dir_ref;
Ok(())
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),
}
Some(FileOrDir::File(_)) => Err(Error::NotADirectory),
None => Err(Error::NotFound),
}
Ok(())
}

/// Returns the value of the environment variable with the given `key`.
Expand Down

0 comments on commit 3a678a9

Please sign in to comment.