1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#![allow(non_camel_case_types)]
use crate::{
cvt::cvt,
ntdll::{
NtQueryInformationFile, RtlNtStatusToDosError, FILE_ACCESS_INFORMATION,
FILE_INFORMATION_CLASS, FILE_MODE_INFORMATION, IO_STATUS_BLOCK,
},
};
use bitflags::bitflags;
use io_lifetimes::BorrowedHandle;
use std::{
ffi::{c_void, OsString},
fs::File,
io,
os::windows::{
io::FromRawHandle,
prelude::{AsRawHandle, OsStringExt},
},
path::{Path, PathBuf},
ptr, slice,
};
use winapi::{
shared::{minwindef::DWORD, ntstatus, winerror},
um::{
ioapiset::DeviceIoControl,
winbase,
winioctl::FSCTL_GET_REPARSE_POINT,
winnt,
winnt::{IO_REPARSE_TAG_MOUNT_POINT, IO_REPARSE_TAG_SYMLINK, WCHAR},
},
};
const WIDE_MAX_PATH: DWORD = 0x7fff;
#[allow(non_snake_case)]
mod c {
use super::*;
use winapi::ctypes::*;
#[repr(C)]
pub(super) struct REPARSE_DATA_BUFFER {
pub(super) ReparseTag: c_uint,
pub(super) ReparseDataLength: c_ushort,
pub(super) Reserved: c_ushort,
pub(super) rest: (),
}
#[repr(C)]
pub(super) struct SYMBOLIC_LINK_REPARSE_BUFFER {
pub(super) SubstituteNameOffset: c_ushort,
pub(super) SubstituteNameLength: c_ushort,
pub(super) PrintNameOffset: c_ushort,
pub(super) PrintNameLength: c_ushort,
pub(super) Flags: c_ulong,
pub(super) PathBuffer: WCHAR,
}
#[repr(C)]
pub struct MOUNT_POINT_REPARSE_BUFFER {
pub(super) SubstituteNameOffset: c_ushort,
pub(super) SubstituteNameLength: c_ushort,
pub(super) PrintNameOffset: c_ushort,
pub(super) PrintNameLength: c_ushort,
pub(super) PathBuffer: WCHAR,
}
pub(super) const SYMLINK_FLAG_RELATIVE: DWORD = 0x00000001;
pub(super) const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
}
bitflags! {
pub struct Flags: DWORD {
const FILE_FLAG_BACKUP_SEMANTICS = winbase::FILE_FLAG_BACKUP_SEMANTICS;
const FILE_FLAG_DELETE_ON_CLOSE = winbase::FILE_FLAG_DELETE_ON_CLOSE;
const FILE_FLAG_NO_BUFFERING = winbase::FILE_FLAG_NO_BUFFERING;
const FILE_FLAG_OPEN_NO_RECALL = winbase::FILE_FLAG_OPEN_NO_RECALL;
const FILE_FLAG_OPEN_REPARSE_POINT = winbase::FILE_FLAG_OPEN_REPARSE_POINT;
const FILE_FLAG_OVERLAPPED = winbase::FILE_FLAG_OVERLAPPED;
const FILE_FLAG_POSIX_SEMANTICS = winbase::FILE_FLAG_POSIX_SEMANTICS;
const FILE_FLAG_RANDOM_ACCESS = winbase::FILE_FLAG_RANDOM_ACCESS;
const FILE_FLAG_SESSION_AWARE = winbase::FILE_FLAG_SESSION_AWARE;
const FILE_FLAG_SEQUENTIAL_SCAN = winbase::FILE_FLAG_SEQUENTIAL_SCAN;
const FILE_FLAG_WRITE_THROUGH = winbase::FILE_FLAG_WRITE_THROUGH;
}
}
bitflags! {
pub struct AccessMode: DWORD {
const FILE_READ_DATA = winnt::FILE_READ_DATA;
const FILE_LIST_DIRECTORY = winnt::FILE_LIST_DIRECTORY;
const FILE_WRITE_DATA = winnt::FILE_WRITE_DATA;
const FILE_ADD_FILE = winnt::FILE_ADD_FILE;
const FILE_APPEND_DATA = winnt::FILE_APPEND_DATA;
const FILE_ADD_SUBDIRECTORY = winnt::FILE_ADD_SUBDIRECTORY;
const FILE_CREATE_PIPE_INSTANCE = winnt::FILE_CREATE_PIPE_INSTANCE;
const FILE_READ_EA = winnt::FILE_READ_EA;
const FILE_WRITE_EA = winnt::FILE_WRITE_EA;
const FILE_EXECUTE = winnt::FILE_EXECUTE;
const FILE_TRAVERSE = winnt::FILE_TRAVERSE;
const FILE_DELETE_CHILD = winnt::FILE_DELETE_CHILD;
const FILE_READ_ATTRIBUTES = winnt::FILE_READ_ATTRIBUTES;
const FILE_WRITE_ATTRIBUTES = winnt::FILE_WRITE_ATTRIBUTES;
const DELETE = winnt::DELETE;
const READ_CONTROL = winnt::READ_CONTROL;
const SYNCHRONIZE = winnt::SYNCHRONIZE;
const WRITE_DAC = winnt::WRITE_DAC;
const WRITE_OWNER = winnt::WRITE_OWNER;
const ACCESS_SYSTEM_SECURITY = winnt::ACCESS_SYSTEM_SECURITY;
const MAXIMUM_ALLOWED = winnt::MAXIMUM_ALLOWED;
const RESERVED1 = 0x4000000;
const RESERVED2 = 0x8000000;
const GENERIC_ALL = winnt::GENERIC_ALL;
const GENERIC_EXECUTE = winnt::GENERIC_EXECUTE;
const GENERIC_WRITE = winnt::GENERIC_WRITE;
const GENERIC_READ = winnt::GENERIC_READ;
const FILE_GENERIC_READ = winnt::FILE_GENERIC_READ;
const FILE_GENERIC_WRITE = winnt::FILE_GENERIC_WRITE;
const FILE_GENERIC_EXECUTE = winnt::FILE_GENERIC_EXECUTE;
const FILE_ALL_ACCESS = winnt::FILE_ALL_ACCESS;
}
}
bitflags! {
pub struct FileModeInformation: DWORD {
const FILE_WRITE_THROUGH = 0x2;
const FILE_SEQUENTIAL_ONLY = 0x4;
const FILE_NO_INTERMEDIATE_BUFFERING = 0x8;
const FILE_SYNCHRONOUS_IO_ALERT = 0x10;
const FILE_SYNCHRONOUS_IO_NONALERT = 0x20;
const FILE_DELETE_ON_CLOSE = 0x1000;
}
}
pub fn get_file_path(file: &File) -> io::Result<PathBuf> {
use winapi::um::fileapi::GetFinalPathNameByHandleW;
let mut raw_path: Vec<u16> = vec![0; WIDE_MAX_PATH as usize];
let handle = file.as_raw_handle();
let read_len =
cvt(unsafe { GetFinalPathNameByHandleW(handle, raw_path.as_mut_ptr(), WIDE_MAX_PATH, 0) })?;
let written_bytes = raw_path
.get(..read_len as usize)
.ok_or(io::Error::from_raw_os_error(
winerror::ERROR_BUFFER_OVERFLOW as i32,
))?;
Ok(PathBuf::from(OsString::from_wide(written_bytes)))
}
pub fn get_full_path(path: &Path) -> io::Result<PathBuf> {
use std::os::windows::ffi::OsStrExt;
use winapi::um::fileapi::GetFullPathNameW;
let mut wide = path.as_os_str().encode_wide().collect::<Vec<_>>();
wide.push(0);
let mut raw_path: Vec<u16> = vec![0; WIDE_MAX_PATH as usize];
let read_len = cvt(unsafe {
GetFullPathNameW(
wide.as_ptr(),
WIDE_MAX_PATH,
raw_path.as_mut_ptr(),
ptr::null_mut(),
)
})?;
if read_len == 0 {
return Err(io::Error::last_os_error());
}
let written_bytes = raw_path
.get(..read_len as usize)
.ok_or(io::Error::from_raw_os_error(
winerror::ERROR_BUFFER_OVERFLOW as i32,
))?;
Ok(PathBuf::from(OsString::from_wide(written_bytes)))
}
pub fn query_access_information(handle: BorrowedHandle<'_>) -> io::Result<AccessMode> {
let mut io_status_block = IO_STATUS_BLOCK::default();
let mut info = FILE_ACCESS_INFORMATION::default();
unsafe {
let status = NtQueryInformationFile(
handle,
&mut io_status_block,
&mut info as *mut _ as *mut c_void,
std::mem::size_of::<FILE_ACCESS_INFORMATION>() as u32,
FILE_INFORMATION_CLASS::FileAccessInformation,
);
if status != ntstatus::STATUS_SUCCESS {
return Err(io::Error::from_raw_os_error(
RtlNtStatusToDosError(status) as i32
));
}
}
Ok(AccessMode::from_bits_truncate(info.AccessFlags))
}
pub fn query_mode_information(handle: BorrowedHandle<'_>) -> io::Result<FileModeInformation> {
let mut io_status_block = IO_STATUS_BLOCK::default();
let mut info = FILE_MODE_INFORMATION::default();
unsafe {
let status = NtQueryInformationFile(
handle,
&mut io_status_block,
&mut info as *mut _ as *mut c_void,
std::mem::size_of::<FILE_MODE_INFORMATION>() as u32,
FILE_INFORMATION_CLASS::FileModeInformation,
);
if status != ntstatus::STATUS_SUCCESS {
return Err(io::Error::from_raw_os_error(
RtlNtStatusToDosError(status) as i32
));
}
}
Ok(FileModeInformation::from_bits_truncate(info.Mode))
}
pub fn reopen_file(
handle: BorrowedHandle<'_>,
access_mode: AccessMode,
flags: Flags,
) -> io::Result<File> {
let new_handle = unsafe {
winbase::ReOpenFile(
handle.as_raw_handle(),
access_mode.bits(),
winnt::FILE_SHARE_DELETE | winnt::FILE_SHARE_READ | winnt::FILE_SHARE_WRITE,
flags.bits(),
)
};
if new_handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
}
Ok(unsafe { File::from_raw_handle(new_handle) })
}
pub fn read_link(file: &File) -> io::Result<PathBuf> {
let mut space = [0_u8; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
let (_bytes, buf) = reparse_point(file, &mut space)?;
unsafe {
let (path_buffer, subst_off, subst_len, relative) = match buf.ReparseTag {
IO_REPARSE_TAG_SYMLINK => {
let info: *const c::SYMBOLIC_LINK_REPARSE_BUFFER =
&buf.rest as *const _ as *const _;
(
&(*info).PathBuffer as *const _ as *const u16,
(*info).SubstituteNameOffset / 2,
(*info).SubstituteNameLength / 2,
(*info).Flags & c::SYMLINK_FLAG_RELATIVE != 0,
)
}
IO_REPARSE_TAG_MOUNT_POINT => {
let info: *const c::MOUNT_POINT_REPARSE_BUFFER = &buf.rest as *const _ as *const _;
(
&(*info).PathBuffer as *const _ as *const u16,
(*info).SubstituteNameOffset / 2,
(*info).SubstituteNameLength / 2,
false,
)
}
_ => {
return Err(io::Error::new(
io::ErrorKind::Other,
"Unsupported reparse point type",
));
}
};
let subst_ptr = path_buffer.offset(subst_off as isize);
let mut subst = slice::from_raw_parts(subst_ptr, subst_len as usize);
if !relative && subst.starts_with(&[92u16, 63u16, 63u16, 92u16]) {
subst = &subst[4..];
}
Ok(PathBuf::from(OsString::from_wide(subst)))
}
}
fn reparse_point<'a>(
file: &File,
space: &'a mut [u8; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE],
) -> io::Result<(DWORD, &'a c::REPARSE_DATA_BUFFER)> {
unsafe {
let mut bytes = 0;
cvt({
DeviceIoControl(
file.as_raw_handle(),
FSCTL_GET_REPARSE_POINT,
ptr::null_mut(),
0,
space.as_mut_ptr() as *mut _,
space.len() as DWORD,
&mut bytes,
ptr::null_mut(),
)
})?;
Ok((bytes, &*(space.as_ptr() as *const c::REPARSE_DATA_BUFFER)))
}
}