@@ -18,7 +18,7 @@ pub struct Metadata {
18
18
impl Metadata {
19
19
/// Returns simplified type of the directory entry
20
20
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 {
22
22
libc:: S_IFREG => SimpleType :: File ,
23
23
libc:: S_IFDIR => SimpleType :: Dir ,
24
24
libc:: S_IFLNK => SimpleType :: Symlink ,
@@ -48,31 +48,31 @@ impl Metadata {
48
48
}
49
49
/// Return low level file type, if available
50
50
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 )
52
52
}
53
53
/// Return device node, if available
54
54
pub fn ino ( & self ) -> Option < libc:: ino_t > {
55
55
Some ( self . stat . st_ino )
56
56
}
57
57
/// Return device node major of the file, if available
58
58
pub fn dev_major ( & self ) -> Option < u32 > {
59
- Some ( unsafe { libc :: major ( self . stat . st_dev ) } )
59
+ Some ( major ( self . stat . st_dev ) )
60
60
}
61
61
/// Return device node minor of the file, if available
62
62
pub fn dev_minor ( & self ) -> Option < u32 > {
63
- Some ( unsafe { libc :: minor ( self . stat . st_dev ) } )
63
+ Some ( minor ( self . stat . st_dev ) )
64
64
}
65
65
/// Return device node major of an device descriptor, if available
66
66
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 ) ) ,
69
69
_ => None ,
70
70
}
71
71
}
72
72
/// Return device node minor of an device descriptor, if available
73
73
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 ) ) ,
76
76
_ => None ,
77
77
}
78
78
}
@@ -102,7 +102,7 @@ impl Metadata {
102
102
}
103
103
/// Returns mode bits, if available
104
104
pub fn file_mode ( & self ) -> Option < u32 > {
105
- Some ( self . stat . st_mode & 0o7777 )
105
+ Some ( self . stat . st_mode as u32 & 0o7777 )
106
106
}
107
107
/// Returns last access time, if available
108
108
pub fn atime ( & self ) -> Option < SystemTime > {
@@ -130,6 +130,28 @@ fn unix_systemtime(sec: libc::time_t, nsec: i64) -> SystemTime {
130
130
UNIX_EPOCH + Duration :: from_secs ( sec as u64 ) + Duration :: from_nanos ( nsec as u64 )
131
131
}
132
132
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
+
133
155
#[ cfg( test) ]
134
156
mod test {
135
157
use super :: * ;
0 commit comments