1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Windows-specific extensions for the primitives in the `std::fs` module.
13 #![stable(feature = "rust1", since = "1.0.0")]
15 use fs::{self, OpenOptions, Metadata};
19 use sys_common::{AsInnerMut, AsInner};
21 /// Windows-specific extensions to [`File`].
23 /// [`File`]: ../../../fs/struct.File.html
24 #[stable(feature = "file_offset", since = "1.15.0")]
26 /// Seeks to a given position and reads a number of bytes.
28 /// Returns the number of bytes read.
30 /// The offset is relative to the start of the file and thus independent
31 /// from the current cursor. The current cursor **is** affected by this
32 /// function, it is set to the end of the read.
34 /// Reading beyond the end of the file will always return with a length of
37 /// Note that similar to `File::read`, it is not an error to return with a
38 /// short read. When returning from such a short read, the file pointer is
45 /// use std::fs::File;
46 /// use std::os::windows::prelude::*;
48 /// # fn foo() -> io::Result<()> {
49 /// let mut file = File::open("foo.txt")?;
50 /// let mut buffer = [0; 10];
52 /// // Read 10 bytes, starting 72 bytes from the
53 /// // start of the file.
54 /// file.seek_read(&mut buffer[..], 72)?;
58 #[stable(feature = "file_offset", since = "1.15.0")]
59 fn seek_read(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
61 /// Seeks to a given position and writes a number of bytes.
63 /// Returns the number of bytes written.
65 /// The offset is relative to the start of the file and thus independent
66 /// from the current cursor. The current cursor **is** affected by this
67 /// function, it is set to the end of the write.
69 /// When writing beyond the end of the file, the file is appropiately
70 /// extended and the intermediate bytes are left uninitialized.
72 /// Note that similar to `File::write`, it is not an error to return a
73 /// short write. When returning from such a short write, the file pointer
79 /// use std::fs::File;
80 /// use std::os::windows::prelude::*;
82 /// # fn foo() -> std::io::Result<()> {
83 /// let mut buffer = File::create("foo.txt")?;
85 /// // Write a byte string starting 72 bytes from
86 /// // the start of the file.
87 /// buffer.seek_write(b"some bytes", 72)?;
91 #[stable(feature = "file_offset", since = "1.15.0")]
92 fn seek_write(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
95 #[stable(feature = "file_offset", since = "1.15.0")]
96 impl FileExt for fs::File {
97 fn seek_read(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
98 self.as_inner().read_at(buf, offset)
101 fn seek_write(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
102 self.as_inner().write_at(buf, offset)
106 /// Windows-specific extensions to [`OpenOptions`].
108 /// [`OpenOptions`]: ../../../fs/struct.OpenOptions.html
109 #[stable(feature = "open_options_ext", since = "1.10.0")]
110 pub trait OpenOptionsExt {
111 /// Overrides the `dwDesiredAccess` argument to the call to [`CreateFile`]
112 /// with the specified value.
114 /// This will override the `read`, `write`, and `append` flags on the
115 /// `OpenOptions` structure. This method provides fine-grained control over
116 /// the permissions to read, write and append data, attributes (like hidden
117 /// and system), and extended attributes.
122 /// use std::fs::OpenOptions;
123 /// use std::os::windows::prelude::*;
125 /// // Open without read and write permission, for example if you only need
126 /// // to call `stat` on the file
127 /// let file = OpenOptions::new().access_mode(0).open("foo.txt");
130 /// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
131 #[stable(feature = "open_options_ext", since = "1.10.0")]
132 fn access_mode(&mut self, access: u32) -> &mut Self;
134 /// Overrides the `dwShareMode` argument to the call to [`CreateFile`] with
135 /// the specified value.
137 /// By default `share_mode` is set to
138 /// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`. This allows
139 /// other processes to to read, write, and delete/rename the same file
140 /// while it is open. Removing any of the flags will prevent other
141 /// processes from performing the corresponding operation until the file
142 /// handle is closed.
147 /// use std::fs::OpenOptions;
148 /// use std::os::windows::prelude::*;
150 /// // Do not allow others to read or modify this file while we have it open
152 /// let file = OpenOptions::new()
155 /// .open("foo.txt");
158 /// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
159 #[stable(feature = "open_options_ext", since = "1.10.0")]
160 fn share_mode(&mut self, val: u32) -> &mut Self;
162 /// Sets extra flags for the `dwFileFlags` argument to the call to
163 /// [`CreateFile2`] to the specified value (or combines it with
164 /// `attributes` and `security_qos_flags` to set the `dwFlagsAndAttributes`
165 /// for [`CreateFile`]).
167 /// Custom flags can only set flags, not remove flags set by Rust's options.
168 /// This option overwrites any previously set custom flags.
173 /// extern crate winapi;
175 /// use std::fs::OpenOptions;
176 /// use std::os::windows::prelude::*;
178 /// let file = OpenOptions::new()
181 /// .custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE)
182 /// .open("foo.txt");
185 /// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
186 /// [`CreateFile2`]: https://msdn.microsoft.com/en-us/library/windows/desktop/hh449422.aspx
187 #[stable(feature = "open_options_ext", since = "1.10.0")]
188 fn custom_flags(&mut self, flags: u32) -> &mut Self;
190 /// Sets the `dwFileAttributes` argument to the call to [`CreateFile2`] to
191 /// the specified value (or combines it with `custom_flags` and
192 /// `security_qos_flags` to set the `dwFlagsAndAttributes` for
195 /// If a _new_ file is created because it does not yet exist and
196 /// `.create(true)` or `.create_new(true)` are specified, the new file is
197 /// given the attributes declared with `.attributes()`.
199 /// If an _existing_ file is opened with `.create(true).truncate(true)`, its
200 /// existing attributes are preserved and combined with the ones declared
201 /// with `.attributes()`.
203 /// In all other cases the attributes get ignored.
208 /// extern crate winapi;
210 /// use std::fs::OpenOptions;
211 /// use std::os::windows::prelude::*;
213 /// let file = OpenOptions::new()
216 /// .attributes(winapi::FILE_ATTRIBUTE_HIDDEN)
217 /// .open("foo.txt");
220 /// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
221 /// [`CreateFile2`]: https://msdn.microsoft.com/en-us/library/windows/desktop/hh449422.aspx
222 #[stable(feature = "open_options_ext", since = "1.10.0")]
223 fn attributes(&mut self, val: u32) -> &mut Self;
225 /// Sets the `dwSecurityQosFlags` argument to the call to [`CreateFile2`] to
226 /// the specified value (or combines it with `custom_flags` and `attributes`
227 /// to set the `dwFlagsAndAttributes` for [`CreateFile`]).
229 /// By default, `security_qos_flags` is set to `SECURITY_ANONYMOUS`. For
230 /// information about possible values, see [Impersonation Levels] on the
231 /// Windows Dev Center site.
236 /// use std::fs::OpenOptions;
237 /// use std::os::windows::prelude::*;
239 /// let file = OpenOptions::new()
243 /// // Sets the flag value to `SecurityIdentification`.
244 /// options.security_qos_flags(1)
246 /// .open("foo.txt");
249 /// [`CreateFile`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
250 /// [`CreateFile2`]: https://msdn.microsoft.com/en-us/library/windows/desktop/hh449422.aspx
251 /// [Impersonation Levels]:
252 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa379572.aspx
253 #[stable(feature = "open_options_ext", since = "1.10.0")]
254 fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions;
257 #[stable(feature = "open_options_ext", since = "1.10.0")]
258 impl OpenOptionsExt for OpenOptions {
259 fn access_mode(&mut self, access: u32) -> &mut OpenOptions {
260 self.as_inner_mut().access_mode(access); self
263 fn share_mode(&mut self, share: u32) -> &mut OpenOptions {
264 self.as_inner_mut().share_mode(share); self
267 fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
268 self.as_inner_mut().custom_flags(flags); self
271 fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
272 self.as_inner_mut().attributes(attributes); self
275 fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions {
276 self.as_inner_mut().security_qos_flags(flags); self
280 /// Extension methods for [`fs::Metadata`] to access the raw fields contained
283 /// The data members that this trait exposes correspond to the members
284 /// of the [`BY_HANDLE_FILE_INFORMATION`] structure.
286 /// [`fs::Metadata`]: ../../../fs/struct.Metadata.html
287 /// [`BY_HANDLE_FILE_INFORMATION`]:
288 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363788.aspx
289 #[stable(feature = "metadata_ext", since = "1.1.0")]
290 pub trait MetadataExt {
291 /// Returns the value of the `dwFileAttributes` field of this metadata.
293 /// This field contains the file system attribute information for a file
294 /// or directory. For possible values and their descriptions, see
295 /// [File Attribute Constants] in the Windows Dev Center.
302 /// use std::os::windows::prelude::*;
304 /// # fn foo() -> io::Result<()> {
305 /// let metadata = fs::metadata("foo.txt")?;
306 /// let attributes = metadata.file_attributes();
311 /// [File Attribute Constants]:
312 /// https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117.aspx
313 #[stable(feature = "metadata_ext", since = "1.1.0")]
314 fn file_attributes(&self) -> u32;
316 /// Returns the value of the `ftCreationTime` field of this metadata.
318 /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
319 /// which represents the number of 100-nanosecond intervals since
320 /// January 1, 1601 (UTC). The struct is automatically
321 /// converted to a `u64` value, as that is the recommended way
324 /// If the underlying filesystem does not support creation time, the
325 /// returned value is 0.
332 /// use std::os::windows::prelude::*;
334 /// # fn foo() -> io::Result<()> {
335 /// let metadata = fs::metadata("foo.txt")?;
336 /// let creation_time = metadata.creation_time();
341 /// [`FILETIME`]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
342 #[stable(feature = "metadata_ext", since = "1.1.0")]
343 fn creation_time(&self) -> u64;
345 /// Returns the value of the `ftLastAccessTime` field of this metadata.
347 /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
348 /// which represents the number of 100-nanosecond intervals since
349 /// January 1, 1601 (UTC). The struct is automatically
350 /// converted to a `u64` value, as that is the recommended way
353 /// For a file, the value specifies the last time that a file was read
354 /// from or written to. For a directory, the value specifies when
355 /// the directory was created. For both files and directories, the
356 /// specified date is correct, but the time of day is always set to
359 /// If the underlying filesystem does not support last access time, the
360 /// returned value is 0.
367 /// use std::os::windows::prelude::*;
369 /// # fn foo() -> io::Result<()> {
370 /// let metadata = fs::metadata("foo.txt")?;
371 /// let last_access_time = metadata.last_access_time();
376 /// [`FILETIME`]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
377 #[stable(feature = "metadata_ext", since = "1.1.0")]
378 fn last_access_time(&self) -> u64;
380 /// Returns the value of the `ftLastWriteTime` field of this metadata.
382 /// The returned 64-bit value is equivalent to a [`FILETIME`] struct,
383 /// which represents the number of 100-nanosecond intervals since
384 /// January 1, 1601 (UTC). The struct is automatically
385 /// converted to a `u64` value, as that is the recommended way
388 /// For a file, the value specifies the last time that a file was written
389 /// to. For a directory, the structure specifies when the directory was
392 /// If the underlying filesystem does not support the last write time
393 /// time, the returned value is 0.
400 /// use std::os::windows::prelude::*;
402 /// # fn foo() -> io::Result<()> {
403 /// let metadata = fs::metadata("foo.txt")?;
404 /// let last_write_time = metadata.last_write_time();
409 /// [`FILETIME`]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
410 #[stable(feature = "metadata_ext", since = "1.1.0")]
411 fn last_write_time(&self) -> u64;
413 /// Returns the value of the `nFileSize{High,Low}` fields of this
416 /// The returned value does not have meaning for directories.
423 /// use std::os::windows::prelude::*;
425 /// # fn foo() -> io::Result<()> {
426 /// let metadata = fs::metadata("foo.txt")?;
427 /// let file_size = metadata.file_size();
431 #[stable(feature = "metadata_ext", since = "1.1.0")]
432 fn file_size(&self) -> u64;
435 #[stable(feature = "metadata_ext", since = "1.1.0")]
436 impl MetadataExt for Metadata {
437 fn file_attributes(&self) -> u32 { self.as_inner().attrs() }
438 fn creation_time(&self) -> u64 { self.as_inner().created_u64() }
439 fn last_access_time(&self) -> u64 { self.as_inner().accessed_u64() }
440 fn last_write_time(&self) -> u64 { self.as_inner().modified_u64() }
441 fn file_size(&self) -> u64 { self.as_inner().size() }
444 /// Creates a new file symbolic link on the filesystem.
446 /// The `dst` path will be a file symbolic link pointing to the `src`
452 /// use std::os::windows::fs;
454 /// # fn foo() -> std::io::Result<()> {
455 /// fs::symlink_file("a.txt", "b.txt")?;
459 #[stable(feature = "symlink", since = "1.1.0")]
460 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
462 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
465 /// Creates a new directory symlink on the filesystem.
467 /// The `dst` path will be a directory symbolic link pointing to the `src`
473 /// use std::os::windows::fs;
475 /// # fn foo() -> std::io::Result<()> {
476 /// fs::symlink_file("a", "b")?;
480 #[stable(feature = "symlink", since = "1.1.0")]
481 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
483 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)