From 04fae7ca7fa09e3f8166b1df7f3688f36ee45072 Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 6 Nov 2026 14:04:16 +0000 Subject: [PATCH 1/2] fix(desktop): use macOS Available space for card ingest statvfs only counted empty APFS blocks, so ingest refused ~86 GB copies while System Settings still showed ~97 GB Available (purgeable). Read ImportantUsage capacity instead. --- apps/desktop/src-tauri/build.rs | 6 ++ apps/desktop/src-tauri/src/card.rs | 86 ++++++++++++++++++++++++- apps/desktop/src-tauri/src/volume_mac.m | 45 +++++++++++++ 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 apps/desktop/src-tauri/src/volume_mac.m diff --git a/apps/desktop/src-tauri/build.rs b/apps/desktop/src-tauri/build.rs index 196adc2..1ba8b13 100644 --- a/apps/desktop/src-tauri/build.rs +++ b/apps/desktop/src-tauri/build.rs @@ -6,10 +6,16 @@ fn main() { .file("src/camera_mac.m") .flag("-fobjc-arc") .compile("camera_mac"); + // APFS Available (ImportantUsage) for card ingest free-space checks. + cc::Build::new() + .file("src/volume_mac.m") + .flag("-fobjc-arc") + .compile("volume_mac"); println!("cargo:rustc-link-lib=framework=Foundation"); println!("cargo:rustc-link-lib=framework=ImageCaptureCore"); println!("cargo:rustc-link-lib=framework=IOKit"); println!("cargo:rerun-if-changed=src/camera_mac.m"); + println!("cargo:rerun-if-changed=src/volume_mac.m"); } // The keychain, local FTP and USB camera commands get ACL permissions, so the web app loaded from the server // (a remote origin) can be granted them explicitly in capabilities/default.json. diff --git a/apps/desktop/src-tauri/src/card.rs b/apps/desktop/src-tauri/src/card.rs index d93c8a4..711c9ed 100644 --- a/apps/desktop/src-tauri/src/card.rs +++ b/apps/desktop/src-tauri/src/card.rs @@ -300,6 +300,7 @@ pub struct CardScan { #[serde(skip_serializing_if = "Option::is_none")] pub free_bytes: Option, /// Whether the photos (and sidecars) fit in that free space, with a gigabyte left over. + /// Free space on macOS is System Settings Available (purgeable included). #[serde(skip_serializing_if = "Option::is_none")] pub fits: Option, } @@ -448,9 +449,9 @@ fn report(app: Option<&AppHandle>, progress: &Progress) { } } -/// Bytes still free for a normal user on the volume that holds `path` (the nearest existing parent). +/// Nearest existing parent of `path` (or `.` when empty). #[cfg(unix)] -fn volume_free(path: &Path) -> Option { +fn existing_parent(path: &Path) -> Option { let mut cursor = if path.as_os_str().is_empty() { PathBuf::from(".") } else { @@ -458,12 +459,19 @@ fn volume_free(path: &Path) -> Option { }; loop { if cursor.exists() { - break; + return Some(cursor); } if !cursor.pop() { return None; } } +} + +/// `statvfs` free blocks. On APFS this is only the space already empty — not purgeable caches — +/// so it is often far below System Settings "Available". +#[cfg(unix)] +fn volume_free_statvfs(path: &Path) -> Option { + let cursor = existing_parent(path)?; let Ok(c) = std::ffi::CString::new(cursor.to_string_lossy().as_bytes()) else { return None; }; @@ -474,6 +482,29 @@ fn volume_free(path: &Path) -> Option { Some((stat.f_bavail as u64).saturating_mul(stat.f_frsize as u64)) } +/// Bytes free for a card copy on the volume that holds `path`. +/// On macOS this matches System Settings Available (ImportantUsage, including purgeable space). +#[cfg(target_os = "macos")] +fn volume_free(path: &Path) -> Option { + let cursor = existing_parent(path)?; + let Ok(c) = std::ffi::CString::new(cursor.to_string_lossy().as_bytes()) else { + return volume_free_statvfs(path); + }; + unsafe extern "C" { + fn pr_volume_free_important(path: *const std::ffi::c_char) -> i64; + } + let n = unsafe { pr_volume_free_important(c.as_ptr()) }; + if n >= 0 { + return Some(n as u64); + } + volume_free_statvfs(path) +} + +#[cfg(all(unix, not(target_os = "macos")))] +fn volume_free(path: &Path) -> Option { + volume_free_statvfs(path) +} + #[cfg(not(unix))] fn volume_free(_path: &Path) -> Option { None @@ -878,4 +909,53 @@ mod tests { assert!(!done.notes.is_empty()); let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn days_that_fit_takes_whole_days_until_room_runs_out() { + let days = vec![ + DayPlan { + key: "2026-11-03".into(), + items: Vec::new(), + write_len: 10, + }, + DayPlan { + key: "2026-11-04".into(), + items: Vec::new(), + write_len: 50, + }, + DayPlan { + key: "2026-11-05".into(), + items: Vec::new(), + write_len: 5, + }, + ]; + let (take, held) = days_that_fit(days, Some(55)); + assert_eq!(take.len(), 1); + assert_eq!(take[0].key, "2026-11-03"); + assert_eq!(held, vec!["2026-11-04".to_string(), "2026-11-05".to_string()]); + let (all, none) = days_that_fit( + vec![DayPlan { + key: "2026-11-04".into(), + items: Vec::new(), + write_len: 85_600_000_000, + }], + Some(97_000_000_000), + ); + assert_eq!(all.len(), 1); + assert!(none.is_empty()); + } + + /// macOS Available (ImportantUsage) is at least the raw `statvfs` free blocks. When APFS has + /// purgeable space, ImportantUsage is larger — that is what System Settings shows as Available. + #[cfg(target_os = "macos")] + #[test] + fn macos_important_usage_is_at_least_statvfs_free() { + let path = dirs::home_dir().unwrap_or_else(|| PathBuf::from("/")); + let important = volume_free(&path).expect("ImportantUsage"); + let blocks = volume_free_statvfs(&path).expect("statvfs"); + assert!( + important >= blocks, + "important={important} statvfs={blocks}" + ); + } } diff --git a/apps/desktop/src-tauri/src/volume_mac.m b/apps/desktop/src-tauri/src/volume_mac.m new file mode 100644 index 0000000..09f4def --- /dev/null +++ b/apps/desktop/src-tauri/src/volume_mac.m @@ -0,0 +1,45 @@ +// Free space the photographer can actually use for a card copy. On APFS, System Settings +// "Available" includes purgeable space (caches, local iCloud) that `statvfs` does not count. +// `NSURLVolumeAvailableCapacityForImportantUsageKey` is the figure macOS uses for important +// user-initiated writes: the OS will free that room when the copy needs it. + +#import + +/// Bytes available for an important copy on the volume that holds `path`. +/// Walks up to an existing parent. Returns -1 when the path or key cannot be read. +int64_t pr_volume_free_important(const char *path) { + if (!path || path[0] == '\0') { + return -1; + } + @autoreleasepool { + NSString *s = [[NSString alloc] initWithUTF8String:path]; + if (!s) { + return -1; + } + NSURL *url = [NSURL fileURLWithPath:s]; + while (url.path.length > 0) { + Boolean reachable = [url checkResourceIsReachableAndReturnError:nil]; + if (reachable) { + break; + } + NSURL *parent = [url URLByDeletingLastPathComponent]; + if ([parent.path isEqualToString:url.path]) { + return -1; + } + url = parent; + } + NSNumber *n = nil; + NSError *err = nil; + BOOL ok = [url getResourceValue:&n + forKey:NSURLVolumeAvailableCapacityForImportantUsageKey + error:&err]; + if (!ok || n == nil) { + return -1; + } + long long v = n.longLongValue; + if (v < 0) { + return -1; + } + return (int64_t)v; + } +} -- 2.47.3 From ccffa858772432889c2766bc557b6df386272d46 Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 6 Nov 2026 14:06:13 +0000 Subject: [PATCH 2/2] chore(desktop): 0.3.28 for card ingest free-space fix --- apps/desktop/package.json | 2 +- apps/desktop/src-tauri/Cargo.toml | 2 +- apps/desktop/src-tauri/tauri.conf.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/desktop/package.json b/apps/desktop/package.json index dc1c753..506c4b4 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -1,6 +1,6 @@ { "name": "@photoreport/desktop", - "version": "0.3.27", + "version": "0.3.28", "private": true, "type": "module", "scripts": { diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 3f2cedb..3a3e44c 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "photoreport-desktop" -version = "0.3.27" +version = "0.3.28" edition = "2024" publish = false description = "PhotoReport desktop: the web app in a native shell, with keychain sign-in and (later) local FTP and the native engine." diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json index d821d69..ad40402 100644 --- a/apps/desktop/src-tauri/tauri.conf.json +++ b/apps/desktop/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "PhotoReport", - "version": "0.3.27", + "version": "0.3.28", "identifier": "pt.photoreport.desktop", "build": { "devUrl": "http://localhost:5173", -- 2.47.3