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