Skip to content

Commit

Permalink
Remove redundant harded parent directory check
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hsiao authored and Nathan Hsiao committed Sep 6, 2024
1 parent f9ea294 commit 531c847
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions kernel/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,17 @@ impl Environment {
/// Changes the current working directory.
#[doc(alias("change"))]
pub fn chdir(&mut self, path: &Path) -> Result<()> {
if path == Path::new("..") {
// Obtain the parent directory in a separate step
let parent_dir = {
let working_dir_lock = self.working_dir.lock();
working_dir_lock.get_parent_dir()
};

match parent_dir {
Some(parent_dir) => {
// Now safely update `self.working_dir` outside of the lock
self.working_dir = Arc::clone(&parent_dir);
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;
}
None => return Err(Error::NotFound), // Or another suitable error
Some(FileOrDir::File(_)) => return Err(Error::NotADirectory),
None => return Err(Error::NotFound),
}
Ok(())
}

else {
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),
}
}
Ok(())
}
Ok(())
}

pub fn chdir_path(&mut self, path: &Path) -> Result<()> {
Expand Down

0 comments on commit 531c847

Please sign in to comment.