@@ -53,3 +53,174 @@ pub async fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io:
53
53
let dst = dst. as_ref ( ) . to_owned ( ) ;
54
54
spawn_blocking ( move || std:: os:: windows:: fs:: symlink_file ( & src, & dst) ) . await
55
55
}
56
+
57
+ cfg_not_docs ! {
58
+ pub use std:: os:: windows:: fs:: { OpenOptionsExt } ;
59
+ }
60
+
61
+ cfg_docs ! {
62
+ /// Windows-specific extensions to `OpenOptions`.
63
+ pub trait OpenOptionsExt {
64
+ /// Overrides the `dwDesiredAccess` argument to the call to [`CreateFile`]
65
+ /// with the specified value.
66
+ ///
67
+ /// This will override the `read`, `write`, and `append` flags on the
68
+ /// `OpenOptions` structure. This method provides fine-grained control over
69
+ /// the permissions to read, write and append data, attributes (like hidden
70
+ /// and system), and extended attributes.
71
+ ///
72
+ /// # Examples
73
+ ///
74
+ /// ```no_run
75
+ /// use async_std::fs::OpenOptions;
76
+ /// use async_std::os::windows::prelude::*;
77
+ ///
78
+ /// // Open without read and write permission, for example if you only need
79
+ /// // to call `stat` on the file
80
+ /// let file = OpenOptions::new().access_mode(0).open("foo.txt").await?;
81
+ /// ```
82
+ ///
83
+ /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
84
+ fn access_mode( & mut self , access: u32 ) -> & mut Self ;
85
+
86
+ /// Overrides the `dwShareMode` argument to the call to [`CreateFile`] with
87
+ /// the specified value.
88
+ ///
89
+ /// By default `share_mode` is set to
90
+ /// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`. This allows
91
+ /// other processes to read, write, and delete/rename the same file
92
+ /// while it is open. Removing any of the flags will prevent other
93
+ /// processes from performing the corresponding operation until the file
94
+ /// handle is closed.
95
+ ///
96
+ /// # Examples
97
+ ///
98
+ /// ```no_run
99
+ /// use async_std::fs::OpenOptions;
100
+ /// use async_std::os::windows::prelude::*;
101
+ ///
102
+ /// // Do not allow others to read or modify this file while we have it open
103
+ /// // for writing.
104
+ /// let file = OpenOptions::new()
105
+ /// .write(true)
106
+ /// .share_mode(0)
107
+ /// .open("foo.txt")
108
+ /// .await?;
109
+ /// ```
110
+ ///
111
+ /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
112
+ fn share_mode( & mut self , val: u32 ) -> & mut Self ;
113
+
114
+ /// Sets extra flags for the `dwFileFlags` argument to the call to
115
+ /// [`CreateFile2`] to the specified value (or combines it with
116
+ /// `attributes` and `security_qos_flags` to set the `dwFlagsAndAttributes`
117
+ /// for [`CreateFile`]).
118
+ ///
119
+ /// Custom flags can only set flags, not remove flags set by Rust's options.
120
+ /// This option overwrites any previously set custom flags.
121
+ ///
122
+ /// # Examples
123
+ ///
124
+ /// ```no_run
125
+ /// # #[cfg(for_demonstration_only)]
126
+ /// extern crate winapi;
127
+ /// # mod winapi { pub const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x04000000; }
128
+ ///
129
+ /// use async_std::fs::OpenOptions;
130
+ /// use async_std::os::windows::prelude::*;
131
+ ///
132
+ /// let file = OpenOptions::new()
133
+ /// .create(true)
134
+ /// .write(true)
135
+ /// .custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE)
136
+ /// .open("foo.txt")
137
+ /// .await?;
138
+ /// ```
139
+ ///
140
+ /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
141
+ /// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
142
+ fn custom_flags( & mut self , flags: u32 ) -> & mut Self ;
143
+
144
+ /// Sets the `dwFileAttributes` argument to the call to [`CreateFile2`] to
145
+ /// the specified value (or combines it with `custom_flags` and
146
+ /// `security_qos_flags` to set the `dwFlagsAndAttributes` for
147
+ /// [`CreateFile`]).
148
+ ///
149
+ /// If a _new_ file is created because it does not yet exist and
150
+ /// `.create(true)` or `.create_new(true)` are specified, the new file is
151
+ /// given the attributes declared with `.attributes()`.
152
+ ///
153
+ /// If an _existing_ file is opened with `.create(true).truncate(true)`, its
154
+ /// existing attributes are preserved and combined with the ones declared
155
+ /// with `.attributes()`.
156
+ ///
157
+ /// In all other cases the attributes get ignored.
158
+ ///
159
+ /// # Examples
160
+ ///
161
+ /// ```no_run
162
+ /// # #[cfg(for_demonstration_only)]
163
+ /// extern crate winapi;
164
+ /// # mod winapi { pub const FILE_ATTRIBUTE_HIDDEN: u32 = 2; }
165
+ ///
166
+ /// use async_std::fs::OpenOptions;
167
+ /// use async_std::os::windows::prelude::*;
168
+ ///
169
+ /// let file = OpenOptions::new()
170
+ /// .write(true)
171
+ /// .create(true)
172
+ /// .attributes(winapi::FILE_ATTRIBUTE_HIDDEN)
173
+ /// .open("foo.txt")
174
+ /// .await?;
175
+ /// ```
176
+ ///
177
+ /// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
178
+ /// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
179
+ fn attributes( & mut self , val: u32 ) -> & mut Self ;
180
+
181
+ /// Sets the `dwSecurityQosFlags` argument to the call to [`CreateFile2`] to
182
+ /// the specified value (or combines it with `custom_flags` and `attributes`
183
+ /// to set the `dwFlagsAndAttributes` for [`CreateFile`]).
184
+ ///
185
+ /// By default `security_qos_flags` is not set. It should be specified when
186
+ /// opening a named pipe, to control to which degree a server process can
187
+ /// act on behalf of a client process (security impersonation level).
188
+ ///
189
+ /// When `security_qos_flags` is not set, a malicious program can gain the
190
+ /// elevated privileges of a privileged Rust process when it allows opening
191
+ /// user-specified paths, by tricking it into opening a named pipe. So
192
+ /// arguably `security_qos_flags` should also be set when opening arbitrary
193
+ /// paths. However the bits can then conflict with other flags, specifically
194
+ /// `FILE_FLAG_OPEN_NO_RECALL`.
195
+ ///
196
+ /// For information about possible values, see [Impersonation Levels] on the
197
+ /// Windows Dev Center site. The `SECURITY_SQOS_PRESENT` flag is set
198
+ /// automatically when using this method.
199
+
200
+ /// # Examples
201
+ ///
202
+ /// ```no_run
203
+ /// # #[cfg(for_demonstration_only)]
204
+ /// extern crate winapi;
205
+ /// # mod winapi { pub const SECURITY_IDENTIFICATION: u32 = 0; }
206
+ /// use async_std::fs::OpenOptions;
207
+ /// use async_std::os::windows::prelude::*;
208
+ ///
209
+ /// let file = OpenOptions::new()
210
+ /// .write(true)
211
+ /// .create(true)
212
+ ///
213
+ /// // Sets the flag value to `SecurityIdentification`.
214
+ /// .security_qos_flags(winapi::SECURITY_IDENTIFICATION)
215
+ ///
216
+ /// .open(r"\\.\pipe\MyPipe")
217
+ /// .await?;
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
+ /// [Impersonation Levels]:
223
+ /// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-security_impersonation_level
224
+ fn security_qos_flags( & mut self , flags: u32 ) -> & mut Self ;
225
+ }
226
+ }
0 commit comments