Merge pull request #61 from lespea/fixWinBreaks

Fix get_metadata on windows
This commit is contained in:
andy boot
2020-01-21 22:06:00 +00:00
committed by GitHub
3 changed files with 31 additions and 14 deletions

10
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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<u64, io::Error> {
}
#[cfg(target_family = "windows")]
pub fn get_device(file_path: &str) -> Result<u64, io::Error> {
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<u64, io::Error> {
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<u64, io::Error> {
None
let h = Handle::from_path(file_path)?;
let info = information(&h)?;
Ok(info.volume_serial_number())
}