std\sys\fs/
windows.rs

1#![allow(nonstandard_style)]
2
3use crate::alloc::{Layout, alloc, dealloc};
4use crate::borrow::Cow;
5use crate::ffi::{OsStr, OsString, c_void};
6use crate::fs::TryLockError;
7use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
8use crate::mem::{self, MaybeUninit, offset_of};
9use crate::os::windows::io::{AsHandle, BorrowedHandle};
10use crate::os::windows::prelude::*;
11use crate::path::{Path, PathBuf};
12use crate::sync::Arc;
13use crate::sys::handle::Handle;
14use crate::sys::pal::api::{self, WinError, set_file_information_by_handle};
15use crate::sys::pal::{IoResult, fill_utf16_buf, to_u16s, truncate_utf16_at_nul};
16use crate::sys::path::{WCStr, maybe_verbatim};
17use crate::sys::time::SystemTime;
18use crate::sys::{Align8, c, cvt};
19use crate::sys_common::{AsInner, FromInner, IntoInner};
20use crate::{fmt, ptr, slice};
21
22mod remove_dir_all;
23use remove_dir_all::remove_dir_all_iterative;
24
25pub struct File {
26    handle: Handle,
27}
28
29#[derive(Clone)]
30pub struct FileAttr {
31    attributes: u32,
32    creation_time: c::FILETIME,
33    last_access_time: c::FILETIME,
34    last_write_time: c::FILETIME,
35    change_time: Option<c::FILETIME>,
36    file_size: u64,
37    reparse_tag: u32,
38    volume_serial_number: Option<u32>,
39    number_of_links: Option<u32>,
40    file_index: Option<u64>,
41}
42
43#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
44pub struct FileType {
45    is_directory: bool,
46    is_symlink: bool,
47}
48
49pub struct ReadDir {
50    handle: Option<FindNextFileHandle>,
51    root: Arc<PathBuf>,
52    first: Option<c::WIN32_FIND_DATAW>,
53}
54
55struct FindNextFileHandle(c::HANDLE);
56
57unsafe impl Send for FindNextFileHandle {}
58unsafe impl Sync for FindNextFileHandle {}
59
60pub struct DirEntry {
61    root: Arc<PathBuf>,
62    data: c::WIN32_FIND_DATAW,
63}
64
65unsafe impl Send for OpenOptions {}
66unsafe impl Sync for OpenOptions {}
67
68#[derive(Clone, Debug)]
69pub struct OpenOptions {
70    // generic
71    read: bool,
72    write: bool,
73    append: bool,
74    truncate: bool,
75    create: bool,
76    create_new: bool,
77    // system-specific
78    custom_flags: u32,
79    access_mode: Option<u32>,
80    attributes: u32,
81    share_mode: u32,
82    security_qos_flags: u32,
83    inherit_handle: bool,
84}
85
86#[derive(Clone, PartialEq, Eq, Debug)]
87pub struct FilePermissions {
88    attrs: u32,
89}
90
91#[derive(Copy, Clone, Debug, Default)]
92pub struct FileTimes {
93    accessed: Option<c::FILETIME>,
94    modified: Option<c::FILETIME>,
95    created: Option<c::FILETIME>,
96}
97
98impl fmt::Debug for c::FILETIME {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        let time = ((self.dwHighDateTime as u64) << 32) | self.dwLowDateTime as u64;
101        f.debug_tuple("FILETIME").field(&time).finish()
102    }
103}
104
105#[derive(Debug)]
106pub struct DirBuilder;
107
108impl fmt::Debug for ReadDir {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        // This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
111        // Thus the result will be e g 'ReadDir("C:\")'
112        fmt::Debug::fmt(&*self.root, f)
113    }
114}
115
116impl Iterator for ReadDir {
117    type Item = io::Result<DirEntry>;
118    fn next(&mut self) -> Option<io::Result<DirEntry>> {
119        let Some(handle) = self.handle.as_ref() else {
120            // This iterator was initialized with an `INVALID_HANDLE_VALUE` as its handle.
121            // Simply return `None` because this is only the case when `FindFirstFileExW` in
122            // the construction of this iterator returns `ERROR_FILE_NOT_FOUND` which means
123            // no matchhing files can be found.
124            return None;
125        };
126        if let Some(first) = self.first.take() {
127            if let Some(e) = DirEntry::new(&self.root, &first) {
128                return Some(Ok(e));
129            }
130        }
131        unsafe {
132            let mut wfd = mem::zeroed();
133            loop {
134                if c::FindNextFileW(handle.0, &mut wfd) == 0 {
135                    self.handle = None;
136                    match api::get_last_error() {
137                        WinError::NO_MORE_FILES => return None,
138                        WinError { code } => {
139                            return Some(Err(Error::from_raw_os_error(code as i32)));
140                        }
141                    }
142                }
143                if let Some(e) = DirEntry::new(&self.root, &wfd) {
144                    return Some(Ok(e));
145                }
146            }
147        }
148    }
149}
150
151impl Drop for FindNextFileHandle {
152    fn drop(&mut self) {
153        let r = unsafe { c::FindClose(self.0) };
154        debug_assert!(r != 0);
155    }
156}
157
158impl DirEntry {
159    fn new(root: &Arc<PathBuf>, wfd: &c::WIN32_FIND_DATAW) -> Option<DirEntry> {
160        match &wfd.cFileName[0..3] {
161            // check for '.' and '..'
162            &[46, 0, ..] | &[46, 46, 0, ..] => return None,
163            _ => {}
164        }
165
166        Some(DirEntry { root: root.clone(), data: *wfd })
167    }
168
169    pub fn path(&self) -> PathBuf {
170        self.root.join(self.file_name())
171    }
172
173    pub fn file_name(&self) -> OsString {
174        let filename = truncate_utf16_at_nul(&self.data.cFileName);
175        OsString::from_wide(filename)
176    }
177
178    pub fn file_type(&self) -> io::Result<FileType> {
179        Ok(FileType::new(
180            self.data.dwFileAttributes,
181            /* reparse_tag = */ self.data.dwReserved0,
182        ))
183    }
184
185    pub fn metadata(&self) -> io::Result<FileAttr> {
186        Ok(self.data.into())
187    }
188}
189
190impl OpenOptions {
191    pub fn new() -> OpenOptions {
192        OpenOptions {
193            // generic
194            read: false,
195            write: false,
196            append: false,
197            truncate: false,
198            create: false,
199            create_new: false,
200            // system-specific
201            custom_flags: 0,
202            access_mode: None,
203            share_mode: c::FILE_SHARE_READ | c::FILE_SHARE_WRITE | c::FILE_SHARE_DELETE,
204            attributes: 0,
205            security_qos_flags: 0,
206            inherit_handle: false,
207        }
208    }
209
210    pub fn read(&mut self, read: bool) {
211        self.read = read;
212    }
213    pub fn write(&mut self, write: bool) {
214        self.write = write;
215    }
216    pub fn append(&mut self, append: bool) {
217        self.append = append;
218    }
219    pub fn truncate(&mut self, truncate: bool) {
220        self.truncate = truncate;
221    }
222    pub fn create(&mut self, create: bool) {
223        self.create = create;
224    }
225    pub fn create_new(&mut self, create_new: bool) {
226        self.create_new = create_new;
227    }
228
229    pub fn custom_flags(&mut self, flags: u32) {
230        self.custom_flags = flags;
231    }
232    pub fn access_mode(&mut self, access_mode: u32) {
233        self.access_mode = Some(access_mode);
234    }
235    pub fn share_mode(&mut self, share_mode: u32) {
236        self.share_mode = share_mode;
237    }
238    pub fn attributes(&mut self, attrs: u32) {
239        self.attributes = attrs;
240    }
241    pub fn security_qos_flags(&mut self, flags: u32) {
242        // We have to set `SECURITY_SQOS_PRESENT` here, because one of the valid flags we can
243        // receive is `SECURITY_ANONYMOUS = 0x0`, which we can't check for later on.
244        self.security_qos_flags = flags | c::SECURITY_SQOS_PRESENT;
245    }
246    pub fn inherit_handle(&mut self, inherit: bool) {
247        self.inherit_handle = inherit;
248    }
249
250    fn get_access_mode(&self) -> io::Result<u32> {
251        match (self.read, self.write, self.append, self.access_mode) {
252            (.., Some(mode)) => Ok(mode),
253            (true, false, false, None) => Ok(c::GENERIC_READ),
254            (false, true, false, None) => Ok(c::GENERIC_WRITE),
255            (true, true, false, None) => Ok(c::GENERIC_READ | c::GENERIC_WRITE),
256            (false, _, true, None) => Ok(c::FILE_GENERIC_WRITE & !c::FILE_WRITE_DATA),
257            (true, _, true, None) => {
258                Ok(c::GENERIC_READ | (c::FILE_GENERIC_WRITE & !c::FILE_WRITE_DATA))
259            }
260            (false, false, false, None) => {
261                Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32))
262            }
263        }
264    }
265
266    fn get_creation_mode(&self) -> io::Result<u32> {
267        match (self.write, self.append) {
268            (true, false) => {}
269            (false, false) => {
270                if self.truncate || self.create || self.create_new {
271                    return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32));
272                }
273            }
274            (_, true) => {
275                if self.truncate && !self.create_new {
276                    return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32));
277                }
278            }
279        }
280
281        Ok(match (self.create, self.truncate, self.create_new) {
282            (false, false, false) => c::OPEN_EXISTING,
283            (true, false, false) => c::OPEN_ALWAYS,
284            (false, true, false) => c::TRUNCATE_EXISTING,
285            // `CREATE_ALWAYS` has weird semantics so we emulate it using
286            // `OPEN_ALWAYS` and a manual truncation step. See #115745.
287            (true, true, false) => c::OPEN_ALWAYS,
288            (_, _, true) => c::CREATE_NEW,
289        })
290    }
291
292    fn get_flags_and_attributes(&self) -> u32 {
293        self.custom_flags
294            | self.attributes
295            | self.security_qos_flags
296            | if self.create_new { c::FILE_FLAG_OPEN_REPARSE_POINT } else { 0 }
297    }
298}
299
300impl File {
301    pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
302        let path = maybe_verbatim(path)?;
303        // SAFETY: maybe_verbatim returns null-terminated strings
304        let path = unsafe { WCStr::from_wchars_with_null_unchecked(&path) };
305        Self::open_native(&path, opts)
306    }
307
308    fn open_native(path: &WCStr, opts: &OpenOptions) -> io::Result<File> {
309        let creation = opts.get_creation_mode()?;
310        let sa = c::SECURITY_ATTRIBUTES {
311            nLength: size_of::<c::SECURITY_ATTRIBUTES>() as u32,
312            lpSecurityDescriptor: ptr::null_mut(),
313            bInheritHandle: opts.inherit_handle as c::BOOL,
314        };
315        let handle = unsafe {
316            c::CreateFileW(
317                path.as_ptr(),
318                opts.get_access_mode()?,
319                opts.share_mode,
320                if opts.inherit_handle { &sa } else { ptr::null() },
321                creation,
322                opts.get_flags_and_attributes(),
323                ptr::null_mut(),
324            )
325        };
326        let handle = unsafe { HandleOrInvalid::from_raw_handle(handle) };
327        if let Ok(handle) = OwnedHandle::try_from(handle) {
328            // Manual truncation. See #115745.
329            if opts.truncate
330                && creation == c::OPEN_ALWAYS
331                && api::get_last_error() == WinError::ALREADY_EXISTS
332            {
333                // This first tries `FileAllocationInfo` but falls back to
334                // `FileEndOfFileInfo` in order to support WINE.
335                // If WINE gains support for FileAllocationInfo, we should
336                // remove the fallback.
337                let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 };
338                set_file_information_by_handle(handle.as_raw_handle(), &alloc)
339                    .or_else(|_| {
340                        let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
341                        set_file_information_by_handle(handle.as_raw_handle(), &eof)
342                    })
343                    .io_result()?;
344            }
345            Ok(File { handle: Handle::from_inner(handle) })
346        } else {
347            Err(Error::last_os_error())
348        }
349    }
350
351    pub fn fsync(&self) -> io::Result<()> {
352        cvt(unsafe { c::FlushFileBuffers(self.handle.as_raw_handle()) })?;
353        Ok(())
354    }
355
356    pub fn datasync(&self) -> io::Result<()> {
357        self.fsync()
358    }
359
360    fn acquire_lock(&self, flags: c::LOCK_FILE_FLAGS) -> io::Result<()> {
361        unsafe {
362            let mut overlapped: c::OVERLAPPED = mem::zeroed();
363            let event = c::CreateEventW(ptr::null_mut(), c::FALSE, c::FALSE, ptr::null());
364            if event.is_null() {
365                return Err(io::Error::last_os_error());
366            }
367            overlapped.hEvent = event;
368            let lock_result = cvt(c::LockFileEx(
369                self.handle.as_raw_handle(),
370                flags,
371                0,
372                u32::MAX,
373                u32::MAX,
374                &mut overlapped,
375            ));
376
377            let final_result = match lock_result {
378                Ok(_) => Ok(()),
379                Err(err) => {
380                    if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
381                        // Wait for the lock to be acquired, and get the lock operation status.
382                        // This can happen asynchronously, if the file handle was opened for async IO
383                        let mut bytes_transferred = 0;
384                        cvt(c::GetOverlappedResult(
385                            self.handle.as_raw_handle(),
386                            &mut overlapped,
387                            &mut bytes_transferred,
388                            c::TRUE,
389                        ))
390                        .map(|_| ())
391                    } else {
392                        Err(err)
393                    }
394                }
395            };
396            c::CloseHandle(overlapped.hEvent);
397            final_result
398        }
399    }
400
401    pub fn lock(&self) -> io::Result<()> {
402        self.acquire_lock(c::LOCKFILE_EXCLUSIVE_LOCK)
403    }
404
405    pub fn lock_shared(&self) -> io::Result<()> {
406        self.acquire_lock(0)
407    }
408
409    pub fn try_lock(&self) -> Result<(), TryLockError> {
410        let result = cvt(unsafe {
411            let mut overlapped = mem::zeroed();
412            c::LockFileEx(
413                self.handle.as_raw_handle(),
414                c::LOCKFILE_EXCLUSIVE_LOCK | c::LOCKFILE_FAIL_IMMEDIATELY,
415                0,
416                u32::MAX,
417                u32::MAX,
418                &mut overlapped,
419            )
420        });
421
422        match result {
423            Ok(_) => Ok(()),
424            Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
425                Err(TryLockError::WouldBlock)
426            }
427            Err(err) => Err(TryLockError::Error(err)),
428        }
429    }
430
431    pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
432        let result = cvt(unsafe {
433            let mut overlapped = mem::zeroed();
434            c::LockFileEx(
435                self.handle.as_raw_handle(),
436                c::LOCKFILE_FAIL_IMMEDIATELY,
437                0,
438                u32::MAX,
439                u32::MAX,
440                &mut overlapped,
441            )
442        });
443
444        match result {
445            Ok(_) => Ok(()),
446            Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
447                Err(TryLockError::WouldBlock)
448            }
449            Err(err) => Err(TryLockError::Error(err)),
450        }
451    }
452
453    pub fn unlock(&self) -> io::Result<()> {
454        // Unlock the handle twice because LockFileEx() allows a file handle to acquire
455        // both an exclusive and shared lock, in which case the documentation states that:
456        // "...two unlock operations are necessary to unlock the region; the first unlock operation
457        // unlocks the exclusive lock, the second unlock operation unlocks the shared lock"
458        cvt(unsafe { c::UnlockFile(self.handle.as_raw_handle(), 0, 0, u32::MAX, u32::MAX) })?;
459        let result =
460            cvt(unsafe { c::UnlockFile(self.handle.as_raw_handle(), 0, 0, u32::MAX, u32::MAX) });
461        match result {
462            Ok(_) => Ok(()),
463            Err(err) if err.raw_os_error() == Some(c::ERROR_NOT_LOCKED as i32) => Ok(()),
464            Err(err) => Err(err),
465        }
466    }
467
468    pub fn truncate(&self, size: u64) -> io::Result<()> {
469        let info = c::FILE_END_OF_FILE_INFO { EndOfFile: size as i64 };
470        api::set_file_information_by_handle(self.handle.as_raw_handle(), &info).io_result()
471    }
472
473    #[cfg(not(target_vendor = "uwp"))]
474    pub fn file_attr(&self) -> io::Result<FileAttr> {
475        unsafe {
476            let mut info: c::BY_HANDLE_FILE_INFORMATION = mem::zeroed();
477            cvt(c::GetFileInformationByHandle(self.handle.as_raw_handle(), &mut info))?;
478            let mut reparse_tag = 0;
479            if info.dwFileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
480                let mut attr_tag: c::FILE_ATTRIBUTE_TAG_INFO = mem::zeroed();
481                cvt(c::GetFileInformationByHandleEx(
482                    self.handle.as_raw_handle(),
483                    c::FileAttributeTagInfo,
484                    (&raw mut attr_tag).cast(),
485                    size_of::<c::FILE_ATTRIBUTE_TAG_INFO>().try_into().unwrap(),
486                ))?;
487                if attr_tag.FileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
488                    reparse_tag = attr_tag.ReparseTag;
489                }
490            }
491            Ok(FileAttr {
492                attributes: info.dwFileAttributes,
493                creation_time: info.ftCreationTime,
494                last_access_time: info.ftLastAccessTime,
495                last_write_time: info.ftLastWriteTime,
496                change_time: None, // Only available in FILE_BASIC_INFO
497                file_size: (info.nFileSizeLow as u64) | ((info.nFileSizeHigh as u64) << 32),
498                reparse_tag,
499                volume_serial_number: Some(info.dwVolumeSerialNumber),
500                number_of_links: Some(info.nNumberOfLinks),
501                file_index: Some(
502                    (info.nFileIndexLow as u64) | ((info.nFileIndexHigh as u64) << 32),
503                ),
504            })
505        }
506    }
507
508    #[cfg(target_vendor = "uwp")]
509    pub fn file_attr(&self) -> io::Result<FileAttr> {
510        unsafe {
511            let mut info: c::FILE_BASIC_INFO = mem::zeroed();
512            let size = size_of_val(&info);
513            cvt(c::GetFileInformationByHandleEx(
514                self.handle.as_raw_handle(),
515                c::FileBasicInfo,
516                (&raw mut info) as *mut c_void,
517                size as u32,
518            ))?;
519            let mut attr = FileAttr {
520                attributes: info.FileAttributes,
521                creation_time: c::FILETIME {
522                    dwLowDateTime: info.CreationTime as u32,
523                    dwHighDateTime: (info.CreationTime >> 32) as u32,
524                },
525                last_access_time: c::FILETIME {
526                    dwLowDateTime: info.LastAccessTime as u32,
527                    dwHighDateTime: (info.LastAccessTime >> 32) as u32,
528                },
529                last_write_time: c::FILETIME {
530                    dwLowDateTime: info.LastWriteTime as u32,
531                    dwHighDateTime: (info.LastWriteTime >> 32) as u32,
532                },
533                change_time: Some(c::FILETIME {
534                    dwLowDateTime: info.ChangeTime as u32,
535                    dwHighDateTime: (info.ChangeTime >> 32) as u32,
536                }),
537                file_size: 0,
538                reparse_tag: 0,
539                volume_serial_number: None,
540                number_of_links: None,
541                file_index: None,
542            };
543            let mut info: c::FILE_STANDARD_INFO = mem::zeroed();
544            let size = size_of_val(&info);
545            cvt(c::GetFileInformationByHandleEx(
546                self.handle.as_raw_handle(),
547                c::FileStandardInfo,
548                (&raw mut info) as *mut c_void,
549                size as u32,
550            ))?;
551            attr.file_size = info.AllocationSize as u64;
552            attr.number_of_links = Some(info.NumberOfLinks);
553            if attr.attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
554                let mut attr_tag: c::FILE_ATTRIBUTE_TAG_INFO = mem::zeroed();
555                cvt(c::GetFileInformationByHandleEx(
556                    self.handle.as_raw_handle(),
557                    c::FileAttributeTagInfo,
558                    (&raw mut attr_tag).cast(),
559                    size_of::<c::FILE_ATTRIBUTE_TAG_INFO>().try_into().unwrap(),
560                ))?;
561                if attr_tag.FileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
562                    attr.reparse_tag = attr_tag.ReparseTag;
563                }
564            }
565            Ok(attr)
566        }
567    }
568
569    pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
570        self.handle.read(buf)
571    }
572
573    pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
574        self.handle.read_vectored(bufs)
575    }
576
577    #[inline]
578    pub fn is_read_vectored(&self) -> bool {
579        self.handle.is_read_vectored()
580    }
581
582    pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
583        self.handle.read_at(buf, offset)
584    }
585
586    pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
587        self.handle.read_buf(cursor)
588    }
589
590    pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
591        self.handle.write(buf)
592    }
593
594    pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
595        self.handle.write_vectored(bufs)
596    }
597
598    #[inline]
599    pub fn is_write_vectored(&self) -> bool {
600        self.handle.is_write_vectored()
601    }
602
603    pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
604        self.handle.write_at(buf, offset)
605    }
606
607    pub fn flush(&self) -> io::Result<()> {
608        Ok(())
609    }
610
611    pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
612        let (whence, pos) = match pos {
613            // Casting to `i64` is fine, `SetFilePointerEx` reinterprets this
614            // integer as `u64`.
615            SeekFrom::Start(n) => (c::FILE_BEGIN, n as i64),
616            SeekFrom::End(n) => (c::FILE_END, n),
617            SeekFrom::Current(n) => (c::FILE_CURRENT, n),
618        };
619        let pos = pos as i64;
620        let mut newpos = 0;
621        cvt(unsafe { c::SetFilePointerEx(self.handle.as_raw_handle(), pos, &mut newpos, whence) })?;
622        Ok(newpos as u64)
623    }
624
625    pub fn size(&self) -> Option<io::Result<u64>> {
626        let mut result = 0;
627        Some(
628            cvt(unsafe { c::GetFileSizeEx(self.handle.as_raw_handle(), &mut result) })
629                .map(|_| result as u64),
630        )
631    }
632
633    pub fn tell(&self) -> io::Result<u64> {
634        self.seek(SeekFrom::Current(0))
635    }
636
637    pub fn duplicate(&self) -> io::Result<File> {
638        Ok(Self { handle: self.handle.try_clone()? })
639    }
640
641    // NB: returned pointer is derived from `space`, and has provenance to
642    // match. A raw pointer is returned rather than a reference in order to
643    // avoid narrowing provenance to the actual `REPARSE_DATA_BUFFER`.
644    fn reparse_point(
645        &self,
646        space: &mut Align8<[MaybeUninit<u8>]>,
647    ) -> io::Result<(u32, *mut c::REPARSE_DATA_BUFFER)> {
648        unsafe {
649            let mut bytes = 0;
650            cvt({
651                // Grab this in advance to avoid it invalidating the pointer
652                // we get from `space.0.as_mut_ptr()`.
653                let len = space.0.len();
654                c::DeviceIoControl(
655                    self.handle.as_raw_handle(),
656                    c::FSCTL_GET_REPARSE_POINT,
657                    ptr::null_mut(),
658                    0,
659                    space.0.as_mut_ptr().cast(),
660                    len as u32,
661                    &mut bytes,
662                    ptr::null_mut(),
663                )
664            })?;
665            const _: () = assert!(align_of::<c::REPARSE_DATA_BUFFER>() <= 8);
666            Ok((bytes, space.0.as_mut_ptr().cast::<c::REPARSE_DATA_BUFFER>()))
667        }
668    }
669
670    fn readlink(&self) -> io::Result<PathBuf> {
671        let mut space =
672            Align8([MaybeUninit::<u8>::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize]);
673        let (_bytes, buf) = self.reparse_point(&mut space)?;
674        unsafe {
675            let (path_buffer, subst_off, subst_len, relative) = match (*buf).ReparseTag {
676                c::IO_REPARSE_TAG_SYMLINK => {
677                    let info: *mut c::SYMBOLIC_LINK_REPARSE_BUFFER = (&raw mut (*buf).rest).cast();
678                    assert!(info.is_aligned());
679                    (
680                        (&raw mut (*info).PathBuffer).cast::<u16>(),
681                        (*info).SubstituteNameOffset / 2,
682                        (*info).SubstituteNameLength / 2,
683                        (*info).Flags & c::SYMLINK_FLAG_RELATIVE != 0,
684                    )
685                }
686                c::IO_REPARSE_TAG_MOUNT_POINT => {
687                    let info: *mut c::MOUNT_POINT_REPARSE_BUFFER = (&raw mut (*buf).rest).cast();
688                    assert!(info.is_aligned());
689                    (
690                        (&raw mut (*info).PathBuffer).cast::<u16>(),
691                        (*info).SubstituteNameOffset / 2,
692                        (*info).SubstituteNameLength / 2,
693                        false,
694                    )
695                }
696                _ => {
697                    return Err(io::const_error!(
698                        io::ErrorKind::Uncategorized,
699                        "Unsupported reparse point type",
700                    ));
701                }
702            };
703            let subst_ptr = path_buffer.add(subst_off.into());
704            let subst = slice::from_raw_parts_mut(subst_ptr, subst_len as usize);
705            // Absolute paths start with an NT internal namespace prefix `\??\`
706            // We should not let it leak through.
707            if !relative && subst.starts_with(&[92u16, 63u16, 63u16, 92u16]) {
708                // Turn `\??\` into `\\?\` (a verbatim path).
709                subst[1] = b'\\' as u16;
710                // Attempt to convert to a more user-friendly path.
711                let user = crate::sys::args::from_wide_to_user_path(
712                    subst.iter().copied().chain([0]).collect(),
713                )?;
714                Ok(PathBuf::from(OsString::from_wide(user.strip_suffix(&[0]).unwrap_or(&user))))
715            } else {
716                Ok(PathBuf::from(OsString::from_wide(subst)))
717            }
718        }
719    }
720
721    pub fn set_permissions(&self, perm: FilePermissions) -> io::Result<()> {
722        let info = c::FILE_BASIC_INFO {
723            CreationTime: 0,
724            LastAccessTime: 0,
725            LastWriteTime: 0,
726            ChangeTime: 0,
727            FileAttributes: perm.attrs,
728        };
729        api::set_file_information_by_handle(self.handle.as_raw_handle(), &info).io_result()
730    }
731
732    pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
733        let is_zero = |t: c::FILETIME| t.dwLowDateTime == 0 && t.dwHighDateTime == 0;
734        if times.accessed.map_or(false, is_zero)
735            || times.modified.map_or(false, is_zero)
736            || times.created.map_or(false, is_zero)
737        {
738            return Err(io::const_error!(
739                io::ErrorKind::InvalidInput,
740                "cannot set file timestamp to 0",
741            ));
742        }
743        let is_max = |t: c::FILETIME| t.dwLowDateTime == u32::MAX && t.dwHighDateTime == u32::MAX;
744        if times.accessed.map_or(false, is_max)
745            || times.modified.map_or(false, is_max)
746            || times.created.map_or(false, is_max)
747        {
748            return Err(io::const_error!(
749                io::ErrorKind::InvalidInput,
750                "cannot set file timestamp to 0xFFFF_FFFF_FFFF_FFFF",
751            ));
752        }
753        cvt(unsafe {
754            let created =
755                times.created.as_ref().map(|a| a as *const c::FILETIME).unwrap_or(ptr::null());
756            let accessed =
757                times.accessed.as_ref().map(|a| a as *const c::FILETIME).unwrap_or(ptr::null());
758            let modified =
759                times.modified.as_ref().map(|a| a as *const c::FILETIME).unwrap_or(ptr::null());
760            c::SetFileTime(self.as_raw_handle(), created, accessed, modified)
761        })?;
762        Ok(())
763    }
764
765    /// Gets only basic file information such as attributes and file times.
766    fn basic_info(&self) -> io::Result<c::FILE_BASIC_INFO> {
767        unsafe {
768            let mut info: c::FILE_BASIC_INFO = mem::zeroed();
769            let size = size_of_val(&info);
770            cvt(c::GetFileInformationByHandleEx(
771                self.handle.as_raw_handle(),
772                c::FileBasicInfo,
773                (&raw mut info) as *mut c_void,
774                size as u32,
775            ))?;
776            Ok(info)
777        }
778    }
779
780    /// Deletes the file, consuming the file handle to ensure the delete occurs
781    /// as immediately as possible.
782    /// This attempts to use `posix_delete` but falls back to `win32_delete`
783    /// if that is not supported by the filesystem.
784    #[allow(unused)]
785    fn delete(self) -> Result<(), WinError> {
786        // If POSIX delete is not supported for this filesystem then fallback to win32 delete.
787        match self.posix_delete() {
788            Err(WinError::INVALID_PARAMETER)
789            | Err(WinError::NOT_SUPPORTED)
790            | Err(WinError::INVALID_FUNCTION) => self.win32_delete(),
791            result => result,
792        }
793    }
794
795    /// Delete using POSIX semantics.
796    ///
797    /// Files will be deleted as soon as the handle is closed. This is supported
798    /// for Windows 10 1607 (aka RS1) and later. However some filesystem
799    /// drivers will not support it even then, e.g. FAT32.
800    ///
801    /// If the operation is not supported for this filesystem or OS version
802    /// then errors will be `ERROR_NOT_SUPPORTED` or `ERROR_INVALID_PARAMETER`.
803    #[allow(unused)]
804    fn posix_delete(&self) -> Result<(), WinError> {
805        let info = c::FILE_DISPOSITION_INFO_EX {
806            Flags: c::FILE_DISPOSITION_FLAG_DELETE
807                | c::FILE_DISPOSITION_FLAG_POSIX_SEMANTICS
808                | c::FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE,
809        };
810        api::set_file_information_by_handle(self.handle.as_raw_handle(), &info)
811    }
812
813    /// Delete a file using win32 semantics. The file won't actually be deleted
814    /// until all file handles are closed. However, marking a file for deletion
815    /// will prevent anyone from opening a new handle to the file.
816    #[allow(unused)]
817    fn win32_delete(&self) -> Result<(), WinError> {
818        let info = c::FILE_DISPOSITION_INFO { DeleteFile: true };
819        api::set_file_information_by_handle(self.handle.as_raw_handle(), &info)
820    }
821
822    /// Fill the given buffer with as many directory entries as will fit.
823    /// This will remember its position and continue from the last call unless
824    /// `restart` is set to `true`.
825    ///
826    /// The returned bool indicates if there are more entries or not.
827    /// It is an error if `self` is not a directory.
828    ///
829    /// # Symlinks and other reparse points
830    ///
831    /// On Windows a file is either a directory or a non-directory.
832    /// A symlink directory is simply an empty directory with some "reparse" metadata attached.
833    /// So if you open a link (not its target) and iterate the directory,
834    /// you will always iterate an empty directory regardless of the target.
835    #[allow(unused)]
836    fn fill_dir_buff(&self, buffer: &mut DirBuff, restart: bool) -> Result<bool, WinError> {
837        let class =
838            if restart { c::FileIdBothDirectoryRestartInfo } else { c::FileIdBothDirectoryInfo };
839
840        unsafe {
841            let result = c::GetFileInformationByHandleEx(
842                self.as_raw_handle(),
843                class,
844                buffer.as_mut_ptr().cast(),
845                buffer.capacity() as _,
846            );
847            if result == 0 {
848                let err = api::get_last_error();
849                if err.code == c::ERROR_NO_MORE_FILES { Ok(false) } else { Err(err) }
850            } else {
851                Ok(true)
852            }
853        }
854    }
855}
856
857/// A buffer for holding directory entries.
858struct DirBuff {
859    buffer: Box<Align8<[MaybeUninit<u8>; Self::BUFFER_SIZE]>>,
860}
861impl DirBuff {
862    const BUFFER_SIZE: usize = 1024;
863    fn new() -> Self {
864        Self {
865            // Safety: `Align8<[MaybeUninit<u8>; N]>` does not need
866            // initialization.
867            buffer: unsafe { Box::new_uninit().assume_init() },
868        }
869    }
870    fn capacity(&self) -> usize {
871        self.buffer.0.len()
872    }
873    fn as_mut_ptr(&mut self) -> *mut u8 {
874        self.buffer.0.as_mut_ptr().cast()
875    }
876    /// Returns a `DirBuffIter`.
877    fn iter(&self) -> DirBuffIter<'_> {
878        DirBuffIter::new(self)
879    }
880}
881impl AsRef<[MaybeUninit<u8>]> for DirBuff {
882    fn as_ref(&self) -> &[MaybeUninit<u8>] {
883        &self.buffer.0
884    }
885}
886
887/// An iterator over entries stored in a `DirBuff`.
888///
889/// Currently only returns file names (UTF-16 encoded).
890struct DirBuffIter<'a> {
891    buffer: Option<&'a [MaybeUninit<u8>]>,
892    cursor: usize,
893}
894impl<'a> DirBuffIter<'a> {
895    fn new(buffer: &'a DirBuff) -> Self {
896        Self { buffer: Some(buffer.as_ref()), cursor: 0 }
897    }
898}
899impl<'a> Iterator for DirBuffIter<'a> {
900    type Item = (Cow<'a, [u16]>, bool);
901    fn next(&mut self) -> Option<Self::Item> {
902        let buffer = &self.buffer?[self.cursor..];
903
904        // Get the name and next entry from the buffer.
905        // SAFETY:
906        // - The buffer contains a `FILE_ID_BOTH_DIR_INFO` struct but the last
907        //   field (the file name) is unsized. So an offset has to be used to
908        //   get the file name slice.
909        // - The OS has guaranteed initialization of the fields of
910        //   `FILE_ID_BOTH_DIR_INFO` and the trailing filename (for at least
911        //   `FileNameLength` bytes)
912        let (name, is_directory, next_entry) = unsafe {
913            let info = buffer.as_ptr().cast::<c::FILE_ID_BOTH_DIR_INFO>();
914            // While this is guaranteed to be aligned in documentation for
915            // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_id_both_dir_info
916            // it does not seem that reality is so kind, and assuming this
917            // caused crashes in some cases (https://github.com/rust-lang/rust/issues/104530)
918            // presumably, this can be blamed on buggy filesystem drivers, but who knows.
919            let next_entry = (&raw const (*info).NextEntryOffset).read_unaligned() as usize;
920            let length = (&raw const (*info).FileNameLength).read_unaligned() as usize;
921            let attrs = (&raw const (*info).FileAttributes).read_unaligned();
922            let name = from_maybe_unaligned(
923                (&raw const (*info).FileName).cast::<u16>(),
924                length / size_of::<u16>(),
925            );
926            let is_directory = (attrs & c::FILE_ATTRIBUTE_DIRECTORY) != 0;
927
928            (name, is_directory, next_entry)
929        };
930
931        if next_entry == 0 {
932            self.buffer = None
933        } else {
934            self.cursor += next_entry
935        }
936
937        // Skip `.` and `..` pseudo entries.
938        const DOT: u16 = b'.' as u16;
939        match &name[..] {
940            [DOT] | [DOT, DOT] => self.next(),
941            _ => Some((name, is_directory)),
942        }
943    }
944}
945
946unsafe fn from_maybe_unaligned<'a>(p: *const u16, len: usize) -> Cow<'a, [u16]> {
947    unsafe {
948        if p.is_aligned() {
949            Cow::Borrowed(crate::slice::from_raw_parts(p, len))
950        } else {
951            Cow::Owned((0..len).map(|i| p.add(i).read_unaligned()).collect())
952        }
953    }
954}
955
956impl AsInner<Handle> for File {
957    #[inline]
958    fn as_inner(&self) -> &Handle {
959        &self.handle
960    }
961}
962
963impl IntoInner<Handle> for File {
964    fn into_inner(self) -> Handle {
965        self.handle
966    }
967}
968
969impl FromInner<Handle> for File {
970    fn from_inner(handle: Handle) -> File {
971        File { handle }
972    }
973}
974
975impl AsHandle for File {
976    fn as_handle(&self) -> BorrowedHandle<'_> {
977        self.as_inner().as_handle()
978    }
979}
980
981impl AsRawHandle for File {
982    fn as_raw_handle(&self) -> RawHandle {
983        self.as_inner().as_raw_handle()
984    }
985}
986
987impl IntoRawHandle for File {
988    fn into_raw_handle(self) -> RawHandle {
989        self.into_inner().into_raw_handle()
990    }
991}
992
993impl FromRawHandle for File {
994    unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
995        unsafe {
996            Self { handle: FromInner::from_inner(FromRawHandle::from_raw_handle(raw_handle)) }
997        }
998    }
999}
1000
1001impl fmt::Debug for File {
1002    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1003        // FIXME(#24570): add more info here (e.g., mode)
1004        let mut b = f.debug_struct("File");
1005        b.field("handle", &self.handle.as_raw_handle());
1006        if let Ok(path) = get_path(self) {
1007            b.field("path", &path);
1008        }
1009        b.finish()
1010    }
1011}
1012
1013impl FileAttr {
1014    pub fn size(&self) -> u64 {
1015        self.file_size
1016    }
1017
1018    pub fn perm(&self) -> FilePermissions {
1019        FilePermissions { attrs: self.attributes }
1020    }
1021
1022    pub fn attrs(&self) -> u32 {
1023        self.attributes
1024    }
1025
1026    pub fn file_type(&self) -> FileType {
1027        FileType::new(self.attributes, self.reparse_tag)
1028    }
1029
1030    pub fn modified(&self) -> io::Result<SystemTime> {
1031        Ok(SystemTime::from(self.last_write_time))
1032    }
1033
1034    pub fn accessed(&self) -> io::Result<SystemTime> {
1035        Ok(SystemTime::from(self.last_access_time))
1036    }
1037
1038    pub fn created(&self) -> io::Result<SystemTime> {
1039        Ok(SystemTime::from(self.creation_time))
1040    }
1041
1042    pub fn modified_u64(&self) -> u64 {
1043        to_u64(&self.last_write_time)
1044    }
1045
1046    pub fn accessed_u64(&self) -> u64 {
1047        to_u64(&self.last_access_time)
1048    }
1049
1050    pub fn created_u64(&self) -> u64 {
1051        to_u64(&self.creation_time)
1052    }
1053
1054    pub fn changed_u64(&self) -> Option<u64> {
1055        self.change_time.as_ref().map(|c| to_u64(c))
1056    }
1057
1058    pub fn volume_serial_number(&self) -> Option<u32> {
1059        self.volume_serial_number
1060    }
1061
1062    pub fn number_of_links(&self) -> Option<u32> {
1063        self.number_of_links
1064    }
1065
1066    pub fn file_index(&self) -> Option<u64> {
1067        self.file_index
1068    }
1069}
1070impl From<c::WIN32_FIND_DATAW> for FileAttr {
1071    fn from(wfd: c::WIN32_FIND_DATAW) -> Self {
1072        FileAttr {
1073            attributes: wfd.dwFileAttributes,
1074            creation_time: wfd.ftCreationTime,
1075            last_access_time: wfd.ftLastAccessTime,
1076            last_write_time: wfd.ftLastWriteTime,
1077            change_time: None,
1078            file_size: ((wfd.nFileSizeHigh as u64) << 32) | (wfd.nFileSizeLow as u64),
1079            reparse_tag: if wfd.dwFileAttributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
1080                // reserved unless this is a reparse point
1081                wfd.dwReserved0
1082            } else {
1083                0
1084            },
1085            volume_serial_number: None,
1086            number_of_links: None,
1087            file_index: None,
1088        }
1089    }
1090}
1091
1092fn to_u64(ft: &c::FILETIME) -> u64 {
1093    (ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32)
1094}
1095
1096impl FilePermissions {
1097    pub fn readonly(&self) -> bool {
1098        self.attrs & c::FILE_ATTRIBUTE_READONLY != 0
1099    }
1100
1101    pub fn set_readonly(&mut self, readonly: bool) {
1102        if readonly {
1103            self.attrs |= c::FILE_ATTRIBUTE_READONLY;
1104        } else {
1105            self.attrs &= !c::FILE_ATTRIBUTE_READONLY;
1106        }
1107    }
1108}
1109
1110impl FileTimes {
1111    pub fn set_accessed(&mut self, t: SystemTime) {
1112        self.accessed = Some(t.into_inner());
1113    }
1114
1115    pub fn set_modified(&mut self, t: SystemTime) {
1116        self.modified = Some(t.into_inner());
1117    }
1118
1119    pub fn set_created(&mut self, t: SystemTime) {
1120        self.created = Some(t.into_inner());
1121    }
1122}
1123
1124impl FileType {
1125    fn new(attributes: u32, reparse_tag: u32) -> FileType {
1126        let is_directory = attributes & c::FILE_ATTRIBUTE_DIRECTORY != 0;
1127        let is_symlink = {
1128            let is_reparse_point = attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0;
1129            let is_reparse_tag_name_surrogate = reparse_tag & 0x20000000 != 0;
1130            is_reparse_point && is_reparse_tag_name_surrogate
1131        };
1132        FileType { is_directory, is_symlink }
1133    }
1134    pub fn is_dir(&self) -> bool {
1135        !self.is_symlink && self.is_directory
1136    }
1137    pub fn is_file(&self) -> bool {
1138        !self.is_symlink && !self.is_directory
1139    }
1140    pub fn is_symlink(&self) -> bool {
1141        self.is_symlink
1142    }
1143    pub fn is_symlink_dir(&self) -> bool {
1144        self.is_symlink && self.is_directory
1145    }
1146    pub fn is_symlink_file(&self) -> bool {
1147        self.is_symlink && !self.is_directory
1148    }
1149}
1150
1151impl DirBuilder {
1152    pub fn new() -> DirBuilder {
1153        DirBuilder
1154    }
1155
1156    pub fn mkdir(&self, p: &Path) -> io::Result<()> {
1157        let p = maybe_verbatim(p)?;
1158        cvt(unsafe { c::CreateDirectoryW(p.as_ptr(), ptr::null_mut()) })?;
1159        Ok(())
1160    }
1161}
1162
1163pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1164    // We push a `*` to the end of the path which cause the empty path to be
1165    // treated as the current directory. So, for consistency with other platforms,
1166    // we explicitly error on the empty path.
1167    if p.as_os_str().is_empty() {
1168        // Return an error code consistent with other ways of opening files.
1169        // E.g. fs::metadata or File::open.
1170        return Err(io::Error::from_raw_os_error(c::ERROR_PATH_NOT_FOUND as i32));
1171    }
1172    let root = p.to_path_buf();
1173    let star = p.join("*");
1174    let path = maybe_verbatim(&star)?;
1175
1176    unsafe {
1177        let mut wfd: c::WIN32_FIND_DATAW = mem::zeroed();
1178        // this is like FindFirstFileW (see https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfileexw),
1179        // but with FindExInfoBasic it should skip filling WIN32_FIND_DATAW.cAlternateFileName
1180        // (see https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw)
1181        // (which will be always null string value and currently unused) and should be faster.
1182        //
1183        // We can pass FIND_FIRST_EX_LARGE_FETCH to dwAdditionalFlags to speed up things more,
1184        // but as we don't know user's use profile of this function, lets be conservative.
1185        let find_handle = c::FindFirstFileExW(
1186            path.as_ptr(),
1187            c::FindExInfoBasic,
1188            &mut wfd as *mut _ as _,
1189            c::FindExSearchNameMatch,
1190            ptr::null(),
1191            0,
1192        );
1193
1194        if find_handle != c::INVALID_HANDLE_VALUE {
1195            Ok(ReadDir {
1196                handle: Some(FindNextFileHandle(find_handle)),
1197                root: Arc::new(root),
1198                first: Some(wfd),
1199            })
1200        } else {
1201            // The status `ERROR_FILE_NOT_FOUND` is returned by the `FindFirstFileExW` function
1202            // if no matching files can be found, but not necessarily that the path to find the
1203            // files in does not exist.
1204            //
1205            // Hence, a check for whether the path to search in exists is added when the last
1206            // os error returned by Windows is `ERROR_FILE_NOT_FOUND` to handle this scenario.
1207            // If that is the case, an empty `ReadDir` iterator is returned as it returns `None`
1208            // in the initial `.next()` invocation because `ERROR_NO_MORE_FILES` would have been
1209            // returned by the `FindNextFileW` function.
1210            //
1211            // See issue #120040: https://github.com/rust-lang/rust/issues/120040.
1212            let last_error = api::get_last_error();
1213            if last_error == WinError::FILE_NOT_FOUND {
1214                return Ok(ReadDir { handle: None, root: Arc::new(root), first: None });
1215            }
1216
1217            // Just return the error constructed from the raw OS error if the above is not the case.
1218            //
1219            // Note: `ERROR_PATH_NOT_FOUND` would have been returned by the `FindFirstFileExW` function
1220            // when the path to search in does not exist in the first place.
1221            Err(Error::from_raw_os_error(last_error.code as i32))
1222        }
1223    }
1224}
1225
1226pub fn unlink(path: &WCStr) -> io::Result<()> {
1227    if unsafe { c::DeleteFileW(path.as_ptr()) } == 0 {
1228        let err = api::get_last_error();
1229        // if `DeleteFileW` fails with ERROR_ACCESS_DENIED then try to remove
1230        // the file while ignoring the readonly attribute.
1231        // This is accomplished by calling the `posix_delete` function on an open file handle.
1232        if err == WinError::ACCESS_DENIED {
1233            let mut opts = OpenOptions::new();
1234            opts.access_mode(c::DELETE);
1235            opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT);
1236            if let Ok(f) = File::open_native(&path, &opts) {
1237                if f.posix_delete().is_ok() {
1238                    return Ok(());
1239                }
1240            }
1241        }
1242        // return the original error if any of the above fails.
1243        Err(io::Error::from_raw_os_error(err.code as i32))
1244    } else {
1245        Ok(())
1246    }
1247}
1248
1249pub fn rename(old: &WCStr, new: &WCStr) -> io::Result<()> {
1250    if unsafe { c::MoveFileExW(old.as_ptr(), new.as_ptr(), c::MOVEFILE_REPLACE_EXISTING) } == 0 {
1251        let err = api::get_last_error();
1252        // if `MoveFileExW` fails with ERROR_ACCESS_DENIED then try to move
1253        // the file while ignoring the readonly attribute.
1254        // This is accomplished by calling `SetFileInformationByHandle` with `FileRenameInfoEx`.
1255        if err == WinError::ACCESS_DENIED {
1256            let mut opts = OpenOptions::new();
1257            opts.access_mode(c::DELETE);
1258            opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
1259            let Ok(f) = File::open_native(&old, &opts) else { return Err(err).io_result() };
1260
1261            // Calculate the layout of the `FILE_RENAME_INFO` we pass to `SetFileInformation`
1262            // This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
1263            let Ok(new_len_without_nul_in_bytes): Result<u32, _> =
1264                ((new.count_bytes() - 1) * 2).try_into()
1265            else {
1266                return Err(err).io_result();
1267            };
1268            let offset: u32 = offset_of!(c::FILE_RENAME_INFO, FileName).try_into().unwrap();
1269            let struct_size = offset + new_len_without_nul_in_bytes + 2;
1270            let layout =
1271                Layout::from_size_align(struct_size as usize, align_of::<c::FILE_RENAME_INFO>())
1272                    .unwrap();
1273
1274            // SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
1275            let file_rename_info;
1276            unsafe {
1277                file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
1278                if file_rename_info.is_null() {
1279                    return Err(io::ErrorKind::OutOfMemory.into());
1280                }
1281
1282                (&raw mut (*file_rename_info).Anonymous).write(c::FILE_RENAME_INFO_0 {
1283                    Flags: c::FILE_RENAME_FLAG_REPLACE_IF_EXISTS
1284                        | c::FILE_RENAME_FLAG_POSIX_SEMANTICS,
1285                });
1286
1287                (&raw mut (*file_rename_info).RootDirectory).write(ptr::null_mut());
1288                // Don't include the NULL in the size
1289                (&raw mut (*file_rename_info).FileNameLength).write(new_len_without_nul_in_bytes);
1290
1291                new.as_ptr().copy_to_nonoverlapping(
1292                    (&raw mut (*file_rename_info).FileName).cast::<u16>(),
1293                    new.count_bytes(),
1294                );
1295            }
1296
1297            let result = unsafe {
1298                c::SetFileInformationByHandle(
1299                    f.as_raw_handle(),
1300                    c::FileRenameInfoEx,
1301                    file_rename_info.cast::<c_void>(),
1302                    struct_size,
1303                )
1304            };
1305            unsafe { dealloc(file_rename_info.cast::<u8>(), layout) };
1306            if result == 0 {
1307                if api::get_last_error() == WinError::DIR_NOT_EMPTY {
1308                    return Err(WinError::DIR_NOT_EMPTY).io_result();
1309                } else {
1310                    return Err(err).io_result();
1311                }
1312            }
1313        } else {
1314            return Err(err).io_result();
1315        }
1316    }
1317    Ok(())
1318}
1319
1320pub fn rmdir(p: &WCStr) -> io::Result<()> {
1321    cvt(unsafe { c::RemoveDirectoryW(p.as_ptr()) })?;
1322    Ok(())
1323}
1324
1325pub fn remove_dir_all(path: &WCStr) -> io::Result<()> {
1326    // Open a file or directory without following symlinks.
1327    let mut opts = OpenOptions::new();
1328    opts.access_mode(c::FILE_LIST_DIRECTORY);
1329    // `FILE_FLAG_BACKUP_SEMANTICS` allows opening directories.
1330    // `FILE_FLAG_OPEN_REPARSE_POINT` opens a link instead of its target.
1331    opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | c::FILE_FLAG_OPEN_REPARSE_POINT);
1332    let file = File::open_native(path, &opts)?;
1333
1334    // Test if the file is not a directory or a symlink to a directory.
1335    if (file.basic_info()?.FileAttributes & c::FILE_ATTRIBUTE_DIRECTORY) == 0 {
1336        return Err(io::Error::from_raw_os_error(c::ERROR_DIRECTORY as _));
1337    }
1338
1339    // Remove the directory and all its contents.
1340    remove_dir_all_iterative(file).io_result()
1341}
1342
1343pub fn readlink(path: &WCStr) -> io::Result<PathBuf> {
1344    // Open the link with no access mode, instead of generic read.
1345    // By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and Settings", so
1346    // this is needed for a common case.
1347    let mut opts = OpenOptions::new();
1348    opts.access_mode(0);
1349    opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
1350    let file = File::open_native(&path, &opts)?;
1351    file.readlink()
1352}
1353
1354pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
1355    symlink_inner(original, link, false)
1356}
1357
1358pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()> {
1359    let original = to_u16s(original)?;
1360    let link = maybe_verbatim(link)?;
1361    let flags = if dir { c::SYMBOLIC_LINK_FLAG_DIRECTORY } else { 0 };
1362    // Formerly, symlink creation required the SeCreateSymbolicLink privilege. For the Windows 10
1363    // Creators Update, Microsoft loosened this to allow unprivileged symlink creation if the
1364    // computer is in Developer Mode, but SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE must be
1365    // added to dwFlags to opt into this behavior.
1366    let result = cvt(unsafe {
1367        c::CreateSymbolicLinkW(
1368            link.as_ptr(),
1369            original.as_ptr(),
1370            flags | c::SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
1371        ) as c::BOOL
1372    });
1373    if let Err(err) = result {
1374        if err.raw_os_error() == Some(c::ERROR_INVALID_PARAMETER as i32) {
1375            // Older Windows objects to SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
1376            // so if we encounter ERROR_INVALID_PARAMETER, retry without that flag.
1377            cvt(unsafe {
1378                c::CreateSymbolicLinkW(link.as_ptr(), original.as_ptr(), flags) as c::BOOL
1379            })?;
1380        } else {
1381            return Err(err);
1382        }
1383    }
1384    Ok(())
1385}
1386
1387#[cfg(not(target_vendor = "uwp"))]
1388pub fn link(original: &WCStr, link: &WCStr) -> io::Result<()> {
1389    cvt(unsafe { c::CreateHardLinkW(link.as_ptr(), original.as_ptr(), ptr::null_mut()) })?;
1390    Ok(())
1391}
1392
1393#[cfg(target_vendor = "uwp")]
1394pub fn link(_original: &WCStr, _link: &WCStr) -> io::Result<()> {
1395    return Err(io::const_error!(io::ErrorKind::Unsupported, "hard link are not supported on UWP"));
1396}
1397
1398pub fn stat(path: &WCStr) -> io::Result<FileAttr> {
1399    match metadata(path, ReparsePoint::Follow) {
1400        Err(err) if err.raw_os_error() == Some(c::ERROR_CANT_ACCESS_FILE as i32) => {
1401            if let Ok(attrs) = lstat(path) {
1402                if !attrs.file_type().is_symlink() {
1403                    return Ok(attrs);
1404                }
1405            }
1406            Err(err)
1407        }
1408        result => result,
1409    }
1410}
1411
1412pub fn lstat(path: &WCStr) -> io::Result<FileAttr> {
1413    metadata(path, ReparsePoint::Open)
1414}
1415
1416#[repr(u32)]
1417#[derive(Clone, Copy, PartialEq, Eq)]
1418enum ReparsePoint {
1419    Follow = 0,
1420    Open = c::FILE_FLAG_OPEN_REPARSE_POINT,
1421}
1422impl ReparsePoint {
1423    fn as_flag(self) -> u32 {
1424        self as u32
1425    }
1426}
1427
1428fn metadata(path: &WCStr, reparse: ReparsePoint) -> io::Result<FileAttr> {
1429    let mut opts = OpenOptions::new();
1430    // No read or write permissions are necessary
1431    opts.access_mode(0);
1432    opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | reparse.as_flag());
1433
1434    // Attempt to open the file normally.
1435    // If that fails with `ERROR_SHARING_VIOLATION` then retry using `FindFirstFileExW`.
1436    // If the fallback fails for any reason we return the original error.
1437    match File::open_native(&path, &opts) {
1438        Ok(file) => file.file_attr(),
1439        Err(e)
1440            if [Some(c::ERROR_SHARING_VIOLATION as _), Some(c::ERROR_ACCESS_DENIED as _)]
1441                .contains(&e.raw_os_error()) =>
1442        {
1443            // `ERROR_ACCESS_DENIED` is returned when the user doesn't have permission for the resource.
1444            // One such example is `System Volume Information` as default but can be created as well
1445            // `ERROR_SHARING_VIOLATION` will almost never be returned.
1446            // Usually if a file is locked you can still read some metadata.
1447            // However, there are special system files, such as
1448            // `C:\hiberfil.sys`, that are locked in a way that denies even that.
1449            unsafe {
1450                // `FindFirstFileExW` accepts wildcard file names.
1451                // Fortunately wildcards are not valid file names and
1452                // `ERROR_SHARING_VIOLATION` means the file exists (but is locked)
1453                // therefore it's safe to assume the file name given does not
1454                // include wildcards.
1455                let mut wfd: c::WIN32_FIND_DATAW = mem::zeroed();
1456                let handle = c::FindFirstFileExW(
1457                    path.as_ptr(),
1458                    c::FindExInfoBasic,
1459                    &mut wfd as *mut _ as _,
1460                    c::FindExSearchNameMatch,
1461                    ptr::null(),
1462                    0,
1463                );
1464
1465                if handle == c::INVALID_HANDLE_VALUE {
1466                    // This can fail if the user does not have read access to the
1467                    // directory.
1468                    Err(e)
1469                } else {
1470                    // We no longer need the find handle.
1471                    c::FindClose(handle);
1472
1473                    // `FindFirstFileExW` reads the cached file information from the
1474                    // directory. The downside is that this metadata may be outdated.
1475                    let attrs = FileAttr::from(wfd);
1476                    if reparse == ReparsePoint::Follow && attrs.file_type().is_symlink() {
1477                        Err(e)
1478                    } else {
1479                        Ok(attrs)
1480                    }
1481                }
1482            }
1483        }
1484        Err(e) => Err(e),
1485    }
1486}
1487
1488pub fn set_perm(p: &WCStr, perm: FilePermissions) -> io::Result<()> {
1489    unsafe {
1490        cvt(c::SetFileAttributesW(p.as_ptr(), perm.attrs))?;
1491        Ok(())
1492    }
1493}
1494
1495fn get_path(f: &File) -> io::Result<PathBuf> {
1496    fill_utf16_buf(
1497        |buf, sz| unsafe {
1498            c::GetFinalPathNameByHandleW(f.handle.as_raw_handle(), buf, sz, c::VOLUME_NAME_DOS)
1499        },
1500        |buf| PathBuf::from(OsString::from_wide(buf)),
1501    )
1502}
1503
1504pub fn canonicalize(p: &WCStr) -> io::Result<PathBuf> {
1505    let mut opts = OpenOptions::new();
1506    // No read or write permissions are necessary
1507    opts.access_mode(0);
1508    // This flag is so we can open directories too
1509    opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS);
1510    let f = File::open_native(p, &opts)?;
1511    get_path(&f)
1512}
1513
1514pub fn copy(from: &WCStr, to: &WCStr) -> io::Result<u64> {
1515    unsafe extern "system" fn callback(
1516        _TotalFileSize: i64,
1517        _TotalBytesTransferred: i64,
1518        _StreamSize: i64,
1519        StreamBytesTransferred: i64,
1520        dwStreamNumber: u32,
1521        _dwCallbackReason: u32,
1522        _hSourceFile: c::HANDLE,
1523        _hDestinationFile: c::HANDLE,
1524        lpData: *const c_void,
1525    ) -> u32 {
1526        unsafe {
1527            if dwStreamNumber == 1 {
1528                *(lpData as *mut i64) = StreamBytesTransferred;
1529            }
1530            c::PROGRESS_CONTINUE
1531        }
1532    }
1533    let mut size = 0i64;
1534    cvt(unsafe {
1535        c::CopyFileExW(
1536            from.as_ptr(),
1537            to.as_ptr(),
1538            Some(callback),
1539            (&raw mut size) as *mut _,
1540            ptr::null_mut(),
1541            0,
1542        )
1543    })?;
1544    Ok(size as u64)
1545}
1546
1547pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
1548    // Create and open a new directory in one go.
1549    let mut opts = OpenOptions::new();
1550    opts.create_new(true);
1551    opts.write(true);
1552    opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | c::FILE_FLAG_POSIX_SEMANTICS);
1553    opts.attributes(c::FILE_ATTRIBUTE_DIRECTORY);
1554
1555    let d = File::open(link, &opts)?;
1556
1557    // We need to get an absolute, NT-style path.
1558    let path_bytes = original.as_os_str().as_encoded_bytes();
1559    let abs_path: Vec<u16> = if path_bytes.starts_with(br"\\?\") || path_bytes.starts_with(br"\??\")
1560    {
1561        // It's already an absolute path, we just need to convert the prefix to `\??\`
1562        let bytes = unsafe { OsStr::from_encoded_bytes_unchecked(&path_bytes[4..]) };
1563        r"\??\".encode_utf16().chain(bytes.encode_wide()).collect()
1564    } else {
1565        // Get an absolute path and then convert the prefix to `\??\`
1566        let abs_path = crate::path::absolute(original)?.into_os_string().into_encoded_bytes();
1567        if abs_path.len() > 0 && abs_path[1..].starts_with(br":\") {
1568            let bytes = unsafe { OsStr::from_encoded_bytes_unchecked(&abs_path) };
1569            r"\??\".encode_utf16().chain(bytes.encode_wide()).collect()
1570        } else if abs_path.starts_with(br"\\.\") {
1571            let bytes = unsafe { OsStr::from_encoded_bytes_unchecked(&abs_path[4..]) };
1572            r"\??\".encode_utf16().chain(bytes.encode_wide()).collect()
1573        } else if abs_path.starts_with(br"\\") {
1574            let bytes = unsafe { OsStr::from_encoded_bytes_unchecked(&abs_path[2..]) };
1575            r"\??\UNC\".encode_utf16().chain(bytes.encode_wide()).collect()
1576        } else {
1577            return Err(io::const_error!(io::ErrorKind::InvalidInput, "path is not valid"));
1578        }
1579    };
1580    // Defined inline so we don't have to mess about with variable length buffer.
1581    #[repr(C)]
1582    pub struct MountPointBuffer {
1583        ReparseTag: u32,
1584        ReparseDataLength: u16,
1585        Reserved: u16,
1586        SubstituteNameOffset: u16,
1587        SubstituteNameLength: u16,
1588        PrintNameOffset: u16,
1589        PrintNameLength: u16,
1590        PathBuffer: [MaybeUninit<u16>; c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize],
1591    }
1592    let data_len = 12 + (abs_path.len() * 2);
1593    if data_len > u16::MAX as usize {
1594        return Err(io::const_error!(io::ErrorKind::InvalidInput, "`original` path is too long"));
1595    }
1596    let data_len = data_len as u16;
1597    let mut header = MountPointBuffer {
1598        ReparseTag: c::IO_REPARSE_TAG_MOUNT_POINT,
1599        ReparseDataLength: data_len,
1600        Reserved: 0,
1601        SubstituteNameOffset: 0,
1602        SubstituteNameLength: (abs_path.len() * 2) as u16,
1603        PrintNameOffset: ((abs_path.len() + 1) * 2) as u16,
1604        PrintNameLength: 0,
1605        PathBuffer: [MaybeUninit::uninit(); c::MAXIMUM_REPARSE_DATA_BUFFER_SIZE as usize],
1606    };
1607    unsafe {
1608        let ptr = header.PathBuffer.as_mut_ptr();
1609        ptr.copy_from(abs_path.as_ptr().cast_uninit(), abs_path.len());
1610
1611        let mut ret = 0;
1612        cvt(c::DeviceIoControl(
1613            d.as_raw_handle(),
1614            c::FSCTL_SET_REPARSE_POINT,
1615            (&raw const header).cast::<c_void>(),
1616            data_len as u32 + 8,
1617            ptr::null_mut(),
1618            0,
1619            &mut ret,
1620            ptr::null_mut(),
1621        ))
1622        .map(drop)
1623    }
1624}
1625
1626// Try to see if a file exists but, unlike `exists`, report I/O errors.
1627pub fn exists(path: &WCStr) -> io::Result<bool> {
1628    // Open the file to ensure any symlinks are followed to their target.
1629    let mut opts = OpenOptions::new();
1630    // No read, write, etc access rights are needed.
1631    opts.access_mode(0);
1632    // Backup semantics enables opening directories as well as files.
1633    opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS);
1634    match File::open_native(path, &opts) {
1635        Err(e) => match e.kind() {
1636            // The file definitely does not exist
1637            io::ErrorKind::NotFound => Ok(false),
1638
1639            // `ERROR_SHARING_VIOLATION` means that the file has been locked by
1640            // another process. This is often temporary so we simply report it
1641            // as the file existing.
1642            _ if e.raw_os_error() == Some(c::ERROR_SHARING_VIOLATION as i32) => Ok(true),
1643
1644            // `ERROR_CANT_ACCESS_FILE` means that a file exists but that the
1645            // reparse point could not be handled by `CreateFile`.
1646            // This can happen for special files such as:
1647            // * Unix domain sockets which you need to `connect` to
1648            // * App exec links which require using `CreateProcess`
1649            _ if e.raw_os_error() == Some(c::ERROR_CANT_ACCESS_FILE as i32) => Ok(true),
1650
1651            // Other errors such as `ERROR_ACCESS_DENIED` may indicate that the
1652            // file exists. However, these types of errors are usually more
1653            // permanent so we report them here.
1654            _ => Err(e),
1655        },
1656        // The file was opened successfully therefore it must exist,
1657        Ok(_) => Ok(true),
1658    }
1659}