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