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 #[unstable(feature = "open_options_ext",
23 reason = "may require more thought/methods",
25 pub trait OpenOptionsExt {
26 /// Overrides the `dwDesiredAccess` argument to the call to `CreateFile`
27 /// with the specified value.
28 fn desired_access(&mut self, access: u32) -> &mut Self;
30 /// Overrides the `dwCreationDisposition` argument to the call to
31 /// `CreateFile` with the specified value.
33 /// This will override any values of the standard `create` flags, for
35 fn creation_disposition(&mut self, val: u32) -> &mut Self;
37 /// Overrides the `dwFlagsAndAttributes` argument to the call to
38 /// `CreateFile` with the specified value.
40 /// This will override any values of the standard flags on the
41 /// `OpenOptions` structure.
42 fn flags_and_attributes(&mut self, val: u32) -> &mut Self;
44 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
45 /// the specified value.
47 /// This will override any values of the standard flags on the
48 /// `OpenOptions` structure.
49 fn share_mode(&mut self, val: u32) -> &mut Self;
52 #[unstable(feature = "open_options_ext",
53 reason = "may require more thought/methods",
55 impl OpenOptionsExt for OpenOptions {
56 fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
57 self.as_inner_mut().desired_access(access); self
59 fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
60 self.as_inner_mut().creation_disposition(access); self
62 fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
63 self.as_inner_mut().flags_and_attributes(access); self
65 fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
66 self.as_inner_mut().share_mode(access); self
70 /// Extension methods for `fs::Metadata` to access the raw fields contained
72 #[stable(feature = "metadata_ext", since = "1.1.0")]
73 pub trait MetadataExt {
74 /// Returns the value of the `dwFileAttributes` field of this metadata.
76 /// This field contains the file system attribute information for a file
78 #[stable(feature = "metadata_ext", since = "1.1.0")]
79 fn file_attributes(&self) -> u32;
81 /// Returns the value of the `ftCreationTime` field of this metadata.
83 /// The returned 64-bit value represents the number of 100-nanosecond
84 /// intervals since January 1, 1601 (UTC).
85 #[stable(feature = "metadata_ext", since = "1.1.0")]
86 fn creation_time(&self) -> u64;
88 /// Returns the value of the `ftLastAccessTime` field of this metadata.
90 /// The returned 64-bit value represents the number of 100-nanosecond
91 /// intervals since January 1, 1601 (UTC).
92 #[stable(feature = "metadata_ext", since = "1.1.0")]
93 fn last_access_time(&self) -> u64;
95 /// Returns the value of the `ftLastWriteTime` field of this metadata.
97 /// The returned 64-bit value represents the number of 100-nanosecond
98 /// intervals since January 1, 1601 (UTC).
99 #[stable(feature = "metadata_ext", since = "1.1.0")]
100 fn last_write_time(&self) -> u64;
102 /// Returns the value of the `nFileSize{High,Low}` fields of this
105 /// The returned value does not have meaning for directories.
106 #[stable(feature = "metadata_ext", since = "1.1.0")]
107 fn file_size(&self) -> u64;
110 #[stable(feature = "metadata_ext", since = "1.1.0")]
111 impl MetadataExt for Metadata {
112 fn file_attributes(&self) -> u32 { self.as_inner().attrs() }
113 fn creation_time(&self) -> u64 { self.as_inner().created() }
114 fn last_access_time(&self) -> u64 { self.as_inner().accessed() }
115 fn last_write_time(&self) -> u64 { self.as_inner().modified() }
116 fn file_size(&self) -> u64 { self.as_inner().size() }
119 /// Creates a new file symbolic link on the filesystem.
121 /// The `dst` path will be a file symbolic link pointing to the `src`
127 /// use std::os::windows::fs;
129 /// # fn foo() -> std::io::Result<()> {
130 /// try!(fs::symlink_file("a.txt", "b.txt"));
134 #[stable(feature = "symlink", since = "1.1.0")]
135 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
137 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
140 /// Creates a new directory symlink on the filesystem.
142 /// The `dst` path will be a directory symbolic link pointing to the `src`
148 /// use std::os::windows::fs;
150 /// # fn foo() -> std::io::Result<()> {
151 /// try!(fs::symlink_file("a", "b"));
155 #[stable(feature = "symlink", since = "1.1.0")]
156 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
158 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)