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