Skip to main content

std/os/darwin/
fs.rs

1//! Darwin-specific extension traits to [`fs`].
2//!
3//! [`fs`]: crate::fs
4#![stable(feature = "metadata_ext", since = "1.1.0")]
5
6use crate::fs::{self, Metadata};
7use crate::macros::rustdoc_skip_body;
8use crate::sys::{AsInner, AsInnerMut, IntoInner};
9use crate::time::SystemTime;
10
11mod dirs;
12
13#[unstable(feature = "fs_home_dirs", issue = "162082")]
14pub use dirs::HomeDirsExt;
15#[unstable(feature = "fs_media_dirs", issue = "162083")]
16pub use dirs::MediaDirsExt;
17
18/// OS-specific extensions to [`fs::Metadata`].
19///
20/// [`fs::Metadata`]: crate::fs::Metadata
21#[stable(feature = "metadata_ext", since = "1.1.0")]
22pub trait MetadataExt {
23    /// Gain a reference to the underlying `stat` structure which contains
24    /// the raw information returned by the OS.
25    ///
26    /// The contents of the returned `stat` are **not** consistent across
27    /// Unix platforms. The `os::unix::fs::MetadataExt` trait contains the
28    /// cross-Unix abstractions contained within the raw stat.
29    #[stable(feature = "metadata_ext", since = "1.1.0")]
30    #[deprecated(
31        since = "1.8.0",
32        note = "deprecated in favor of the accessor \
33                methods of this trait"
34    )]
35    #[allow(deprecated)]
36    // Only available on macOS and iOS, since they were stably exposed there.
37    #[cfg(any(doc, target_os = "macos", target_os = "ios"))]
38    #[doc(cfg(any(target_os = "macos", target_os = "ios")))]
39    fn as_raw_stat(&self) -> &super::raw::stat;
40
41    #[stable(feature = "metadata_ext2", since = "1.8.0")]
42    fn st_dev(&self) -> u64;
43    #[stable(feature = "metadata_ext2", since = "1.8.0")]
44    fn st_ino(&self) -> u64;
45    #[stable(feature = "metadata_ext2", since = "1.8.0")]
46    fn st_mode(&self) -> u32;
47    #[stable(feature = "metadata_ext2", since = "1.8.0")]
48    fn st_nlink(&self) -> u64;
49    #[stable(feature = "metadata_ext2", since = "1.8.0")]
50    fn st_uid(&self) -> u32;
51    #[stable(feature = "metadata_ext2", since = "1.8.0")]
52    fn st_gid(&self) -> u32;
53    #[stable(feature = "metadata_ext2", since = "1.8.0")]
54    fn st_rdev(&self) -> u64;
55    #[stable(feature = "metadata_ext2", since = "1.8.0")]
56    fn st_size(&self) -> u64;
57    #[stable(feature = "metadata_ext2", since = "1.8.0")]
58    fn st_atime(&self) -> i64;
59    #[stable(feature = "metadata_ext2", since = "1.8.0")]
60    fn st_atime_nsec(&self) -> i64;
61    #[stable(feature = "metadata_ext2", since = "1.8.0")]
62    fn st_mtime(&self) -> i64;
63    #[stable(feature = "metadata_ext2", since = "1.8.0")]
64    fn st_mtime_nsec(&self) -> i64;
65    #[stable(feature = "metadata_ext2", since = "1.8.0")]
66    fn st_ctime(&self) -> i64;
67    #[stable(feature = "metadata_ext2", since = "1.8.0")]
68    fn st_ctime_nsec(&self) -> i64;
69    #[stable(feature = "metadata_ext2", since = "1.8.0")]
70    fn st_birthtime(&self) -> i64;
71    #[stable(feature = "metadata_ext2", since = "1.8.0")]
72    fn st_birthtime_nsec(&self) -> i64;
73    #[stable(feature = "metadata_ext2", since = "1.8.0")]
74    fn st_blksize(&self) -> u64;
75    #[stable(feature = "metadata_ext2", since = "1.8.0")]
76    fn st_blocks(&self) -> u64;
77    #[stable(feature = "metadata_ext2", since = "1.8.0")]
78    fn st_flags(&self) -> u32;
79    #[stable(feature = "metadata_ext2", since = "1.8.0")]
80    fn st_gen(&self) -> u32;
81    #[stable(feature = "metadata_ext2", since = "1.8.0")]
82    fn st_lspare(&self) -> u32;
83    #[cfg(target_os = "macos")]
84    #[stable(feature = "metadata_ext2", since = "1.8.0")]
85    fn st_qspare(&self) -> [u64; 2];
86}
87
88#[stable(feature = "metadata_ext", since = "1.1.0")]
89impl MetadataExt for Metadata {
90    #[allow(deprecated)]
91    #[cfg(any(doc, target_os = "macos", target_os = "ios"))]
92    #[rustdoc_skip_body]
93    fn as_raw_stat(&self) -> &super::raw::stat {
94        unsafe { &*(self.as_inner().as_inner() as *const libc::stat as *const super::raw::stat) }
95    }
96    #[rustdoc_skip_body]
97    fn st_dev(&self) -> u64 {
98        self.as_inner().as_inner().st_dev as u64
99    }
100    #[rustdoc_skip_body]
101    fn st_ino(&self) -> u64 {
102        self.as_inner().as_inner().st_ino as u64
103    }
104    #[rustdoc_skip_body]
105    fn st_mode(&self) -> u32 {
106        self.as_inner().as_inner().st_mode as u32
107    }
108    #[rustdoc_skip_body]
109    fn st_nlink(&self) -> u64 {
110        self.as_inner().as_inner().st_nlink as u64
111    }
112    #[rustdoc_skip_body]
113    fn st_uid(&self) -> u32 {
114        self.as_inner().as_inner().st_uid as u32
115    }
116    #[rustdoc_skip_body]
117    fn st_gid(&self) -> u32 {
118        self.as_inner().as_inner().st_gid as u32
119    }
120    #[rustdoc_skip_body]
121    fn st_rdev(&self) -> u64 {
122        self.as_inner().as_inner().st_rdev as u64
123    }
124    #[rustdoc_skip_body]
125    fn st_size(&self) -> u64 {
126        self.as_inner().as_inner().st_size as u64
127    }
128    #[rustdoc_skip_body]
129    fn st_atime(&self) -> i64 {
130        self.as_inner().as_inner().st_atime as i64
131    }
132    #[rustdoc_skip_body]
133    fn st_atime_nsec(&self) -> i64 {
134        self.as_inner().as_inner().st_atime_nsec as i64
135    }
136    #[rustdoc_skip_body]
137    fn st_mtime(&self) -> i64 {
138        self.as_inner().as_inner().st_mtime as i64
139    }
140    #[rustdoc_skip_body]
141    fn st_mtime_nsec(&self) -> i64 {
142        self.as_inner().as_inner().st_mtime_nsec as i64
143    }
144    #[rustdoc_skip_body]
145    fn st_ctime(&self) -> i64 {
146        self.as_inner().as_inner().st_ctime as i64
147    }
148    #[rustdoc_skip_body]
149    fn st_ctime_nsec(&self) -> i64 {
150        self.as_inner().as_inner().st_ctime_nsec as i64
151    }
152    #[rustdoc_skip_body]
153    fn st_birthtime(&self) -> i64 {
154        self.as_inner().as_inner().st_birthtime as i64
155    }
156    #[rustdoc_skip_body]
157    fn st_birthtime_nsec(&self) -> i64 {
158        self.as_inner().as_inner().st_birthtime_nsec as i64
159    }
160    #[rustdoc_skip_body]
161    fn st_blksize(&self) -> u64 {
162        self.as_inner().as_inner().st_blksize as u64
163    }
164    #[rustdoc_skip_body]
165    fn st_blocks(&self) -> u64 {
166        self.as_inner().as_inner().st_blocks as u64
167    }
168    #[rustdoc_skip_body]
169    fn st_gen(&self) -> u32 {
170        self.as_inner().as_inner().st_gen as u32
171    }
172    #[rustdoc_skip_body]
173    fn st_flags(&self) -> u32 {
174        self.as_inner().as_inner().st_flags as u32
175    }
176    #[rustdoc_skip_body]
177    fn st_lspare(&self) -> u32 {
178        self.as_inner().as_inner().st_lspare as u32
179    }
180    #[cfg(target_os = "macos")]
181    fn st_qspare(&self) -> [u64; 2] {
182        let qspare = self.as_inner().as_inner().st_qspare;
183        [qspare[0] as u64, qspare[1] as u64]
184    }
185}
186
187/// OS-specific extensions to [`fs::FileTimes`].
188#[stable(feature = "file_set_times", since = "1.75.0")]
189pub impl(self) trait FileTimesExt {
190    /// Set the creation time of a file.
191    #[stable(feature = "file_set_times", since = "1.75.0")]
192    fn set_created(self, t: SystemTime) -> Self;
193}
194
195#[stable(feature = "file_set_times", since = "1.75.0")]
196impl FileTimesExt for fs::FileTimes {
197    #[rustdoc_skip_body]
198    fn set_created(mut self, t: SystemTime) -> Self {
199        self.as_inner_mut().set_created(t.into_inner());
200        self
201    }
202}