Skip to content

Commit a76e93c

Browse files
committed
WIP: changes to make macos happy (hopefully)
1 parent c502cc2 commit a76e93c

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/metadata.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Metadata {
1818
impl Metadata {
1919
/// Returns simplified type of the directory entry
2020
pub fn simple_type(&self) -> SimpleType {
21-
match self.file_type().unwrap_or(0) {
21+
match self.file_type().unwrap_or(0) as libc::mode_t {
2222
libc::S_IFREG => SimpleType::File,
2323
libc::S_IFDIR => SimpleType::Dir,
2424
libc::S_IFLNK => SimpleType::Symlink,
@@ -48,31 +48,31 @@ impl Metadata {
4848
}
4949
/// Return low level file type, if available
5050
pub fn file_type(&self) -> Option<u32> {
51-
Some(self.stat.st_mode & libc::S_IFMT)
51+
Some(self.stat.st_mode as u32 & libc::S_IFMT as u32)
5252
}
5353
/// Return device node, if available
5454
pub fn ino(&self) -> Option<libc::ino_t> {
5555
Some(self.stat.st_ino)
5656
}
5757
/// Return device node major of the file, if available
5858
pub fn dev_major(&self) -> Option<u32> {
59-
Some(unsafe { libc::major(self.stat.st_dev) })
59+
Some(major(self.stat.st_dev))
6060
}
6161
/// Return device node minor of the file, if available
6262
pub fn dev_minor(&self) -> Option<u32> {
63-
Some(unsafe { libc::minor(self.stat.st_dev) })
63+
Some(minor(self.stat.st_dev))
6464
}
6565
/// Return device node major of an device descriptor, if available
6666
pub fn rdev_major(&self) -> Option<u32> {
67-
match self.file_type()? {
68-
libc::S_IFBLK | libc::S_IFCHR => Some(unsafe { libc::major(self.stat.st_rdev) }),
67+
match self.file_type()? as libc::mode_t {
68+
libc::S_IFBLK | libc::S_IFCHR => Some(major(self.stat.st_rdev)),
6969
_ => None,
7070
}
7171
}
7272
/// Return device node minor of an device descriptor, if available
7373
pub fn rdev_minor(&self) -> Option<u32> {
74-
match self.file_type()? {
75-
libc::S_IFBLK | libc::S_IFCHR => Some(unsafe { libc::minor(self.stat.st_rdev) }),
74+
match self.file_type()? as libc::mode_t {
75+
libc::S_IFBLK | libc::S_IFCHR => Some(minor(self.stat.st_rdev)),
7676
_ => None,
7777
}
7878
}
@@ -102,7 +102,7 @@ impl Metadata {
102102
}
103103
/// Returns mode bits, if available
104104
pub fn file_mode(&self) -> Option<u32> {
105-
Some(self.stat.st_mode & 0o7777)
105+
Some(self.stat.st_mode as u32 & 0o7777)
106106
}
107107
/// Returns last access time, if available
108108
pub fn atime(&self) -> Option<SystemTime> {
@@ -130,6 +130,28 @@ fn unix_systemtime(sec: libc::time_t, nsec: i64) -> SystemTime {
130130
UNIX_EPOCH + Duration::from_secs(sec as u64) + Duration::from_nanos(nsec as u64)
131131
}
132132

133+
#[cfg(not(target_os = "macos"))]
134+
pub fn major(dev: libc::dev_t) -> u32 {
135+
unsafe { libc::major(dev) }
136+
}
137+
138+
#[cfg(not(target_os = "macos"))]
139+
pub fn minor(dev: libc::dev_t) -> u32 {
140+
unsafe { libc::minor(dev) }
141+
}
142+
143+
// major/minor are not in rust's darwin libc (why)
144+
// see https://github.com/apple/darwin-xnu/blob/main/bsd/sys/types.h
145+
#[cfg(target_os = "macos")]
146+
pub fn major(dev: libc::dev_t) -> u32 {
147+
(dev as u32 >> 24) & 0xff
148+
}
149+
150+
#[cfg(target_os = "macos")]
151+
pub fn minor(dev: libc::dev_t) -> u32 {
152+
dev as u32 & 0xffffff
153+
}
154+
133155
#[cfg(test)]
134156
mod test {
135157
use super::*;

0 commit comments

Comments
 (0)