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