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")]
15 use fs::{OpenOptions, Metadata};
19 use sys_common::{AsInnerMut, AsInner};
21 /// Windows-specific extensions to `OpenOptions`
22 #[unstable(feature = "open_options_ext",
23 reason = "may require more thought/methods",
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;
30 /// Overrides the `dwCreationDisposition` argument to the call to
31 /// `CreateFile` with the specified value.
33 /// This will override any values of the standard `create` flags, for
35 fn creation_disposition(&mut self, val: u32) -> &mut Self;
37 /// Overrides the `dwFlagsAndAttributes` argument to the call to
38 /// `CreateFile` with the specified value.
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;
44 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
45 /// the specified value.
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;
52 impl OpenOptionsExt for OpenOptions {
53 fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
54 self.as_inner_mut().desired_access(access); self
56 fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
57 self.as_inner_mut().creation_disposition(access); self
59 fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
60 self.as_inner_mut().flags_and_attributes(access); self
62 fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
63 self.as_inner_mut().share_mode(access); self
67 /// Extension methods for `fs::Metadata` to access the raw fields contained
69 #[stable(feature = "metadata_ext", since = "1.1.0")]
70 pub trait MetadataExt {
71 /// Returns the value of the `dwFileAttributes` field of this metadata.
73 /// This field contains the file system attribute information for a file
75 #[stable(feature = "metadata_ext", since = "1.1.0")]
76 fn file_attributes(&self) -> u32;
78 /// Returns the value of the `ftCreationTime` field of this metadata.
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;
85 /// Returns the value of the `ftLastAccessTime` field of this metadata.
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;
92 /// Returns the value of the `ftLastWriteTime` field of this metadata.
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;
99 /// Returns the value of the `nFileSize{High,Low}` fields of this
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;
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() }
116 /// Creates a new file symbolic link on the filesystem.
118 /// The `dst` path will be a file symbolic link pointing to the `src`
124 /// use std::os::windows::fs;
126 /// # fn foo() -> std::io::Result<()> {
127 /// try!(fs::symlink_file("a.txt", "b.txt"));
131 #[stable(feature = "symlink", since = "1.1.0")]
132 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
134 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
137 /// Creates a new directory symlink on the filesystem.
139 /// The `dst` path will be a directory symbolic link pointing to the `src`
145 /// use std::os::windows::fs;
147 /// # fn foo() -> std::io::Result<()> {
148 /// try!(fs::symlink_file("a", "b"));
152 #[stable(feature = "symlink", since = "1.1.0")]
153 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
155 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)