Skip to content

Commit

Permalink
feat(win): use size on disk for apparent size for OneDrive files (#370)
Browse files Browse the repository at this point in the history
* feat(win): use size on disk for apparent size for OneDrive files

* apply changes from code review

* run cargo fmt
  • Loading branch information
pandaninjas authored Feb 24, 2024
1 parent 4a2778b commit a4b5d85
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ sysinfo = "0.27"

[target.'cfg(windows)'.dependencies]
winapi-util = "0.1"
filesize = "0.2.0"

[dev-dependencies]
assert_cmd = "2"
Expand Down
46 changes: 35 additions & 11 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn get_metadata(d: &Path, use_apparent_size: bool) -> Option<(u64, Option<(u
}

#[cfg(target_family = "windows")]
pub fn get_metadata(d: &Path, _use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
pub fn get_metadata(d: &Path, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
// On windows opening the file to get size, file ID and volume can be very
// expensive because 1) it causes a few system calls, and more importantly 2) it can cause
// windows defender to scan the file.
Expand Down Expand Up @@ -90,16 +90,27 @@ pub fn get_metadata(d: &Path, _use_apparent_size: bool) -> Option<(u64, Option<(
Ok(Handle::from_file(file))
}

fn get_metadata_expensive(d: &Path) -> Option<(u64, Option<(u64, u64)>)> {
fn get_metadata_expensive(
d: &Path,
use_apparent_size: bool,
) -> Option<(u64, Option<(u64, u64)>)> {
use winapi_util::file::information;

let h = handle_from_path_limited(d).ok()?;
let info = information(&h).ok()?;

Some((
info.file_size(),
Some((info.file_index(), info.volume_serial_number())),
))
if use_apparent_size {
use filesize::PathExt;
Some((
d.size_on_disk().ok()?,
Some((info.file_index(), info.volume_serial_number())),
))
} else {
Some((
info.file_size(),
Some((info.file_index(), info.volume_serial_number())),
))
}
}

use std::os::windows::fs::MetadataExt;
Expand All @@ -111,18 +122,31 @@ pub fn get_metadata(d: &Path, _use_apparent_size: bool) -> Option<(u64, Option<(
const FILE_ATTRIBUTE_SYSTEM: u32 = 0x04;
const FILE_ATTRIBUTE_NORMAL: u32 = 0x80;
const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x10;

const FILE_ATTRIBUTE_SPARSE_FILE: u32 = 0x00000200;
const FILE_ATTRIBUTE_PINNED: u32 = 0x00080000;
const FILE_ATTRIBUTE_UNPINNED: u32 = 0x00100000;
const FILE_ATTRIBUTE_RECALL_ON_OPEN: u32 = 0x00040000;
const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS: u32 = 0x00400000;
const FILE_ATTRIBUTE_OFFLINE: u32 = 0x00001000;
// normally FILE_ATTRIBUTE_SPARSE_FILE would be enough, however Windows sometimes likes to mask it out. see: https://stackoverflow.com/q/54560454
const IS_PROBABLY_ONEDRIVE: u32 = FILE_ATTRIBUTE_SPARSE_FILE
| FILE_ATTRIBUTE_PINNED
| FILE_ATTRIBUTE_UNPINNED
| FILE_ATTRIBUTE_RECALL_ON_OPEN
| FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS
| FILE_ATTRIBUTE_OFFLINE;
let attr_filtered = md.file_attributes()
& !(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM);
if (attr_filtered & FILE_ATTRIBUTE_ARCHIVE) != 0
if ((attr_filtered & FILE_ATTRIBUTE_ARCHIVE) != 0
|| (attr_filtered & FILE_ATTRIBUTE_DIRECTORY) != 0
|| md.file_attributes() == FILE_ATTRIBUTE_NORMAL
|| md.file_attributes() == FILE_ATTRIBUTE_NORMAL)
&& !((attr_filtered & IS_PROBABLY_ONEDRIVE != 0) && use_apparent_size)
{
Some((md.len(), None))
} else {
get_metadata_expensive(d)
get_metadata_expensive(d, use_apparent_size)
}
}
_ => get_metadata_expensive(d),
_ => get_metadata_expensive(d, use_apparent_size),
}
}

0 comments on commit a4b5d85

Please sign in to comment.