BurntSushi / walkdir

Rust library for walking directories recursively.
The Unlicense
1.3k stars 109 forks source link

entry.unwrap() fails when root is symlink and points to non-existing target #175

Closed flokli closed 1 year ago

flokli commented 1 year ago

If the root path we point WalkDir::new to is a symlink pointing to a non-existent target, WalkDir fails to unwrap that entry.

See the two tests for reference

#[test]
fn sym_nonenexisting_target() {
    let dir = Dir::tmp();
    dir.symlink_file("/into/the/void", "a");

    let mut entries: Vec<DirEntry> = vec![];

    for entry in WalkDir::new(dir.path()) {
        entries.push(entry.unwrap());
    }

    assert_eq!(2, entries.len());
}

#[test]
fn sym_nonenexisting_target_root() {
    let dir = Dir::tmp();
    dir.symlink_file("/into/the/void", "root");

    let mut entries: Vec<DirEntry> = vec![];

    for entry in WalkDir::new(dir.join("root")) {
        entries.push(entry.unwrap());
    }

    assert_eq!(1, entries.len());
}

The latter currently fails.

BurntSushi commented 1 year ago

Well, can you show the failure? If it's failing at entry.unwrap() then that seems... right to me?

flokli commented 1 year ago

That's the failing error output:

---- tests::recursive::sym_nonenexisting_target_root stdout ----
thread 'tests::recursive::sym_nonenexisting_target_root' panicked at 'called `Result::unwrap()` on an `Err` value: Error { depth: 0, inner: Io { path: Some("/tmp/rust-walkdir/39/root"), err: Os { code: 2, kind: NotFound, message: "No such file or directory" } } }', src/tests/recursive.rs:379:28
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The problem is that it seems to try to stat the symlink target, not the symlink itself?

BurntSushi commented 1 year ago

Looks correct to me. From the docs:

If root is a symlink, then it is always followed for the purposes of directory traversal. (A root DirEntry still obeys its documentation with respect to symlinks and the follow_links setting.)

See also https://github.com/BurntSushi/walkdir/pull/170.

flokli commented 1 year ago

Ah yes nice, #170 should solve this. Thanks for linking to it!