Measure what NTFS actually allocated, and unbreak the /Applications test
Real Windows 10 hardware turned up two failures the macOS and cross-compile checks could not see: - squid-core::fs reported every file's logical length as its allocated size, because `allocated_bytes` returned 0 on non-Unix and `on_disk()` then fell back to logical. NTFS only answers the allocation question through a handle, so ask `GetFileInformationByHandleEx(FileStandardInfo)` for files big enough to distort a total. Files below the cutoff keep counting as their length, which is also what keeps MFT-resident entries — they report an allocation of 0 — from shrinking a cache cleanup down to nothing. - the "a plain user can still uninstall into /Applications" safety test hard coded a Unix path, so on Windows it asserted that a folder the policy never looked at was refused. The fixture now comes from the policy's own shared install folder, `/Applications` on Unix and the machine-wide program-data folders on Windows. Also added: - `a_written_file_reports_what_the_volume_gave_it`, the cross-platform invariant the missing backend silently violated. - the sparse-file test now marks its file sparse via `fsutil` on Windows and skips when the volume will not cooperate, instead of pretending a hole exists. - `scripts/win-sync.sh`, `scripts/win-check.ps1` and `scripts/win-hwcheck.ps1` for iterating against a LAN Windows host over SSH without git credentials.main
parent
1d73effc5c
commit
7584811e91
|
|
@ -23,6 +23,8 @@ rayon.workspace = true
|
|||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.58", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_Storage_FileSystem",
|
||||
"Win32_System_Com",
|
||||
"Win32_UI_Shell",
|
||||
] }
|
||||
|
|
|
|||
|
|
@ -72,19 +72,96 @@ impl SizeInfo {
|
|||
|
||||
pub use crate::bytes::format_thousands;
|
||||
|
||||
fn allocated_bytes(meta: &Metadata) -> u64 {
|
||||
/// Bytes the volume really gave up for one file, or 0 when the platform
|
||||
/// cannot say.
|
||||
///
|
||||
/// Unix answers from `st_blocks`, which `stat` already handed us. Windows only
|
||||
/// answers through a handle, and opening one per file would dominate the walk,
|
||||
/// so we ask only for files large enough to distort a total and let the rest
|
||||
/// count as their length. The cutoff is not a precision trade-off so much as a
|
||||
/// correctness one: NTFS keeps small files inside the MFT record, where they
|
||||
/// report an allocation of 0, and summing that over a cache full of 2 KB
|
||||
/// entries would announce a cleanup that gave back nothing.
|
||||
fn allocated_bytes(path: &Path, meta: &Metadata) -> u64 {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let _ = path;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
meta.blocks().saturating_mul(512)
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let _ = meta;
|
||||
windows_allocation(path, meta.len())
|
||||
}
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
{
|
||||
let _ = (path, meta);
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Anything below this cannot change a reported total by more than the odd
|
||||
/// cluster, and is far too common to be worth a syscall each.
|
||||
///
|
||||
/// Only Windows consults it — `stat` hands Unix the answer for free — but the
|
||||
/// size test uses it on every platform so the two stay tied together.
|
||||
#[cfg_attr(not(windows), allow(dead_code))]
|
||||
const ALLOCATION_PROBE_MIN: u64 = 8 << 20;
|
||||
|
||||
/// `GetFileInformationByHandleEx(FileStandardInfo).AllocationSize`.
|
||||
///
|
||||
/// `FILE_READ_ATTRIBUTES` is the cheapest access right that still hands back a
|
||||
/// handle: it needs no read access to the contents, so it succeeds on files
|
||||
/// another process holds open for writing — exactly the cache files a scan
|
||||
/// walks past. `FILE_FLAG_BACKUP_SEMANTICS` keeps it working when the ACL only
|
||||
/// grants us permission to look at the entry.
|
||||
#[cfg(windows)]
|
||||
fn windows_allocation(path: &Path, len: u64) -> u64 {
|
||||
if len < ALLOCATION_PROBE_MIN {
|
||||
return len;
|
||||
}
|
||||
use std::ffi::c_void;
|
||||
use windows::core::PCWSTR;
|
||||
use windows::Win32::Foundation::CloseHandle;
|
||||
use windows::Win32::Storage::FileSystem::{
|
||||
CreateFileW, FileStandardInfo, GetFileInformationByHandleEx, FILE_FLAG_BACKUP_SEMANTICS,
|
||||
FILE_READ_ATTRIBUTES, FILE_SHARE_MODE, FILE_STANDARD_INFO, OPEN_EXISTING,
|
||||
};
|
||||
|
||||
let wide: Vec<u16> = path
|
||||
.to_string_lossy()
|
||||
.encode_utf16()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
unsafe {
|
||||
let Ok(handle) = CreateFileW(
|
||||
PCWSTR(wide.as_ptr()),
|
||||
FILE_READ_ATTRIBUTES.0,
|
||||
FILE_SHARE_MODE(0),
|
||||
None,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS,
|
||||
None,
|
||||
) else {
|
||||
return len;
|
||||
};
|
||||
let mut info = FILE_STANDARD_INFO::default();
|
||||
let read = GetFileInformationByHandleEx(
|
||||
handle,
|
||||
FileStandardInfo,
|
||||
&mut info as *mut FILE_STANDARD_INFO as *mut c_void,
|
||||
std::mem::size_of::<FILE_STANDARD_INFO>() as u32,
|
||||
);
|
||||
let _ = CloseHandle(handle);
|
||||
match read {
|
||||
// A deleted-but-still-open file reports 0; never let that shrink a
|
||||
// total the user is about to act on.
|
||||
Ok(()) if info.AllocationSize > 0 => info.AllocationSize as u64,
|
||||
_ => len,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Measure one path. `Contents` skips the directory entry itself so the size
|
||||
/// matches what removing its children would free.
|
||||
pub fn measure(path: &Path, kind: ItemKind) -> SizeInfo {
|
||||
|
|
@ -102,7 +179,7 @@ pub fn measure(path: &Path, kind: ItemKind) -> SizeInfo {
|
|||
ItemKind::File => {
|
||||
info.files += 1;
|
||||
info.logical += Bytes(meta.len());
|
||||
info.allocated += Bytes(allocated_bytes(&meta));
|
||||
info.allocated += Bytes(allocated_bytes(path, &meta));
|
||||
return info;
|
||||
}
|
||||
ItemKind::Path | ItemKind::Contents => {}
|
||||
|
|
@ -110,7 +187,7 @@ pub fn measure(path: &Path, kind: ItemKind) -> SizeInfo {
|
|||
if !meta.is_dir() {
|
||||
info.files += 1;
|
||||
info.logical += Bytes(meta.len());
|
||||
info.allocated += Bytes(allocated_bytes(&meta));
|
||||
info.allocated += Bytes(allocated_bytes(path, &meta));
|
||||
return info;
|
||||
}
|
||||
|
||||
|
|
@ -139,7 +216,7 @@ pub fn measure(path: &Path, kind: ItemKind) -> SizeInfo {
|
|||
Ok(meta) => {
|
||||
info.files += 1;
|
||||
info.logical += Bytes(meta.len());
|
||||
info.allocated += Bytes(allocated_bytes(&meta));
|
||||
info.allocated += Bytes(allocated_bytes(entry.path(), &meta));
|
||||
}
|
||||
Err(_) => info.unreadable += 1,
|
||||
}
|
||||
|
|
@ -569,6 +646,7 @@ pub type SharedCancel = Arc<CancelToken>;
|
|||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::io::Write as _;
|
||||
|
||||
#[test]
|
||||
fn measures_contents_and_path() {
|
||||
|
|
@ -601,6 +679,16 @@ mod tests {
|
|||
let file = fs::File::create(&raw).unwrap();
|
||||
file.set_len(8 << 30).unwrap();
|
||||
drop(file);
|
||||
if !mark_sparse(&raw) {
|
||||
eprintln!("skipped: this volume refuses to make a file sparse");
|
||||
let _ = fs::remove_dir_all(&root);
|
||||
return;
|
||||
}
|
||||
// One block of real data, so the file has both a hole and a tail the
|
||||
// volume had to hand over — the shape of an actual thin disk image.
|
||||
let mut hole = fs::OpenOptions::new().write(true).open(&raw).unwrap();
|
||||
hole.write_all(&[3u8; 4096]).unwrap();
|
||||
drop(hole);
|
||||
|
||||
let info = measure(&root, ItemKind::Path);
|
||||
assert!(info.logical.as_u64() >= 8 << 30, "logical length is 8 GiB");
|
||||
|
|
@ -613,6 +701,55 @@ mod tests {
|
|||
let _ = fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Ask the volume to make a file sparse.
|
||||
///
|
||||
/// `fsutil` is the only way to do this without pulling a second set of
|
||||
/// Windows bindings into a dev-dependency just for one `DeviceIoControl`.
|
||||
/// It fails when the volume is not NTFS or we lack the right, and the
|
||||
/// caller treats that as "this machine cannot demonstrate the property"
|
||||
/// rather than as a failure.
|
||||
#[cfg(windows)]
|
||||
fn mark_sparse(path: &std::path::Path) -> bool {
|
||||
std::process::Command::new("fsutil")
|
||||
.args(["file", "setSparse", &path.to_string_lossy()])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.status()
|
||||
.map(|status| status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// POSIX holes a file by extending it, which is all `set_len` has to do.
|
||||
#[cfg(not(windows))]
|
||||
fn mark_sparse(_: &std::path::Path) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// The invariant both platforms must satisfy: a file that was really
|
||||
/// written occupies at least what it logically holds. A port that forgets
|
||||
/// to answer the allocation question returns 0 here, and every "freed"
|
||||
/// number downstream quietly becomes the logical size.
|
||||
#[test]
|
||||
fn a_written_file_reports_what_the_volume_gave_it() {
|
||||
let root = std::env::temp_dir().join(format!("squid-alloc-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&root);
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
let mut file = fs::File::create(root.join("solid.bin")).unwrap();
|
||||
file.set_len(ALLOCATION_PROBE_MIN).unwrap();
|
||||
file.write_all(&[9u8; 4096]).unwrap();
|
||||
drop(file);
|
||||
|
||||
let info = measure(&root, ItemKind::Path);
|
||||
assert_eq!(info.logical, Bytes(ALLOCATION_PROBE_MIN));
|
||||
assert!(
|
||||
info.allocated >= info.logical,
|
||||
"a freshly written file reported {} of {} allocated; clusters only round up",
|
||||
info.allocated,
|
||||
info.logical
|
||||
);
|
||||
let _ = fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thousand_separators() {
|
||||
assert_eq!(format_thousands(0), "0");
|
||||
|
|
|
|||
|
|
@ -403,23 +403,45 @@ mod tests {
|
|||
// The bug: as a normal user the home-only rule refused every bundle,
|
||||
// so `squid uninstall` approved the app and then deleted only its
|
||||
// leftovers.
|
||||
//
|
||||
// The shared install folder is `/Applications` on Unix and the
|
||||
// machine-wide program-data folders on Windows, so the fixture is read
|
||||
// back from the policy instead of being hardcoded to one OS's spelling
|
||||
// — a literal `/Applications` on Windows is not even an absolute path,
|
||||
// and the test would have been asserting that Windows refuses an app
|
||||
// folder it never actually looked at.
|
||||
let policy = SafetyPolicy::user_only();
|
||||
let anchors = vec![PathBuf::from("/Applications")];
|
||||
assert!(policy
|
||||
.check(Path::new("/Applications/Demo.app"), &anchors)
|
||||
.is_ok());
|
||||
let shared = policy
|
||||
.outside_home_ok
|
||||
.first()
|
||||
.cloned()
|
||||
.expect("every platform names a shared install folder");
|
||||
let app = if cfg!(windows) {
|
||||
shared.join("Demo")
|
||||
} else {
|
||||
shared.join("Demo.app")
|
||||
};
|
||||
let anchors = vec![shared.clone()];
|
||||
assert!(
|
||||
policy
|
||||
.denies(Path::new("/Applications"), &anchors)
|
||||
.is_some(),
|
||||
policy.check(&app, &anchors).is_ok(),
|
||||
"{app:?} has to be removable without elevation"
|
||||
);
|
||||
assert!(
|
||||
policy.denies(&shared, &anchors).is_some(),
|
||||
"the folder itself stays untouchable"
|
||||
);
|
||||
assert!(policy
|
||||
.denies(Path::new("/Library/LaunchDaemons/x.plist"), &anchors)
|
||||
.is_some());
|
||||
assert!(policy
|
||||
.denies(Path::new("/System/Applications/Notes.app"), &anchors)
|
||||
.is_some());
|
||||
assert!(policy.denies(Path::new(system_file()), &anchors).is_some());
|
||||
}
|
||||
|
||||
/// A file no run of squid may ever delete, on any platform.
|
||||
#[cfg(not(windows))]
|
||||
fn system_file() -> &'static str {
|
||||
"/System/Applications/Notes.app"
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn system_file() -> &'static str {
|
||||
r"C:\Windows\System32\drivers\ndis.sys"
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
# 在 Windows 真机上跑一整套校验:fmt 之外的 test / clippy / release build。
|
||||
# 由 scripts/win-sync.sh 上传并调用;也可以单独在 PowerShell 里直接跑。
|
||||
$ErrorActionPreference = 'Continue'
|
||||
# 中文输出必须 UTF-8,否则 SSH 那头看到的全是乱码。
|
||||
$null = & cmd /c 'chcp 65001 >nul'
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$env:Path = "$env:USERPROFILE\.cargo\bin;$env:Path"
|
||||
# 增量构建产物放 D 盘之外无所谓,但统一放 C:\dev\target 以免被 tar 覆盖。
|
||||
$env:CARGO_TARGET_DIR = 'C:\dev\target'
|
||||
Set-Location C:\dev\nex_squid
|
||||
|
||||
$fail = 0
|
||||
foreach ($step in @(
|
||||
@{ n = 'cargo test'; a = @('test') },
|
||||
@{ n = 'cargo clippy'; a = @('clippy', '--all-targets', '--', '-D', 'warnings') },
|
||||
@{ n = 'cargo build'; a = @('build', '--release', '-p', 'squid-cli') }
|
||||
)) {
|
||||
Write-Output "`n==> $($step.n)"
|
||||
& cargo @($step.a)
|
||||
if ($LASTEXITCODE -ne 0) { Write-Output "!!! $($step.n) FAILED (exit $LASTEXITCODE)"; $fail = 1 }
|
||||
}
|
||||
if ($fail) { Write-Output "`nRESULT: FAIL" ; exit 1 }
|
||||
Write-Output "`nRESULT: OK"
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
# 真机验收脚本:只读检查 + 一个自建的回收站往返实验,不碰用户真实数据。
|
||||
# 由 scripts/win-sync.sh 同步后单独执行:
|
||||
# ssh KANGWJ@host powershell -NoProfile -ExecutionPolicy Bypass -File C:\Users\kangwj\win-hwcheck.ps1
|
||||
$ErrorActionPreference = 'Continue'
|
||||
$null = & cmd /c 'chcp 65001 >nul'
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$env:Path = "$env:USERPROFILE\.cargo\bin;$env:Path"
|
||||
$env:CARGO_TARGET_DIR = 'C:\dev\target'
|
||||
$squid = 'C:\dev\target\release\squid.exe'
|
||||
$scratch = Join-Path $env:USERPROFILE 'squid-rb-test'
|
||||
$fails = @()
|
||||
function Step($name) { Write-Output "`n=== $name ===" }
|
||||
function Fail($m) { $script:fails += $m; Write-Output "!!! FAIL: $m" }
|
||||
function Ok($m) { Write-Output "ok : $m" }
|
||||
|
||||
if (-not (Test-Path $squid)) { Write-Output "missing $squid"; exit 1 }
|
||||
|
||||
Step 'doctor'
|
||||
& $squid doctor --lang en
|
||||
Step 'status (json)'
|
||||
$status = & $squid status --json | ConvertFrom-Json
|
||||
Ok ("os={0} host={1} disks={2}" -f $status.os, $status.hostname, $status.disks.Count)
|
||||
|
||||
Step 'installed apps vs registry'
|
||||
$apps = & $squid uninstall --list --json | ConvertFrom-Json
|
||||
$reg = @()
|
||||
foreach ($hive in 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
|
||||
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall',
|
||||
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall') {
|
||||
$reg += Get-ChildItem $hive -ErrorAction SilentlyContinue | ForEach-Object {
|
||||
$p = Get-ItemProperty $_.PSPath
|
||||
if ($p.DisplayName -and -not $p.SystemComponent) { $p.DisplayName }
|
||||
}
|
||||
}
|
||||
$regNames = $reg | Sort-Object -Unique
|
||||
Ok ("squid={0} registry={1}" -f $apps.Count, $regNames.Count)
|
||||
$missing = $regNames | Where-Object { $apps.name -notcontains $_ }
|
||||
if ($missing) { Write-Output ("missing in squid: " + ($missing -join ' | ')) }
|
||||
|
||||
Step 'clean --dry-run (safety invariant)'
|
||||
$report = & $squid clean --dry-run --json | ConvertFrom-Json
|
||||
Ok ("items={0} freed={1} unreadable={2}" -f $report.items.Count, $report.freed_on_disk.human, $report.unreadable)
|
||||
$report.items | Group-Object category | Sort-Object Count -Descending | ForEach-Object {
|
||||
Write-Output (" {0,-14} {1}" -f $_.Name, $_.Count)
|
||||
}
|
||||
# 核心安全不变量:清理项绝不能落在系统目录里。
|
||||
$forbidden = @('C:\Windows', 'C:\Program Files', 'C:\Program Files (x86)', '$Recycle.Bin')
|
||||
$bad = $report.items | Where-Object {
|
||||
$p = $_.path.ToLower()
|
||||
foreach ($f in $forbidden) { if ($p.StartsWith($f.ToLower())) { return $true } }
|
||||
return $false
|
||||
}
|
||||
if ($bad) { Fail ("clean offered {0} system-dir item(s): " -f $bad.Count + (($bad | Select-Object -First 3).path -join ' | ')) }
|
||||
else { Ok 'no clean item inside system directories' }
|
||||
|
||||
Step 'recycle-bin round trip'
|
||||
Remove-Item $scratch -Recurse -Force -ErrorAction SilentlyContinue
|
||||
$pkg = Join-Path $scratch 'node_modules\pkg'
|
||||
New-Item -ItemType Directory -Force -Path $pkg | Out-Null
|
||||
[IO.File]::WriteAllBytes((Join-Path $pkg 'blob.bin'), (New-Object byte[] 2MB))
|
||||
Ok ("fixture created: $scratch (2 MB)")
|
||||
$before = (New-Object -ComObject Shell.Application).Namespace(0xA).Items().Count
|
||||
& $squid purge $scratch --yes --trash --json
|
||||
$after = (New-Object -ComObject Shell.Application).Namespace(0xA).Items().Count
|
||||
if (Test-Path $scratch) { Fail 'scratch folder still on disk after trash' } else { Ok 'scratch folder left the disk' }
|
||||
if ($after -le $before) { Fail "recycle bin did not grow ($before -> $after)" } else { Ok "recycle bin grew $before -> $after" }
|
||||
$names = (New-Object -ComObject Shell.Application).Namespace(0xA).Items() | ForEach-Object { $_.Name }
|
||||
if ($names -match 'squid-rb-test') { Ok 'original name recoverable from recycle bin' } else { Fail 'fixture not found in recycle bin by name' }
|
||||
& $squid undo 2>&1 | Select-Object -First 4
|
||||
|
||||
Step 'result'
|
||||
if ($fails.Count) { Write-Output ("FAILURES: " + $fails.Count); $fails | ForEach-Object { Write-Output " - $_" }; exit 1 }
|
||||
Write-Output 'ALL CHECKS PASSED'
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env bash
|
||||
# 把当前 HEAD 同步到局域网里的 Windows 真机(不走 git 认证,直接 tarball)。
|
||||
#
|
||||
# ./scripts/win-sync.sh # 同步 + 在远端跑 check(win-check.ps1)
|
||||
# ./scripts/win-sync.sh --no-verify # 只同步,不跑校验
|
||||
#
|
||||
# 依赖: scripts/win-check.ps1 也会一起上传,所以远端永远是最新的那份。
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
WIN_HOST="${WIN_HOST:-KANGWJ@192.168.2.92}"
|
||||
WIN_KEY="${WIN_KEY:-$HOME/.ssh/id_ed25519_windows}"
|
||||
WIN_DIR="${WIN_DIR:-C:/dev/nex_squid}"
|
||||
REMOTE_TMP='C:\Users\kangwj\nex_squid.tgz'
|
||||
|
||||
ssh_run() { ssh -i "$WIN_KEY" -o BatchMode=yes "$WIN_HOST" "$1"; }
|
||||
scp_to() { scp -i "$WIN_KEY" -o BatchMode=yes -q "$1" "$WIN_HOST:$2"; }
|
||||
|
||||
TARBALL="$(mktemp -t nexsquid).tgz"
|
||||
trap 'rm -f "$TARBALL"' EXIT
|
||||
|
||||
printf '\033[1;36m==>\033[0m 打包 HEAD (%s)\n' "$(git rev-parse --short HEAD)"
|
||||
git archive --format=tar HEAD | gzip > "$TARBALL"
|
||||
scp_to "$TARBALL" "$REMOTE_TMP"
|
||||
|
||||
printf '\033[1;36m==>\033[0m 解压到 %s\n' "$WIN_DIR"
|
||||
# 只覆盖源码:target/ 和 .cargo/ 不在 tarball 里,远端构建缓存得以保留。
|
||||
ssh_run "mkdir C:\dev\nex_squid 2>nul & tar -xzf $REMOTE_TMP -C $WIN_DIR && echo extracted"
|
||||
|
||||
scp_to scripts/win-check.ps1 'C:\Users\kangwj\win-check.ps1'
|
||||
scp_to scripts/win-hwcheck.ps1 'C:\Users\kangwj\win-hwcheck.ps1'
|
||||
|
||||
if [ "${1:-}" = "--no-verify" ]; then exit 0; fi
|
||||
|
||||
printf '\033[1;36m==>\033[0m 远端校验 (test / clippy / build)\n'
|
||||
# chcp 65001:否则中文输出在 GBK 控制台下是乱码,日志没法看。
|
||||
ssh_run "powershell -NoProfile -ExecutionPolicy Bypass -File C:\Users\kangwj\win-check.ps1"
|
||||
Loading…
Reference in New Issue