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.

Drive folder: changes from other devices show on the next look

Yashwanth Kamireddi4c29bc3d66d5
diff --git a/crates/quince-vfs/src/fuse.rs b/crates/quince-vfs/src/fuse.rs
index 3ddb510..f432762 100644
--- a/crates/quince-vfs/src/fuse.rs
+++ b/crates/quince-vfs/src/fuse.rs
@@ -34,6 +34,9 @@ const TTL: Duration = Duration::from_secs(1);
/// How long a folder never opened before may take to come from Apple before the file manager
/// is told it could not be read (and can ask again).
const LIST_WAIT: Duration = Duration::from_secs(30);
+/// How long a folder's listing is taken as current before a look at it asks Apple again (in
+/// the background: what is stored is shown at once). Apple pushes nothing for iCloud Drive.
+const FRESH: Duration = Duration::from_secs(10);
/// How long a change (a new folder, a rename, a delete) may take at Apple.
const CHANGE_WAIT: Duration = Duration::from_secs(60);
@@ -57,6 +60,8 @@ 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>>,
+ /// 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)`.
space: Mutex<Option<((u64, u64), std::time::Instant)>>,
/// Nothing in it can be changed.
@@ -91,6 +96,7 @@ impl DriveFs {
filled: Mutex::new(std::collections::HashSet::new()),
writes: None,
space: Mutex::new(None),
+ looked: Mutex::new(std::collections::HashMap::new()),
read_only: false,
add_only: false,
}))
@@ -238,6 +244,35 @@ impl Inner {
Ok(done)
}
+ /// 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 {
+ let mut looked = self
+ .looked
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let now = std::time::Instant::now();
+ if looked
+ .get(folder)
+ .is_some_and(|at| now.duration_since(*at) < FRESH)
+ {
+ return false;
+ }
+ looked.insert(folder.to_string(), now);
+ true
+ }
+
+ /// 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 {
+ let _ = tokio::time::timeout(LIST_WAIT, remote.list(&id)).await;
+ });
+ }
+ }
+
fn relist(&self, folder: &str) {
let (remote, id) = (Arc::clone(&self.remote), folder.to_string());
let _ = self
@@ -832,8 +867,19 @@ fn quiet_note(e: &str) {
impl Filesystem for DriveFs {
fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {
- match self.0.child(parent.0, name) {
- Ok((ino, item)) => reply.entry(&TTL, &self.0.attr(ino, &item), Generation(0)),
+ let inner = &self.0;
+ let found = inner.child(parent.0, name);
+ // A name not there in a listing gone stale — a file just saved on the phone, asked for
+ // by its path — has its folder asked of Apple behind the answer, never in front of it:
+ // file managers ask after names that are not there all the time (.hidden, desktop.ini),
+ // 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);
+ }
+ }
+ match found {
+ Ok((ino, item)) => reply.entry(&TTL, &inner.attr(ino, &item), Generation(0)),
Err(e) => reply.error(e),
}
}
@@ -914,6 +960,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);
}
}
let parent = inner
diff --git a/probe/examples/drive_throwaway.rs b/probe/examples/drive_throwaway.rs
new file mode 100644
index 0000000..b28be33
--- /dev/null
+++ b/probe/examples/drive_throwaway.rs
@@ -0,0 +1,86 @@
+//! Live, for mount tests: a change made "on another device" — a throwaway folder made in Drive's
+//! top level straight through Apple (`make <name>`, the name must start "Quince mount test"),
+//! or every such throwaway deleted and cleared from Recently Deleted (`purge`).
+use anyhow::{anyhow, Result};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let drivews = client
+ .session()
+ .service_url("drivews")
+ .ok_or_else(|| anyhow!("no drivews"))?;
+ let (http, p) = (client.http().clone(), client.service_params());
+ let mut args = std::env::args().skip(1);
+ match args.next().as_deref() {
+ Some("make") => {
+ let name = args.next().ok_or_else(|| anyhow!("name"))?;
+ if !name.starts_with("Quince mount test") {
+ return Err(anyhow!("throwaways only"));
+ }
+ quince_icloud::drive::create_folder(
+ &http,
+ &drivews,
+ p,
+ quince_core::drive::ROOT,
+ &name,
+ )
+ .await?;
+ println!("made");
+ }
+ Some("purge") => {
+ let top = quince_icloud::drive::fetch_folder(
+ &http,
+ &drivews,
+ p.clone(),
+ quince_core::drive::ROOT,
+ )
+ .await?;
+ for item in top
+ .iter()
+ .filter(|i| i.name.starts_with("Quince mount test"))
+ {
+ quince_icloud::drive::delete(&http, &drivews, p.clone(), item).await?;
+ }
+ let trash = quince_icloud::drive::trash(&http, &drivews, p.clone()).await?;
+ let mine: Vec<_> = trash
+ .into_iter()
+ .filter(|i| i.name.starts_with("Quince mount test"))
+ .collect();
+ if !mine.is_empty() {
+ quince_icloud::drive::delete_forever_many(&http, &drivews, p, &mine).await?;
+ }
+ println!("purged {}", mine.len());
+ }
+ Some("find") => {
+ // How long Apple's listing of the top level takes, and whether it has the name yet.
+ let name = args.next().ok_or_else(|| anyhow!("name"))?;
+ for _ in 0..10 {
+ let t = std::time::Instant::now();
+ let top = quince_icloud::drive::fetch_folder(
+ &http,
+ &drivews,
+ p.clone(),
+ quince_core::drive::ROOT,
+ )
+ .await?;
+ let has = top.iter().any(|i| i.name == name);
+ println!(
+ "listed {} items in {} ms; has it: {has}",
+ top.len(),
+ t.elapsed().as_millis()
+ );
+ if has {
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_secs(2)).await;
+ }
+ }
+ _ => return Err(anyhow!("make <name> | find <name> | purge")),
+ }
+ Ok(())
+}