Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::os::windows::fs::symlink_file appears to incorrectly handle absolute paths #138688

Closed
valarauca opened this issue Mar 19, 2025 · 6 comments
Closed
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@valarauca
Copy link

I tried this code:

fn main() {
    let args = std::env::args().collect::<Vec<String>>();
    match std::os::windows::fs::symlink_file(&args[1], &args[2]) {
        Ok(()) => { },
        Err(e) => panic!("{:?}", e),
    };
}

I expected to see this happen:

When ran locally, it works fine.

.\simple_symlink.exe img.jpeg img2.jpeg

When ran with absolute paths

.\simple_symlink.exe 'E:\storage\videos\blobs\6B\7F\70\DB\Top Gun + Maverick (2022).mp4' 'E:\stroage\videos\actors\Tom Cruise\Top Gun + Maverick (2022).mp4'

thread 'main' panicked at src\main.rs:5:19:
Os { code: 87, kind: InvalidInput, message: "The parameter is incorrect." }

To be clear, the path does exist.

Instead, this happened: explanation

I expect a symlink is created.

I say this because, I am in developer mode.

Image

I have granted myself the ability to create symlinks via secpol.msc as well.

Image

So I don't believe it is a permission issue.

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-pc-windows-msvc
release: 1.85.0
LLVM version: 19.1.7

Windows 11 Pro for Workstations (AMD64)

@valarauca valarauca added the C-bug Category: This is a bug. label Mar 19, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 19, 2025
@valarauca
Copy link
Author

As the path in actually was longer than 260 characters, the sample used as simplified.

@jieyouxu jieyouxu added O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 19, 2025
@bjorn3
Copy link
Member

bjorn3 commented Mar 19, 2025

'E:\stroage\videos\actors\Tom Cruise\Top Gun + Maverick (2022).mp4'

You got a typo there I think. (storage vs stroage)

@ChrisDenton
Copy link
Member

symlink_file is a fairly thin wrapper around CreateSymbolicLinkW. For absolute paths greater than MAX_PATH you'll currently need to use std::fs::canoncalize to get a path starting with \\?\.

The standard library could definitely be smarter here and do that automatically for you. Though care needs to be taken not to do this for relative paths as those might be intentionally relative (e.g. so a directory can be moved around without breaking the symlinks it contains).

@valarauca
Copy link
Author

You got a typo there I think. (storage vs stroage)

Yeah this was a quick substitution as the real path was much longer and somewhat NSFW.

The standard library could definitely be smarter here and do that automatically for you.

It does?

The problem AFAIT is that a Manifest isn't embedded within the binary to actually enable long path functionality?

@ChrisDenton
Copy link
Member

It does?

No it only does it for the path to the symlink, not the link's target

let original = to_u16s(original)?;

This would need some additional logic to pass through simple relative paths but to make verbatim absolute paths longer than MAX_PATH.

@valarauca
Copy link
Author

Cool.

Forcing long paths everywhere via the registery & embedding a manifest appears to have fixed my issue. I"ll close this as "Error exists between keyboard & chair".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants