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