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")]
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;
29 /// Overrides the `dwCreationDisposition` argument to the call to
30 /// `CreateFile` with the specified value.
32 /// This will override any values of the standard `create` flags, for
34 fn creation_disposition(&mut self, val: u32) -> &mut Self;
36 /// Overrides the `dwFlagsAndAttributes` argument to the call to
37 /// `CreateFile` with the specified value.
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;
43 /// Overrides the `dwShareMode` argument to the call to `CreateFile` with
44 /// the specified value.
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;
51 impl OpenOptionsExt for OpenOptions {
52 fn desired_access(&mut self, access: u32) -> &mut OpenOptions {
53 self.as_inner_mut().desired_access(access); self
55 fn creation_disposition(&mut self, access: u32) -> &mut OpenOptions {
56 self.as_inner_mut().creation_disposition(access); self
58 fn flags_and_attributes(&mut self, access: u32) -> &mut OpenOptions {
59 self.as_inner_mut().flags_and_attributes(access); self
61 fn share_mode(&mut self, access: u32) -> &mut OpenOptions {
62 self.as_inner_mut().share_mode(access); self
66 /// Extension methods for `fs::Metadata` to access the raw fields contained
68 #[stable(feature = "metadata_ext", since = "1.1.0")]
69 pub trait MetadataExt {
70 /// Returns the value of the `dwFileAttributes` field of this metadata.
72 /// This field contains the file system attribute information for a file
74 #[stable(feature = "metadata_ext", since = "1.1.0")]
75 fn file_attributes(&self) -> u32;
77 /// Returns the value of the `ftCreationTime` field of this metadata.
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;
84 /// Returns the value of the `ftLastAccessTime` field of this metadata.
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;
91 /// Returns the value of the `ftLastWriteTime` field of this metadata.
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;
98 /// Returns the value of the `nFileSize{High,Low}` fields of this
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;
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() }
115 /// Creates a new file symbolic link on the filesystem.
117 /// The `dst` path will be a file symbolic link pointing to the `src`
123 /// use std::os::windows::fs;
125 /// # fn foo() -> std::io::Result<()> {
126 /// try!(fs::symlink_file("a.txt", "b.txt"));
130 #[stable(feature = "symlink", since = "1.1.0")]
131 pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
133 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), false)
136 /// Creates a new directory symlink on the filesystem.
138 /// The `dst` path will be a directory symbolic link pointing to the `src`
144 /// use std::os::windows::fs;
146 /// # fn foo() -> std::io::Result<()> {
147 /// try!(fs::symlink_file("a", "b"));
151 #[stable(feature = "symlink", since = "1.1.0")]
152 pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q)
154 sys::fs::symlink_inner(src.as_ref(), dst.as_ref(), true)