Skip to content

Commit 05ba7bb

Browse files
committed
fix symlink test on windows
1 parent 1849b41 commit 05ba7bb

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

library/std/src/sys/fs/windows.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,12 +1199,16 @@ impl Dir {
11991199
let mut opts = OpenOptions::new();
12001200
opts.write(true);
12011201
let linkfile = File::open(link, &opts)?;
1202-
let utf16: Vec<u16> = original.iter().chain(original).copied().collect();
12031202
let file_name_len = u16::try_from(original.len()).or(Err(TOO_LONG_ERR))?;
1203+
let utf16 = b"\\??\\"
1204+
.iter()
1205+
.map(|&n| n as u16)
1206+
.chain(original.iter().copied())
1207+
.collect::<Vec<u16>>();
12041208
let sym_buffer = c::SYMBOLIC_LINK_REPARSE_BUFFER {
12051209
SubstituteNameOffset: 0,
1206-
SubstituteNameLength: file_name_len,
1207-
PrintNameOffset: file_name_len,
1210+
SubstituteNameLength: file_name_len + 4,
1211+
PrintNameOffset: 4,
12081212
PrintNameLength: file_name_len,
12091213
Flags: if relative { c::SYMLINK_FLAG_RELATIVE } else { 0 },
12101214
PathBuffer: 0,
@@ -1222,7 +1226,13 @@ impl Dir {
12221226
unsafe {
12231227
buffer.write(c::REPARSE_DATA_BUFFER {
12241228
ReparseTag: c::IO_REPARSE_TAG_SYMLINK,
1225-
ReparseDataLength: u16::try_from(size_of_val(&sym_buffer)).or(Err(TOO_LONG_ERR))?,
1229+
ReparseDataLength: u16::try_from(
1230+
size_of::<c::REPARSE_DATA_BUFFER>()
1231+
+ size_of::<c::SYMBOLIC_LINK_REPARSE_BUFFER>()
1232+
+ usize::from(file_name_len) * 2
1233+
- offset_of!(c::REPARSE_DATA_BUFFER, Reserved),
1234+
)
1235+
.or(Err(TOO_LONG_ERR))?,
12261236
Reserved: 0,
12271237
rest: (),
12281238
});
@@ -1244,7 +1254,12 @@ impl Dir {
12441254
linkfile.handle.as_raw_handle(),
12451255
c::FSCTL_SET_REPARSE_POINT,
12461256
&raw const buffer as *const c_void,
1247-
u32::try_from(size_of_val(&buffer)).or(Err(TOO_LONG_ERR))?,
1257+
u32::try_from(
1258+
size_of::<c::REPARSE_DATA_BUFFER>()
1259+
+ size_of::<c::SYMBOLIC_LINK_REPARSE_BUFFER>()
1260+
+ usize::from(file_name_len) * 2,
1261+
)
1262+
.or(Err(TOO_LONG_ERR))?,
12481263
ptr::null_mut(),
12491264
0,
12501265
ptr::null_mut(),

0 commit comments

Comments
 (0)