std/os/windows/
fs.rs

1//! Windows-specific extensions to primitives in the [`std::fs`] module.
2//!
3//! [`std::fs`]: crate::fs
4
5#![stable(feature = "rust1", since = "1.0.0")]
6
7use crate::fs::{self, Metadata, OpenOptions};
8use crate::io::BorrowedCursor;
9use crate::path::Path;
10use crate::sealed::Sealed;
11use crate::sys_common::{AsInner, AsInnerMut, IntoInner};
12use crate::time::SystemTime;
13use crate::{io, sys};
14
15/// Windows-specific extensions to [`fs::File`].
16#[stable(feature = "file_offset", since = "1.15.0")]
17pub trait FileExt {
18    /// Seeks to a given position and reads a number of bytes.
19    ///
20    /// Returns the number of bytes read.
21    ///
22    /// The offset is relative to the start of the file and thus independent
23    /// from the current cursor. The current cursor **is** affected by this
24    /// function, it is set to the end of the read.
25    ///
26    /// Reading beyond the end of the file will always return with a length of
27    /// 0\.
28    ///
29    /// Note that similar to `File::read`, it is not an error to return with a
30    /// short read. When returning from such a short read, the file pointer is
31    /// still updated.
32    ///
33    /// # Examples
34    ///
35    /// ```no_run
36    /// use std::io;
37    /// use std::fs::File;
38    /// use std::os::windows::prelude::*;
39    ///
40    /// fn main() -> io::Result<()> {
41    ///     let mut file = File::open("foo.txt")?;
42    ///     let mut buffer = [0; 10];
43    ///
44    ///     // Read 10 bytes, starting 72 bytes from the
45    ///     // start of the file.
46    ///     file.seek_read(&mut buffer[..], 72)?;
47    ///     Ok(())
48    /// }
49    /// ```
50    #[stable(feature = "file_offset", since = "1.15.0")]
51    fn seek_read(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
52
53    /// Seeks to a given position and reads some bytes into the buffer.
54    ///
55    /// This is equivalent to the [`seek_read`](FileExt::seek_read) method, except that it is passed
56    /// a [`BorrowedCursor`] rather than `&mut [u8]` to allow use with uninitialized buffers. The
57    /// new data will be appended to any existing contents of `buf`.
58    ///
59    /// Reading beyond the end of the file will always succeed without reading any bytes.
60    ///
61    /// # Examples
62    ///
63    /// ```no_run
64    /// #![feature(core_io_borrowed_buf)]
65    /// #![feature(read_buf_at)]
66    ///
67    /// use std::io;
68    /// use std::io::BorrowedBuf;
69    /// use std::fs::File;
70    /// use std::mem::MaybeUninit;
71    /// use std::os::windows::prelude::*;
72    ///
73    /// fn main() -> io::Result<()> {
74    ///     let mut file = File::open("pi.txt")?;
75    ///
76    ///     // Read some bytes starting from offset 2
77    ///     let mut buf: [MaybeUninit<u8>; 10] = [MaybeUninit::uninit(); 10];
78    ///     let mut buf = BorrowedBuf::from(buf.as_mut_slice());
79    ///     file.seek_read_buf(buf.unfilled(), 2)?;
80    ///
81    ///     assert!(buf.filled().starts_with(b"1"));
82    ///
83    ///     Ok(())
84    /// }
85    /// ```
86    #[unstable(feature = "read_buf_at", issue = "140771")]
87    fn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
88        io::default_read_buf(|b| self.seek_read(b, offset), buf)
89    }
90
91    /// Seeks to a given position and writes a number of bytes.
92    ///
93    /// Returns the number of bytes written.
94    ///
95    /// The offset is relative to the start of the file and thus independent
96    /// from the current cursor. The current cursor **is** affected by this
97    /// function, it is set to the end of the write.
98    ///
99    /// When writing beyond the end of the file, the file is appropriately
100    /// extended and the intermediate bytes are set to zero.
101    ///
102    /// Note that similar to `File::write`, it is not an error to return a
103    /// short write. When returning from such a short write, the file pointer
104    /// is still updated.
105    ///
106    /// # Examples
107    ///
108    /// ```no_run
109    /// use std::fs::File;
110    /// use std::os::windows::prelude::*;
111    ///
112    /// fn main() -> std::io::Result<()> {
113    ///     let mut buffer = File::create("foo.txt")?;
114    ///
115    ///     // Write a byte string starting 72 bytes from
116    ///     // the start of the file.
117    ///     buffer.seek_write(b"some bytes", 72)?;
118    ///     Ok(())
119    /// }
120    /// ```
121    #[stable(feature = "file_offset", since = "1.15.0")]
122    fn seek_write(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
123}
124
125#[stable(feature = "file_offset", since = "1.15.0")]
126impl FileExt for fs::File {
127    fn seek_read(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
128        self.as_inner().read_at(buf, offset)
129    }
130
131    fn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
132        self.as_inner().read_buf_at(buf, offset)
133    }
134
135    fn seek_write(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
136        self.as_inner().write_at(buf, offset)
137    }
138}
139
140/// Windows-specific extensions to [`fs::OpenOptions`].
141#[stable(feature = "open_options_ext", since = "1.10.0")]
142pub trait OpenOptionsExt {
143    /// Overrides the `dwDesiredAccess` argument to the call to [`CreateFile`]
144    /// with the specified value.
145    ///
146    /// This will override the `read`, `write`, and `append` flags on the
147    /// `OpenOptions` structure. This method provides fine-grained control over
148    /// the permissions to read, write and append data, attributes (like hidden
149    /// and system), and extended attributes.
150    ///
151    /// # Examples
152    ///
153    /// ```no_run
154    /// use std::fs::OpenOptions;
155    /// use std::os::windows::prelude::*;
156    ///
157    /// // Open without read and write permission, for example if you only need
158    /// // to call `stat` on the file
159    /// let file = OpenOptions::new().access_mode(0).open("foo.txt");
160    /// ```
161    ///
162    /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
163    #[stable(feature = "open_options_ext", since = "1.10.0")]
164    fn access_mode(&mut self, access: u32) -> &mut Self;
165
166    /// Overrides the `dwShareMode` argument to the call to [`CreateFile`] with
167    /// the specified value.
168    ///
169    /// By default `share_mode` is set to
170    /// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`. This allows
171    /// other processes to read, write, and delete/rename the same file
172    /// while it is open. Removing any of the flags will prevent other
173    /// processes from performing the corresponding operation until the file
174    /// handle is closed.
175    ///
176    /// # Examples
177    ///
178    /// ```no_run
179    /// use std::fs::OpenOptions;
180    /// use std::os::windows::prelude::*;
181    ///
182    /// // Do not allow others to read or modify this file while we have it open
183    /// // for writing.
184    /// let file = OpenOptions::new()
185    ///     .write(true)
186    ///     .share_mode(0)
187    ///     .open("foo.txt");
188    /// ```
189    ///
190    /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
191    #[stable(feature = "open_options_ext", since = "1.10.0")]
192    fn share_mode(&mut self, val: u32) -> &mut Self;
193
194    /// Sets extra flags for the `dwFileFlags` argument to the call to
195    /// [`CreateFile2`] to the specified value (or combines it with
196    /// `attributes` and `security_qos_flags` to set the `dwFlagsAndAttributes`
197    /// for [`CreateFile`]).
198    ///
199    /// Custom flags can only set flags, not remove flags set by Rust's options.
200    /// This option overwrites any previously set custom flags.
201    ///
202    /// # Examples
203    ///
204    /// ```no_run
205    /// # #![allow(unexpected_cfgs)]
206    /// # #[cfg(for_demonstration_only)]
207    /// extern crate winapi;
208    /// # mod winapi { pub const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x04000000; }
209    ///
210    /// use std::fs::OpenOptions;
211    /// use std::os::windows::prelude::*;
212    ///
213    /// let file = OpenOptions::new()
214    ///     .create(true)
215    ///     .write(true)
216    ///     .custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE)
217    ///     .open("foo.txt");
218    /// ```
219    ///
220    /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
221    /// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
222    #[stable(feature = "open_options_ext", since = "1.10.0")]
223    fn custom_flags(&mut self, flags: u32) -> &mut Self;
224
225    /// Sets the `dwFileAttributes` argument to the call to [`CreateFile2`] to
226    /// the specified value (or combines it with `custom_flags` and
227    /// `security_qos_flags` to set the `dwFlagsAndAttributes` for
228    /// [`CreateFile`]).
229    ///
230    /// If a _new_ file is created because it does not yet exist and
231    /// `.create(true)` or `.create_new(true)` are specified, the new file is
232    /// given the attributes declared with `.attributes()`.
233    ///
234    /// If an _existing_ file is opened with `.create(true).truncate(true)`, its
235    /// existing attributes are preserved and combined with the ones declared
236    /// with `.attributes()`.
237    ///
238    /// In all other cases the attributes get ignored.
239    ///
240    /// # Examples
241    ///
242    /// ```no_run
243    /// # #![allow(unexpected_cfgs)]
244    /// # #[cfg(for_demonstration_only)]
245    /// extern crate winapi;
246    /// # mod winapi { pub const FILE_ATTRIBUTE_HIDDEN: u32 = 2; }
247    ///
248    /// use std::fs::OpenOptions;
249    /// use std::os::windows::prelude::*;
250    ///
251    /// let file = OpenOptions::new()
252    ///     .write(true)
253    ///     .create(true)
254    ///     .attributes(winapi::FILE_ATTRIBUTE_HIDDEN)
255    ///     .open("foo.txt");
256    /// ```
257    ///
258    /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
259    /// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
260    #[stable(feature = "open_options_ext", since = "1.10.0")]
261    fn attributes(&mut self, val: u32) -> &mut Self;
262
263    /// Sets the `dwSecurityQosFlags` argument to the call to [`CreateFile2`] to
264    /// the specified value (or combines it with `custom_flags` and `attributes`
265    /// to set the `dwFlagsAndAttributes` for [`CreateFile`]).
266    ///
267    /// By default `security_qos_flags` is not set. It should be specified when
268    /// opening a named pipe, to control to which degree a server process can
269    /// act on behalf of a client process (security impersonation level).
270    ///
271    /// When `security_qos_flags` is not set, a malicious program can gain the
272    /// elevated privileges of a privileged Rust process when it allows opening
273    /// user-specified paths, by tricking it into opening a named pipe. So
274    /// arguably `security_qos_flags` should also be set when opening arbitrary
275    /// paths. However the bits can then conflict with other flags, specifically
276    /// `FILE_FLAG_OPEN_NO_RECALL`.
277    ///
278    /// For information about possible values, see [Impersonation Levels] on the
279    /// Windows Dev Center site. The `SECURITY_SQOS_PRESENT` flag is set
280    /// automatically when using this method.
281
282    /// # Examples
283    ///
284    /// ```no_run
285    /// # #![allow(unexpected_cfgs)]
286    /// # #[cfg(for_demonstration_only)]
287    /// extern crate winapi;
288    /// # mod winapi { pub const SECURITY_IDENTIFICATION: u32 = 0; }
289    /// use std::fs::OpenOptions;
290    /// use std::os::windows::prelude::*;
291    ///
292    /// let file = OpenOptions::new()
293    ///     .write(true)
294    ///     .create(true)
295    ///
296    ///     // Sets the flag value to `SecurityIdentification`.
297    ///     .security_qos_flags(winapi::SECURITY_IDENTIFICATION)
298    ///
299    ///     .open(r"\\.\pipe\MyPipe");
300    /// ```
301    ///
302    /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
303    /// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
304    /// [Impersonation Levels]:
305    ///     https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-security_impersonation_level
306    #[stable(feature = "open_options_ext", since = "1.10.0")]
307    fn security_qos_flags(&mut self, flags: u32) -> &mut Self;
308}
309
310#[stable(feature = "open_options_ext", since = "1.10.0")]
311impl OpenOptionsExt for OpenOptions {
312    fn access_mode(&mut self, access: u32) -> &mut OpenOptions {
313        self.as_inner_mut().access_mode(access);
314        self
315    }
316
317    fn share_mode(&mut self, share: u32) -> &mut OpenOptions {
318        self.as_inner_mut().share_mode(share);
319        self
320    }
321
322    fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
323        self.as_inner_mut().custom_flags(flags);
324        self
325    }
326
327    fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
328        self.as_inner_mut().attributes(attributes);
329        self
330    }
331
332    fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions {
333        self.as_inner_mut().security_qos_flags(flags);
334        self
335    }
336}
337
338/// Windows-specific extensions to [`fs::Metadata`].
339///
340/// The data members that this trait exposes correspond to the members
341/// of the [`BY_HANDLE_FILE_INFORMATION`] structure.
342///
343/// [`BY_HANDLE_FILE_INFORMATION`]:
344///     https://docs.microsoft.com/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
345#[stable(feature = "metadata_ext", since = "1.1.0")]
346pub trait MetadataExt {
347    /// Returns the value of the `dwFileAttributes` field of this metadata.
348    ///
349    /// This field contains the file system attribute information for a file
350    /// or directory. For possible values and their descriptions, see
351    /// [File Attribute Constants] in the Windows Dev Center.
352    ///
353    /// # Examples
354    ///
355    /// ```no_run
356    /// use std::io;
357    /// use std::fs;
358    /// use std::os::windows::prelude::*;
359    ///
360    /// fn main() -> io::Result<()> {
361    ///     let metadata = fs::metadata("foo.txt")?;
362    ///     let attributes = metadata.file_attributes();
363    ///     Ok(())
364    /// }
365    /// ```
366    ///
367    /// [File Attribute Constants]:
368    ///     https://docs.microsoft.com/windows/win32/fileio/file-attribute-constants
369    #[stable(feature = "metadata_ext", since = "1.1.0")]
370    fn file_attributes(&self) -> u32;
371
372    /// Returns the value of the `ftCreationTime` field of this metadata.
373    ///
374    /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
375    /// which represents the number of 100-nanosecond intervals since
376    /// January 1, 1601 (UTC). The struct is automatically
377    /// converted to a `u64` value, as that is the recommended way
378    /// to use it.
379    ///
380    /// If the underlying filesystem does not support creation time, the
381    /// returned value is 0.
382    ///
383    /// # Examples
384    ///
385    /// ```no_run
386    /// use std::io;
387    /// use std::fs;
388    /// use std::os::windows::prelude::*;
389    ///
390    /// fn main() -> io::Result<()> {
391    ///     let metadata = fs::metadata("foo.txt")?;
392    ///     let creation_time = metadata.creation_time();
393    ///     Ok(())
394    /// }
395    /// ```
396    ///
397    /// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
398    #[stable(feature = "metadata_ext", since = "1.1.0")]
399    fn creation_time(&self) -> u64;
400
401    /// Returns the value of the `ftLastAccessTime` field of this metadata.
402    ///
403    /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
404    /// which represents the number of 100-nanosecond intervals since
405    /// January 1, 1601 (UTC). The struct is automatically
406    /// converted to a `u64` value, as that is the recommended way
407    /// to use it.
408    ///
409    /// For a file, the value specifies the last time that a file was read
410    /// from or written to. For a directory, the value specifies when
411    /// the directory was created. For both files and directories, the
412    /// specified date is correct, but the time of day is always set to
413    /// midnight.
414    ///
415    /// If the underlying filesystem does not support last access time, the
416    /// returned value is 0.
417    ///
418    /// # Examples
419    ///
420    /// ```no_run
421    /// use std::io;
422    /// use std::fs;
423    /// use std::os::windows::prelude::*;
424    ///
425    /// fn main() -> io::Result<()> {
426    ///     let metadata = fs::metadata("foo.txt")?;
427    ///     let last_access_time = metadata.last_access_time();
428    ///     Ok(())
429    /// }
430    /// ```
431    ///
432    /// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
433    #[stable(feature = "metadata_ext", since = "1.1.0")]
434    fn last_access_time(&self) -> u64;
435
436    /// Returns the value of the `ftLastWriteTime` field of this metadata.
437    ///
438    /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
439    /// which represents the number of 100-nanosecond intervals since
440    /// January 1, 1601 (UTC). The struct is automatically
441    /// converted to a `u64` value, as that is the recommended way
442    /// to use it.
443    ///
444    /// For a file, the value specifies the last time that a file was written
445    /// to. For a directory, the structure specifies when the directory was
446    /// created.
447    ///
448    /// If the underlying filesystem does not support the last write time,
449    /// the returned value is 0.
450    ///
451    /// # Examples
452    ///
453    /// ```no_run
454    /// use std::io;
455    /// use std::fs;
456    /// use std::os::windows::prelude::*;
457    ///
458    /// fn main() -> io::Result<()> {
459    ///     let metadata = fs::metadata("foo.txt")?;
460    ///     let last_write_time = metadata.last_write_time();
461    ///     Ok(())
462    /// }
463    /// ```
464    ///
465    /// [`FILETIME`]: https://docs.microsoft.com/windows/win32/api/minwinbase/ns-minwinbase-filetime
466    #[stable(feature = "metadata_ext", since = "1.1.0")]
467    fn last_write_time(&self) -> u64;
468
469    /// Returns the value of the `nFileSize` fields of this
470    /// metadata.
471    ///
472    /// The returned value does not have meaning for directories.
473    ///
474    /// # Examples
475    ///
476    /// ```no_run
477    /// use std::io;
478    /// use std::fs;
479    /// use std::os::windows::prelude::*;
480    ///
481    /// fn main() -> io::Result<()> {
482    ///     let metadata = fs::metadata("foo.txt")?;
483    ///     let file_size = metadata.file_size();
484    ///     Ok(())
485    /// }
486    /// ```
487    #[stable(feature = "metadata_ext", since = "1.1.0")]
488    fn file_size(&self) -> u64;
489
490    /// Returns the value of the `dwVolumeSerialNumber` field of this
491    /// metadata.
492    ///
493    /// This will return `None` if the `Metadata` instance was created from a
494    /// call to `DirEntry::metadata`. If this `Metadata` was created by using
495    /// `fs::metadata` or `File::metadata`, then this will return `Some`.
496    #[unstable(feature = "windows_by_handle", issue = "63010")]
497    fn volume_serial_number(&self) -> Option<u32>;
498
499    /// Returns the value of the `nNumberOfLinks` field of this
500    /// metadata.
501    ///
502    /// This will return `None` if the `Metadata` instance was created from a
503    /// call to `DirEntry::metadata`. If this `Metadata` was created by using
504    /// `fs::metadata` or `File::metadata`, then this will return `Some`.
505    #[unstable(feature = "windows_by_handle", issue = "63010")]
506    fn number_of_links(&self) -> Option<u32>;
507
508    /// Returns the value of the `nFileIndex` fields of this
509    /// metadata.
510    ///
511    /// This will return `None` if the `Metadata` instance was created from a
512    /// call to `DirEntry::metadata`. If this `Metadata` was created by using
513    /// `fs::metadata` or `File::metadata`, then this will return `Some`.
514    #[unstable(feature = "windows_by_handle", issue = "63010")]
515    fn file_index(&self) -> Option<u64>;
516
517    /// Returns the value of the `ChangeTime` fields of this metadata.
518    ///
519    /// `ChangeTime` is the last time file metadata was changed, such as
520    /// renames, attributes, etc.
521    ///
522    /// This will return `None` if `Metadata` instance was created from a call to
523    /// `DirEntry::metadata` or if the `target_vendor` is outside the current platform
524    /// support for this api.
525    #[unstable(feature = "windows_change_time", issue = "121478")]
526    fn change_time(&self) -> Option<u64>;
527}
528
529#[stable(feature = "metadata_ext", since = "1.1.0")]
530impl MetadataExt for Metadata {
531    fn file_attributes(&self) -> u32 {
532        self.as_inner().attrs()
533    }
534    fn creation_time(&self) -> u64 {
535        self.as_inner().created_u64()
536    }
537    fn last_access_time(&self) -> u64 {
538        self.as_inner().accessed_u64()
539    }
540    fn last_write_time(&self) -> u64 {
541        self.as_inner().modified_u64()
542    }
543    fn file_size(&self) -> u64 {
544        self.as_inner().size()
545    }
546    fn volume_serial_number(&self) -> Option<u32> {
547        self.as_inner().volume_serial_number()
548    }
549    fn number_of_links(&self) -> Option<u32> {
550        self.as_inner().number_of_links()
551    }
552    fn file_index(&self) -> Option<u64> {
553        self.as_inner().file_index()
554    }
555    fn change_time(&self) -> Option<u64> {
556        self.as_inner().changed_u64()
557    }
558}
559
560/// Windows-specific extensions to [`fs::FileType`].
561///
562/// On Windows, a symbolic link knows whether it is a file or directory.
563#[stable(feature = "windows_file_type_ext", since = "1.64.0")]
564pub trait FileTypeExt: Sealed {
565    /// Returns `true` if this file type is a symbolic link that is also a directory.
566    #[stable(feature = "windows_file_type_ext", since = "1.64.0")]
567    fn is_symlink_dir(&self) -> bool;
568    /// Returns `true` if this file type is a symbolic link that is also a file.
569    #[stable(feature = "windows_file_type_ext", since = "1.64.0")]
570    fn is_symlink_file(&self) -> bool;
571}
572
573#[stable(feature = "windows_file_type_ext", since = "1.64.0")]
574impl Sealed for fs::FileType {}
575
576#[stable(feature = "windows_file_type_ext", since = "1.64.0")]
577impl FileTypeExt for fs::FileType {
578    fn is_symlink_dir(&self) -> bool {
579        self.as_inner().is_symlink_dir()
580    }
581    fn is_symlink_file(&self) -> bool {
582        self.as_inner().is_symlink_file()
583    }
584}
585
586/// Windows-specific extensions to [`fs::FileTimes`].
587#[stable(feature = "file_set_times", since = "1.75.0")]
588pub trait FileTimesExt: Sealed {
589    /// Set the creation time of a file.
590    #[stable(feature = "file_set_times", since = "1.75.0")]
591    fn set_created(self, t: SystemTime) -> Self;
592}
593
594#[stable(feature = "file_set_times", since = "1.75.0")]
595impl FileTimesExt for fs::FileTimes {
596    fn set_created(mut self, t: SystemTime) -> Self {
597        self.as_inner_mut().set_created(t.into_inner());
598        self
599    }
600}
601
602/// Creates a new symlink to a non-directory file on the filesystem.
603///
604/// The `link` path will be a file symbolic link pointing to the `original`
605/// path.
606///
607/// The `original` path should not be a directory or a symlink to a directory,
608/// otherwise the symlink will be broken. Use [`symlink_dir`] for directories.
609///
610/// This function currently corresponds to [`CreateSymbolicLinkW`][CreateSymbolicLinkW].
611/// Note that this [may change in the future][changes].
612///
613/// [CreateSymbolicLinkW]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
614/// [changes]: io#platform-specific-behavior
615///
616/// # Examples
617///
618/// ```no_run
619/// use std::os::windows::fs;
620///
621/// fn main() -> std::io::Result<()> {
622///     fs::symlink_file("a.txt", "b.txt")?;
623///     Ok(())
624/// }
625/// ```
626///
627/// # Limitations
628///
629/// Windows treats symlink creation as a [privileged action][symlink-security],
630/// therefore this function is likely to fail unless the user makes changes to
631/// their system to permit symlink creation. Users can try enabling Developer
632/// Mode, granting the `SeCreateSymbolicLinkPrivilege` privilege, or running
633/// the process as an administrator.
634///
635/// [symlink-security]: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
636#[stable(feature = "symlink", since = "1.1.0")]
637pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
638    sys::fs::symlink_inner(original.as_ref(), link.as_ref(), false)
639}
640
641/// Creates a new symlink to a directory on the filesystem.
642///
643/// The `link` path will be a directory symbolic link pointing to the `original`
644/// path.
645///
646/// The `original` path must be a directory or a symlink to a directory,
647/// otherwise the symlink will be broken. Use [`symlink_file`] for other files.
648///
649/// This function currently corresponds to [`CreateSymbolicLinkW`][CreateSymbolicLinkW].
650/// Note that this [may change in the future][changes].
651///
652/// [CreateSymbolicLinkW]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
653/// [changes]: io#platform-specific-behavior
654///
655/// # Examples
656///
657/// ```no_run
658/// use std::os::windows::fs;
659///
660/// fn main() -> std::io::Result<()> {
661///     fs::symlink_dir("a", "b")?;
662///     Ok(())
663/// }
664/// ```
665///
666/// # Limitations
667///
668/// Windows treats symlink creation as a [privileged action][symlink-security],
669/// therefore this function is likely to fail unless the user makes changes to
670/// their system to permit symlink creation. Users can try enabling Developer
671/// Mode, granting the `SeCreateSymbolicLinkPrivilege` privilege, or running
672/// the process as an administrator.
673///
674/// [symlink-security]: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
675#[stable(feature = "symlink", since = "1.1.0")]
676pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
677    sys::fs::symlink_inner(original.as_ref(), link.as_ref(), true)
678}
679
680/// Creates a junction point.
681///
682/// The `link` path will be a directory junction pointing to the original path.
683/// If `link` is a relative path then it will be made absolute prior to creating the junction point.
684/// The `original` path must be a directory or a link to a directory, otherwise the junction point will be broken.
685///
686/// If either path is not a local file path then this will fail.
687#[unstable(feature = "junction_point", issue = "121709")]
688pub fn junction_point<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
689    sys::fs::junction_point(original.as_ref(), link.as_ref())
690}