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")]
17 use fs::{OpenOptions, Metadata};
21 use sys_common::{AsInnerMut, AsInner};
23 /// Windows-specific extensions to `OpenOptions`
24 #[unstable(feature = "open_options_ext",
25 reason = "may require more thought/methods")]
26 pub trait OpenOptionsExt {
27 /// Overrides the `dwDesiredAccess` argument to the call to `CreateFile`
28 /// with the specified value.
29 fn desired_access(&mut self, access: u32) -> &mut Self;
31 /// Overrides the `dwCreationDisposition` argument to the call to
32 /// `CreateFile` with the specified value.
34 /// This will override any values of the standard `create` flags, for
36 fn creation_disposition(&mut self, val: u32) -> &mut Self;
38 /// Overrides the `dwFlagsAndAttributes` argument to the call to
39 /// `CreateFile` with the specified value.
41 /// This will override any values of the standard flags on the
42 /// `OpenOptions` structure.
43 fn flags_and_attributes(&mut self, val: u32) -> &mut Self;
45 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
46 /// the specified value.
48 /// This will override any values of the standard flags on the
49 /// `OpenOptions` structure.
50 fn share_mode(&mut self, val: u32) -> &mut Self;
53 impl OpenOptionsExt for OpenOptions {
54 fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
55 self.as_inner_mut().desired_access(access); self
57 fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
58 self.as_inner_mut().creation_disposition(access); self
60 fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
61 self.as_inner_mut().flags_and_attributes(access); self
63 fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
64 self.as_inner_mut().share_mode(access); self
68 /// Extension methods for `fs::Metadata` to access the raw fields contained
70 #[stable(feature = "metadata_ext", since = "1.1.0")]
71 pub trait MetadataExt {
72 /// Returns the value of the `dwFileAttributes` field of this metadata.
74 /// This field contains the file system attribute information for a file
76 #[stable(feature = "metadata_ext", since = "1.1.0")]
77 fn file_attributes(&self) -> u32;
79 /// Returns the value of the `ftCreationTime` field of this metadata.
81 /// The returned 64-bit value represents the number of 100-nanosecond
82 /// intervals since January 1, 1601 (UTC).
83 #[stable(feature = "metadata_ext", since = "1.1.0")]
84 fn creation_time(&self) -> u64;
86 /// Returns the value of the `ftLastAccessTime` field of this metadata.
88 /// The returned 64-bit value represents the number of 100-nanosecond
89 /// intervals since January 1, 1601 (UTC).
90 #[stable(feature = "metadata_ext", since = "1.1.0")]
91 fn last_access_time(&self) -> u64;
93 /// Returns the value of the `ftLastWriteTime` field of this metadata.
95 /// The returned 64-bit value represents the number of 100-nanosecond
96 /// intervals since January 1, 1601 (UTC).
97 #[stable(feature = "metadata_ext", since = "1.1.0")]
98 fn last_write_time(&self) -> u64;
100 /// Returns the value of the `nFileSize{High,Low}` fields of this
103 /// The returned value does not have meaning for directories.
104 #[stable(feature = "metadata_ext", since = "1.1.0")]
105 fn file_size(&self) -> u64;
108 #[stable(feature = "metadata_ext", since = "1.1.0")]
109 impl MetadataExt for Metadata {
110 fn file_attributes(&self) -> u32 { self.as_inner().attrs() }
111 fn creation_time(&self) -> u64 { self.as_inner().created() }
112 fn last_access_time(&self) -> u64 { self.as_inner().accessed() }
113 fn last_write_time(&self) -> u64 { self.as_inner().modified() }
114 fn file_size(&self) -> u64 { self.as_inner().size() }
117 /// Creates a new file symbolic link on the filesystem.
119 /// The `dst` path will be a file symbolic link pointing to the `src`
125 /// use std::os::windows::fs;
127 /// # fn foo() -> std::io::Result<()> {
128 /// try!(fs::symlink_file("a.txt", "b.txt"));
132 #[stable(feature = "symlink", since = "1.1.0")]
133 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
135 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
138 /// Creates a new directory symlink on the filesystem.
140 /// The `dst` path will be a directory symbolic link pointing to the `src`
146 /// use std::os::windows::fs;
148 /// # fn foo() -> std::io::Result<()> {
149 /// try!(fs::symlink_file("a", "b"));
153 #[stable(feature = "symlink", since = "1.1.0")]
154 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
156 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)