fix(desktop): use macOS Available space for card ingest #191
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@photoreport/desktop",
|
||||
"version": "0.3.27",
|
||||
"version": "0.3.28",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -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."
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -300,6 +300,7 @@ pub struct CardScan {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub free_bytes: Option<u64>,
|
||||
/// 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<bool>,
|
||||
}
|
||||
@@ -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<u64> {
|
||||
fn existing_parent(path: &Path) -> Option<PathBuf> {
|
||||
let mut cursor = if path.as_os_str().is_empty() {
|
||||
PathBuf::from(".")
|
||||
} else {
|
||||
@@ -458,12 +459,19 @@ fn volume_free(path: &Path) -> Option<u64> {
|
||||
};
|
||||
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<u64> {
|
||||
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<u64> {
|
||||
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<u64> {
|
||||
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<u64> {
|
||||
volume_free_statvfs(path)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn volume_free(_path: &Path) -> Option<u64> {
|
||||
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}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 <Foundation/Foundation.h>
|
||||
|
||||
/// 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;
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user