From 0c7b05fec9f503ac63d0f75cb456ee931d4304aa Mon Sep 17 00:00:00 2001 From: Adam Lesperance Date: Tue, 21 Jan 2020 00:06:41 -0600 Subject: [PATCH] Fix get_metadata on windows The MetadataExt functions are nightly-only right now so we need to call the windows api ourselves to get the device number. The winapi_util package has a nice wrapper to do this for us. The get_device function doesn't appear to be used anywhere so I removed it. --- Cargo.lock | 10 ++++++++++ Cargo.toml | 3 +++ src/utils/platform.rs | 32 ++++++++++++++++++-------------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8683faf..087c1b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,7 @@ dependencies = [ "jwalk 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -504,6 +505,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -572,4 +581,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 14f786c..1e2a6f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,9 @@ clap = "=2.33" jwalk = "0.4.0" num_cpus = "1.12" +[target.'cfg(windows)'.dependencies] +winapi-util = "0.1" + [dev-dependencies] assert_cli = "=0.6" tempfile = "=3" diff --git a/src/utils/platform.rs b/src/utils/platform.rs index 43d1a66..450e1db 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -1,4 +1,5 @@ use jwalk::DirEntry; +#[allow(unused_imports)] use std::fs; use std::io; @@ -23,12 +24,17 @@ pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Optio } #[cfg(target_family = "windows")] -pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::windows::fs::MetadataExt; - d.metadata.as_ref().unwrap().as_ref().ok().map(|md| { - let windows_equivalent_of_inode = Some((md.file_index(), md.volume_serial_number())); - (md.file_size(), windows_equivalent_of_inode) - }) +pub fn get_metadata(d: &DirEntry, _use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { + use winapi_util::file::information; + use winapi_util::Handle; + + let h = Handle::from_path(d.path()).ok()?; + let info = information(&h).ok()?; + + Some(( + info.file_size(), + Some((info.file_index(), info.volume_serial_number())), + )) } #[cfg(all(not(target_family = "windows"), not(target_family = "unix")))] @@ -49,13 +55,11 @@ pub fn get_filesystem(file_path: &str) -> Result { } #[cfg(target_family = "windows")] -pub fn get_device(file_path: &str) -> Result { - use std::os::windows::fs::MetadataExt; - let metadata = fs::metadata(file_path)?; - Ok(metadata.volume_serial_number()) -} +pub fn get_filesystem(file_path: &str) -> Result { + use winapi_util::file::information; + use winapi_util::Handle; -#[cfg(all(not(target_family = "windows"), not(target_family = "unix")))] -pub fn get_device(file_path: &str) -> Result { - None + let h = Handle::from_path(file_path)?; + let info = information(&h)?; + Ok(info.volume_serial_number()) }