Skip to content

Commit d831760

Browse files
DK26tesuji
authored andcommitted
Fix verbatim prefix handling (#30)
Junction creation now correctly handles paths with Windows verbatim prefix (\\?\). The prefix is stripped before constructing the reparse point data, preventing double-prefix corruption.
1 parent 908ea79 commit d831760

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/internals.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub fn create(target: &Path, junction: &Path) -> io::Result<()> {
2929
// For example, forward slashes cannot be used as a path separator, so we should try to
3030
// canonicalize the path first.
3131
let target = helpers::get_full_path(target)?;
32+
// Strip Win32 verbatim prefix (\\?\) if present - we add NT prefix (\??\) ourselves
33+
const VERBATIM_PREFIX: [u16; 4] = helpers::utf16s(br"\\?\");
34+
let target = target.strip_prefix(VERBATIM_PREFIX.as_slice()).unwrap_or(&target);
3235
fs::create_dir(junction)?;
3336
let file = helpers::open_reparse_point(junction, true)?;
3437
let target_len_in_bytes = {

src/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,11 @@ fn create_with_verbatim_prefix_paths() {
257257
fs::create_dir_all(&link_parent).unwrap();
258258

259259
// std::fs::canonicalize() returns paths with \\?\ verbatim prefix on Windows
260-
let target = fs::canonicalize(&target).unwrap();
260+
let target_canonical = fs::canonicalize(&target).unwrap();
261261
let junction = fs::canonicalize(&link_parent).unwrap().join("junction");
262262

263-
super::create(&target, &junction).unwrap();
263+
super::create(&target_canonical, &junction).unwrap();
264264
assert!(super::exists(&junction).unwrap(), "junction should exist");
265+
// get_target returns path without verbatim prefix
265266
assert_eq!(super::get_target(&junction).unwrap(), target);
266267
}

0 commit comments

Comments
 (0)