]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/fs.rs
Auto merge of #27822 - arielb1:inline-round-take-2, r=Gankro
[rust.git] / src / libstd / sys / unix / 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 //! Unix-specific extensions to primitives in the `std::fs` module.
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use fs::{self, Permissions, OpenOptions};
16 use io;
17 use libc;
18 use os::raw::c_long;
19 use os::unix::raw;
20 use path::Path;
21 use sys::fs::MetadataExt as UnixMetadataExt;
22 use sys;
23 use sys_common::{FromInner, AsInner, AsInnerMut};
24
25 #[unstable(feature = "fs_mode", reason = "recently added API")]
26 pub const USER_READ: raw::mode_t = 0o400;
27 #[unstable(feature = "fs_mode", reason = "recently added API")]
28 pub const USER_WRITE: raw::mode_t = 0o200;
29 #[unstable(feature = "fs_mode", reason = "recently added API")]
30 pub const USER_EXECUTE: raw::mode_t = 0o100;
31 #[unstable(feature = "fs_mode", reason = "recently added API")]
32 pub const USER_RWX: raw::mode_t = 0o700;
33 #[unstable(feature = "fs_mode", reason = "recently added API")]
34 pub const GROUP_READ: raw::mode_t = 0o040;
35 #[unstable(feature = "fs_mode", reason = "recently added API")]
36 pub const GROUP_WRITE: raw::mode_t = 0o020;
37 #[unstable(feature = "fs_mode", reason = "recently added API")]
38 pub const GROUP_EXECUTE: raw::mode_t = 0o010;
39 #[unstable(feature = "fs_mode", reason = "recently added API")]
40 pub const GROUP_RWX: raw::mode_t = 0o070;
41 #[unstable(feature = "fs_mode", reason = "recently added API")]
42 pub const OTHER_READ: raw::mode_t = 0o004;
43 #[unstable(feature = "fs_mode", reason = "recently added API")]
44 pub const OTHER_WRITE: raw::mode_t = 0o002;
45 #[unstable(feature = "fs_mode", reason = "recently added API")]
46 pub const OTHER_EXECUTE: raw::mode_t = 0o001;
47 #[unstable(feature = "fs_mode", reason = "recently added API")]
48 pub const OTHER_RWX: raw::mode_t = 0o007;
49 #[unstable(feature = "fs_mode", reason = "recently added API")]
50 pub const ALL_READ: raw::mode_t = 0o444;
51 #[unstable(feature = "fs_mode", reason = "recently added API")]
52 pub const ALL_WRITE: raw::mode_t = 0o222;
53 #[unstable(feature = "fs_mode", reason = "recently added API")]
54 pub const ALL_EXECUTE: raw::mode_t = 0o111;
55 #[unstable(feature = "fs_mode", reason = "recently added API")]
56 pub const ALL_RWX: raw::mode_t = 0o777;
57 #[unstable(feature = "fs_mode", reason = "recently added API")]
58 pub const SETUID: raw::mode_t = 0o4000;
59 #[unstable(feature = "fs_mode", reason = "recently added API")]
60 pub const SETGID: raw::mode_t = 0o2000;
61 #[unstable(feature = "fs_mode", reason = "recently added API")]
62 pub const STICKY_BIT: raw::mode_t = 0o1000;
63
64 /// Unix-specific extensions to `Permissions`
65 #[stable(feature = "fs_ext", since = "1.1.0")]
66 pub trait PermissionsExt {
67     /// Returns the underlying raw `mode_t` bits that are the standard Unix
68     /// permissions for this file.
69     #[stable(feature = "fs_ext", since = "1.1.0")]
70     fn mode(&self) -> raw::mode_t;
71
72     /// Sets the underlying raw `mode_t` bits for this set of permissions.
73     #[stable(feature = "fs_ext", since = "1.1.0")]
74     fn set_mode(&mut self, mode: raw::mode_t);
75
76     /// Creates a new instance of `Permissions` from the given set of Unix
77     /// permission bits.
78     #[stable(feature = "fs_ext", since = "1.1.0")]
79     fn from_mode(mode: raw::mode_t) -> Self;
80 }
81
82 #[stable(feature = "fs_ext", since = "1.1.0")]
83 impl PermissionsExt for Permissions {
84     fn mode(&self) -> raw::mode_t { self.as_inner().mode() }
85
86     fn set_mode(&mut self, mode: raw::mode_t) {
87         *self = FromInner::from_inner(FromInner::from_inner(mode));
88     }
89
90     fn from_mode(mode: raw::mode_t) -> Permissions {
91         FromInner::from_inner(FromInner::from_inner(mode))
92     }
93 }
94
95 /// Unix-specific extensions to `OpenOptions`
96 #[stable(feature = "fs_ext", since = "1.1.0")]
97 pub trait OpenOptionsExt {
98     /// Sets the mode bits that a new file will be created with.
99     ///
100     /// If a new file is created as part of a `File::open_opts` call then this
101     /// specified `mode` will be used as the permission bits for the new file.
102     #[stable(feature = "fs_ext", since = "1.1.0")]
103     fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
104 }
105
106 #[stable(feature = "fs_ext", since = "1.1.0")]
107 impl OpenOptionsExt for OpenOptions {
108     fn mode(&mut self, mode: raw::mode_t) -> &mut OpenOptions {
109         self.as_inner_mut().mode(mode); self
110     }
111 }
112
113 // Hm, why are there casts here to the returned type, shouldn't the types always
114 // be the same? Right you are! Turns out, however, on android at least the types
115 // in the raw `stat` structure are not the same as the types being returned. Who
116 // knew!
117 //
118 // As a result to make sure this compiles for all platforms we do the manual
119 // casts and rely on manual lowering to `stat` if the raw type is desired.
120 #[stable(feature = "metadata_ext", since = "1.1.0")]
121 pub trait MetadataExt {
122     #[stable(feature = "metadata_ext", since = "1.1.0")]
123     fn dev(&self) -> raw::dev_t;
124     #[stable(feature = "metadata_ext", since = "1.1.0")]
125     fn ino(&self) -> raw::ino_t;
126     #[stable(feature = "metadata_ext", since = "1.1.0")]
127     fn mode(&self) -> raw::mode_t;
128     #[stable(feature = "metadata_ext", since = "1.1.0")]
129     fn nlink(&self) -> raw::nlink_t;
130     #[stable(feature = "metadata_ext", since = "1.1.0")]
131     fn uid(&self) -> raw::uid_t;
132     #[stable(feature = "metadata_ext", since = "1.1.0")]
133     fn gid(&self) -> raw::gid_t;
134     #[stable(feature = "metadata_ext", since = "1.1.0")]
135     fn rdev(&self) -> raw::dev_t;
136     #[stable(feature = "metadata_ext", since = "1.1.0")]
137     fn size(&self) -> raw::off_t;
138     #[stable(feature = "metadata_ext", since = "1.1.0")]
139     fn atime(&self) -> raw::time_t;
140     #[stable(feature = "metadata_ext", since = "1.1.0")]
141     fn atime_nsec(&self) -> c_long;
142     #[stable(feature = "metadata_ext", since = "1.1.0")]
143     fn mtime(&self) -> raw::time_t;
144     #[stable(feature = "metadata_ext", since = "1.1.0")]
145     fn mtime_nsec(&self) -> c_long;
146     #[stable(feature = "metadata_ext", since = "1.1.0")]
147     fn ctime(&self) -> raw::time_t;
148     #[stable(feature = "metadata_ext", since = "1.1.0")]
149     fn ctime_nsec(&self) -> c_long;
150     #[stable(feature = "metadata_ext", since = "1.1.0")]
151     fn blksize(&self) -> raw::blksize_t;
152     #[stable(feature = "metadata_ext", since = "1.1.0")]
153     fn blocks(&self) -> raw::blkcnt_t;
154 }
155
156 impl MetadataExt for fs::Metadata {
157     fn dev(&self) -> raw::dev_t { self.as_raw_stat().st_dev as raw::dev_t }
158     fn ino(&self) -> raw::ino_t { self.as_raw_stat().st_ino as raw::ino_t }
159     fn mode(&self) -> raw::mode_t { self.as_raw_stat().st_mode as raw::mode_t }
160     fn nlink(&self) -> raw::nlink_t { self.as_raw_stat().st_nlink as raw::nlink_t }
161     fn uid(&self) -> raw::uid_t { self.as_raw_stat().st_uid as raw::uid_t }
162     fn gid(&self) -> raw::gid_t { self.as_raw_stat().st_gid as raw::gid_t }
163     fn rdev(&self) -> raw::dev_t { self.as_raw_stat().st_rdev as raw::dev_t }
164     fn size(&self) -> raw::off_t { self.as_raw_stat().st_size as raw::off_t }
165     fn atime(&self) -> raw::time_t { self.as_raw_stat().st_atime }
166     fn atime_nsec(&self) -> c_long { self.as_raw_stat().st_atime_nsec as c_long }
167     fn mtime(&self) -> raw::time_t { self.as_raw_stat().st_mtime }
168     fn mtime_nsec(&self) -> c_long { self.as_raw_stat().st_mtime_nsec as c_long }
169     fn ctime(&self) -> raw::time_t { self.as_raw_stat().st_ctime }
170     fn ctime_nsec(&self) -> c_long { self.as_raw_stat().st_ctime_nsec as c_long }
171
172     fn blksize(&self) -> raw::blksize_t {
173         self.as_raw_stat().st_blksize as raw::blksize_t
174     }
175     fn blocks(&self) -> raw::blkcnt_t {
176         self.as_raw_stat().st_blocks as raw::blkcnt_t
177     }
178 }
179
180 /// Add special unix types (block/char device, fifo and socket)
181 #[unstable(feature = "file_type_ext", reason = "recently added API")]
182 pub trait FileTypeExt {
183     /// Returns whether this file type is a block device.
184     fn is_block_device(&self) -> bool;
185     /// Returns whether this file type is a char device.
186     fn is_char_device(&self) -> bool;
187     /// Returns whether this file type is a fifo.
188     fn is_fifo(&self) -> bool;
189     /// Returns whether this file type is a socket.
190     fn is_socket(&self) -> bool;
191 }
192
193 #[unstable(feature = "file_type_ext", reason = "recently added API")]
194 impl FileTypeExt for fs::FileType {
195     fn is_block_device(&self) -> bool { self.as_inner().is(libc::S_IFBLK) }
196     fn is_char_device(&self) -> bool { self.as_inner().is(libc::S_IFCHR) }
197     fn is_fifo(&self) -> bool { self.as_inner().is(libc::S_IFIFO) }
198     fn is_socket(&self) -> bool { self.as_inner().is(libc::S_IFSOCK) }
199 }
200
201 /// Unix-specific extension methods for `fs::DirEntry`
202 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
203 pub trait DirEntryExt {
204     /// Returns the underlying `d_ino` field in the contained `dirent`
205     /// structure.
206     #[stable(feature = "dir_entry_ext", since = "1.1.0")]
207     fn ino(&self) -> raw::ino_t;
208 }
209
210 impl DirEntryExt for fs::DirEntry {
211     fn ino(&self) -> raw::ino_t { self.as_inner().ino() }
212 }
213
214 /// Creates a new symbolic link on the filesystem.
215 ///
216 /// The `dst` path will be a symbolic link pointing to the `src` path.
217 ///
218 /// # Note
219 ///
220 /// On Windows, you must specify whether a symbolic link points to a file
221 /// or directory.  Use `os::windows::fs::symlink_file` to create a
222 /// symbolic link to a file, or `os::windows::fs::symlink_dir` to create a
223 /// symbolic link to a directory.  Additionally, the process must have
224 /// `SeCreateSymbolicLinkPrivilege` in order to be able to create a
225 /// symbolic link.
226 ///
227 /// # Examples
228 ///
229 /// ```
230 /// use std::os::unix::fs;
231 ///
232 /// # fn foo() -> std::io::Result<()> {
233 /// try!(fs::symlink("a.txt", "b.txt"));
234 /// # Ok(())
235 /// # }
236 /// ```
237 #[stable(feature = "symlink", since = "1.1.0")]
238 pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()>
239 {
240     sys::fs::symlink(src.as_ref(), dst.as_ref())
241 }
242
243 #[unstable(feature = "dir_builder", reason = "recently added API")]
244 /// An extension trait for `fs::DirBuilder` for unix-specific options.
245 pub trait DirBuilderExt {
246     /// Sets the mode to create new directories with. This option defaults to
247     /// 0o777.
248     fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
249 }
250
251 impl DirBuilderExt for fs::DirBuilder {
252     fn mode(&mut self, mode: raw::mode_t) -> &mut fs::DirBuilder {
253         self.as_inner_mut().set_mode(mode);
254         self
255     }
256 }
257