Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my canonicalized path get prefixed with \\?\

I'm working on a personal project that I was trying to solve via canonicalizing a relative path in Rust. However, whenever I do so, the new path gets prefixed with a strange \\?\ sequence. For example, something as simple as:

let p = fs::canonicalize(".").unwrap();
println!("{}", p.display());

will result in something like the following output:

\\?\C:\Users\[...]\rustprojects\projectname

This isn't a particular problem because I can accomplish what I'm attempting in other ways. However, it seems like odd behavior, especially if you are going to use the string form of the path in some way that requires accuracy. Why is this sequence of characters prepending the result, and how can I avoid it?

like image 377
MutantOctopus Avatar asked Dec 14 '22 01:12

MutantOctopus


1 Answers

The \\?\ prefix tells Windows to treat the path as is, i.e. it disables the special meaning of . and .., special device names like CON are not interpreted and the path is assumed to be absolute. It also enables using paths up to 32,767 characters (UTF-16 code units), whereas otherwise the limit is 260 (unless you're on Windows 10, version 1607 or later, and your application opts in to longer paths).

Therefore, the \\?\ prefix ensures that you'll get a usable path; removing that prefix may yield a path that is unusable or that resolves to a different file! As such, I would recommend that you keep that prefix in your paths.

like image 151
Francis Gagné Avatar answered Dec 19 '22 12:12

Francis Gagné