]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/ext/fs.rs
Auto merge of #25570 - oli-obk:const_indexing, r=nikomatsakis
[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 fs::{OpenOptions, Metadata};
16 use io;
17 use path::Path;
18 use sys;
19 use sys_common::{AsInnerMut, AsInner};
20
21 /// Windows-specific extensions to `OpenOptions`
22 #[unstable(feature = "open_options_ext",
23            reason = "may require more thought/methods",
24            issue = "27720")]
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;
29
30     /// Overrides the `dwCreationDisposition` argument to the call to
31     /// `CreateFile` with the specified value.
32     ///
33     /// This will override any values of the standard `create` flags, for
34     /// example.
35     fn creation_disposition(&mut self, val: u32) -> &mut Self;
36
37     /// Overrides the `dwFlagsAndAttributes` argument to the call to
38     /// `CreateFile` with the specified value.
39     ///
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;
43
44     /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
45     /// the specified value.
46     ///
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;
50 }
51
52 #[unstable(feature = "open_options_ext",
53            reason = "may require more thought/methods",
54            issue = "27720")]
55 impl OpenOptionsExt for OpenOptions {
56     fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
57         self.as_inner_mut().desired_access(access); self
58     }
59     fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
60         self.as_inner_mut().creation_disposition(access); self
61     }
62     fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
63         self.as_inner_mut().flags_and_attributes(access); self
64     }
65     fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
66         self.as_inner_mut().share_mode(access); self
67     }
68 }
69
70 /// Extension methods for `fs::Metadata` to access the raw fields contained
71 /// within.
72 #[stable(feature = "metadata_ext", since = "1.1.0")]
73 pub trait MetadataExt {
74     /// Returns the value of the `dwFileAttributes` field of this metadata.
75     ///
76     /// This field contains the file system attribute information for a file
77     /// or directory.
78     #[stable(feature = "metadata_ext", since = "1.1.0")]
79     fn file_attributes(&self) -> u32;
80
81     /// Returns the value of the `ftCreationTime` field of this metadata.
82     ///
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;
87
88     /// Returns the value of the `ftLastAccessTime` field of this metadata.
89     ///
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;
94
95     /// Returns the value of the `ftLastWriteTime` field of this metadata.
96     ///
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;
101
102     /// Returns the value of the `nFileSize{High,Low}` fields of this
103     /// metadata.
104     ///
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;
108 }
109
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() }
117 }
118
119 /// Creates a new file symbolic link on the filesystem.
120 ///
121 /// The `dst` path will be a file symbolic link pointing to the `src`
122 /// path.
123 ///
124 /// # Examples
125 ///
126 /// ```ignore
127 /// use std::os::windows::fs;
128 ///
129 /// # fn foo() -> std::io::Result<()> {
130 /// try!(fs::symlink_file("a.txt", "b.txt"));
131 /// # Ok(())
132 /// # }
133 /// ```
134 #[stable(feature = "symlink", since = "1.1.0")]
135 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
136                                                     -> io::Result<()> {
137     sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
138 }
139
140 /// Creates a new directory symlink on the filesystem.
141 ///
142 /// The `dst` path will be a directory symbolic link pointing to the `src`
143 /// path.
144 ///
145 /// # Examples
146 ///
147 /// ```ignore
148 /// use std::os::windows::fs;
149 ///
150 /// # fn foo() -> std::io::Result<()> {
151 /// try!(fs::symlink_file("a", "b"));
152 /// # Ok(())
153 /// # }
154 /// ```
155 #[stable(feature = "symlink", since = "1.1.0")]
156 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
157                                                    -> io::Result<()> {
158     sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)
159 }