Skip to content

Commit

Permalink
Fix bug and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewliebenow committed Oct 19, 2024
1 parent d49ed0a commit 66c1889
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 15 additions & 4 deletions tree/readlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,25 @@ fn recursive_resolve(starting_path_buf: PathBuf) -> Result<PathBuf, String> {
loop {
match fs::read_link(current_path_buf.as_path()) {
Ok(pa) => {
if !encountered_paths.insert(pa.as_os_str().to_owned()) {
if pa.is_absolute() {
current_path_buf = pa;
} else {
if !current_path_buf.pop() {
return Err(format!(
"Could not remove last path segment from path \"{}\")",
current_path_buf.to_string_lossy()
));
}

current_path_buf.push(pa);
}

if !encountered_paths.insert(current_path_buf.as_os_str().to_owned()) {
return Err(format!(
"Infinite symbolic link loop detected at \"{}\")",
pa.to_string_lossy()
current_path_buf.to_string_lossy()
));
}

current_path_buf = pa;
}
Err(_) => {
break;
Expand Down
7 changes: 5 additions & 2 deletions tree/tests/readlink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ fn test_readlink_non_existent_file() {

run_test(TestPlan {
cmd: String::from("readlink"),
args: vec![non_existent_path.to_str().unwrap().to_string()],
args: vec![
"-v".to_owned(),
non_existent_path.to_str().unwrap().to_string(),
],
stdin_data: String::new(),
expected_out: String::new(),
expected_err: format!(
Expand All @@ -84,7 +87,7 @@ fn test_readlink_not_a_symlink() {

run_test(TestPlan {
cmd: String::from("readlink"),
args: vec![file_path.to_str().unwrap().to_string()],
args: vec!["-v".to_owned(), file_path.to_str().unwrap().to_string()],
stdin_data: String::new(),
expected_out: String::new(),
expected_err: format!(
Expand Down

0 comments on commit 66c1889

Please sign in to comment.