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.
This commit is contained in:
Adam Lesperance
2020-01-21 00:06:41 -06:00
parent bc895879e6
commit 0c7b05fec9
3 changed files with 31 additions and 14 deletions
+18 -14
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())
}