]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/ext/fs.rs
Auto merge of #26520 - oli-obk:three-tuple-transitive-traits, r=bluss
[rust.git] / src / libstd / sys / windows / ext / fs.rs
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.
4 //
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.
10
11 //! Windows-specific extensions for the primitives in `std::fs`
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use prelude::v1::*;
16
17 use fs::{OpenOptions, Metadata};
18 use io;
19 use path::Path;
20 use sys;
21 use sys_common::{AsInnerMut, AsInner};
22
23 /// Windows-specific extensions to `OpenOptions`
24 #[unstable(feature = "open_options_ext",
25            reason = "may require more thought/methods")]
26 pub trait OpenOptionsExt {
27     /// Overrides the `dwDesiredAccess` argument to the call to `CreateFile`
28     /// with the specified value.
29     fn desired_access(&mut self, access: u32) -> &mut Self;
30
31     /// Overrides the `dwCreationDisposition` argument to the call to
32     /// `CreateFile` with the specified value.
33     ///
34     /// This will override any values of the standard `create` flags, for
35     /// example.
36     fn creation_disposition(&mut self, val: u32) -> &mut Self;
37
38     /// Overrides the `dwFlagsAndAttributes` argument to the call to
39     /// `CreateFile` with the specified value.
40     ///
41     /// This will override any values of the standard flags on the
42     /// `OpenOptions` structure.
43     fn flags_and_attributes(&mut self, val: u32) -> &mut Self;
44
45     /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
46     /// the specified value.
47     ///
48     /// This will override any values of the standard flags on the
49     /// `OpenOptions` structure.
50     fn share_mode(&mut self, val: u32) -> &mut Self;
51 }
52
53 impl OpenOptionsExt for OpenOptions {
54     fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
55         self.as_inner_mut().desired_access(access); self
56     }
57     fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
58         self.as_inner_mut().creation_disposition(access); self
59     }
60     fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
61         self.as_inner_mut().flags_and_attributes(access); self
62     }
63     fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
64         self.as_inner_mut().share_mode(access); self
65     }
66 }
67
68 /// Extension methods for `fs::Metadata` to access the raw fields contained
69 /// within.
70 #[stable(feature = "metadata_ext", since = "1.1.0")]
71 pub trait MetadataExt {
72     /// Returns the value of the `dwFileAttributes` field of this metadata.
73     ///
74     /// This field contains the file system attribute information for a file
75     /// or directory.
76     #[stable(feature = "metadata_ext", since = "1.1.0")]
77     fn file_attributes(&self) -> u32;
78
79     /// Returns the value of the `ftCreationTime` field of this metadata.
80     ///
81     /// The returned 64-bit value represents the number of 100-nanosecond
82     /// intervals since January 1, 1601 (UTC).
83     #[stable(feature = "metadata_ext", since = "1.1.0")]
84     fn creation_time(&self) -> u64;
85
86     /// Returns the value of the `ftLastAccessTime` field of this metadata.
87     ///
88     /// The returned 64-bit value represents the number of 100-nanosecond
89     /// intervals since January 1, 1601 (UTC).
90     #[stable(feature = "metadata_ext", since = "1.1.0")]
91     fn last_access_time(&self) -> u64;
92
93     /// Returns the value of the `ftLastWriteTime` field of this metadata.
94     ///
95     /// The returned 64-bit value represents the number of 100-nanosecond
96     /// intervals since January 1, 1601 (UTC).
97     #[stable(feature = "metadata_ext", since = "1.1.0")]
98     fn last_write_time(&self) -> u64;
99
100     /// Returns the value of the `nFileSize{High,Low}` fields of this
101     /// metadata.
102     ///
103     /// The returned value does not have meaning for directories.
104     #[stable(feature = "metadata_ext", since = "1.1.0")]
105     fn file_size(&self) -> u64;
106 }
107
108 #[stable(feature = "metadata_ext", since = "1.1.0")]
109 impl MetadataExt for Metadata {
110     fn file_attributes(&self) -> u32 { self.as_inner().attrs() }
111     fn creation_time(&self) -> u64 { self.as_inner().created() }
112     fn last_access_time(&self) -> u64 { self.as_inner().accessed() }
113     fn last_write_time(&self) -> u64 { self.as_inner().modified() }
114     fn file_size(&self) -> u64 { self.as_inner().size() }
115 }
116
117 /// Creates a new file symbolic link on the filesystem.
118 ///
119 /// The `dst` path will be a file symbolic link pointing to the `src`
120 /// path.
121 ///
122 /// # Examples
123 ///
124 /// ```ignore
125 /// use std::os::windows::fs;
126 ///
127 /// # fn foo() -> std::io::Result<()> {
128 /// try!(fs::symlink_file("a.txt", "b.txt"));
129 /// # Ok(())
130 /// # }
131 /// ```
132 #[stable(feature = "symlink", since = "1.1.0")]
133 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
134                                                     -> io::Result<()> {
135     sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
136 }
137
138 /// Creates a new directory symlink on the filesystem.
139 ///
140 /// The `dst` path will be a directory symbolic link pointing to the `src`
141 /// path.
142 ///
143 /// # Examples
144 ///
145 /// ```ignore
146 /// use std::os::windows::fs;
147 ///
148 /// # fn foo() -> std::io::Result<()> {
149 /// try!(fs::symlink_file("a", "b"));
150 /// # Ok(())
151 /// # }
152 /// ```
153 #[stable(feature = "symlink", since = "1.1.0")]
154 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
155                                                    -> io::Result<()> {
156     sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)
157 }