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.
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.
11 //! Windows-specific extensions for the primitives in `std::fs`
13 #![stable(feature = "rust1", since = "1.0.0")]
18 use fs::{OpenOptions, Metadata};
22 use sys_common::{AsInnerMut, AsInner};
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;
32 /// Overrides the `dwCreationDisposition` argument to the call to
33 /// `CreateFile` with the specified value.
35 /// This will override any values of the standard `create` flags, for
37 fn creation_disposition(&mut self, val: u32) -> &mut Self;
39 /// Overrides the `dwFlagsAndAttributes` argument to the call to
40 /// `CreateFile` with the specified value.
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;
46 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
47 /// the specified value.
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;
54 impl OpenOptionsExt for OpenOptions {
55 fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
56 self.as_inner_mut().desired_access(access); self
58 fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
59 self.as_inner_mut().creation_disposition(access); self
61 fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
62 self.as_inner_mut().flags_and_attributes(access); self
64 fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
65 self.as_inner_mut().share_mode(access); self
69 /// Extension methods for `fs::Metadata` to access the raw fields contained
71 #[stable(feature = "metadata_ext", since = "1.1.0")]
72 pub trait MetadataExt {
73 /// Returns the value of the `dwFileAttributes` field of this metadata.
75 /// This field contains the file system attribute information for a file
77 #[stable(feature = "metadata_ext", since = "1.1.0")]
78 fn file_attributes(&self) -> u32;
80 /// Returns the value of the `ftCreationTime` field of this metadata.
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;
87 /// Returns the value of the `ftLastAccessTime` field of this metadata.
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;
94 /// Returns the value of the `ftLastWriteTime` field of this metadata.
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;
101 /// Returns the value of the `nFileSize{High,Low}` fields of this
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;
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() }
118 /// Creates a new file symbolic link on the filesystem.
120 /// The `dst` path will be a file symbolic link pointing to the `src`
126 /// use std::os::windows::fs;
128 /// # fn foo() -> std::io::Result<()> {
129 /// try!(fs::symlink_file("a.txt", "b.txt"));
133 #[stable(feature = "symlink", since = "1.1.0")]
134 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
136 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
139 /// Creates a new directory symlink on the filesystem.
141 /// The `dst` path will be a directory symbolic link pointing to the `src`
147 /// use std::os::windows::fs;
149 /// # fn foo() -> std::io::Result<()> {
150 /// try!(fs::symlink_file("a", "b"));
154 #[stable(feature = "symlink", since = "1.1.0")]
155 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
157 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)