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 `std::fs`
13 #![stable(feature = "rust1", since = "1.0.0")]
15 use fs::{OpenOptions, Metadata};
19 use sys_common::{AsInnerMut, AsInner};
21 /// Windows-specific extensions to `OpenOptions`
22 #[stable(feature = "open_options_ext", since = "1.10.0")]
23 pub trait OpenOptionsExt {
24 /// Overrides the `dwDesiredAccess` argument to the call to `CreateFile`
25 /// with the specified value.
27 /// This will override the `read`, `write`, and `append` flags on the
28 /// `OpenOptions` structure. This method provides fine-grained control over
29 /// the permissions to read, write and append data, attributes (like hidden
30 /// and system) and extended attributes.
35 /// use std::fs::OpenOptions;
36 /// use std::os::windows::fs::OpenOptionsExt;
38 /// // Open without read and write permission, for example if you only need
39 /// // to call `stat()` on the file
40 /// let file = OpenOptions::new().access_mode(0).open("foo.txt");
42 #[stable(feature = "open_options_ext", since = "1.10.0")]
43 fn access_mode(&mut self, access: u32) -> &mut Self;
45 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
46 /// the specified value.
48 /// By default `share_mode` is set to
49 /// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`. Specifying
50 /// less permissions denies others to read from, write to and/or delete the
51 /// file while it is open.
56 /// use std::fs::OpenOptions;
57 /// use std::os::windows::fs::OpenOptionsExt;
59 /// // Do not allow others to read or modify this file while we have it open
61 /// let file = OpenOptions::new().write(true)
65 #[stable(feature = "open_options_ext", since = "1.10.0")]
66 fn share_mode(&mut self, val: u32) -> &mut Self;
68 /// Sets extra flags for the `dwFileFlags` argument to the call to
69 /// `CreateFile2` (or combines it with `attributes` and `security_qos_flags`
70 /// to set the `dwFlagsAndAttributes` for `CreateFile`).
72 /// Custom flags can only set flags, not remove flags set by Rusts options.
73 /// This options overwrites any previously set custom flags.
78 /// extern crate winapi;
79 /// use std::fs::OpenOptions;
80 /// use std::os::windows::fs::OpenOptionsExt;
82 /// let mut options = OpenOptions::new();
83 /// options.create(true).write(true);
84 /// if cfg!(windows) {
85 /// options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE);
87 /// let file = options.open("foo.txt");
89 #[stable(feature = "open_options_ext", since = "1.10.0")]
90 fn custom_flags(&mut self, flags: u32) -> &mut Self;
92 /// Sets the `dwFileAttributes` argument to the call to `CreateFile2` to
93 /// the specified value (or combines it with `custom_flags` and
94 /// `security_qos_flags` to set the `dwFlagsAndAttributes` for
97 /// If a _new_ file is created because it does not yet exist and
98 ///`.create(true)` or `.create_new(true)` are specified, the new file is
99 /// given the attributes declared with `.attributes()`.
101 /// If an _existing_ file is opened with `.create(true).truncate(true)`, its
102 /// existing attributes are preserved and combined with the ones declared
103 /// with `.attributes()`.
105 /// In all other cases the attributes get ignored.
110 /// extern crate winapi;
111 /// use std::fs::OpenOptions;
112 /// use std::os::windows::fs::OpenOptionsExt;
114 /// let file = OpenOptions::new().write(true).create(true)
115 /// .attributes(winapi::FILE_ATTRIBUTE_HIDDEN)
116 /// .open("foo.txt");
118 #[stable(feature = "open_options_ext", since = "1.10.0")]
119 fn attributes(&mut self, val: u32) -> &mut Self;
121 /// Sets the `dwSecurityQosFlags` argument to the call to `CreateFile2` to
122 /// the specified value (or combines it with `custom_flags` and `attributes`
123 /// to set the `dwFlagsAndAttributes` for `CreateFile`).
124 #[stable(feature = "open_options_ext", since = "1.10.0")]
125 fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions;
128 #[stable(feature = "open_options_ext", since = "1.10.0")]
129 impl OpenOptionsExt for OpenOptions {
130 fn access_mode(&mut self, access: u32) -> &mut OpenOptions {
131 self.as_inner_mut().access_mode(access); self
134 fn share_mode(&mut self, share: u32) -> &mut OpenOptions {
135 self.as_inner_mut().share_mode(share); self
138 fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
139 self.as_inner_mut().custom_flags(flags); self
142 fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
143 self.as_inner_mut().attributes(attributes); self
146 fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions {
147 self.as_inner_mut().security_qos_flags(flags); self
151 /// Extension methods for `fs::Metadata` to access the raw fields contained
153 #[stable(feature = "metadata_ext", since = "1.1.0")]
154 pub trait MetadataExt {
155 /// Returns the value of the `dwFileAttributes` field of this metadata.
157 /// This field contains the file system attribute information for a file
159 #[stable(feature = "metadata_ext", since = "1.1.0")]
160 fn file_attributes(&self) -> u32;
162 /// Returns the value of the `ftCreationTime` field of this metadata.
164 /// The returned 64-bit value represents the number of 100-nanosecond
165 /// intervals since January 1, 1601 (UTC).
166 #[stable(feature = "metadata_ext", since = "1.1.0")]
167 fn creation_time(&self) -> u64;
169 /// Returns the value of the `ftLastAccessTime` field of this metadata.
171 /// The returned 64-bit value represents the number of 100-nanosecond
172 /// intervals since January 1, 1601 (UTC).
173 #[stable(feature = "metadata_ext", since = "1.1.0")]
174 fn last_access_time(&self) -> u64;
176 /// Returns the value of the `ftLastWriteTime` field of this metadata.
178 /// The returned 64-bit value represents the number of 100-nanosecond
179 /// intervals since January 1, 1601 (UTC).
180 #[stable(feature = "metadata_ext", since = "1.1.0")]
181 fn last_write_time(&self) -> u64;
183 /// Returns the value of the `nFileSize{High,Low}` fields of this
186 /// The returned value does not have meaning for directories.
187 #[stable(feature = "metadata_ext", since = "1.1.0")]
188 fn file_size(&self) -> u64;
191 #[stable(feature = "metadata_ext", since = "1.1.0")]
192 impl MetadataExt for Metadata {
193 fn file_attributes(&self) -> u32 { self.as_inner().attrs() }
194 fn creation_time(&self) -> u64 { self.as_inner().created_u64() }
195 fn last_access_time(&self) -> u64 { self.as_inner().accessed_u64() }
196 fn last_write_time(&self) -> u64 { self.as_inner().modified_u64() }
197 fn file_size(&self) -> u64 { self.as_inner().size() }
200 /// Creates a new file symbolic link on the filesystem.
202 /// The `dst` path will be a file symbolic link pointing to the `src`
208 /// use std::os::windows::fs;
210 /// # fn foo() -> std::io::Result<()> {
211 /// try!(fs::symlink_file("a.txt", "b.txt"));
215 #[stable(feature = "symlink", since = "1.1.0")]
216 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
218 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
221 /// Creates a new directory symlink on the filesystem.
223 /// The `dst` path will be a directory symbolic link pointing to the `src`
229 /// use std::os::windows::fs;
231 /// # fn foo() -> std::io::Result<()> {
232 /// try!(fs::symlink_file("a", "b"));
236 #[stable(feature = "symlink", since = "1.1.0")]
237 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
239 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)