Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
iCloud Drive folder: files written, kept here first, sent behind
Yashwanth Kamireddie107f95ac601
diff --git a/Cargo.lock b/Cargo.lockindex 9145fa2..668419a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3758,6 +3758,8 @@ dependencies = ["md-5","quince-core","quince-media",+ "serde",+ "serde_json","tempfile","tokio",]diff --git a/crates/quince-vfs/Cargo.toml b/crates/quince-vfs/Cargo.tomlindex 871077c..1de981a 100644--- a/crates/quince-vfs/Cargo.toml+++ b/crates/quince-vfs/Cargo.toml@@ -11,6 +11,8 @@ tokio = { workspace = true, features = ["sync", "rt", "time"] }libc = "0.2"quince-media = { path = "../quince-media" }md-5 = "0.10"+serde = { workspace = true }+serde_json = { workspace = true }[target.'cfg(target_os = "linux")'.dependencies]fuser = { version = "0.18", default-features = false }diff --git a/crates/quince-vfs/src/fuse.rs b/crates/quince-vfs/src/fuse.rsindex 2bc21a2..b393d8a 100644--- a/crates/quince-vfs/src/fuse.rs+++ b/crates/quince-vfs/src/fuse.rs@@ -1,20 +1,25 @@//! The engine as a Linux folder, through FUSE (fuser).//!-//! Read-only for now. Names, sizes and dates come from the library, never the network, so a-//! file manager browsing is never kept waiting; a folder never opened before is fetched once.-//! A file's bytes come from the cache, each part fetched from Apple the first time it is read.+//! Names, sizes and dates come from the library, never the network, so a file manager+//! browsing is never kept waiting; a folder never opened before is fetched once. A file's+//! bytes come from the cache, each part fetched from Apple the first time it is read.+//! Folders, renames, moves and deletes are made at Apple as they happen. A file's own bytes+//! are written here first and sent behind the program writing them (see writes.rs).+use crate::writes::{self, Pending, Writes};use crate::{is_thumbnailer, named, Cache, Inodes, Remote, Store};use fuser::{- Config, Errno, FileAttr, FileHandle, FileType, Filesystem, FopenFlags, Generation, INodeNo,- LockOwner, MountOption, OpenAccMode, OpenFlags, RenameFlags, ReplyAttr, ReplyData,- ReplyDirectory, ReplyEmpty, ReplyEntry, ReplyOpen, ReplyStatfs, Request,+ 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,};use quince_core::drive::DriveItem;use std::ffi::OsStr;+use std::os::unix::fs::FileExt;use std::path::Path;use std::sync::{Arc, Mutex};-use std::time::{Duration, UNIX_EPOCH};+use std::time::{Duration, SystemTime, UNIX_EPOCH};/// How long the kernel may keep what it was told about a name or a file. Short, as rclone's/// is: a file changed on another device must not be read with its old length.@@ -27,7 +32,11 @@ const LIST_WAIT: Duration = Duration::from_secs(30);/// How long a change (a new folder, a rename, a delete) may take at Apple.const CHANGE_WAIT: Duration = Duration::from_secs(60);-pub struct DriveFs {+/// The longest wait between tries of a send Apple would not take.+const LONGEST_RETRY: Duration = Duration::from_secs(300);++/// What the folder's threads and its sends share.+struct Inner {remote: Arc<dyn Remote>,store: Arc<dyn Store>,cache: Arc<Cache>,@@ -35,13 +44,19 @@ pub struct DriveFs {rt: tokio::runtime::Handle,uid: u32,gid: u32,- mounted: std::time::SystemTime,+ mounted: SystemTime,/// Where the folder is mounted and the thumbnail cache's root, when thumbnails are wanted.thumbs: Option<(std::path::PathBuf, std::path::PathBuf)>,/// Folders whose thumbnails were written, by their items' versions: written once.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>>,+ /// The account's storage as last asked, `(used, total)`.+ space: Mutex<Option<((u64, u64), std::time::Instant)>>,}+pub struct DriveFs(Arc<Inner>);+impl DriveFs {#[must_use]pub fn new(@@ -52,28 +67,77 @@ impl DriveFs {) -> Self {// SAFETY: getuid and getgid cannot fail and touch no memory.let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) };- Self {+ Self(Arc::new(Inner {remote,store,cache,inodes: Mutex::new(Inodes::new()),- mounted: std::time::SystemTime::now(),- thumbs: None,- filled: Mutex::new(std::collections::HashSet::new()),rt,uid,gid,- }+ mounted: SystemTime::now(),+ thumbs: None,+ filled: Mutex::new(std::collections::HashSet::new()),+ writes: None,+ space: Mutex::new(None),+ }))+ }++ fn inner_mut(&mut self) -> &mut Inner {+ Arc::get_mut(&mut self.0).expect("configured before it is mounted")}/// Apple's pictures of each folder listed put in the thumbnail cache (the crate's/// write_thumbnails), for a folder mounted at `mount`.#[must_use]pub fn with_thumbnails(mut self, mount: std::path::PathBuf, cache: std::path::PathBuf) -> Self {- self.thumbs = Some((mount, cache));+ self.inner_mut().thumbs = Some((mount, cache));self}+ /// Files can be written, their bytes kept in `dir` until Apple has them. What was waiting+ /// when the folder last went away is picked up again and sent.+ ///+ /// # Errors+ /// When the folder for the bytes cannot be made.+ pub fn with_writes(mut self, dir: &Path) -> std::io::Result<Self> {+ let writes = Arc::new(Writes::new(dir)?);+ let waiting = writes.load();+ self.inner_mut().writes = Some(Arc::clone(&writes));+ for (old, pending) in waiting {+ let ino = self+ .0+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .local(&pending.folder, &pending.name);+ // Its bytes moved to the inode it has now.+ let from = writes.staging(old, &pending.name);+ let to = writes.staging(ino, &pending.name);+ if let Some(dir) = to.parent() {+ std::fs::create_dir_all(dir)?;+ }+ std::fs::rename(&from, &to)?;+ writes.drop_staging(old);+ writes+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(+ ino,+ Pending {+ dirty: true,+ ..pending+ },+ );+ Inner::schedule(&self.0, ino, Duration::from_millis(500));+ }+ writes.save();+ Ok(self)+ }+}++impl Inner {/// In the background, once per folder version: the folder's thumbnails written.fn fill_thumbnails(&self, folder: &DriveItem, files: &[(String, u64, DriveItem)]) {let Some((mount, cache)) = self.thumbs.clone() else {@@ -86,7 +150,10 @@ impl DriveFs {.inodes.lock().unwrap_or_else(std::sync::PoisonError::into_inner);- for (_, ino, item) in files.iter().filter(|(_, _, i)| !i.is_container()) {+ for (_, ino, item) in files+ .iter()+ .filter(|(_, _, i)| !i.is_container() && !i.drivewsid.starts_with("LOCAL::"))+ {if let Some(p) = inodes.path(*ino) {paths.push((p.to_string(), item.clone()));fingerprint.push_str(item.etag.as_deref().unwrap_or(""));@@ -132,31 +199,45 @@ impl DriveFs {Errno::EIO})?;for folder in touched {- let (remote, id) = (Arc::clone(&self.remote), folder.drivewsid.clone());- let _ = self- .rt- .block_on(async move { tokio::time::timeout(LIST_WAIT, remote.list(&id)).await });+ self.relist(&folder.drivewsid);}Ok(done)}- /// The item called `name` in folder `parent`.- fn child(&self, parent: INodeNo, name: &OsStr) -> Result<(u64, DriveItem), Errno> {- self.children(parent)?- .into_iter()- .find(|(n, ..)| OsStr::new(n) == name)- .map(|(_, ino, item)| (ino, item))- .ok_or(Errno::ENOENT)+ fn relist(&self, folder: &str) {+ let (remote, id) = (Arc::clone(&self.remote), folder.to_string());+ let _ = self+ .rt+ .block_on(async move { tokio::time::timeout(LIST_WAIT, remote.list(&id)).await });}- fn item(&self, ino: INodeNo) -> Option<DriveItem> {+ fn item(&self, ino: u64) -> Option<DriveItem> {self.inodes.lock().unwrap_or_else(std::sync::PoisonError::into_inner)- .item(ino.0)+ .item(ino)+ .cloned()+ }++ fn pending(&self, ino: u64) -> Option<Pending> {+ self.writes+ .as_ref()?+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .get(&ino).cloned()}+ fn with_pending<R>(&self, ino: u64, f: impl FnOnce(&mut Pending) -> R) -> Option<R> {+ let writes = self.writes.as_ref()?;+ let mut items = writes+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ items.get_mut(&ino).map(f)+ }+fn attr(&self, ino: u64, item: &DriveItem) -> FileAttr {// Apple lists no dates for a folder: it shows when the folder was mounted rather than// 1 January 1970, which a file manager sorts and labels as if it were real.@@ -167,25 +248,37 @@ impl DriveFs {})};let dir = item.is_container();- let size = if dir {- 0- } else {- u64::try_from(item.size.unwrap_or(0)).unwrap_or(0)- };+ let (mut size, mut mtime) = (+ if dir {+ 0+ } else {+ u64::try_from(item.size.unwrap_or(0)).unwrap_or(0)+ },+ secs(item.modified_at),+ );+ // A file being written shows what has been written, as of when.+ if let (Some(p), Some(w)) = (self.pending(ino), self.writes.as_ref()) {+ size = std::fs::metadata(w.staging(ino, &p.name)).map_or(size, |m| m.len());+ mtime = p.modified;+ }FileAttr {ino: INodeNo(ino),size,blocks: size.div_ceil(512),- atime: secs(item.modified_at),- mtime: secs(item.modified_at),- ctime: secs(item.modified_at),+ atime: mtime,+ mtime,+ ctime: mtime,crtime: secs(item.created_at.or(item.modified_at)),kind: if dir {FileType::Directory} else {FileType::RegularFile},- perm: if dir { 0o755 } else { 0o444 },+ perm: match (dir, self.writes.is_some()) {+ (true, _) => 0o755,+ (false, true) => 0o644,+ (false, false) => 0o444,+ },nlink: if dir { 2 } else { 1 },uid: self.uid,gid: self.gid,@@ -195,21 +288,21 @@ impl DriveFs {}}- /// A folder's items with their names and inodes: from the library, or — never opened- /// before — from Apple, once.- fn children(&self, ino: INodeNo) -> Result<Vec<(String, u64, DriveItem)>, Errno> {- let item = self.item(ino).ok_or(Errno::ENOENT)?;- if !item.is_container() {+ /// A folder's items with their names and inodes: from the library — or, never opened+ /// before, from Apple once — with the files being written here among them.+ fn children(&self, ino: u64) -> Result<Vec<(String, u64, DriveItem)>, Errno> {+ let folder = self.item(ino).ok_or(Errno::ENOENT)?;+ if !folder.is_container() {return Err(Errno::ENOTDIR);}- let stored = self.store.children(&item.drivewsid);+ let stored = self.store.children(&folder.drivewsid);// A folder Apple says has things in it, with nothing stored, was never listed.- let listed = stored.filter(|s| !s.is_empty() || item.child_count == Some(0));- let items = match listed {+ let listed = stored.filter(|s| !s.is_empty() || folder.child_count == Some(0));+ let mut items = match listed {Some(items) => items,None => {let remote = Arc::clone(&self.remote);- let id = item.drivewsid.clone();+ let id = folder.drivewsid.clone();self.rt.block_on(async move { tokio::time::timeout(LIST_WAIT, remote.list(&id)).await },@@ -218,52 +311,443 @@ impl DriveFs {.map_err(|_| Errno::EIO)?}};+ // Files being written here: a new one is shown beside Apple's; one taking another's+ // place is shown in it, under its own inode.+ let mut local: Vec<(u64, Pending)> = Vec::new();+ if let Some(w) = &self.writes {+ let pending = w+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ local = pending+ .iter()+ .filter(|(_, p)| p.folder == folder.drivewsid)+ .map(|(i, p)| (*i, p.clone()))+ .collect();+ }let mut inodes = self.inodes.lock().unwrap_or_else(std::sync::PoisonError::into_inner);- Ok(named(&items)+ for (pino, p) in &local {+ if let Some(r) = &p.replaces {+ items.retain(|i| i.drivewsid != r.drivewsid);+ }+ // Its own inode keeps the name it is being written under.+ if let Some(it) = inodes.item(*pino).cloned() {+ if it.drivewsid.starts_with("LOCAL::")+ || p.replaces+ .as_ref()+ .is_some_and(|r| r.drivewsid == it.drivewsid)+ {+ items.retain(|i| i.drivewsid != it.drivewsid);+ }+ }+ }+ let mut out: Vec<(String, u64, DriveItem)> = named(&items).into_iter().map(|(name, it)| {let child = inodes.ino(&it);- inodes.place(child, ino.0, &name);+ inodes.place(child, ino, &name);(name, child, it)})- .collect())+ .collect();+ for (pino, p) in local {+ let Some(it) = inodes.item(pino).cloned().or_else(|| p.replaces.clone()) else {+ continue;+ };+ inodes.place(pino, ino, &p.name);+ out.push((p.name.clone(), pino, it));+ }+ Ok(out)}-}-impl DriveFs {- /// A file or (an empty) folder deleted, into Apple's Recently Deleted.- fn remove(&self, parent: INodeNo, name: &OsStr, folder: bool, reply: ReplyEmpty) {- let Some(from) = self.item(parent) else {- return reply.error(Errno::ENOENT);- };- let (ino, item) = match self.child(parent, name) {- Ok(c) => c,- Err(e) => return reply.error(e),+ /// The item called `name` in folder `parent`.+ fn child(&self, parent: u64, name: &OsStr) -> Result<(u64, DriveItem), Errno> {+ self.children(parent)?+ .into_iter()+ .find(|(n, ..)| OsStr::new(n) == name)+ .map(|(_, ino, item)| (ino, item))+ .ok_or(Errno::ENOENT)+ }++ /// A file of Apple's made writable: its bytes copied here (unless they are about to be+ /// thrown away), to be sent back in its place.+ fn begin_writing(&self, ino: u64, item: &DriveItem, keep: bool) -> Result<(), Errno> {+ let writes = self.writes.as_ref().ok_or(Errno::EROFS)?;+ if self.pending(ino).is_some() {+ return Ok(());+ }+ let name = {+ let inodes = self+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ inodes+ .path(ino)+ .and_then(|p| p.rsplit('/').next())+ .map_or_else(|| item.display_name(), str::to_string)};+ let staged = writes.staging(ino, &name);+ std::fs::create_dir_all(staged.parent().ok_or(Errno::EIO)?).map_err(|_| Errno::EIO)?;+ let file = std::fs::File::create(&staged).map_err(|_| Errno::EIO)?;+ if keep {+ let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);+ let (cache, remote, it) = (+ Arc::clone(&self.cache),+ Arc::clone(&self.remote),+ item.clone(),+ );+ let mut at = 0;+ while at < size {+ let len = crate::CHUNK.min(size - at);+ let (c, r, i) = (Arc::clone(&cache), Arc::clone(&remote), it.clone());+ let bytes = self+ .rt+ .block_on(async move { c.read(r.as_ref(), &i, at, len).await })+ .map_err(|_| Errno::EIO)?;+ file.write_all_at(&bytes, at).map_err(|_| Errno::EIO)?;+ at += len;+ }+ }+ writes+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(+ ino,+ Pending {+ folder: item+ .parent_id+ .clone()+ .unwrap_or_else(|| crate::ROOT.to_string()),+ name,+ replaces: Some(item.clone()),+ open: 0,+ dirty: !keep,+ generation: 0,+ modified: SystemTime::now(),+ sender: false,+ quiet_since: None,+ },+ );+ Ok(())+ }++ /// Sent to Apple once left alone a moment (writes::SETTLE), and tried again until Apple+ /// takes it. Only the bytes that were sent are counted as sent.+ fn schedule(me: &Arc<Self>, ino: u64, after: Duration) {+ // One sender per file: a second close, or a read-back, never starts a second send of+ // the same bytes. Claimed under the same lock that counts who has it open.+ let claimed = me+ .with_pending(ino, |p| {+ p.quiet_since = Some(std::time::Instant::now());+ if p.sender {+ false+ } else {+ p.sender = true;+ true+ }+ })+ .unwrap_or(false);+ if !claimed {+ return;+ }+ let me = Arc::clone(me);+ let rt = me.rt.clone();+ rt.spawn(async move {+ tokio::time::sleep(after).await;+ let mut wait = Duration::from_secs(5);+ loop {+ // Left alone long enough? Open again, or nothing to send: the sender stands+ // down, and the next close claims it again.+ loop {+ let state = me.with_pending(ino, |p| {+ if p.open > 0 || !p.dirty || writes::stays_local(&p.name) {+ p.sender = false;+ Some(false)+ } else if p.quiet_since.is_some_and(|q| q.elapsed() < writes::SETTLE) {+ None+ } else {+ Some(true)+ }+ });+ match state {+ None | Some(Some(false)) => return,+ Some(Some(true)) => break,+ Some(None) => tokio::time::sleep(Duration::from_millis(250)).await,+ }+ }+ let Some(p) = me.pending(ino) else { return };+ let generation = p.generation;+ let Some(writes) = me.writes.clone() else {+ return;+ };+ let folder = {+ let inodes = me+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ inodes+ .ino_of(&p.folder)+ .and_then(|f| inodes.item(f).cloned())+ };+ let Some(folder) = folder else {+ me.with_pending(ino, |p| p.sender = false);+ return;+ };+ let key = format!("{}/{}", p.folder, p.name);+ // Behind any send of the same name still under way, so this one finds it+ // finished and replaces it as this computer's own.+ let lane = writes.lane(&key);+ let _turn = lane.lock().await;+ let ours = writes+ .sent+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .get(&key)+ .cloned();+ // Sent from a copy of the bytes as they are now: a write during the send+ // changes the file, never what is on its way.+ let out_dir = writes.staging(ino, &p.name).with_file_name(".out");+ let out = out_dir.join(&p.name);+ let copied = std::fs::create_dir_all(&out_dir).is_ok()+ && std::fs::copy(writes.staging(ino, &p.name), &out).is_ok();+ let result = if copied {+ let r = writes::send(+ me.remote.as_ref(),+ &folder,+ &out,+ p.replaces.as_ref(),+ ours.as_ref(),+ )+ .await;+ let _ = std::fs::remove_dir_all(&out_dir);+ r+ } else {+ Err("the bytes could not be read".into())+ };+ match result {+ Ok(sent) => {+ if !sent.conflicted {+ // This computer's own, so its next save replaces it as such.+ writes+ .sent+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(key, sent.item.clone());+ }+ let done = me+ .with_pending(ino, |now| {+ if now.generation == generation {+ true+ } else {+ // Written again while on its way: what Apple has now is+ // what the next send replaces.+ now.replaces = Some(sent.item.clone());+ false+ }+ })+ .unwrap_or(true);+ let _ = me.remote.list(&folder.drivewsid).await;+ if done {+ if let Some(w) = &me.writes {+ w.items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .remove(&ino);+ w.drop_staging(ino);+ w.save();+ }+ if !sent.conflicted {+ me.inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .bind(ino, &sent.item);+ }+ return;+ }+ }+ Err(e) => {+ quiet_note(&e);+ tokio::time::sleep(wait).await;+ wait = (wait * 2).min(LONGEST_RETRY);+ }+ }+ }+ });+ }++ /// A file or (an empty) folder deleted, into Apple's Recently Deleted. A file made here+ /// and never sent is simply let go of.+ fn remove(&self, parent: u64, name: &OsStr, folder: bool) -> Result<(), Errno> {+ let from = self.item(parent).ok_or(Errno::ENOENT)?;+ let (ino, item) = self.child(parent, name)?;if folder != item.is_container() {- return reply.error(if folder {+ return Err(if folder {Errno::ENOTDIR} else {Errno::EISDIR});}+ if let Some(p) = self.pending(ino) {+ if let Some(w) = &self.writes {+ w.items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .remove(&ino);+ w.drop_staging(ino);+ w.save();+ }+ return match p.replaces {+ Some(old) => self.change(self.remote.delete(&old), &[&from]),+ None => Ok(()),+ };+ }// A folder goes only when empty, as on any disk: Apple would take everything in it,// and a file manager or `rm -r` empties it first, file by file.- if folder- && !self- .children(INodeNo(ino))- .map(|c| c.is_empty())- .unwrap_or(false)- {- return reply.error(Errno::ENOTEMPTY);+ if folder && !self.children(ino).map(|c| c.is_empty()).unwrap_or(false) {+ return Err(Errno::ENOTEMPTY);}- match self.change(self.remote.delete(&item), &[&from]) {- Ok(()) => reply.ok(),- Err(e) => reply.error(e),+ self.change(self.remote.delete(&item), &[&from])+ }++ /// An item moved and renamed at Apple: into `to`, then given `newname`.+ fn move_and_rename(+ &self,+ item: &DriveItem,+ from: &DriveItem,+ to: &DriveItem,+ name: &OsStr,+ newname: &str,+ ) -> Result<(), Errno> {+ if from.drivewsid != to.drivewsid {+ self.change(self.remote.move_to(item, to), &[from, to])?;}+ if name != OsStr::new(newname) {+ let moved = self+ .store+ .children(&to.drivewsid)+ .and_then(|c| c.into_iter().find(|i| i.drivewsid == item.drivewsid))+ .unwrap_or_else(|| item.clone());+ self.change(self.remote.rename(&moved, newname), &[to])?;+ }+ Ok(())+ }++ fn rename(+ me: &Arc<Self>,+ parent: u64,+ name: &OsStr,+ newparent: u64,+ newname: &OsStr,+ ) -> Result<(), Errno> {+ let (from, to) = (+ me.item(parent).ok_or(Errno::ENOENT)?,+ me.item(newparent).ok_or(Errno::ENOENT)?,+ );+ let (ino, item) = me.child(parent, name)?;+ let newname = newname.to_str().ok_or(Errno::EINVAL)?;+ let target = me.child(newparent, OsStr::new(newname)).ok();+ if let Some((tino, titem)) = &target {+ if *tino == ino {+ return Ok(());+ }+ if titem.is_container() || item.is_container() {+ return Err(Errno::EEXIST);+ }+ }+ let pending = me.pending(ino);+ match (pending, target) {+ // A file written here taking another's name — an editor's save: what it replaces+ // (Apple's file, or another being written) goes, and Apple's into Recently Deleted+ // only once the new bytes are there (writes::send).+ (Some(p), Some((tino, titem))) => {+ let theirs = me.pending(tino);+ if let Some(w) = &me.writes {+ w.items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .remove(&tino);+ w.drop_staging(tino);+ }+ let replaced = theirs.map_or(Some(titem), |t| t.replaces);+ // The file this one was copied from, if it was Apple's and is left behind by+ // the rename, goes as a rename would take it.+ if let Some(old) = p+ .replaces+ .as_ref()+ .filter(|o| replaced.as_ref().is_none_or(|r| r.drivewsid != o.drivewsid))+ {+ me.change(me.remote.delete(old), &[&from])?;+ }+ me.restage(ino, &p.name, newname)?;+ me.with_pending(ino, |now| {+ now.folder = to.drivewsid.clone();+ now.name = newname.to_string();+ now.replaces = replaced;+ now.dirty = true;+ now.generation += 1;+ });+ }+ // Apple's file taking another's name: the other into Recently Deleted first.+ (None, Some((tino, titem))) => {+ match me.pending(tino) {+ Some(t) => {+ if let Some(w) = &me.writes {+ w.items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .remove(&tino);+ w.drop_staging(tino);+ }+ if let Some(old) = t.replaces {+ me.change(me.remote.delete(&old), &[&to])?;+ }+ }+ None => me.change(me.remote.delete(&titem), &[&to])?,+ }+ me.move_and_rename(&item, &from, &to, name, newname)?;+ }+ // A file written here, to a free name: Apple's copy (if any) is moved and renamed+ // there, and what is waiting follows it.+ (Some(p), None) => {+ if let Some(old) = &p.replaces {+ me.move_and_rename(old, &from, &to, name, newname)?;+ let now = me+ .store+ .children(&to.drivewsid)+ .and_then(|c| c.into_iter().find(|i| i.drivewsid == old.drivewsid));+ me.with_pending(ino, |w| {+ if let Some(n) = now {+ // Its version moved with the rename; not a change made elsewhere.+ w.replaces = Some(n);+ }+ });+ }+ me.restage(ino, &p.name, newname)?;+ me.with_pending(ino, |now| {+ now.folder = to.drivewsid.clone();+ now.name = newname.to_string();+ });+ }+ (None, None) => me.move_and_rename(&item, &from, &to, name, newname)?,+ }+ if let Some(w) = &me.writes {+ w.save();+ }+ if me.pending(ino).is_some_and(|p| p.open == 0 && p.dirty) {+ Self::schedule(me, ino, writes::SETTLE);+ }+ Ok(())+ }++ fn restage(&self, ino: u64, from: &str, to: &str) -> Result<(), Errno> {+ self.writes+ .as_ref()+ .ok_or(Errno::EROFS)?+ .rename_staging(ino, from, to)+ .map_err(|_| Errno::EIO)}}@@ -279,22 +763,69 @@ fn quiet_note(e: &str) {impl Filesystem for DriveFs {fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {- match self.children(parent) {- Ok(children) => match children.into_iter().find(|(n, ..)| OsStr::new(n) == name) {- Some((_, ino, item)) => reply.entry(&TTL, &self.attr(ino, &item), Generation(0)),- None => reply.error(Errno::ENOENT),- },+ match self.0.child(parent.0, name) {+ Ok((ino, item)) => reply.entry(&TTL, &self.0.attr(ino, &item), Generation(0)),Err(e) => reply.error(e),}}fn getattr(&self, _req: &Request, ino: INodeNo, _fh: Option<FileHandle>, reply: ReplyAttr) {- match self.item(ino) {- Some(item) => reply.attr(&TTL, &self.attr(ino.0, &item)),+ match self.0.item(ino.0) {+ Some(item) => reply.attr(&TTL, &self.0.attr(ino.0, &item)),None => reply.error(Errno::ENOENT),}}+ fn setattr(+ &self,+ _req: &Request,+ ino: INodeNo,+ _mode: Option<u32>,+ _uid: Option<u32>,+ _gid: Option<u32>,+ size: Option<u64>,+ _atime: Option<TimeOrNow>,+ _mtime: Option<TimeOrNow>,+ _ctime: Option<SystemTime>,+ _fh: Option<FileHandle>,+ _crtime: Option<SystemTime>,+ _chgtime: Option<SystemTime>,+ _bkuptime: Option<SystemTime>,+ _flags: Option<BsdFileFlags>,+ reply: ReplyAttr,+ ) {+ let inner = &self.0;+ let Some(item) = inner.item(ino.0) else {+ return reply.error(Errno::ENOENT);+ };+ // Only a file's length is Apple's to keep; modes and times are taken as they are.+ if let (Some(size), false) = (size, item.is_container()) {+ if let Err(e) = inner.begin_writing(ino.0, &item, size > 0) {+ return reply.error(e);+ }+ let (Some(w), Some(p)) = (&inner.writes, inner.pending(ino.0)) else {+ return reply.error(Errno::EROFS);+ };+ let ok = std::fs::OpenOptions::new()+ .write(true)+ .open(w.staging(ino.0, &p.name))+ .and_then(|f| f.set_len(size))+ .is_ok();+ if !ok {+ return reply.error(Errno::EIO);+ }+ inner.with_pending(ino.0, |p| {+ p.dirty = true;+ p.generation += 1;+ p.modified = SystemTime::now();+ });+ if inner.pending(ino.0).is_some_and(|p| p.open == 0) {+ Inner::schedule(inner, ino.0, writes::SETTLE);+ }+ }+ reply.attr(&TTL, &inner.attr(ino.0, &item));+ }+fn readdir(&self,_req: &Request,@@ -303,19 +834,20 @@ impl Filesystem for DriveFs {offset: u64,mut reply: ReplyDirectory,) {- let children = match self.children(ino) {+ let inner = &self.0;+ let children = match inner.children(ino.0) {Ok(c) => c,Err(e) => return reply.error(e),};if offset == 0 {- if let Some(folder) = self.item(ino) {- self.fill_thumbnails(&folder, &children);+ if let Some(folder) = inner.item(ino.0) {+ inner.fill_thumbnails(&folder, &children);}}- let parent = self- .item(ino)+ let parent = inner+ .item(ino.0).and_then(|i| i.parent_id)- .and_then(|p| self.inodes.lock().ok().and_then(|n| n.ino_of(&p)))+ .and_then(|p| inner.inodes.lock().ok().and_then(|n| n.ino_of(&p))).unwrap_or(1);let mut all: Vec<(u64, FileType, String)> = vec![(ino.0, FileType::Directory, ".".into()),@@ -345,25 +877,162 @@ impl Filesystem for DriveFs {}fn open(&self, req: &Request, ino: INodeNo, flags: OpenFlags, reply: ReplyOpen) {- let Some(item) = self.item(ino) else {+ let inner = &self.0;+ let Some(item) = inner.item(ino.0) else {return reply.error(Errno::ENOENT);};if item.is_container() {return reply.error(Errno::EISDIR);}- if flags.acc_mode() != OpenAccMode::O_RDONLY {+ let writing = flags.acc_mode() != OpenAccMode::O_RDONLY;+ let truncate = flags.0 & libc::O_TRUNC != 0;+ if writing && inner.writes.is_none() {return reply.error(Errno::EROFS);}- // A thumbnailer is not let download a file only to draw it small (see is_thumbnailer).- let pid = req.pid();- let comm = std::fs::read_to_string(format!("/proc/{pid}/comm")).unwrap_or_default();- let cmdline = std::fs::read(format!("/proc/{pid}/cmdline"))- .map(|b| String::from_utf8_lossy(&b).into_owned())- .unwrap_or_default();- if is_thumbnailer(&comm, &cmdline) && !self.cache.whole(&item) {- return reply.error(Errno::EACCES);+ if inner.pending(ino.0).is_none() {+ if writing {+ if let Err(e) = inner.begin_writing(ino.0, &item, !truncate) {+ return reply.error(e);+ }+ } else {+ // A thumbnailer is not let download a file only to draw it small.+ let pid = req.pid();+ let comm = std::fs::read_to_string(format!("/proc/{pid}/comm")).unwrap_or_default();+ let cmdline = std::fs::read(format!("/proc/{pid}/cmdline"))+ .map(|b| String::from_utf8_lossy(&b).into_owned())+ .unwrap_or_default();+ if is_thumbnailer(&comm, &cmdline) && !inner.cache.whole(&item) {+ return reply.error(Errno::EACCES);+ }+ return reply.opened(FileHandle(0), FopenFlags::empty());+ }+ }+ if truncate {+ if let (Some(w), Some(p)) = (&inner.writes, inner.pending(ino.0)) {+ let _ = std::fs::OpenOptions::new()+ .write(true)+ .open(w.staging(ino.0, &p.name))+ .and_then(|f| f.set_len(0));+ }}- reply.opened(FileHandle(0), FopenFlags::empty());+ inner.with_pending(ino.0, |p| {+ p.open += 1;+ if truncate {+ p.dirty = true;+ p.generation += 1;+ p.modified = SystemTime::now();+ }+ });+ // The kernel's page cache is not kept for a file being written: every read is ours.+ reply.opened(FileHandle(ino.0), FopenFlags::FOPEN_DIRECT_IO);+ }++ fn create(+ &self,+ req: &Request,+ parent: INodeNo,+ name: &OsStr,+ _mode: u32,+ _umask: u32,+ flags: i32,+ reply: ReplyCreate,+ ) {+ let inner = &self.0;+ let Some(writes) = inner.writes.clone() else {+ return reply.error(Errno::EROFS);+ };+ let Some(folder) = inner.item(parent.0) else {+ return reply.error(Errno::ENOENT);+ };+ let Some(name) = name.to_str() else {+ return reply.error(Errno::EINVAL);+ };+ if let Ok((ino, item)) = inner.child(parent.0, OsStr::new(name)) {+ if flags & libc::O_EXCL != 0 {+ return reply.error(Errno::EEXIST);+ }+ if item.is_container() {+ return reply.error(Errno::EISDIR);+ }+ let truncate = flags & libc::O_TRUNC != 0;+ if inner.pending(ino).is_none() {+ if let Err(e) = inner.begin_writing(ino, &item, !truncate) {+ return reply.error(e);+ }+ }+ if truncate {+ if let Some(p) = inner.pending(ino) {+ let _ = std::fs::OpenOptions::new()+ .write(true)+ .open(writes.staging(ino, &p.name))+ .and_then(|f| f.set_len(0));+ }+ }+ inner.with_pending(ino, |p| {+ p.open += 1;+ if truncate {+ p.dirty = true;+ p.generation += 1;+ }+ });+ let _ = req;+ return reply.created(+ &TTL,+ &inner.attr(ino, &item),+ Generation(0),+ FileHandle(ino),+ FopenFlags::FOPEN_DIRECT_IO,+ );+ }+ let ino = inner+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .local(&folder.drivewsid, name);+ let staged = writes.staging(ino, name);+ let made = staged+ .parent()+ .map(std::fs::create_dir_all)+ .transpose()+ .is_ok()+ && std::fs::File::create(&staged).is_ok();+ if !made {+ return reply.error(Errno::EIO);+ }+ writes+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(+ ino,+ Pending {+ folder: folder.drivewsid.clone(),+ name: name.to_string(),+ replaces: None,+ open: 1,+ dirty: true,+ generation: 1,+ modified: SystemTime::now(),+ sender: false,+ quiet_since: None,+ },+ );+ inner+ .inodes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .place(ino, parent.0, name);+ writes.save();+ let Some(item) = inner.item(ino) else {+ return reply.error(Errno::EIO);+ };+ reply.created(+ &TTL,+ &inner.attr(ino, &item),+ Generation(0),+ FileHandle(ino),+ FopenFlags::FOPEN_DIRECT_IO,+ );}fn read(@@ -377,12 +1046,23 @@ impl Filesystem for DriveFs {_lock: Option<LockOwner>,reply: ReplyData,) {- let Some(item) = self.item(ino) else {+ let inner = &self.0;+ // A file being written reads what has been written.+ if let (Some(w), Some(p)) = (&inner.writes, inner.pending(ino.0)) {+ let mut buf = vec![0u8; size as usize];+ return match std::fs::File::open(w.staging(ino.0, &p.name))+ .and_then(|f| f.read_at(&mut buf, offset))+ {+ Ok(n) => reply.data(&buf[..n]),+ Err(_) => reply.error(Errno::EIO),+ };+ }+ let Some(item) = inner.item(ino.0) else {return reply.error(Errno::ENOENT);};- let (cache, remote) = (Arc::clone(&self.cache), Arc::clone(&self.remote));+ let (cache, remote) = (Arc::clone(&inner.cache), Arc::clone(&inner.remote));// Answered when the bytes are here, without holding up the folder's other requests.- self.rt.spawn(async move {+ inner.rt.spawn(async move {match cache.read(remote.as_ref(), &item, offset, u64::from(size)).await@@ -393,6 +1073,87 @@ impl Filesystem for DriveFs {});}+ fn write(+ &self,+ _req: &Request,+ ino: INodeNo,+ _fh: FileHandle,+ offset: u64,+ data: &[u8],+ _write_flags: WriteFlags,+ _flags: OpenFlags,+ _lock: Option<LockOwner>,+ reply: ReplyWrite,+ ) {+ let inner = &self.0;+ let (Some(w), Some(p)) = (&inner.writes, inner.pending(ino.0)) else {+ return reply.error(Errno::EBADF);+ };+ let written = std::fs::OpenOptions::new()+ .write(true)+ .open(w.staging(ino.0, &p.name))+ .and_then(|f| f.write_all_at(data, offset));+ if written.is_err() {+ return reply.error(Errno::EIO);+ }+ inner.with_pending(ino.0, |p| {+ p.dirty = true;+ p.generation += 1;+ p.modified = SystemTime::now();+ });+ reply.written(u32::try_from(data.len()).unwrap_or(u32::MAX));+ }++ fn flush(+ &self,+ _req: &Request,+ _ino: INodeNo,+ _fh: FileHandle,+ _lock: LockOwner,+ reply: ReplyEmpty,+ ) {+ reply.ok();+ }++ fn fsync(+ &self,+ _req: &Request,+ ino: INodeNo,+ _fh: FileHandle,+ _datasync: bool,+ reply: ReplyEmpty,+ ) {+ // On this computer's disk is as far as a sync can promise; Apple's copy follows.+ if let (Some(w), Some(p)) = (&self.0.writes, self.0.pending(ino.0)) {+ let _ = std::fs::File::open(w.staging(ino.0, &p.name)).and_then(|f| f.sync_all());+ }+ reply.ok();+ }++ fn release(+ &self,+ _req: &Request,+ ino: INodeNo,+ _fh: FileHandle,+ _flags: OpenFlags,+ _lock: Option<LockOwner>,+ _flush: bool,+ reply: ReplyEmpty,+ ) {+ let inner = &self.0;+ let last = inner.with_pending(ino.0, |p| {+ p.open = p.open.saturating_sub(1);+ p.open == 0 && p.dirty+ });+ if last == Some(true) {+ if let Some(w) = &inner.writes {+ w.save();+ }+ Inner::schedule(inner, ino.0, writes::SETTLE);+ }+ reply.ok();+ }+fn mkdir(&self,_req: &Request,@@ -402,18 +1163,19 @@ impl Filesystem for DriveFs {_umask: u32,reply: ReplyEntry,) {- let Some(folder) = self.item(parent) else {+ let inner = &self.0;+ let Some(folder) = inner.item(parent.0) else {return reply.error(Errno::ENOENT);};let Some(name) = name.to_str() else {return reply.error(Errno::EINVAL);};- if self.child(parent, OsStr::new(name)).is_ok() {+ if inner.child(parent.0, OsStr::new(name)).is_ok() {return reply.error(Errno::EEXIST);}- match self.change(self.remote.mkdir(&folder, name), &[&folder]) {- Ok(_) => match self.child(parent, OsStr::new(name)) {- Ok((ino, item)) => reply.entry(&TTL, &self.attr(ino, &item), Generation(0)),+ match inner.change(inner.remote.mkdir(&folder, name), &[&folder]) {+ Ok(_) => match inner.child(parent.0, OsStr::new(name)) {+ Ok((ino, item)) => reply.entry(&TTL, &inner.attr(ino, &item), Generation(0)),Err(e) => reply.error(e),},Err(e) => reply.error(e),@@ -421,11 +1183,17 @@ impl Filesystem for DriveFs {}fn unlink(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {- self.remove(parent, name, false, reply);+ match self.0.remove(parent.0, name, false) {+ Ok(()) => reply.ok(),+ Err(e) => reply.error(e),+ }}fn rmdir(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {- self.remove(parent, name, true, reply);+ match self.0.remove(parent.0, name, true) {+ Ok(()) => reply.ok(),+ Err(e) => reply.error(e),+ }}fn rename(@@ -438,42 +1206,49 @@ impl Filesystem for DriveFs {_flags: RenameFlags,reply: ReplyEmpty,) {- let (Some(from), Some(to)) = (self.item(parent), self.item(newparent)) else {- return reply.error(Errno::ENOENT);- };- let (_, item) = match self.child(parent, name) {- Ok(c) => c,- Err(e) => return reply.error(e),- };- let Some(newname) = newname.to_str() else {- return reply.error(Errno::EINVAL);- };- // Over something already there is not done yet: replacing a file is a write, and- // nothing is ever overwritten before writes keep a copy of what they replace.- if self.child(newparent, OsStr::new(newname)).is_ok() {- return reply.error(Errno::EEXIST);- }- if parent != newparent {- if let Err(e) = self.change(self.remote.move_to(&item, &to), &[&from, &to]) {- return reply.error(e);- }- }- if name != OsStr::new(newname) {- // Moved, it is found again in its new folder under its old name, then renamed.- let moved = match self.child(newparent, name) {- Ok((_, i)) => i,- Err(e) => return reply.error(e),- };- if let Err(e) = self.change(self.remote.rename(&moved, newname), &[&to]) {- return reply.error(e);- }+ match Inner::rename(&self.0, parent.0, name, newparent.0, newname) {+ Ok(()) => reply.ok(),+ Err(e) => reply.error(e),}- reply.ok();}fn statfs(&self, _req: &Request, _ino: INodeNo, reply: ReplyStatfs) {- // Read-only: nothing free to write into, as a mounted disc says.- reply.statfs(0, 0, 0, 0, 0, 4096, 255, 4096);+ let inner = &self.0;+ // The account's own storage, asked at most once a minute: a file manager checks there+ // is room before copying in.+ let known = *inner+ .space+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let space = match known {+ Some((s, at)) if at.elapsed() < Duration::from_secs(60) => Some(s),+ _ => {+ let remote = Arc::clone(&inner.remote);+ let got = inner+ .rt+ .block_on(async move {+ tokio::time::timeout(Duration::from_secs(5), remote.space()).await+ })+ .ok()+ .and_then(Result::ok);+ if let Some(s) = got {+ *inner+ .space+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner) =+ Some((s, std::time::Instant::now()));+ }+ got.or(known.map(|(s, _)| s))+ }+ };+ let block = 4096u64;+ match (space, inner.writes.is_some()) {+ (Some((used, total)), true) => {+ let free = total.saturating_sub(used) / block;+ reply.statfs(total / block, free, free, 0, 0, 4096, 255, 4096);+ }+ _ => reply.statfs(0, 0, 0, 0, 0, 4096, 255, 4096),+ }}}diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rsindex 9c23865..4230e34 100644--- a/crates/quince-vfs/src/lib.rs+++ b/crates/quince-vfs/src/lib.rs@@ -8,6 +8,7 @@#[cfg(target_os = "linux")]pub mod fuse;+pub mod writes;use quince_core::drive::DriveItem;use std::collections::HashMap;@@ -37,10 +38,20 @@ pub trait Remote: Send + Sync + 'static {fn move_to(&self, _item: &DriveItem, _folder: &DriveItem) -> Fut<()> {Box::pin(async { Err("read-only".to_string()) })}+ /// A file sent into `folder`, named after the file at `path`; Apple names it "name 2"+ /// when the name is taken. Answers the file as Apple filed it.+ fn upload(&self, _folder: &DriveItem, _path: &Path) -> Fut<DriveItem> {+ Box::pin(async { Err("read-only".to_string()) })+ }/// An item deleted — into Apple's Recently Deleted, where it can be put back for 30 days.fn delete(&self, _item: &DriveItem) -> Fut<()> {Box::pin(async { Err("read-only".to_string()) })}+ /// The account's iCloud storage, `(used, total)` in bytes: a file manager checks there is+ /// room before it copies anything in.+ fn space(&self) -> Fut<(u64, u64)> {+ Box::pin(async { Err("unknown".to_string()) })+ }/// Apple's own pictures of a folder's files, by item id: for a file manager's thumbnails,/// so it never downloads a file to draw one. None by default.fn thumbnails(&self, _folder: &str) -> Fut<HashMap<String, Vec<u8>>> {@@ -126,6 +137,48 @@ impl Inodes {self.paths.insert(ino, at);}+ /// An inode for a file made on this computer and not yet at Apple: its own id+ /// ("LOCAL::<inode>") until Apple files it.+ pub fn local(&mut self, folder: &str, name: &str) -> u64 {+ let ino = self.next;+ self.next += 1;+ let (stem, ext) = match name.rsplit_once('.') {+ Some((s, e)) if !s.is_empty() => (s.to_string(), Some(e.to_string())),+ _ => (name.to_string(), None),+ };+ let item = DriveItem {+ drivewsid: format!("LOCAL::{ino}"),+ parent_id: Some(folder.to_string()),+ name: stem,+ extension: ext,+ kind: "FILE".into(),+ size: Some(0),+ child_count: None,+ created_at: None,+ modified_at: None,+ etag: None,+ };+ self.by_id.insert(item.drivewsid.clone(), ino);+ self.by_ino.insert(ino, item);+ ino+ }++ /// An inode given to the item Apple filed for it (a local file sent, or a file replaced):+ /// the same inode, so a program holding it open carries on.+ pub fn bind(&mut self, ino: u64, item: &DriveItem) {+ if let Some(old) = self.by_ino.get(&ino).map(|i| i.drivewsid.clone()) {+ if old != item.drivewsid {+ self.by_id.remove(&old);+ }+ }+ if let Some(other) = self.by_id.insert(item.drivewsid.clone(), ino) {+ if other != ino {+ self.by_ino.remove(&other);+ }+ }+ self.by_ino.insert(ino, item.clone());+ }+/// The inode an item already has, by Apple's id.#[must_use]pub fn ino_of(&self, id: &str) -> Option<u64> {diff --git a/crates/quince-vfs/src/writes.rs b/crates/quince-vfs/src/writes.rsnew file mode 100644index 0000000..1d63d98--- /dev/null+++ b/crates/quince-vfs/src/writes.rs@@ -0,0 +1,338 @@+//! A file's own bytes written through the folder: kept on this computer first, then sent to+//! Apple behind the app writing them.+//!+//! An app saving a file never waits on the network. What it writes goes into a private copy;+//! a few seconds after the last program closes it, the copy goes to Apple. Replacing a file+//! is done the safe way round — the new one sent, the old one moved into Recently Deleted+//! (where it can be put back for thirty days), the new one given its name — and a file+//! changed on another device meanwhile is never overwritten: this computer's version is kept+//! beside it as a conflicted copy. What is waiting to go is written down, so a restart, a+//! crash or an hour offline loses nothing: it goes when it can.++use crate::Remote;+use quince_core::drive::DriveItem;+use std::collections::HashMap;+use std::path::{Path, PathBuf};+use std::sync::Mutex;+use std::time::{Duration, SystemTime};++/// How long a file must be left alone after its last writer closes before it is sent: one+/// save is often several writes and closes, and a scratch file often lives a second.+pub const SETTLE: Duration = Duration::from_secs(2);++/// One file written through the folder and not yet at Apple as written.+#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]+pub struct Pending {+ /// The folder it goes into (Apple's id).+ pub folder: String,+ /// Its name, extension and all, as the folder shows it.+ pub name: String,+ /// The Apple file it will replace, as it was when this computer's copy was taken: its+ /// version tells whether it changed elsewhere since. `None` for a new file.+ pub replaces: Option<DriveItem>,+ /// Programs that have it open now (not kept across restarts).+ #[serde(skip)]+ pub open: u32,+ /// Written since it was last sent.+ pub dirty: bool,+ /// Bumped on every write: a send is only counted done for the bytes it sent.+ #[serde(skip)]+ pub generation: u64,+ pub modified: SystemTime,+ /// A send of it is waiting or under way (one at a time; not kept across restarts).+ #[serde(skip)]+ pub sender: bool,+ /// Since when nothing has touched it: it is sent once left alone (SETTLE).+ #[serde(skip)]+ pub quiet_since: Option<std::time::Instant>,+}++/// Names programs use for their own scratch files, which never leave this computer: Vim's+/// swap files and its "4913" probe, Emacs's locks and backups, LibreOffice's lock files,+/// GLib's atomic-save temporaries, and anything ending "~".+#[must_use]+pub fn stays_local(name: &str) -> bool {+ let lower = name.to_ascii_lowercase();+ name == "4913"+ || name.ends_with('~')+ || name.starts_with(".#")+ || name.starts_with(".~lock.")+ || name.starts_with(".goutputstream-")+ || [".swp", ".swo", ".swx", ".tmp", ".part", ".crdownload"]+ .iter()+ .any(|s| lower.ends_with(s))+ || (name.starts_with('#') && name.ends_with('#'))+}++/// A conflicted copy's name, as Dropbox and OneDrive name theirs: "Report (conflicted copy+/// 2026-10-20 18:40).docx" — beside the other device's version, never over it.+#[must_use]+pub fn conflicted(name: &str, when: SystemTime) -> String {+ let secs = when+ .duration_since(SystemTime::UNIX_EPOCH)+ .map_or(0, |d| d.as_secs());+ let (days, rest) = (secs / 86_400, secs % 86_400);+ // Days since 1970 as a calendar date (UTC), without a date crate for one label.+ let (y, m, d) = civil(i64::try_from(days).unwrap_or(0));+ let stamp = format!(+ "{y:04}-{m:02}-{d:02} {:02}.{:02}",+ rest / 3600,+ (rest % 3600) / 60+ );+ match name.rsplit_once('.') {+ Some((stem, ext)) if !stem.is_empty() => format!("{stem} (conflicted copy {stamp}).{ext}"),+ _ => format!("{name} (conflicted copy {stamp})"),+ }+}++/// Days since 1970-01-01 as (year, month, day): Howard Hinnant's civil_from_days.+fn civil(z: i64) -> (i64, u32, u32) {+ let z = z + 719_468;+ let era = z.div_euclid(146_097);+ let doe = z.rem_euclid(146_097);+ let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;+ let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);+ let mp = (5 * doy + 2) / 153;+ let d = u32::try_from(doy - (153 * mp + 2) / 5 + 1).unwrap_or(1);+ let m = u32::try_from(if mp < 10 { mp + 3 } else { mp - 9 }).unwrap_or(1);+ (yoe + era * 400 + i64::from(m <= 2), m, d)+}++/// What is waiting to go, by inode, with its bytes in a folder of its own on disk.+pub struct Writes {+ dir: PathBuf,+ pub(crate) items: Mutex<HashMap<u64, Pending>>,+ /// What this computer last sent under each name ("folder/name"): the next save of that+ /// name replaces it as its own, never mistaking it for a change made elsewhere.+ pub(crate) sent: Mutex<HashMap<String, DriveItem>>,+ /// One send at a time per name ("folder/name"): a replace is three steps at Apple (send,+ /// old into Recently Deleted, rename), and a second save of the same name arriving+ /// between them took the name and left the first as "name 2" — measured live.+ lanes: Mutex<HashMap<String, std::sync::Arc<tokio::sync::Mutex<()>>>>,+}++impl Writes {+ /// # Errors+ /// When the folder cannot be made.+ pub fn new(dir: &Path) -> std::io::Result<Self> {+ std::fs::create_dir_all(dir)?;+ Ok(Self {+ dir: dir.to_path_buf(),+ items: Mutex::new(HashMap::new()),+ sent: Mutex::new(HashMap::new()),+ lanes: Mutex::new(HashMap::new()),+ })+ }++ /// Where a pending file's bytes are: a folder of its own, the file named as it will be+ /// sent (Apple names an upload after the file).+ #[must_use]+ pub fn staging(&self, ino: u64, name: &str) -> PathBuf {+ self.dir.join(ino.to_string()).join(name)+ }++ /// The bytes moved when the file is renamed before it is sent.+ ///+ /// # Errors+ /// When the disk refuses.+ pub fn rename_staging(&self, ino: u64, from: &str, to: &str) -> std::io::Result<()> {+ if from == to {+ return Ok(());+ }+ std::fs::rename(self.staging(ino, from), self.staging(ino, to))+ }++ /// The bytes let go of, once sent or given up.+ pub fn drop_staging(&self, ino: u64) {+ let _ = std::fs::remove_dir_all(self.dir.join(ino.to_string()));+ }++ /// The lane sends under one name wait in, one after another.+ pub fn lane(&self, key: &str) -> std::sync::Arc<tokio::sync::Mutex<()>> {+ let mut lanes = self+ .lanes+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ std::sync::Arc::clone(lanes.entry(key.to_string()).or_default())+ }++ fn journal(&self) -> PathBuf {+ self.dir.join("pending.json")+ }++ /// What is waiting written down, whole or not at all, so a restart finds it.+ pub fn save(&self) {+ let items = self+ .items+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let keep: Vec<(u64, &Pending)> = items+ .iter()+ .filter(|(_, p)| p.dirty && !stays_local(&p.name))+ .map(|(i, p)| (*i, p))+ .collect();+ if let Ok(json) = serde_json::to_vec(&keep) {+ let part = self.journal().with_extension("json.part");+ if std::fs::write(&part, json).is_ok() {+ let _ = std::fs::rename(&part, self.journal());+ }+ }+ }++ /// What was waiting when the folder last went away, with where its bytes are.+ #[must_use]+ pub fn load(&self) -> Vec<(u64, Pending)> {+ std::fs::read(self.journal())+ .ok()+ .and_then(|b| serde_json::from_slice::<Vec<(u64, Pending)>>(&b).ok())+ .unwrap_or_default()+ .into_iter()+ .filter(|(ino, p)| self.staging(*ino, &p.name).exists())+ .collect()+ }+}++/// How a send ended: the file as Apple now has it, and whether it went as a conflicted copy.+pub struct Sent {+ pub item: DriveItem,+ pub conflicted: bool,+}++/// One pending file sent to Apple, the safe way round (see the module's notes). `folder` is+/// the folder's item; `staged` the bytes, named as the file is to be called; `base` the+/// version of Apple's file this computer's copy was taken from; `ours` what this computer+/// itself last sent under that name.+///+/// What it replaces is found when it is sent, not when it was opened: by the version it was+/// taken from, else by its name. It is replaced only if it is that version or this+/// computer's own last send; anything else is a change made elsewhere, and both are kept.+///+/// # Errors+/// When Apple cannot be reached or refuses; nothing is lost — the copy stays, and the send+/// is tried again.+pub async fn send(+ remote: &dyn Remote,+ folder: &DriveItem,+ staged: &Path,+ base: Option<&DriveItem>,+ ours: Option<&DriveItem>,+) -> Result<Sent, String> {+ let name = staged+ .file_name()+ .and_then(|n| n.to_str())+ .ok_or("no name")?+ .to_string();+ let now = remote.list(&folder.drivewsid).await?;+ let current = base+ .and_then(|b| now.iter().find(|i| i.drivewsid == b.drivewsid))+ .or_else(|| now.iter().find(|i| i.display_name() == name))+ .cloned();+ let same = |a: &DriveItem, b: &DriveItem| {+ a.drivewsid == b.drivewsid && a.etag == b.etag && a.size == b.size+ };+ let changed_elsewhere = current+ .as_ref()+ .is_some_and(|c| !(base.is_some_and(|b| same(b, c)) || ours.is_some_and(|o| same(o, c))));+ if changed_elsewhere {+ // Both kept: this computer's beside the other device's, never over it.+ let copy = staged.with_file_name(conflicted(&name, SystemTime::now()));+ std::fs::copy(staged, ©).map_err(|e| e.to_string())?;+ let sent = remote.upload(folder, ©).await;+ let _ = std::fs::remove_file(©);+ return Ok(Sent {+ item: sent?,+ conflicted: true,+ });+ }+ let new = remote.upload(folder, staged).await?;+ if let Some(old) = current {+ // The old one into Recently Deleted, then the new one given its name: if anything+ // fails between, both are still there to be found — nothing is ever lost.+ remote.delete(&old).await?;+ if new.display_name() != name {+ remote.rename(&new, &name).await?;+ }+ let after = remote.list(&folder.drivewsid).await?;+ let item = after+ .into_iter()+ .find(|i| i.drivewsid == new.drivewsid)+ .unwrap_or(new);+ return Ok(Sent {+ item,+ conflicted: false,+ });+ }+ Ok(Sent {+ item: new,+ conflicted: false,+ })+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn scratch_files_never_leave() {+ for n in [+ ".report.docx.swp",+ "4913",+ "notes.txt~",+ ".#notes.txt",+ ".~lock.Deck.odp#",+ ".goutputstream-ABC123",+ "x.crdownload",+ ] {+ assert!(stays_local(n), "{n}");+ }+ for n in ["Report.docx", "notes.txt", "photo.jpg", "a~b.txt"] {+ assert!(!stays_local(n), "{n}");+ }+ }++ #[test]+ fn a_conflicted_copy_is_named_beside() {+ let when = SystemTime::UNIX_EPOCH + Duration::from_secs(1_792_521_600); // 2026-10-20 18:40 UTC+ assert_eq!(+ conflicted("Report.docx", when),+ "Report (conflicted copy 2026-10-20 18.40).docx"+ );+ assert_eq!(+ conflicted("Makefile", when),+ "Makefile (conflicted copy 2026-10-20 18.40)"+ );+ }++ #[test]+ fn what_waits_survives_a_restart() {+ let dir = tempfile::tempdir().unwrap();+ let w = Writes::new(dir.path()).unwrap();+ let p = Pending {+ folder: "F".into(),+ name: "a.txt".into(),+ replaces: None,+ open: 1,+ dirty: true,+ generation: 3,+ modified: SystemTime::now(),+ sender: false,+ quiet_since: None,+ };+ std::fs::create_dir_all(w.staging(7, "a.txt").parent().unwrap()).unwrap();+ std::fs::write(w.staging(7, "a.txt"), b"hi").unwrap();+ w.items.lock().unwrap().insert(7, p.clone());+ w.items.lock().unwrap().insert(+ 8,+ Pending {+ name: ".a.txt.swp".into(),+ ..p.clone()+ },+ );+ w.save();+ let again = Writes::new(dir.path()).unwrap().load();+ assert_eq!(again.len(), 1, "the scratch file is not kept");+ assert_eq!(again[0].0, 7);+ assert_eq!(again[0].1.name, "a.txt");+ assert_eq!(again[0].1.open, 0, "nothing is open after a restart");+ }+}diff --git a/crates/quince-vfs/tests/changes.rs b/crates/quince-vfs/tests/changes.rsindex 4061da3..ce84eb9 100644--- a/crates/quince-vfs/tests/changes.rs+++ b/crates/quince-vfs/tests/changes.rs@@ -188,13 +188,12 @@ fn changes_through_the_folder_are_made_at_apple() {"moved back out under a new name");- let clash = std::fs::rename(root.join("Itinerary.pdf"), root.join("Notes.txt")).unwrap_err();- assert_eq!(- clash.raw_os_error(),- Some(libc::EEXIST),- "a rename never replaces what is there"- );- assert_eq!(names(root), ["Itinerary.pdf", "Notes.txt", "Trips"]);+ // A rename over a file that is there replaces it, as on any disk — the replaced one into+ // Apple's Recently Deleted, never gone.+ std::fs::rename(root.join("Itinerary.pdf"), root.join("Notes.txt")).unwrap();+ assert_eq!(names(root), ["Notes.txt", "Trips"]);+ assert_eq!(world.deleted.lock().unwrap().len(), 1);+ std::fs::rename(root.join("Notes.txt"), root.join("Itinerary.pdf")).unwrap();std::fs::rename(root.join("Itinerary.pdf"), root.join("Trips/Itinerary.pdf")).unwrap();let full = std::fs::remove_dir(root.join("Trips")).unwrap_err();@@ -205,21 +204,14 @@ fn changes_through_the_folder_are_made_at_apple() {);std::fs::remove_file(root.join("Trips/Itinerary.pdf")).unwrap();std::fs::remove_dir(root.join("Trips")).unwrap();- assert_eq!(- names(root),- ["Notes.txt"],+ assert!(+ names(root).is_empty(),"deleted, file then folder, as rm -r does");assert_eq!(world.deleted.lock().unwrap().len(),- 2,- "each a delete at Apple (into Recently Deleted)"- );- assert_eq!(- std::fs::remove_dir(root.join("Notes.txt"))- .unwrap_err()- .raw_os_error(),- Some(libc::ENOTDIR)+ 3,+ "each a delete at Apple (into Recently Deleted), the replaced file among them");drop(session);diff --git a/crates/quince-vfs/tests/writes.rs b/crates/quince-vfs/tests/writes.rsnew file mode 100644index 0000000..a84927c--- /dev/null+++ b/crates/quince-vfs/tests/writes.rs@@ -0,0 +1,409 @@+//! A file's own bytes written through the folder, as programs write them: a new file, an+//! append, an editor's save (a temporary file renamed over the old), a file changed on+//! another device meanwhile, a scratch file, and a restart before the send. Each checked+//! against what Apple ends up holding. Skipped where FUSE is not available.+#![cfg(target_os = "linux")]++use quince_core::drive::DriveItem;+use quince_vfs::{fuse, Cache, Fut, Remote, Store, ROOT};+use std::collections::HashMap;+use std::io::Write;+use std::path::Path;+use std::sync::{Arc, Mutex};+use std::time::Duration;++#[derive(Default)]+struct World {+ folders: Mutex<HashMap<String, Vec<DriveItem>>>,+ bytes: Mutex<HashMap<String, Vec<u8>>>,+ uploads: Mutex<Vec<String>>,+ /// How long each step at "Apple" takes: slow, as on a real connection.+ slow: Mutex<Duration>,+ deleted: Mutex<Vec<String>>,+ next: Mutex<u32>,+}++impl World {+ fn file(&self, folder: &str, name: &str, bytes: &[u8]) -> DriveItem {+ let mut n = self.next.lock().unwrap();+ *n += 1;+ let (stem, ext) = match name.rsplit_once('.') {+ Some((s, e)) if !s.is_empty() => (s.to_string(), Some(e.to_string())),+ _ => (name.to_string(), None),+ };+ let item = DriveItem {+ drivewsid: format!("FILE::z::{n}"),+ parent_id: Some(folder.into()),+ name: stem,+ extension: ext,+ kind: "FILE".into(),+ size: Some(bytes.len() as i64),+ child_count: None,+ created_at: Some(1_700_000_000),+ modified_at: Some(1_700_000_000 + i64::from(*n)),+ etag: Some(format!("v{n}")),+ };+ self.bytes+ .lock()+ .unwrap()+ .insert(item.drivewsid.clone(), bytes.to_vec());+ self.folders+ .lock()+ .unwrap()+ .entry(folder.into())+ .or_default()+ .push(item.clone());+ item+ }+ fn names(&self, folder: &str) -> Vec<String> {+ let mut n: Vec<String> = self+ .folders+ .lock()+ .unwrap()+ .get(folder)+ .cloned()+ .unwrap_or_default()+ .iter()+ .map(DriveItem::display_name)+ .collect();+ n.sort();+ n+ }+ fn bytes_of(&self, folder: &str, name: &str) -> Option<Vec<u8>> {+ let f = self.folders.lock().unwrap();+ let id = f+ .get(folder)?+ .iter()+ .find(|i| i.display_name() == name)?+ .drivewsid+ .clone();+ self.bytes.lock().unwrap().get(&id).cloned()+ }+}++struct Apple(Arc<World>);+struct Library(Arc<World>);+impl Store for Library {+ fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {+ Some(+ self.0+ .folders+ .lock()+ .unwrap()+ .get(folder)+ .cloned()+ .unwrap_or_default(),+ )+ }+}+impl Remote for Apple {+ fn list(&self, folder: &str) -> Fut<Vec<DriveItem>> {+ let out = self+ .0+ .folders+ .lock()+ .unwrap()+ .get(folder)+ .cloned()+ .unwrap_or_default();+ Box::pin(async move { Ok(out) })+ }+ fn read(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Vec<u8>> {+ let b = self.0.bytes.lock().unwrap()[&item.drivewsid]+ [offset as usize..(offset + len) as usize]+ .to_vec();+ Box::pin(async move { Ok(b) })+ }+ fn upload(&self, folder: &DriveItem, path: &Path) -> Fut<DriveItem> {+ let bytes = std::fs::read(path).unwrap();+ let mut name = path.file_name().unwrap().to_str().unwrap().to_string();+ // A name already taken is filed as "name 2", as Apple files it.+ if self.0.names(&folder.drivewsid).contains(&name) {+ name = match name.rsplit_once('.') {+ Some((s, e)) => format!("{s} 2.{e}"),+ None => format!("{name} 2"),+ };+ }+ self.0.uploads.lock().unwrap().push(name.clone());+ let slow = *self.0.slow.lock().unwrap();+ let (world, folder) = (Arc::clone(&self.0), folder.drivewsid.clone());+ Box::pin(async move {+ tokio::time::sleep(slow).await;+ Ok(world.file(&folder, &name, &bytes))+ })+ }+ fn rename(&self, it: &DriveItem, name: &str) -> Fut<()> {+ // A name already taken is refused, as Apple refuses it.+ let folder = it.parent_id.clone().unwrap_or_default();+ if self.0.names(&folder).iter().any(|n| n == name) {+ return Box::pin(async { Err("name taken".to_string()) });+ }+ for list in self.0.folders.lock().unwrap().values_mut() {+ for x in list.iter_mut().filter(|x| x.drivewsid == it.drivewsid) {+ let (s, e) = name+ .rsplit_once('.')+ .map_or((name, None), |(s, e)| (s, Some(e)));+ x.name = s.into();+ x.extension = e.map(Into::into);+ x.etag = Some(format!("{}r", x.etag.clone().unwrap_or_default()));+ }+ }+ Box::pin(async { Ok(()) })+ }+ fn delete(&self, it: &DriveItem) -> Fut<()> {+ let slow = *self.0.slow.lock().unwrap() / 5;+ let (world, id) = (Arc::clone(&self.0), it.drivewsid.clone());+ Box::pin(async move {+ tokio::time::sleep(slow).await;+ for list in world.folders.lock().unwrap().values_mut() {+ list.retain(|x| x.drivewsid != id);+ }+ world.deleted.lock().unwrap().push(id);+ Ok(())+ })+ }+}++fn mount(+ world: &Arc<World>,+ rt: &tokio::runtime::Runtime,+ at: &Path,+ cache: &Path,+ staging: &Path,+) -> fuser::BackgroundSession {+ let fs = fuse::DriveFs::new(+ Arc::new(Apple(Arc::clone(world))),+ Arc::new(Library(Arc::clone(world))),+ Arc::new(Cache::new(cache).unwrap()),+ rt.handle().clone(),+ )+ .with_writes(staging)+ .unwrap();+ fuse::mount(fs, at).expect("mounted")+}++/// Until Apple holds `want` under `name`, or a few seconds pass.+fn settled(world: &World, name: &str, want: &[u8]) -> bool {+ for _ in 0..80 {+ if world.bytes_of(ROOT, name).as_deref() == Some(want) {+ return true;+ }+ std::thread::sleep(Duration::from_millis(100));+ }+ false+}++#[test]+fn writes_reach_apple_safely() {+ if !Path::new("/dev/fuse").exists() {+ eprintln!("no FUSE here; skipped");+ return;+ }+ let world = Arc::new(World::default());+ world+ .folders+ .lock()+ .unwrap()+ .insert(ROOT.into(), Vec::new());+ world.file(ROOT, "Notes.txt", b"first line\n");+ let rt = tokio::runtime::Runtime::new().unwrap();+ let (cache, staging, at) = (+ tempfile::tempdir().unwrap(),+ tempfile::tempdir().unwrap(),+ tempfile::tempdir().unwrap(),+ );+ let root = at.path();+ let session = mount(&world, &rt, root, cache.path(), staging.path());++ // A new file, as a shell writes one: there at once, at Apple once left alone.+ std::fs::write(root.join("Hello.txt"), b"hello\n").unwrap();+ assert_eq!(+ std::fs::read(root.join("Hello.txt")).unwrap(),+ b"hello\n",+ "reads back at once, before Apple has it"+ );+ assert!(+ settled(&world, "Hello.txt", b"hello\n"),+ "at Apple: {:?}",+ world.names(ROOT)+ );+ let listed: Vec<String> = std::fs::read_dir(root)+ .unwrap()+ .map(|e| e.unwrap().file_name().into_string().unwrap())+ .collect();+ assert_eq!(+ listed.iter().filter(|n| *n == "Hello.txt").count(),+ 1,+ "listed once, not twice: {listed:?}"+ );++ // An append: Apple ends with the whole of it under the same name, the old version in+ // Recently Deleted (never overwritten in place), nothing named "Notes 2.txt" left behind.+ let mut f = std::fs::OpenOptions::new()+ .append(true)+ .open(root.join("Notes.txt"))+ .unwrap();+ f.write_all(b"second line\n").unwrap();+ drop(f);+ assert!(+ settled(&world, "Notes.txt", b"first line\nsecond line\n"),+ "appended at Apple: {:?}",+ world.names(ROOT)+ );+ assert_eq!(world.names(ROOT), ["Hello.txt", "Notes.txt"]);+ assert_eq!(+ world.deleted.lock().unwrap().len(),+ 1,+ "the old version into Recently Deleted"+ );++ // An editor's save: a temporary file written, then renamed over the old one.+ std::fs::write(root.join(".goutputstream-AB12"), b"rewritten\n").unwrap();+ std::fs::rename(root.join(".goutputstream-AB12"), root.join("Notes.txt")).unwrap();+ assert!(+ settled(&world, "Notes.txt", b"rewritten\n"),+ "saved at Apple: {:?}",+ world.names(ROOT)+ );+ assert!(+ !world+ .uploads+ .lock()+ .unwrap()+ .iter()+ .any(|n| n.starts_with(".goutputstream")),+ "the temporary never went to Apple"+ );+ assert_eq!(world.names(ROOT), ["Hello.txt", "Notes.txt"]);++ // Changed on another device while open here: both kept, theirs untouched.+ let mut f = std::fs::OpenOptions::new()+ .write(true)+ .truncate(true)+ .open(root.join("Hello.txt"))+ .unwrap();+ {+ let mut folders = world.folders.lock().unwrap();+ let theirs = folders+ .get_mut(ROOT)+ .unwrap()+ .iter_mut()+ .find(|i| i.display_name() == "Hello.txt")+ .unwrap();+ theirs.etag = Some("changed-on-the-phone".into());+ world+ .bytes+ .lock()+ .unwrap()+ .insert(theirs.drivewsid.clone(), b"from the phone\n".to_vec());+ theirs.size = Some(15);+ }+ f.write_all(b"from this computer\n").unwrap();+ drop(f);+ let mut copy = None;+ for _ in 0..80 {+ copy = world+ .names(ROOT)+ .into_iter()+ .find(|n| n.contains("conflicted copy"));+ if copy.is_some() {+ break;+ }+ std::thread::sleep(Duration::from_millis(100));+ }+ let copy = copy.expect("a conflicted copy");+ assert_eq!(+ world.bytes_of(ROOT, ©).as_deref(),+ Some(&b"from this computer\n"[..])+ );+ assert_eq!(+ world.bytes_of(ROOT, "Hello.txt").as_deref(),+ Some(&b"from the phone\n"[..]),+ "the phone's never overwritten"+ );++ // A scratch file never leaves this computer, and goes without a word to Apple.+ let uploads_before = world.uploads.lock().unwrap().len();+ std::fs::write(root.join(".Notes.txt.swp"), b"vim").unwrap();+ std::thread::sleep(Duration::from_millis(2600));+ std::fs::remove_file(root.join(".Notes.txt.swp")).unwrap();+ assert_eq!(+ world.uploads.lock().unwrap().len(),+ uploads_before,+ "never sent"+ );++ // Written, then the folder gone before it was sent: sent when it comes back.+ std::fs::write(root.join("Late.txt"), b"late\n").unwrap();+ drop(session);+ assert!(+ world.bytes_of(ROOT, "Late.txt").is_none(),+ "not sent before the folder went"+ );+ let again = mount(&world, &rt, root, cache.path(), staging.path());+ assert!(+ settled(&world, "Late.txt", b"late\n"),+ "sent after the restart: {:?}",+ world.names(ROOT)+ );+ drop(again);+}++/// A save arriving while the last save of the same file is still on its way (Apple slow):+/// it waits its turn, and Apple ends with one file under the name holding the newest bytes —+/// never "name 2" left beside it (measured live before the fix).+#[test]+fn a_save_during_a_send_waits_its_turn() {+ if !Path::new("/dev/fuse").exists() {+ return;+ }+ let world = Arc::new(World::default());+ world+ .folders+ .lock()+ .unwrap()+ .insert(ROOT.into(), Vec::new());+ world.file(ROOT, "Note.txt", b"one\n");+ // As slow as Apple was when this was measured: a send longer than the settle time.+ *world.slow.lock().unwrap() = Duration::from_millis(3000);+ let rt = tokio::runtime::Runtime::new().unwrap();+ let (cache, staging, at) = (+ tempfile::tempdir().unwrap(),+ tempfile::tempdir().unwrap(),+ tempfile::tempdir().unwrap(),+ );+ let root = at.path();+ let session = mount(&world, &rt, root, cache.path(), staging.path());+ let mut f = std::fs::OpenOptions::new()+ .append(true)+ .open(root.join("Note.txt"))+ .unwrap();+ f.write_all(b"two\n").unwrap();+ drop(f);+ // Until the first send has begun at Apple…+ for _ in 0..60 {+ if !world.uploads.lock().unwrap().is_empty() {+ break;+ }+ std::thread::sleep(Duration::from_millis(50));+ }+ // …then an editor saves over it.+ std::fs::write(root.join(".goutputstream-Z9"), b"three\n").unwrap();+ std::fs::rename(root.join(".goutputstream-Z9"), root.join("Note.txt")).unwrap();+ assert!(+ settled(&world, "Note.txt", b"three\n"),+ "{:?}",+ world.names(ROOT)+ );+ std::thread::sleep(Duration::from_millis(1500));+ assert_eq!(+ world.names(ROOT),+ ["Note.txt"],+ "one file under the name, nothing left beside it"+ );+ assert_eq!(+ world.bytes_of(ROOT, "Note.txt").as_deref(),+ Some(&b"three\n"[..])+ );+ drop(session);+}diff --git a/probe/examples/drive_mount_live.rs b/probe/examples/drive_mount_live.rsindex ac6c1cf..9839a01 100644--- a/probe/examples/drive_mount_live.rs+++ b/probe/examples/drive_mount_live.rs@@ -76,6 +76,22 @@ impl Remote for Apple {.map_err(|e| e.to_string())})}+ fn upload(&self, folder: &DriveItem, path: &std::path::Path) -> Fut<DriveItem> {+ let (http, docws, drivews, p, folder, path) = (+ self.http.clone(),+ self.docws.clone(),+ self.drivews.clone(),+ self.params.clone(),+ folder.drivewsid.clone(),+ path.to_path_buf(),+ );+ Box::pin(async move {+ let progress: quince_icloud::drive::Progress = Arc::new(|_, _| {});+ quince_icloud::drive::upload(&http, &docws, &drivews, p, &folder, &path, progress)+ .await+ .map_err(|e| e.to_string())+ })+ }fn delete(&self, item: &DriveItem) -> Fut<()> {let (http, root, p, item) = (self.http.clone(),@@ -164,12 +180,15 @@ fn main() -> Result<()> {links: Arc::new(Mutex::new(HashMap::new())),});let cache = tempfile_dir()?;+ let writes_dir =+ std::env::temp_dir().join(format!("quince-mount-writes-{}", std::process::id()));let fs = fuse::DriveFs::new(apple,Arc::new(Library(library)),Arc::new(Cache::new(&cache)?),rt.handle().clone(),- );+ )+ .with_writes(&writes_dir)?;let session = fuse::mount(fs, &at)?;println!("mounted at {}", at.display());std::thread::sleep(std::time::Duration::from_secs(secs));diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rsindex 5d7897b..ef23132 100644--- a/src-tauri/src/mount.rs+++ b/src-tauri/src/mount.rs@@ -1,7 +1,8 @@//! iCloud Drive as a folder on this computer (quince-vfs), served by the app itself: its//! signed-in session, its library, and what it learns kept in the library for the window too.-//! Off until it is turned on in Settings. Folders can be made, items renamed, moved and deleted-//! (into Recently Deleted); a file's own bytes are not written yet. See docs/mount-design.md.+//! Off until it is turned on in Settings. Everything a folder does: files written (kept here,+//! then sent), folders made, renames, moves, deletes into Recently Deleted. See+//! docs/mount-design.md.use crate::{Auth, Data};use quince_core::drive::DriveItem;@@ -90,6 +91,41 @@ impl Remote for AppRemote {})}+ fn upload(&self, folder: &DriveItem, path: &std::path::Path) -> Fut<DriveItem> {+ let (app, folder, path) = (+ self.app.clone(),+ folder.drivewsid.clone(),+ path.to_path_buf(),+ );+ Box::pin(async move {+ let (http, docws, params) = context(&app, "docws").await?;+ let drivews = context(&app, "drivews").await?.1;+ let progress: quince_icloud::drive::Progress = Arc::new(|_, _| {});+ quince_icloud::drive::upload(&http, &docws, &drivews, params, &folder, &path, progress)+ .await+ .map_err(|e| e.to_string())+ })+ }++ fn space(&self) -> Fut<(u64, u64)> {+ let app = self.app.clone();+ Box::pin(async move {+ let (http, setup, params) = {+ let auth = app.state::<Auth>();+ let client = auth.client.lock().await;+ (+ client.http().clone(),+ client.setup_url().to_string(),+ client.service_params(),+ )+ };+ let s = quince_icloud::drive::storage(&http, &setup, params)+ .await+ .map_err(|e| e.to_string())?;+ Ok((s.used_bytes, s.total_bytes))+ })+ }+fn delete(&self, item: &DriveItem) -> Fut<()> {let (app, item) = (self.app.clone(), item.clone());Box::pin(async move {@@ -241,7 +277,10 @@ fn start(app: &AppHandle, at: &std::path::Path) -> Result<(), String> {.filter(|p| p.is_absolute()).or_else(|| app.path().home_dir().ok().map(|h| h.join(".cache"))).map(|c| c.join("thumbnails"));- let mut fs = quince_vfs::fuse::DriveFs::new(remote, store, cache, rt);+ // What is written here waits beside the library (owner-only) until Apple has it.+ let mut fs = quince_vfs::fuse::DriveFs::new(remote, store, cache, rt)+ .with_writes(&crate::state_dir(app).join("drive-writes"))+ .map_err(|e| e.to_string())?;if let Some(thumbs) = thumbs {fs = fs.with_thumbnails(at.to_path_buf(), thumbs);}