Skip to content

Commit 66c1889

Browse files
Fix bug and tests
1 parent d49ed0a commit 66c1889

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

tree/readlink.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,25 @@ fn recursive_resolve(starting_path_buf: PathBuf) -> Result<PathBuf, String> {
210210
loop {
211211
match fs::read_link(current_path_buf.as_path()) {
212212
Ok(pa) => {
213-
if !encountered_paths.insert(pa.as_os_str().to_owned()) {
213+
if pa.is_absolute() {
214+
current_path_buf = pa;
215+
} else {
216+
if !current_path_buf.pop() {
217+
return Err(format!(
218+
"Could not remove last path segment from path \"{}\")",
219+
current_path_buf.to_string_lossy()
220+
));
221+
}
222+
223+
current_path_buf.push(pa);
224+
}
225+
226+
if !encountered_paths.insert(current_path_buf.as_os_str().to_owned()) {
214227
return Err(format!(
215228
"Infinite symbolic link loop detected at \"{}\")",
216-
pa.to_string_lossy()
229+
current_path_buf.to_string_lossy()
217230
));
218231
}
219-
220-
current_path_buf = pa;
221232
}
222233
Err(_) => {
223234
break;

tree/tests/readlink/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ fn test_readlink_non_existent_file() {
6363

6464
run_test(TestPlan {
6565
cmd: String::from("readlink"),
66-
args: vec![non_existent_path.to_str().unwrap().to_string()],
66+
args: vec![
67+
"-v".to_owned(),
68+
non_existent_path.to_str().unwrap().to_string(),
69+
],
6770
stdin_data: String::new(),
6871
expected_out: String::new(),
6972
expected_err: format!(
@@ -84,7 +87,7 @@ fn test_readlink_not_a_symlink() {
8487

8588
run_test(TestPlan {
8689
cmd: String::from("readlink"),
87-
args: vec![file_path.to_str().unwrap().to_string()],
90+
args: vec!["-v".to_owned(), file_path.to_str().unwrap().to_string()],
8891
stdin_data: String::new(),
8992
expected_out: String::new(),
9093
expected_err: format!(

0 commit comments

Comments
 (0)