]> git.proxmox.com Git - pxar.git/commitdiff
add Device::from_dev_t
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 24 Apr 2020 12:40:17 +0000 (14:40 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 24 Apr 2020 12:40:17 +0000 (14:40 +0200)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Cargo.toml
src/format.rs

index 6abe73b269dcd0899d1e2d821668739cb92254b9..39dcf9532f0510d7f5e08d5260506722830a47e9 100644 (file)
@@ -38,3 +38,7 @@ async-example = [
     "tokio/io-driver",
     "tokio/macros",
 ]
+
+[dev-dependencies]
+libc = "0.2"
+anyhow = "1.0"
index e1e3ace702afc911b091047991ebf3b05e3b3c6d..92dea9661708982e2a95b555daaaa3989b2243e8 100644 (file)
@@ -321,9 +321,7 @@ impl XAttr {
     }
 
     pub fn name(&self) -> &CStr {
-        unsafe {
-            CStr::from_bytes_with_nul_unchecked(&self.data[..self.name_len+1])
-        }
+        unsafe { CStr::from_bytes_with_nul_unchecked(&self.data[..self.name_len + 1]) }
     }
 
     pub fn value(&self) -> &[u8] {
@@ -367,6 +365,26 @@ impl Device {
          (self.minor & 0x0000_00ff) |
         ((self.minor & 0xffff_ff00) << 12)
     }
+
+    /// Get a `Device` from a `dev_t` value.
+    #[rustfmt::skip]
+    pub fn from_dev_t(dev: u64) -> Self {
+        // see to_dev_t
+        Self {
+            major: (dev >>  8) & 0x0000_0fff |
+                   (dev >> 32) & 0xffff_f000,
+            minor:  dev        & 0x0000_00ff |
+                   (dev >> 12) & 0xffff_ff00,
+        }
+    }
+}
+
+#[cfg(all(test, target_os = "linux"))]
+#[test]
+fn test_linux_devices() {
+    let c_dev = unsafe { ::libc::makedev(0xabcd_1234, 0xdcba_5678) };
+    let dev = Device::from_dev_t(c_dev);
+    assert_eq!(dev.to_dev_t(), c_dev);
 }
 
 #[derive(Clone, Debug)]