Kaynağa Gözat

Merge pull request #399 from cfxegbert/mac-major-device-fix

fix(fs): Major and minor device on MacOS
Christina Sørensen 2 yıl önce
ebeveyn
işleme
7dce810885
2 değiştirilmiş dosya ile 14 ekleme ve 10 silme
  1. 2 2
      src/fs/fields.rs
  2. 12 8
      src/fs/file.rs

+ 2 - 2
src/fs/fields.rs

@@ -190,8 +190,8 @@ pub enum Size {
 /// - <http://www.lanana.org/docs/device-list/devices-2.6+.txt>
 #[derive(Copy, Clone)]
 pub struct DeviceIDs {
-    pub major: u8,
-    pub minor: u8,
+    pub major: u32,
+    pub minor: u32,
 }
 
 

+ 12 - 8
src/fs/file.rs

@@ -456,15 +456,19 @@ impl<'dir> File<'dir> {
             f::Size::None
         }
         else if self.is_char_device() || self.is_block_device() {
-            let device_ids = self.metadata.rdev().to_be_bytes();
-
-            // In C-land, getting the major and minor device IDs is done with
-            // preprocessor macros called `major` and `minor` that depend on
-            // the size of `dev_t`, but we just take the second-to-last and
-            // last bytes.
+            let device_id = self.metadata.rdev();
+
+            // MacOS and Linux have different arguments and return types for the
+            // functions major and minor.  On Linux the try_into().unwrap() and
+            // the "as u32" cast are not needed.  We turn off the warning to
+            // allow it to compile cleanly on Linux.
+            #[allow(trivial_numeric_casts)]
+            #[allow(clippy::unnecessary_cast)]
+            #[allow(clippy::useless_conversion)]
             f::Size::DeviceIDs(f::DeviceIDs {
-                major: device_ids[6],
-                minor: device_ids[7],
+                // SAFETY: Calling libc function to decompose the device_id
+                major: unsafe { libc::major(device_id.try_into().unwrap()) } as u32,
+                minor: unsafe { libc::minor(device_id.try_into().unwrap()) } as u32,
             })
         }
         else if self.is_link() && self.deref_links {