Skip to content

read_link cannot open some files reported as symbolic links on windows #28528

@BurntSushi

Description

@BurntSushi

rustc on Windows 7 32 bit:

C:\Users\andrew\walkdir\symlinks>rustc --version
rustc 1.5.0-nightly (6e5a32547 2015-09-19)

Test program:

use std::fs;

fn main() {
    let md = fs::symlink_metadata("is_a_symlink").unwrap();
    println!("is symlink? {}", md.file_type().is_symlink());
    match fs::read_link("is_a_symlink") {
        Ok(p) => println!("{}", p.display()),
        Err(err) => println!("{:?}", err),
    }
}

Create a symlink on Windows at is_a_symlink pointing to not_a_symlink and
run above program (working as intended):

C:\Users\andrew\walkdir\symlinks>mklink is_a_symlink C:\Users\andrew\walkdir\symlinks\not_a_symlink
symbolic link created for is_a_symlink <<===>> C:\Users\andrew\walkdir\symlinks\not_a_symlink

C:\Users\andrew\walkdir\symlinks>dir
09/19/2015  11:40 PM    <DIR>          .
09/19/2015  11:40 PM    <DIR>          ..
09/19/2015  11:40 PM    <SYMLINK>      is_a_symlink [C:\Users\andrew\walkdir\symlinks\not_a_symlink]
09/19/2015  11:32 PM                 0 not_a_symlink
09/19/2015  11:36 PM         2,118,989 win_symlink_test.exe
09/19/2015  11:36 PM               261 win_symlink_test.rs

C:\Users\andrew\walkdir\symlinks>win_symlink_test.exe
is symlink? true
\??\C:\Users\andrew\walkdir\symlinks\not_a_symlink

This correctly identifies the file as a symlink and resolves it correctly.

If we instead creation a junction and follow the same steps, we can observe an
inconsistency between file_type().is_symlink() and the behavior of
fs::read_link:

C:\Users\andrew\walkdir\symlinks>mklink /J is_a_symlink C:\Users\andrew\walkdir\symlinks\not_a_symlink
Junction created for is_a_symlink <<===>> C:\Users\andrew\walkdir\symlinks\not_a_symlink

C:\Users\andrew\walkdir\symlinks>dir
09/19/2015  11:46 PM    <DIR>          .
09/19/2015  11:46 PM    <DIR>          ..
09/19/2015  11:46 PM    <JUNCTION>     is_a_symlink [C:\Users\andrew\walkdir\symlinks\not_a_symlink]
09/19/2015  11:32 PM                 0 not_a_symlink
09/19/2015  11:44 PM         2,118,946 win_symlink_test.exe
09/19/2015  11:44 PM               286 win_symlink_test.rs

C:\Users\andrew\walkdir\symlinks>win_symlink_test.exe
is symlink? true
Error { repr: Custom(Custom { kind: Other, error: StringError("not a symlink") }) }

(The key difference is the use of mklink /J to create a junction.)

The output here shows that it is correctly detected as a symlink, but
read_link chokes on it.

I looked briefly at the implementation and I haven't the slightest clue what's
wrong (I'm not familiar with Windows).

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions