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