Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Keep on This Computer for the iCloud Drive folder

Yashwanth Kamireddib0b2cbc6a67b
diff --git a/.uiverify/drive-keep.mjs b/.uiverify/drive-keep.mjs
new file mode 100644
index 0000000..0429ac2
--- /dev/null
+++ b/.uiverify/drive-keep.mjs
@@ -0,0 +1,40 @@
+// Keep on This Computer, as Finder's Keep Downloaded: in a file's menu only while the iCloud
+// Drive folder is up (that is where a kept file lives); a check beside it when kept; chosen,
+// it is kept at once, and chosen again let go of.
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub } from "./stub.mjs";
+const b = await engine.launch();
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+for (const on of [false, true]) {
+ const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });
+ await ctx.addInitScript((on) => { window.__driveFolder = { on, path: "/home/test/iCloud Drive", problem: null }; }, on);
+ await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null, driveFixture: true });
+ const page = await ctx.newPage();
+ page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });
+ await page.goto("http://localhost:4173", { waitUntil: "networkidle" });
+ await page.locator(".nav-item", { hasText: "iCloud Drive" }).click();
+ await page.waitForTimeout(500);
+ const row = page.locator(".drive-row-hit").filter({ hasNot: page.locator("[data-kind='FOLDER']") }).last();
+ const name = (await row.innerText()).split("\n")[0];
+ await row.click({ button: "right" });
+ const item = page.getByRole("menuitemcheckbox", { name: "Keep on This Computer" }).or(page.getByRole("menuitem", { name: "Keep on This Computer" }));
+ if (!on) {
+ ok((await item.count()) === 0, "with the Drive folder off, no Keep on This Computer (there is nowhere to keep it)");
+ await page.keyboard.press("Escape");
+ } else {
+ ok((await item.count()) === 1, `with it on, "${name}" can be kept`);
+ ok((await item.getAttribute("aria-checked")) !== "true", "not kept to begin with");
+ await item.click();
+ await page.waitForTimeout(150);
+ ok((await page.evaluate(() => window.__kept?.length)) === 1, "chosen, it is kept");
+ await row.click({ button: "right" });
+ ok((await item.getAttribute("aria-checked")) === "true", "and the menu checks it");
+ await item.click();
+ await page.waitForTimeout(150);
+ ok((await page.evaluate(() => window.__kept?.length)) === 0, "chosen again, it is let go of");
+ }
+ await ctx.close();
+}
+await b.close();
+process.exitCode = failures ? 1 : 0;
diff --git a/.uiverify/stub.mjs b/.uiverify/stub.mjs
index eb201ad..64a9ef3 100644
--- a/.uiverify/stub.mjs
+++ b/.uiverify/stub.mjs
@@ -631,6 +631,11 @@ export function installStub({ screen, services, twoFactor, calendarFixture, note
case "auth_send_sms": return null;
case "auth_submit_sms_code": return null;
case "auth_sign_out": sessionStorage.setItem("__signedOut", JSON.stringify(args)); return null;
+ case "drive_kept": return window.__driveFolder?.on ? [...(window.__kept ??= [])] : null;
+ case "drive_keep":
+ if (!window.__driveFolder?.on) throw "Turn on the iCloud Drive folder in Settings to keep files on this computer.";
+ window.__kept = (window.__kept ??= []).filter((k) => k !== args.drivewsid).concat(args.on ? [args.drivewsid] : []);
+ return [...window.__kept];
case "login_item_status": return { on: false, command: "\"/usr/bin/quince-app\" --background", honoured: true, ...(window.__login ?? {}) };
case "login_item_set":
if (window.__loginRefuse) throw window.__loginRefuse;
diff --git a/crates/quince-vfs/src/fuse.rs b/crates/quince-vfs/src/fuse.rs
index f432762..e0f7422 100644
--- a/crates/quince-vfs/src/fuse.rs
+++ b/crates/quince-vfs/src/fuse.rs
@@ -12,7 +12,7 @@ use fuser::{
BsdFileFlags, Config, Errno, FileAttr, FileHandle, FileType, Filesystem, FopenFlags,
Generation, INodeNo, LockOwner, MountOption, OpenAccMode, OpenFlags, RenameFlags, ReplyAttr,
ReplyCreate, ReplyData, ReplyDirectory, ReplyEmpty, ReplyEntry, ReplyOpen, ReplyStatfs,
- ReplyWrite, Request, TimeOrNow, WriteFlags,
+ ReplyWrite, ReplyXattr, Request, TimeOrNow, WriteFlags,
};
use quince_core::drive::DriveItem;
use std::ffi::OsStr;
@@ -60,6 +60,9 @@ struct Inner {
filled: Mutex<std::collections::HashSet<String>>,
/// Files written here and not yet at Apple as written. `None`: the folder is read-only.
writes: Option<Arc<Writes>>,
+ /// A walk of what is kept is under way; asked again meanwhile, it walks once more after.
+ keeping: std::sync::atomic::AtomicBool,
+ again: std::sync::atomic::AtomicBool,
/// When each folder was last asked of Apple, by id.
looked: Mutex<std::collections::HashMap<String, std::time::Instant>>,
/// The account's storage as last asked, `(used, total)`.
@@ -73,6 +76,35 @@ struct Inner {
pub struct DriveFs(Arc<Inner>);
+/// The attribute that keeps a file or folder on this computer: `setfattr -n user.quince.keep
+/// -v 1 <path>` keeps it, `-x` lets it go — what the window's "Keep on This Computer" does.
+const KEEP_ATTR: &str = "user.quince.keep";
+
+/// What is kept on this computer, for the app (the window's "Keep on This Computer").
+#[derive(Clone)]
+pub struct Keeper(Arc<Inner>);
+
+impl Keeper {
+ /// A file or folder kept on this computer, or let go of: kept, its bytes (every file in it,
+ /// for a folder, and any added later) are fetched behind and never trimmed.
+ pub fn set(&self, id: &str, on: bool) {
+ Inner::keep(&self.0, id, on);
+ }
+
+ /// The files and folders chosen to be kept.
+ #[must_use]
+ pub fn chosen(&self) -> Vec<String> {
+ self.0.cache.kept().chosen.into_iter().collect()
+ }
+
+ /// Everything kept: what was chosen, and every file inside a chosen folder.
+ #[must_use]
+ pub fn kept(&self) -> Vec<String> {
+ let k = self.0.cache.kept();
+ k.chosen.into_iter().chain(k.within).collect()
+ }
+}
+
impl DriveFs {
#[must_use]
pub fn new(
@@ -97,6 +129,8 @@ impl DriveFs {
writes: None,
space: Mutex::new(None),
looked: Mutex::new(std::collections::HashMap::new()),
+ keeping: std::sync::atomic::AtomicBool::new(false),
+ again: std::sync::atomic::AtomicBool::new(false),
read_only: false,
add_only: false,
}))
@@ -114,6 +148,12 @@ impl DriveFs {
self
}
+ /// What is kept on this computer, for the app to change and show.
+ #[must_use]
+ pub fn keeper(&self) -> Keeper {
+ Keeper(Arc::clone(&self.0))
+ }
+
/// What is written here and not yet at Apple, for the app to show (before mounting).
#[must_use]
pub fn writes(&self) -> Option<Arc<Writes>> {
@@ -244,6 +284,135 @@ impl Inner {
Ok(done)
}
+ /// Whether an item is kept on this computer: chosen, inside a chosen folder (as far up as
+ /// its folders are known), or noted as inside one by the last walk.
+ fn kept_under(&self, item: &DriveItem) -> bool {
+ if self.cache.is_kept(&item.drivewsid) {
+ return true;
+ }
+ let chosen = self.cache.kept().chosen;
+ let inodes = self
+ .inodes
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let mut parent = item.parent_id.clone();
+ for _ in 0..64 {
+ let Some(p) = parent else { break };
+ if chosen.contains(&p) {
+ return true;
+ }
+ parent = inodes
+ .ino_of(&p)
+ .and_then(|i| inodes.item(i))
+ .and_then(|it| it.parent_id.clone());
+ }
+ false
+ }
+
+ fn keep(me: &Arc<Self>, id: &str, on: bool) {
+ me.cache.set_kept(|k| {
+ if on {
+ k.chosen.insert(id.to_string());
+ } else {
+ k.chosen.remove(id);
+ }
+ });
+ Self::settle_kept(me);
+ }
+
+ /// A folder's items for a walk: as stored, or asked of Apple when never listed.
+ fn items_of(&self, folder: &str) -> Vec<DriveItem> {
+ if let Some(stored) = self.store.children(folder).filter(|s| !s.is_empty()) {
+ return stored;
+ }
+ let (remote, id) = (Arc::clone(&self.remote), folder.to_string());
+ self.rt
+ .block_on(async move { tokio::time::timeout(LIST_WAIT, remote.list(&id)).await })
+ .ok()
+ .and_then(Result::ok)
+ .unwrap_or_default()
+ }
+
+ fn walk(&self, folder: &str, files: &mut Vec<DriveItem>, depth: u32) {
+ if depth > 64 {
+ return;
+ }
+ for item in self.items_of(folder) {
+ if item.is_container() {
+ self.walk(&item.drivewsid, files, depth + 1);
+ } else {
+ files.push(item);
+ }
+ }
+ }
+
+ /// Everything kept made whole, behind everything else: each chosen folder walked for the
+ /// files in it (noted, so no trim lets them go), and every kept file's missing parts
+ /// fetched. One walk at a time; asked again meanwhile, it walks once more after. Run when
+ /// the folder comes up, when what is kept changes, and after a kept folder is looked at
+ /// (a file added on another device is kept too).
+ fn settle_kept(me: &Arc<Self>) {
+ use std::sync::atomic::Ordering;
+ if me.cache.kept().chosen.is_empty() {
+ return;
+ }
+ if me.keeping.swap(true, Ordering::SeqCst) {
+ me.again.store(true, Ordering::SeqCst);
+ return;
+ }
+ let me = Arc::clone(me);
+ std::thread::spawn(move || loop {
+ me.again.store(false, Ordering::SeqCst);
+ let chosen = me.cache.kept().chosen;
+ // Files inside a chosen folder (noted as such), and files chosen themselves (kept by
+ // being chosen, never noted as inside: let go of, they must not stay kept).
+ let mut files = Vec::new();
+ let mut alone = Vec::new();
+ let mut unknown = false;
+ for id in &chosen {
+ if id.starts_with("FOLDER::") {
+ me.walk(id, &mut files, 0);
+ } else {
+ let found = {
+ let inodes = me
+ .inodes
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ inodes.ino_of(id).and_then(|i| inodes.item(i)).cloned()
+ };
+ match found {
+ Some(item) => alone.push(item),
+ // Not seen yet this run: fetched once its folder is looked at.
+ None => unknown = true,
+ }
+ }
+ }
+ let within: std::collections::BTreeSet<String> =
+ files.iter().map(|f| f.drivewsid.clone()).collect();
+ me.cache.set_kept(|k| {
+ if unknown {
+ k.within.extend(within);
+ } else {
+ k.within = within;
+ }
+ });
+ for file in files.into_iter().chain(alone) {
+ if me.cache.whole(&file) {
+ continue;
+ }
+ let (cache, remote) = (Arc::clone(&me.cache), Arc::clone(&me.remote));
+ let _ = me
+ .rt
+ .block_on(async move { cache.fetch_whole(remote.as_ref(), &file).await });
+ }
+ me.keeping.store(false, Ordering::SeqCst);
+ if !(me.again.swap(false, Ordering::SeqCst) && !me.keeping.swap(true, Ordering::SeqCst))
+ {
+ break;
+ }
+ });
+ }
+
/// Whether a folder was asked of Apple within `FRESH`; if not, it is marked asked now (so
/// many looks at once ask once).
fn stale(&self, folder: &str) -> bool {
@@ -264,15 +433,30 @@ impl Inner {
/// A folder looked at: shown from what is stored, and asked of Apple behind that when its
/// listing is older than `FRESH` — a file saved on the phone is there on the next look.
- fn revalidate(&self, folder: &str) {
- if self.stale(folder) {
- let (remote, id) = (Arc::clone(&self.remote), folder.to_string());
- self.rt.spawn(async move {
+ fn revalidate(me: &Arc<Self>, folder: &str) {
+ if me.stale(folder) {
+ let (remote, id) = (Arc::clone(&me.remote), folder.to_string());
+ // A kept folder walked again after: what was added on another device is kept too.
+ let kept = me.cache.kept().chosen.contains(folder)
+ || me.item_by_id(folder).is_some_and(|f| me.kept_under(&f));
+ let again = Arc::clone(me);
+ me.rt.spawn(async move {
let _ = tokio::time::timeout(LIST_WAIT, remote.list(&id)).await;
+ if kept {
+ Self::settle_kept(&again);
+ }
});
}
}
+ fn item_by_id(&self, id: &str) -> Option<DriveItem> {
+ let inodes = self
+ .inodes
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ inodes.ino_of(id).and_then(|i| inodes.item(i)).cloned()
+ }
+
fn relist(&self, folder: &str) {
let (remote, id) = (Arc::clone(&self.remote), folder.to_string());
let _ = self
@@ -875,7 +1059,7 @@ impl Filesystem for DriveFs {
// and Apple's first answer can take seconds.
if matches!(found, Err(Errno::ENOENT)) {
if let Some(folder) = inner.item(parent.0) {
- inner.revalidate(&folder.drivewsid);
+ Inner::revalidate(inner, &folder.drivewsid);
}
}
match found {
@@ -960,7 +1144,7 @@ impl Filesystem for DriveFs {
if offset == 0 {
if let Some(folder) = inner.item(ino.0) {
inner.fill_thumbnails(&folder, &children);
- inner.revalidate(&folder.drivewsid);
+ Inner::revalidate(inner, &folder.drivewsid);
}
}
let parent = inner
@@ -1370,6 +1554,87 @@ impl Filesystem for DriveFs {
}
}
+ /// `user.quince.keep`: "1" on a file or folder kept on this computer (chosen, or inside a
+ /// chosen folder); no such attribute otherwise. The only attribute these folders keep.
+ fn getxattr(&self, _req: &Request, ino: INodeNo, name: &OsStr, size: u32, reply: ReplyXattr) {
+ let inner = &self.0;
+ let Some(item) = inner.item(ino.0) else {
+ return reply.error(Errno::ENOENT);
+ };
+ if name != OsStr::new(KEEP_ATTR) || !inner.kept_under(&item) {
+ return reply.error(Errno::NO_XATTR);
+ }
+ match size {
+ 0 => reply.size(1),
+ n if n < 1 => reply.error(Errno::ERANGE),
+ _ => reply.data(b"1"),
+ }
+ }
+
+ fn listxattr(&self, _req: &Request, ino: INodeNo, size: u32, reply: ReplyXattr) {
+ let inner = &self.0;
+ let Some(item) = inner.item(ino.0) else {
+ return reply.error(Errno::ENOENT);
+ };
+ let names: &[u8] = if inner.kept_under(&item) {
+ b"user.quince.keep\0"
+ } else {
+ b""
+ };
+ let len = u32::try_from(names.len()).unwrap_or(u32::MAX);
+ if size == 0 {
+ reply.size(len)
+ } else if size < len {
+ reply.error(Errno::ERANGE)
+ } else {
+ reply.data(names)
+ }
+ }
+
+ /// Kept on this computer (any value but "0"), or let go of ("0"), as the window's Keep on
+ /// This Computer does.
+ fn setxattr(
+ &self,
+ _req: &Request,
+ ino: INodeNo,
+ name: &OsStr,
+ value: &[u8],
+ _flags: i32,
+ _position: u32,
+ reply: ReplyEmpty,
+ ) {
+ let inner = &self.0;
+ if name != OsStr::new(KEEP_ATTR) {
+ return reply.error(Errno::ENOTSUP);
+ }
+ // Only what the folder can fetch is kept: a file still being written here is already
+ // on this computer, and the Photos folder reads from the app's own photo cache.
+ if inner.add_only || inner.read_only || inner.writes.is_none() {
+ return reply.error(Errno::ENOTSUP);
+ }
+ let Some(item) = inner.item(ino.0) else {
+ return reply.error(Errno::ENOENT);
+ };
+ if item.drivewsid.starts_with("LOCAL::") {
+ return reply.error(Errno::ENOTSUP);
+ }
+ Inner::keep(inner, &item.drivewsid, value != b"0");
+ reply.ok();
+ }
+
+ fn removexattr(&self, _req: &Request, ino: INodeNo, name: &OsStr, reply: ReplyEmpty) {
+ let inner = &self.0;
+ let Some(item) = inner.item(ino.0) else {
+ return reply.error(Errno::ENOENT);
+ };
+ // Only what was chosen itself can be let go of; a file kept by its folder stays kept.
+ if name != OsStr::new(KEEP_ATTR) || !inner.cache.kept().chosen.contains(&item.drivewsid) {
+ return reply.error(Errno::NO_XATTR);
+ }
+ Inner::keep(inner, &item.drivewsid, false);
+ reply.ok();
+ }
+
fn statfs(&self, _req: &Request, _ino: INodeNo, reply: ReplyStatfs) {
let inner = &self.0;
// The account's own storage, asked at most once a minute: a file manager checks there
@@ -1436,5 +1701,10 @@ pub fn mount(fs: DriveFs, at: &Path) -> std::io::Result<fuser::BackgroundSession
MountOption::DefaultPermissions,
];
config.n_threads = Some(4);
- fuser::spawn_mount(fs, at, &config)
+ let inner = Arc::clone(&fs.0);
+ let session = fuser::spawn_mount(fs, at, &config)?;
+ // What was kept here before is made whole again, behind (a file may have changed on
+ // another device since).
+ Inner::settle_kept(&inner);
+ Ok(session)
}
diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rs
index 9d59eef..a28235f 100644
--- a/crates/quince-vfs/src/lib.rs
+++ b/crates/quince-vfs/src/lib.rs
@@ -262,6 +262,30 @@ pub struct Cache {
budget: u64,
/// Bytes fetched since it was last counted, to know when to count again.
since: std::sync::atomic::AtomicU64,
+ /// Kept on this computer: never let go of to make room. See [`Kept`].
+ kept: Mutex<Kept>,
+}
+
+/// What is kept on this computer ("Keep on This Computer", as Finder's Keep Downloaded): the
+/// files and folders the person chose, and every file inside a chosen folder (worked out by
+/// the folder, as it lists them). Written beside the bytes, so a trim at launch — before any
+/// folder is listed — already keeps them.
+#[derive(Default, serde::Serialize, serde::Deserialize)]
+pub struct Kept {
+ /// Chosen: files and folders, by Apple's id.
+ pub chosen: std::collections::BTreeSet<String>,
+ /// Files inside a chosen folder, by id.
+ pub within: std::collections::BTreeSet<String>,
+}
+
+/// The cache's own file of what is kept: a dot file, never taken for a cached file.
+const KEPT_FILE: &str = ".kept.json";
+
+/// An id as the cache names its files.
+fn safe_id(s: &str) -> String {
+ s.chars()
+ .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
+ .collect()
}
/// A file read in the last few minutes is never let go of: it may well be open.
@@ -278,9 +302,69 @@ impl Cache {
fetching: Mutex::new(HashMap::new()),
budget: u64::MAX,
since: std::sync::atomic::AtomicU64::new(0),
+ kept: Mutex::new(
+ std::fs::read(dir.join(KEPT_FILE))
+ .ok()
+ .and_then(|b| serde_json::from_slice(&b).ok())
+ .unwrap_or_default(),
+ ),
})
}
+ /// What is kept on this computer, as it stands.
+ #[must_use]
+ pub fn kept(&self) -> Kept {
+ let k = self
+ .kept
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ Kept {
+ chosen: k.chosen.clone(),
+ within: k.within.clone(),
+ }
+ }
+
+ /// What is kept changed, and written down whole (or not at all).
+ pub fn set_kept(&self, change: impl FnOnce(&mut Kept)) {
+ let mut k = self
+ .kept
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ change(&mut k);
+ if let Ok(json) = serde_json::to_vec(&*k) {
+ let part = self.dir.join(".kept.json.part");
+ if std::fs::write(&part, json).is_ok() {
+ let _ = std::fs::rename(&part, self.dir.join(KEPT_FILE));
+ }
+ }
+ }
+
+ /// Whether a file's bytes are kept: chosen, or inside a chosen folder.
+ #[must_use]
+ pub fn is_kept(&self, id: &str) -> bool {
+ let k = self
+ .kept
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ k.chosen.contains(id) || k.within.contains(id)
+ }
+
+ /// Every byte of a file fetched now, a chunk at a time, what is there already skipped:
+ /// for a file kept on this computer.
+ ///
+ /// # Errors
+ /// When Apple cannot be reached, or the disk refuses.
+ pub async fn fetch_whole(&self, remote: &dyn Remote, item: &DriveItem) -> Result<(), String> {
+ let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);
+ let mut at = 0;
+ while at < size {
+ let len = CHUNK.min(size - at);
+ self.read(remote, item, at, len).await?;
+ at += len;
+ }
+ Ok(())
+ }
+
/// The most disk the cache may use. Past it, the files read longest ago are let go of —
/// to fetch again if they are read again — down to four fifths of it (rclone's
/// vfs-cache-max-size, least recently used first).
@@ -297,6 +381,7 @@ impl Cache {
std::fs::read_dir(&self.dir)
.map(|d| {
d.flatten()
+ .filter(|e| !e.file_name().to_string_lossy().starts_with('.'))
.filter_map(|e| e.metadata().ok())
.map(|m| m.blocks() * 512)
.sum()
@@ -313,6 +398,17 @@ impl Cache {
return 0;
}
let target = self.budget / 5 * 4;
+ let held: std::collections::HashSet<String> = {
+ let k = self
+ .kept
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ k.chosen
+ .iter()
+ .chain(&k.within)
+ .map(|i| safe_id(i))
+ .collect()
+ };
let mut files: Vec<(std::time::SystemTime, PathBuf, u64)> = std::fs::read_dir(&self.dir)
.map(|d| {
d.flatten()
@@ -321,6 +417,12 @@ impl Cache {
.extension()
.is_none_or(|x| x != "have" && x != "part")
})
+ // Its own files, and what is kept on this computer, are never let go of.
+ .filter(|e| {
+ let name = e.file_name().to_string_lossy().into_owned();
+ let id = name.split('~').next().unwrap_or_default().to_string();
+ !name.starts_with('.') && !held.contains(&id)
+ })
.filter_map(|e| {
let m = e.metadata().ok()?;
let have = std::fs::metadata(e.path().with_extension("have"))
@@ -352,15 +454,10 @@ impl Cache {
}
fn path(&self, item: &DriveItem) -> PathBuf {
- let safe = |s: &str| -> String {
- s.chars()
- .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
- .collect()
- };
self.dir.join(format!(
"{}~{}",
- safe(&item.drivewsid),
- safe(item.etag.as_deref().unwrap_or("-"))
+ safe_id(&item.drivewsid),
+ safe_id(item.etag.as_deref().unwrap_or("-"))
))
}
diff --git a/crates/quince-vfs/tests/keep.rs b/crates/quince-vfs/tests/keep.rs
new file mode 100644
index 0000000..31ffb02
--- /dev/null
+++ b/crates/quince-vfs/tests/keep.rs
@@ -0,0 +1,194 @@
+//! "Keep on This Computer", through a real FUSE mount of a made-up iCloud Drive, as any program
+//! asks for it: the `user.quince.keep` attribute set on a file fetches it whole, behind; set on
+//! a folder it keeps every file inside; a trim never lets a kept file go; taken off, it can go
+//! again. Skipped where FUSE is not available.
+#![cfg(target_os = "linux")]
+
+use quince_core::drive::DriveItem;
+use quince_vfs::{fuse, Cache, Fut, Remote, Store, CHUNK, ROOT};
+use std::collections::HashMap;
+use std::ffi::CString;
+use std::os::unix::ffi::OsStrExt;
+use std::path::Path;
+use std::sync::{Arc, Mutex};
+use std::time::{Duration, Instant};
+
+fn file(id: &str, parent: &str, name: &str, size: usize) -> DriveItem {
+ DriveItem {
+ drivewsid: id.into(),
+ parent_id: Some(parent.into()),
+ name: name.into(),
+ extension: Some("bin".into()),
+ kind: "FILE".into(),
+ size: Some(size as i64),
+ child_count: None,
+ created_at: Some(1_700_000_000),
+ modified_at: Some(1_700_000_100),
+ etag: Some("e1".into()),
+ }
+}
+
+struct Apple {
+ folders: HashMap<String, Vec<DriveItem>>,
+ files: HashMap<String, Vec<u8>>,
+ reads: Mutex<Vec<String>>,
+}
+impl Remote for Apple {
+ fn list(&self, folder: &str) -> Fut<Vec<DriveItem>> {
+ let out = self.folders.get(folder).cloned().unwrap_or_default();
+ Box::pin(async move { Ok(out) })
+ }
+ fn read(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Vec<u8>> {
+ self.reads.lock().unwrap().push(item.drivewsid.clone());
+ let data = &self.files[&item.drivewsid];
+ let out = data[offset as usize..(offset + len) as usize].to_vec();
+ Box::pin(async move { Ok(out) })
+ }
+}
+struct Library(HashMap<String, Vec<DriveItem>>);
+impl Store for Library {
+ fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {
+ self.0.get(folder).cloned()
+ }
+}
+
+fn c(p: &Path) -> CString {
+ CString::new(p.as_os_str().as_bytes()).unwrap()
+}
+fn set(p: &Path, v: &[u8]) -> i32 {
+ let name = CString::new("user.quince.keep").unwrap();
+ // SAFETY: valid C strings and a buffer of the given length.
+ unsafe { libc::setxattr(c(p).as_ptr(), name.as_ptr(), v.as_ptr().cast(), v.len(), 0) }
+}
+fn get(p: &Path) -> Option<Vec<u8>> {
+ let name = CString::new("user.quince.keep").unwrap();
+ let mut buf = [0u8; 8];
+ // SAFETY: as above; the buffer's length is passed.
+ let n = unsafe {
+ libc::getxattr(
+ c(p).as_ptr(),
+ name.as_ptr(),
+ buf.as_mut_ptr().cast(),
+ buf.len(),
+ )
+ };
+ (n >= 0).then(|| buf[..n as usize].to_vec())
+}
+fn remove(p: &Path) -> i32 {
+ let name = CString::new("user.quince.keep").unwrap();
+ // SAFETY: valid C strings.
+ unsafe { libc::removexattr(c(p).as_ptr(), name.as_ptr()) }
+}
+fn until(what: impl Fn() -> bool) -> bool {
+ let t = Instant::now();
+ while t.elapsed() < Duration::from_secs(10) {
+ if what() {
+ return true;
+ }
+ std::thread::sleep(Duration::from_millis(50));
+ }
+ false
+}
+
+#[test]
+fn kept_on_this_computer_stays_whole() {
+ if !Path::new("/dev/fuse").exists() {
+ eprintln!("no FUSE here; skipped");
+ return;
+ }
+ let big: Vec<u8> = (0..(CHUNK * 2 + 77)).map(|i| (i % 251) as u8).collect();
+ let small = vec![7u8; 1000];
+ let mut trips = file("FOLDER::z::trips", ROOT, "Trips", 0);
+ trips.kind = "FOLDER".into();
+ trips.extension = None;
+ trips.child_count = Some(1);
+ let film = file("FILE::z::film", ROOT, "Film", big.len());
+ let map = file("FILE::z::map", "FOLDER::z::trips", "Map", small.len());
+ let apple = Arc::new(Apple {
+ folders: HashMap::from([("FOLDER::z::trips".to_string(), vec![map.clone()])]),
+ files: HashMap::from([
+ ("FILE::z::film".to_string(), big.clone()),
+ ("FILE::z::map".to_string(), small.clone()),
+ ]),
+ reads: Mutex::new(Vec::new()),
+ });
+ let library = Arc::new(Library(HashMap::from([
+ (ROOT.to_string(), vec![trips, film]),
+ ("FOLDER::z::trips".to_string(), vec![map]),
+ ])));
+ let rt = tokio::runtime::Runtime::new().unwrap();
+ let (cache_dir, writes, at) = (
+ tempfile::tempdir().unwrap(),
+ tempfile::tempdir().unwrap(),
+ tempfile::tempdir().unwrap(),
+ );
+ // A budget smaller than the film: a trim would let it go, were it not kept.
+ let cache = Arc::new(Cache::new(cache_dir.path()).unwrap().with_budget(CHUNK));
+ let fs = fuse::DriveFs::new(
+ apple.clone(),
+ library,
+ Arc::clone(&cache),
+ rt.handle().clone(),
+ )
+ .with_writes(writes.path())
+ .unwrap();
+ let session = fuse::mount(fs, at.path()).expect("mounted");
+ let (film_at, trips_at, map_at) = (
+ at.path().join("Film.bin"),
+ at.path().join("Trips"),
+ at.path().join("Trips/Map.bin"),
+ );
+ let _ = std::fs::read_dir(at.path()).unwrap().count();
+ let _ = std::fs::read_dir(&trips_at).unwrap().count();
+
+ assert_eq!(get(&film_at), None, "not kept to begin with");
+ assert_eq!(set(&film_at, b"1"), 0, "kept by its attribute");
+ assert_eq!(get(&film_at).as_deref(), Some(&b"1"[..]));
+ let whole = |id: &str| {
+ apple
+ .reads
+ .lock()
+ .unwrap()
+ .iter()
+ .filter(|r| *r == id)
+ .count()
+ };
+ assert!(
+ until(|| whole("FILE::z::film") == 3),
+ "fetched whole, behind, every chunk once"
+ );
+ assert_eq!(
+ std::fs::read(&film_at).unwrap(),
+ big,
+ "and reads back, with nothing fetched again"
+ );
+ assert_eq!(whole("FILE::z::film"), 3);
+ cache.evict();
+ std::thread::sleep(Duration::from_millis(100));
+ assert_eq!(std::fs::read(&film_at).unwrap(), big);
+ assert_eq!(
+ whole("FILE::z::film"),
+ 3,
+ "a trim over the budget never lets a kept file go"
+ );
+
+ assert_eq!(set(&trips_at, b"1"), 0, "a folder kept");
+ assert!(
+ until(|| whole("FILE::z::map") == 1),
+ "every file in it fetched"
+ );
+ assert_eq!(
+ get(&map_at).as_deref(),
+ Some(&b"1"[..]),
+ "and shows as kept itself"
+ );
+ assert_ne!(
+ remove(&map_at),
+ 0,
+ "a file kept by its folder is not let go of on its own"
+ );
+
+ assert_eq!(remove(&film_at), 0, "let go of");
+ assert_eq!(get(&film_at), None);
+ drop(session);
+}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index a46b4ac..74a83c8 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -4267,6 +4267,8 @@ fn main() {
auth_sign_out,
account_unsent,
login::login_item_status,
+ mount::drive_kept,
+ mount::drive_keep,
login::login_item_set,
account_services,
account_info,
diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rs
index e078e45..479a84f 100644
--- a/src-tauri/src/mount.rs
+++ b/src-tauri/src/mount.rs
@@ -27,6 +27,10 @@ static SESSIONS: Mutex<Vec<(Which, PathBuf, fuser::BackgroundSession)>> = Mutex:
/// What the iCloud Drive folder has written and not yet sent, while it is up.
static WRITES: Mutex<Option<Arc<quince_vfs::writes::Writes>>> = Mutex::new(None);
+/// What the iCloud Drive folder keeps on this computer, while it is up.
+#[cfg(target_os = "linux")]
+static KEEPER: Mutex<Option<quince_vfs::fuse::Keeper>> = Mutex::new(None);
+
struct AppRemote {
app: AppHandle,
links: Arc<Mutex<HashMap<String, (String, Instant)>>>,
@@ -388,6 +392,9 @@ fn start(app: &AppHandle, which: Which, at: &std::path::Path) -> Result<(), Stri
*WRITES
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = fs.writes();
+ *KEEPER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(fs.keeper());
}
let session = quince_vfs::fuse::mount(fs, at)
.map_err(|e| format!("The folder could not be mounted: {e}"))?;
@@ -412,6 +419,9 @@ fn stop(which: Which) {
*WRITES
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = None;
+ *KEEPER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = None;
}
for (_, at, session) in gone {
drop(session);
@@ -613,3 +623,41 @@ pub async fn photos_folder_set(on: bool, app: AppHandle) -> Result<FolderStatus,
pub async fn photos_folder_open(app: AppHandle) -> Result<(), String> {
open(&app, Which::Photos)
}
+
+/// What the iCloud Drive folder keeps on this computer (Keep on This Computer): the files and
+/// folders chosen, and the files inside a chosen folder. `None` while the folder is off —
+/// there is nothing to keep downloaded without it.
+#[tauri::command]
+pub fn drive_kept() -> Option<Vec<String>> {
+ #[cfg(target_os = "linux")]
+ {
+ KEEPER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .as_ref()
+ .map(quince_vfs::fuse::Keeper::kept)
+ }
+ #[cfg(not(target_os = "linux"))]
+ None
+}
+
+/// A file or folder kept on this computer — fetched whole behind, never trimmed — or let go
+/// of, as Finder's Keep Downloaded. Answers what is kept now.
+#[tauri::command]
+pub fn drive_keep(drivewsid: String, on: bool) -> Result<Vec<String>, String> {
+ #[cfg(target_os = "linux")]
+ {
+ let keeper = KEEPER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .clone()
+ .ok_or("Turn on the iCloud Drive folder in Settings to keep files on this computer.")?;
+ keeper.set(&drivewsid, on);
+ Ok(keeper.kept())
+ }
+ #[cfg(not(target_os = "linux"))]
+ {
+ let _ = (drivewsid, on);
+ Err("Keeping files on this computer needs the iCloud Drive folder.".into())
+ }
+}
diff --git a/src/lib/ipc.ts b/src/lib/ipc.ts
index a777269..f522de9 100644
--- a/src/lib/ipc.ts
+++ b/src/lib/ipc.ts
@@ -104,6 +104,9 @@ export const api = {
driveFolderOpen: () => invoke<void>("drive_folder_open"),
/** The iCloud Photos folder on this computer: the same, for photos (read-only for now). */
photosFolderStatus: () => invoke<DriveFolder>("photos_folder_status"),
+ /** What the iCloud Drive folder keeps on this computer; null while the folder is off. */
+ driveKept: () => invoke<string[] | null>("drive_kept"),
+ driveKeep: (drivewsid: string, on: boolean) => invoke<string[]>("drive_keep", { drivewsid, on }),
photosFolderSet: (on: boolean) => invoke<DriveFolder>("photos_folder_set", { on }),
/** Open at Login: whether it is on, the command it runs, and whether this desktop runs it. */
loginItem: () => invoke<LoginItem>("login_item_status"),
diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsx
index cd557d1..a2e43b5 100644
--- a/src/screens/DriveView.tsx
+++ b/src/screens/DriveView.tsx
@@ -784,6 +784,27 @@ export function DriveView() {
* seconds later — so a press seemed to do nothing, and then a folder appeared from nowhere.
*/
const [pending, setPending] = useState<{ key: string; folder: string; name: string; doing: string }[]>([]);
+ // What is kept on this computer — null while the iCloud Drive folder is off.
+ const [kept, setKept] = useState<Set<string> | null>(null);
+ useEffect(() => {
+ api.driveKept().then((k) => setKept(k ? new Set(k) : null), () => setKept(null));
+ }, [here.id]);
+ /** Kept or let go of at once on screen; put back with the reason if it cannot be. */
+ const keep = async (item: DriveItem, on: boolean) => {
+ const was = kept;
+ setKept((k) => {
+ const next = new Set(k ?? []);
+ if (on) next.add(item.drivewsid);
+ else next.delete(item.drivewsid);
+ return next;
+ });
+ try {
+ setKept(new Set(await api.driveKeep(item.drivewsid, on)));
+ } catch (e) {
+ setKept(was);
+ setError(messageOf(e));
+ }
+ };
const settle = (key: string) => setPending((p) => p.filter((x) => !x.key.startsWith(key)));
const writingRef = useRef(false);
writingRef.current = busyGuid !== null || renaming !== null || pending.length > 0;
@@ -1319,6 +1340,18 @@ export function DriveView() {
at={menu}
onClose={() => setMenu(null)}
entries={[
+ // Keep on This Computer, as Finder's Keep Downloaded: only where the iCloud Drive
+ // folder is up, which is where a kept file lives.
+ ...(kept
+ ? [
+ {
+ label: "Keep on This Computer",
+ checked: kept.has(menu.item.drivewsid),
+ run: () => void keep(menu.item, !kept.has(menu.item.drivewsid)),
+ },
+ null,
+ ]
+ : []),
{ label: isContainer(menu.item) ? "Open" : "Download a Copy", run: () => (isContainer(menu.item) ? enter(menu.item) : void download(menu.item)) },
{ label: "Get Info", run: () => setInfo(menu.item) },
null,