diff --git a/crates/Cargo.lock b/crates/Cargo.lock index 3fbf45f7a..5e7501f00 100644 --- a/crates/Cargo.lock +++ b/crates/Cargo.lock @@ -4311,6 +4311,7 @@ dependencies = [ "serde_json", "tempfile", "thiserror 2.0.18", + "windows-sys 0.61.2", ] [[package]] diff --git a/crates/qbz-local-catalog/Cargo.toml b/crates/qbz-local-catalog/Cargo.toml index 9327c4482..ad56186ca 100644 --- a/crates/qbz-local-catalog/Cargo.toml +++ b/crates/qbz-local-catalog/Cargo.toml @@ -14,5 +14,15 @@ thiserror.workspace = true [target.'cfg(unix)'.dependencies] rustix = { version = "1", features = ["fs"] } +[target.'cfg(windows)'.dependencies] +# GetDiskFreeSpaceExW for the free-space preflight; LockFileEx (+ OVERLAPPED, +# hence Win32_System_IO) for the bootstrap lock. 0.61 is already in the +# workspace lock, so this pulls nothing new. +windows-sys = { version = "0.61", features = [ + "Win32_Foundation", + "Win32_Storage_FileSystem", + "Win32_System_IO", +] } + [dev-dependencies] tempfile = "3" diff --git a/crates/qbz-local-catalog/src/bootstrap.rs b/crates/qbz-local-catalog/src/bootstrap.rs index 124f5dc38..d61370278 100644 --- a/crates/qbz-local-catalog/src/bootstrap.rs +++ b/crates/qbz-local-catalog/src/bootstrap.rs @@ -472,6 +472,41 @@ pub(crate) fn acquire_lock(path: &Path) -> Result { } })?; } + #[cfg(windows)] + { + use std::os::windows::io::AsRawHandle; + use windows_sys::Win32::Foundation::ERROR_LOCK_VIOLATION; + use windows_sys::Win32::Storage::FileSystem::{ + LockFileEx, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, + }; + + let mut overlapped: windows_sys::Win32::System::IO::OVERLAPPED = + unsafe { std::mem::zeroed() }; + // SAFETY: `file` is open, so its handle is valid for the call; the + // zeroed OVERLAPPED lives for the whole call; locking 0..u64::MAX is + // the documented "lock the entire file" idiom. The lock is released + // when `file` drops and the handle closes, matching flock(). + let ok = unsafe { + LockFileEx( + file.as_raw_handle() as _, + LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, + 0, + u32::MAX, + u32::MAX, + &mut overlapped, + ) + }; + if ok == 0 { + let error = std::io::Error::last_os_error(); + return Err( + if error.raw_os_error() == Some(ERROR_LOCK_VIOLATION as i32) { + CatalogError::BootstrapBusy + } else { + CatalogError::Io(error) + }, + ); + } + } Ok(file) } @@ -512,7 +547,39 @@ fn filesystem_available_bytes(path: &Path) -> Result { Ok(status.f_bavail.saturating_mul(status.f_frsize)) } -#[cfg(not(unix))] +#[cfg(windows)] +fn filesystem_available_bytes(path: &Path) -> Result { + use std::os::windows::ffi::OsStrExt; + use windows_sys::Win32::Storage::FileSystem::GetDiskFreeSpaceExW; + + // GetDiskFreeSpaceExW wants a path that exists. `available_bytes()` already + // hands us the nearest existing ancestor, but keep the walk local so a + // future caller cannot turn this into ERROR_PATH_NOT_FOUND on first run. + let probe = nearest_existing_ancestor(path).unwrap_or(path); + let wide: Vec = probe + .as_os_str() + .encode_wide() + .chain(std::iter::once(0)) + .collect(); + let mut free_to_caller: u64 = 0; + // SAFETY: `wide` is NUL-terminated and outlives the call; `free_to_caller` + // is a valid u64 slot; the remaining two out-params are documented as + // optional and are passed as null. + let ok = unsafe { + GetDiskFreeSpaceExW( + wide.as_ptr(), + &mut free_to_caller, + std::ptr::null_mut(), + std::ptr::null_mut(), + ) + }; + if ok == 0 { + return Err(CatalogError::Io(std::io::Error::last_os_error())); + } + Ok(free_to_caller) +} + +#[cfg(not(any(unix, windows)))] fn filesystem_available_bytes(_path: &Path) -> Result { Err(CatalogError::InvalidInput( "filesystem free-space preflight is unsupported on this platform".to_string(),