Skip to content

Commit

Permalink
Attempt to move parent directory logic to kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hsiao authored and Nathan Hsiao committed Aug 31, 2024
1 parent e94fa9e commit df05211
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
26 changes: 8 additions & 18 deletions applications/cd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,18 @@ pub fn main(args: Vec<String>) -> isize {
curr_env.lock().working_dir = Arc::clone(root::get_root());
} else {
let path = matches.free[0].as_ref();
if path == Path::new("..") {
if let Some(parent_dir) = working_dir.lock().get_parent_dir() {
curr_env.lock().working_dir = Arc::clone(&parent_dir);
} else {
println!("failed to get parent directory");
match curr_env.lock().chdir(path) {
Err(environment::Error::NotADirectory) => {
println!("not a directory: {}", path);
return -1;
}
}

else {
match curr_env.lock().chdir(path) {
Err(environment::Error::NotADirectory) => {
println!("not a directory: {}", path);
return -1;
}
Err(environment::Error::NotFound) => {
println!("couldn't find directory: {}", path);
return -1;
}
_ => {}
Err(environment::Error::NotFound) => {
println!("couldn't find directory: {}", path);
return -1;
}
_ => {}
}

}
0
}
Expand Down
35 changes: 27 additions & 8 deletions kernel/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,36 @@ 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;
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);
}
Some(FileOrDir::File(_)) => return Err(Error::NotADirectory),
None => return Err(Error::NotFound),
None => return Err(Error::NotFound), // Or another suitable error
}
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(())
}

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

0 comments on commit df05211

Please sign in to comment.