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: Pages, Numbers and Keynote documents read whole at their served size

Yashwanth Kamireddif8b75ff96e53
diff --git a/crates/quince-vfs/src/fuse.rs b/crates/quince-vfs/src/fuse.rs
index 2e2d075..395ff20 100644
--- a/crates/quince-vfs/src/fuse.rs
+++ b/crates/quince-vfs/src/fuse.rs
@@ -564,12 +564,10 @@ impl Inner {
})
};
let dir = item.is_container();
+ // A file's length as served once that is known (a Pages document's is not its
+ // listing's), else as listed.
let (mut size, mut mtime) = (
- if dir {
- 0
- } else {
- u64::try_from(item.size.unwrap_or(0)).unwrap_or(0)
- },
+ if dir { 0 } else { self.cache.size(item) },
secs(item.modified_at),
);
// A file being written shows what has been written, as of when.
@@ -768,12 +766,19 @@ impl Inner {
std::fs::create_dir_all(staged.parent().ok_or(Errno::EIO)?).map_err(disk)?;
let file = std::fs::File::create(&staged).map_err(disk)?;
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(),
);
+ // All of it, at its length as served: copied at the listing's, a Keynote document
+ // was cut short or padded, and sent back so.
+ let size = {
+ let (c, r, i) = (Arc::clone(&cache), Arc::clone(&remote), it.clone());
+ self.rt
+ .block_on(async move { c.sized(r.as_ref(), &i).await })
+ .map_err(|_| Errno::EIO)?
+ };
let mut at = 0;
while at < size {
let len = crate::CHUNK.min(size - at);
@@ -1399,6 +1404,20 @@ impl Filesystem for DriveFs {
{
return reply.error(Errno::EACCES);
}
+ // A file served at another length than listed (iWork's packages): its length
+ // asked of Apple before it is read, and read past what the kernel last heard,
+ // so it is never cut short at the listing's length.
+ if crate::served_size_differs(&item) && !inner.remote.is_local(&item) {
+ let (c, r, i) = (
+ Arc::clone(&inner.cache),
+ Arc::clone(&inner.remote),
+ item.clone(),
+ );
+ let _ = inner
+ .rt
+ .block_on(async move { c.sized(r.as_ref(), &i).await });
+ return reply.opened(FileHandle(0), FopenFlags::FOPEN_DIRECT_IO);
+ }
return reply.opened(FileHandle(0), FopenFlags::empty());
}
}
diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rs
index d84346b..7ac41d8 100644
--- a/crates/quince-vfs/src/lib.rs
+++ b/crates/quince-vfs/src/lib.rs
@@ -20,12 +20,46 @@ use std::sync::{Arc, Mutex};
/// A future the engine waits on, as the app's own async code gives it.
pub type Fut<T> = Pin<Box<dyn Future<Output = Result<T, String>> + Send>>;
+/// Part of a file as Apple sent it, and how long Apple says the whole file is, where it said.
+#[derive(Debug, Clone, Default)]
+pub struct Part {
+ pub bytes: Vec<u8>,
+ /// The file's length as served (a range answer's `Content-Range` total, a whole answer's
+ /// length): the truth where a listing's size is not.
+ pub total: Option<u64>,
+}
+
+/// Whether a listing's size for `item` may not be the size of the file served for it.
+///
+/// Keynote, Pages and Numbers documents are packages — folders on a Mac — that iCloud lists
+/// with one size and serves, flattened into one file, with another (rclone #9798, measured:
+/// both larger and smaller). Their length is taken from what is served, never the listing.
+#[must_use]
+pub fn served_size_differs(item: &DriveItem) -> bool {
+ !item.is_container()
+ && item.extension.as_deref().is_some_and(|e| {
+ ["pages", "numbers", "key"]
+ .iter()
+ .any(|k| e.eq_ignore_ascii_case(k))
+ })
+}
+
/// Apple's side, as the app reaches it with the signed-in session.
pub trait Remote: Send + Sync + 'static {
/// A folder's items, fetched from Apple (and kept in the library by the app).
fn list(&self, folder: &str) -> Fut<Vec<DriveItem>>;
/// `len` bytes of a file from `offset` (fewer only at its end).
fn read(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Vec<u8>>;
+ /// [`Remote::read`], with the file's length as served where Apple says it.
+ fn read_part(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Part> {
+ let bytes = self.read(item, offset, len);
+ Box::pin(async move {
+ Ok(Part {
+ bytes: bytes.await?,
+ total: None,
+ })
+ })
+ }
/// A new folder in `parent`, as Apple made it. Read-only by default.
fn mkdir(&self, _parent: &DriveItem, _name: &str) -> Fut<DriveItem> {
Box::pin(async { Err("read-only".to_string()) })
@@ -300,6 +334,9 @@ pub struct Cache {
since: std::sync::atomic::AtomicU64,
/// Kept on this computer: never let go of to make room. See [`Kept`].
kept: Mutex<Kept>,
+ /// Files whose length as served is known, by cache path: what they are read, kept and
+ /// shown as (see [`served_size_differs`]). Written beside the bytes (`.size`).
+ sizes: Mutex<HashMap<PathBuf, u64>>,
}
/// What is kept on this computer ("Keep on This Computer", as Finder's Keep Downloaded): the
@@ -344,9 +381,78 @@ impl Cache {
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or_default(),
),
+ sizes: Mutex::new(HashMap::new()),
})
}
+ /// The file's length as served, once known: from a read of it here or written beside its
+ /// bytes by an earlier run.
+ #[must_use]
+ pub fn served_size(&self, item: &DriveItem) -> Option<u64> {
+ let path = self.path(item);
+ let mut sizes = self
+ .sizes
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if let Some(n) = sizes.get(&path) {
+ return Some(*n);
+ }
+ let n = std::fs::read_to_string(path.with_extension("size"))
+ .ok()?
+ .trim()
+ .parse()
+ .ok()?;
+ sizes.insert(path, n);
+ Some(n)
+ }
+
+ /// The file's length: as served where that is known, else as listed. What it is read up
+ /// to and shown as.
+ #[must_use]
+ pub fn size(&self, item: &DriveItem) -> u64 {
+ self.served_size(item)
+ .unwrap_or_else(|| u64::try_from(item.size.unwrap_or(0)).unwrap_or(0))
+ }
+
+ /// The length as served taken as the file's. One differing from what was assumed so far
+ /// starts its bytes here over: kept chunks were cut to a length the file does not have.
+ fn learnt_size(&self, item: &DriveItem, served: u64) -> std::io::Result<()> {
+ let path = self.path(item);
+ if self.size(item) != served {
+ let _ = std::fs::remove_file(&path);
+ let _ = std::fs::remove_file(path.with_extension("have"));
+ self.have
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .remove(&path);
+ }
+ let part = path.with_extension("size.part");
+ std::fs::write(&part, served.to_string())?;
+ std::fs::rename(&part, path.with_extension("size"))?;
+ self.sizes
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(path, served);
+ Ok(())
+ }
+
+ /// The file's length, asked of Apple first where a listing's may not be it and it is not
+ /// known yet (see [`served_size_differs`]).
+ ///
+ /// # Errors
+ /// When Apple cannot be reached to say, or the disk refuses.
+ pub async fn sized(&self, remote: &dyn Remote, item: &DriveItem) -> Result<u64, String> {
+ if !served_size_differs(item) || self.served_size(item).is_some() {
+ return Ok(self.size(item));
+ }
+ // Asked once: where Apple does not say, the listing's length stands until a read of
+ // the file finds it is not.
+ let part = remote.read_part(item, 0, 1).await?;
+ let total = part.total.unwrap_or_else(|| self.size(item));
+ self.learnt_size(item, total).map_err(|e| e.to_string())?;
+ Ok(total)
+ }
+
/// What is kept on this computer, as it stands.
#[must_use]
pub fn kept(&self) -> Kept {
@@ -391,10 +497,10 @@ impl Cache {
/// # 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);
+ // Its length as served, which a read may still learn: asked each time round.
+ while at < self.sized(remote, item).await? {
+ let len = CHUNK.min(self.size(item) - at);
self.read(remote, item, at, len).await?;
at += len;
}
@@ -449,7 +555,7 @@ impl Cache {
.filter(|e| {
e.path()
.extension()
- .is_none_or(|x| x != "have" && x != "part")
+ .is_none_or(|x| x != "have" && x != "part" && x != "size")
})
// Its own files, and what is kept on this computer, are never let go of.
.filter(|e| {
@@ -541,7 +647,11 @@ impl Cache {
/// Whether every byte of a file is on disk already.
#[must_use]
pub fn whole(&self, item: &DriveItem) -> bool {
- let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);
+ // A length not known yet is a file not here yet.
+ if served_size_differs(item) && self.served_size(item).is_none() {
+ return false;
+ }
+ let size = self.size(item);
size == 0 || self.present(&self.path(item), size).iter().all(|&b| b)
}
@@ -556,9 +666,27 @@ impl Cache {
offset: u64,
len: u64,
) -> Result<Vec<u8>, String> {
- let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);
+ // Read again, once, where a fetch learns the file is not the length it was read as.
+ for _ in 0..2 {
+ if let Some(bytes) = self.read_sized(remote, item, offset, len).await? {
+ return Ok(bytes);
+ }
+ }
+ Err("the file's length kept changing while it was read".into())
+ }
+
+ /// [`Cache::read`] at the length known now: `None` when a fetch found the file is another
+ /// length, now taken as its own, to be read again at that.
+ async fn read_sized(
+ &self,
+ remote: &dyn Remote,
+ item: &DriveItem,
+ offset: u64,
+ len: u64,
+ ) -> Result<Option<Vec<u8>>, String> {
+ let size = self.sized(remote, item).await?;
if offset >= size || len == 0 {
- return Ok(Vec::new());
+ return Ok(Some(Vec::new()));
}
let end = (offset + len).min(size);
let path = self.path(item);
@@ -592,7 +720,19 @@ impl Cache {
}
let from = chunk as u64 * CHUNK;
let want = CHUNK.min(size - from);
- let bytes = remote.read(item, from, want).await?;
+ let Part { bytes, total } = remote.read_part(item, from, want).await?;
+ // Served at another length than it was read as: that length is the file's,
+ // and it is read again at it — never cut short at the listing's.
+ if let Some(total) = total.filter(|t| *t != size) {
+ self.learnt_size(item, total).map_err(|e| e.to_string())?;
+ return Ok(None);
+ }
+ // Fewer only at its end: a package served shorter than listed ends here.
+ if total.is_none() && served_size_differs(item) && (bytes.len() as u64) < want {
+ self.learnt_size(item, from + bytes.len() as u64)
+ .map_err(|e| e.to_string())?;
+ return Ok(None);
+ }
if bytes.len() as u64 != want {
return Err(format!("Apple sent {} of {want} bytes", bytes.len()));
}
@@ -632,6 +772,7 @@ impl Cache {
})
.await
.map_err(|e| e.to_string())?
+ .map(Some)
.map_err(|e| e.to_string())
}
}
@@ -841,6 +982,121 @@ mod tests {
}
}
+ /// A file served as Apple's download links serve one: a range answered with what of it
+ /// there is, and the length as served (`Content-Range`'s total) beside it.
+ struct Served(Vec<u8>, Mutex<Vec<(u64, u64)>>);
+ impl Remote for Served {
+ fn list(&self, _: &str) -> Fut<Vec<DriveItem>> {
+ Box::pin(async { Ok(Vec::new()) })
+ }
+ fn read(&self, _: &DriveItem, _: u64, _: u64) -> Fut<Vec<u8>> {
+ unreachable!("read in parts")
+ }
+ fn read_part(&self, _: &DriveItem, offset: u64, len: u64) -> Fut<Part> {
+ self.1.lock().unwrap().push((offset, len));
+ let n = self.0.len() as u64;
+ let (from, to) = (offset.min(n), (offset + len).min(n));
+ let part = Part {
+ bytes: self.0[usize::try_from(from).unwrap()..usize::try_from(to).unwrap()]
+ .to_vec(),
+ total: Some(n),
+ };
+ Box::pin(async move { Ok(part) })
+ }
+ }
+
+ /// Every byte of a file read through the cache, as a program reading to the end would.
+ async fn read_all(cache: &Cache, remote: &dyn Remote, f: &DriveItem) -> Vec<u8> {
+ let mut out = Vec::new();
+ loop {
+ let got = cache
+ .read(remote, f, out.len() as u64, 1024 * 1024)
+ .await
+ .unwrap();
+ if got.is_empty() {
+ return out;
+ }
+ out.extend(got);
+ }
+ }
+
+ /// A Keynote document listed at one size and served, flattened, at a larger one: read
+ /// whole — not cut at the listed length — and shown at its served length from then on,
+ /// across a restart too.
+ #[tokio::test]
+ async fn a_package_served_larger_than_listed_is_read_whole() {
+ let dir = tempfile::tempdir().unwrap();
+ let served: Vec<u8> = (0..CHUNK + 5_000).map(|i| (i % 253) as u8).collect();
+ let remote = Served(served.clone(), Mutex::new(Vec::new()));
+ let f = item("FILE::x::k", "Deck", Some("key"), "FILE", 70_000, "v1");
+ let cache = Cache::new(dir.path()).unwrap();
+ assert_eq!(
+ cache.size(&f),
+ 70_000,
+ "the listing's, until Apple has said"
+ );
+ assert!(!cache.whole(&f));
+ assert_eq!(read_all(&cache, &remote, &f).await, served);
+ assert_eq!(cache.size(&f), served.len() as u64);
+ assert!(cache.whole(&f));
+ // Its length asked once (one byte), then each chunk fetched once.
+ assert_eq!(
+ remote.1.lock().unwrap().clone(),
+ vec![(0, 1), (0, CHUNK), (CHUNK, 5_000)]
+ );
+ let again = Cache::new(dir.path()).unwrap();
+ assert_eq!(again.size(&f), served.len() as u64, "kept beside the bytes");
+ assert_eq!(read_all(&again, &remote, &f).await, served);
+ assert_eq!(remote.1.lock().unwrap().len(), 3, "nothing fetched again");
+ }
+
+ /// A Pages document served smaller than listed: read to its real end, never an error for
+ /// the bytes the listing promised and Apple never had — and found so even by a read that
+ /// was already part way through at the listing's length.
+ #[tokio::test]
+ async fn a_package_served_smaller_than_listed_ends_where_it_ends() {
+ let dir = tempfile::tempdir().unwrap();
+ let served: Vec<u8> = (0..30_000u32).map(|i| (i % 241) as u8).collect();
+ let remote = Served(served.clone(), Mutex::new(Vec::new()));
+ let f = item(
+ "FILE::x::p",
+ "Essay",
+ Some("pages"),
+ "FILE",
+ 9_000_000,
+ "v1",
+ );
+ let cache = Cache::new(dir.path()).unwrap();
+ assert_eq!(read_all(&cache, &remote, &f).await, served);
+ assert_eq!(cache.size(&f), 30_000);
+ let other = tempfile::tempdir().unwrap();
+ let fresh = Cache::new(other.path()).unwrap();
+ // A plain file whose listing is right is read as before: no length asked for.
+ let plain_remote = Served(vec![7; 10], Mutex::new(Vec::new()));
+ let txt = item("FILE::x::t", "notes", Some("txt"), "FILE", 10, "v1");
+ assert_eq!(read_all(&fresh, &plain_remote, &txt).await, vec![7; 10]);
+ assert_eq!(plain_remote.1.lock().unwrap().clone(), vec![(0, 10)]);
+ }
+
+ /// Learnt from a chunk rather than asked for — a remote that cannot say up front — the
+ /// mismatch is still caught: what was kept at the wrong length is let go of and read again.
+ #[tokio::test]
+ async fn a_length_found_mid_read_starts_the_file_over() {
+ let dir = tempfile::tempdir().unwrap();
+ let served: Vec<u8> = (0..CHUNK + 10).map(|i| (i % 239) as u8).collect();
+ let remote = Served(served.clone(), Mutex::new(Vec::new()));
+ let f = item("FILE::x::n", "Budget", Some("numbers"), "FILE", 100, "v1");
+ let cache = Cache::new(dir.path()).unwrap();
+ // As if an earlier run had read it at the listing's length and said so.
+ cache.learnt_size(&f, 100).unwrap();
+ assert_eq!(
+ cache.read(&remote, &f, 0, 100).await.unwrap(),
+ served[..100]
+ );
+ assert_eq!(cache.size(&f), served.len() as u64);
+ assert_eq!(read_all(&cache, &remote, &f).await, served);
+ }
+
/// Only the chunks read are fetched, each once; the bytes are the file's; a new version
/// of the file is never served from the old one's copy.
#[tokio::test]
diff --git a/crates/quince-vfs/tests/packages.rs b/crates/quince-vfs/tests/packages.rs
new file mode 100644
index 0000000..0294c5c
--- /dev/null
+++ b/crates/quince-vfs/tests/packages.rs
@@ -0,0 +1,139 @@
+//! Keynote, Pages and Numbers documents through a real FUSE mount: iCloud lists them at one
+//! size and serves them, flattened, at another (rclone #9798). Read by a program the way any
+//! file is, they come out whole — never cut at the listed size, never an error for bytes the
+//! listing promised — and their size reads as served once Apple has said. Skipped where FUSE
+//! is not available.
+#![cfg(target_os = "linux")]
+
+use quince_core::drive::DriveItem;
+use quince_vfs::{fuse, Cache, Fut, Part, Remote, Store, CHUNK, ROOT};
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
+
+fn item(id: &str, name: &str, ext: &str, listed: i64) -> DriveItem {
+ DriveItem {
+ drivewsid: id.into(),
+ parent_id: Some(ROOT.into()),
+ name: name.into(),
+ extension: Some(ext.into()),
+ kind: "FILE".into(),
+ size: Some(listed),
+ child_count: None,
+ created_at: Some(1_700_000_000),
+ modified_at: Some(1_700_000_100),
+ etag: Some("e1".into()),
+ }
+}
+
+/// Apple's download links: a range answered with what of the file there is, and its length
+/// as served (`Content-Range`'s total).
+struct Apple {
+ files: HashMap<String, Vec<u8>>,
+ reads: Mutex<Vec<(String, u64, u64)>>,
+}
+impl Remote for Apple {
+ fn list(&self, _: &str) -> Fut<Vec<DriveItem>> {
+ Box::pin(async { Ok(Vec::new()) })
+ }
+ fn read(&self, _: &DriveItem, _: u64, _: u64) -> Fut<Vec<u8>> {
+ unreachable!("read in parts")
+ }
+ fn read_part(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Part> {
+ self.reads
+ .lock()
+ .unwrap()
+ .push((item.name.clone(), offset, len));
+ let data = &self.files[&item.drivewsid];
+ let n = data.len() as u64;
+ let (from, to) = (offset.min(n) as usize, (offset + len).min(n) as usize);
+ let part = Part {
+ bytes: data[from..to].to_vec(),
+ total: Some(n),
+ };
+ Box::pin(async move { Ok(part) })
+ }
+}
+
+struct Library(Vec<DriveItem>);
+impl Store for Library {
+ fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {
+ (folder == ROOT).then(|| self.0.clone())
+ }
+}
+
+#[test]
+fn iwork_documents_read_whole_at_their_served_size() {
+ if !std::path::Path::new("/dev/fuse").exists() {
+ eprintln!("no FUSE here; skipped");
+ return;
+ }
+ // Larger as served than listed, across a chunk; and smaller.
+ let deck: Vec<u8> = (0..CHUNK + 70_000).map(|i| (i % 251) as u8).collect();
+ let essay: Vec<u8> = (0..30_000u32).map(|i| (i % 241) as u8).collect();
+ let listed = vec![
+ item("FILE::z::deck", "Deck", "key", 70_000),
+ item("FILE::z::essay", "Essay", "pages", 9_000_000),
+ ];
+ let apple = Arc::new(Apple {
+ files: HashMap::from([
+ ("FILE::z::deck".to_string(), deck.clone()),
+ ("FILE::z::essay".to_string(), essay.clone()),
+ ]),
+ reads: Mutex::new(Vec::new()),
+ });
+ let rt = tokio::runtime::Runtime::new().unwrap();
+ let cache_dir = tempfile::tempdir().unwrap();
+ let at = tempfile::tempdir().unwrap();
+ let fs = fuse::DriveFs::new(
+ apple.clone(),
+ Arc::new(Library(listed)),
+ Arc::new(Cache::new(cache_dir.path()).unwrap()),
+ rt.handle().clone(),
+ );
+ let session = fuse::mount(fs, at.path()).expect("mounted");
+
+ let deck_at = at.path().join("Deck.key");
+ let essay_at = at.path().join("Essay.pages");
+ // Before anything is asked of Apple: the listing's size.
+ assert_eq!(std::fs::metadata(&deck_at).unwrap().len(), 70_000);
+ assert!(
+ apple.reads.lock().unwrap().is_empty(),
+ "a look fetches nothing"
+ );
+
+ assert_eq!(
+ std::fs::read(&deck_at).unwrap(),
+ deck,
+ "whole, not cut at 70,000"
+ );
+ assert_eq!(
+ std::fs::read(&essay_at).unwrap(),
+ essay,
+ "to its end, no error"
+ );
+ // Copied as a file manager copies one: the same bytes.
+ let elsewhere = tempfile::tempdir().unwrap();
+ let copied = elsewhere.path().join("copied.key");
+ std::fs::copy(&deck_at, &copied).unwrap();
+ assert_eq!(std::fs::read(&copied).unwrap(), deck);
+
+ // Once the attributes are looked at again: the size as served.
+ std::thread::sleep(std::time::Duration::from_millis(1_200));
+ assert_eq!(
+ std::fs::metadata(&deck_at).unwrap().len(),
+ deck.len() as u64
+ );
+ assert_eq!(
+ std::fs::metadata(&essay_at).unwrap().len(),
+ essay.len() as u64
+ );
+ // Each byte fetched once: the length asked (one byte), then the chunks.
+ let reads = apple.reads.lock().unwrap().clone();
+ let deck_reads: Vec<(u64, u64)> = reads
+ .iter()
+ .filter(|(n, ..)| n == "Deck")
+ .map(|(_, o, l)| (*o, *l))
+ .collect();
+ assert_eq!(deck_reads, [(0, 1), (0, CHUNK), (CHUNK, 70_000)]);
+ drop(session);
+}
diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rs
index 200dbe9..1791a4e 100644
--- a/src-tauri/src/mount.rs
+++ b/src-tauri/src/mount.rs
@@ -171,6 +171,11 @@ impl Remote for AppRemote {
}
fn read(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Vec<u8>> {
+ let part = self.read_part(item, offset, len);
+ Box::pin(async move { Ok(part.await?.bytes) })
+ }
+
+ fn read_part(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<quince_vfs::Part> {
let (app, links, item) = (self.app.clone(), Arc::clone(&self.links), item.clone());
Box::pin(async move {
let key = format!("{}~{}", item.drivewsid, item.etag.as_deref().unwrap_or(""));
@@ -210,11 +215,28 @@ impl Remote for AppRemote {
continue;
}
let whole = status == 200;
- if status != 206 && !whole {
+ if status != 206 && !whole && status != 416 {
return Err(format!("Apple answered {status}"));
}
+ // The length as served: a range answer's total (`bytes 0-0/52311`), a 416's
+ // (`bytes */52311`: asked past the end of a file shorter than listed).
+ let total = r
+ .headers()
+ .get(reqwest::header::CONTENT_RANGE)
+ .and_then(|v| v.to_str().ok())
+ .and_then(served_total);
+ if status == 416 {
+ return match total {
+ Some(total) => Ok(quince_vfs::Part {
+ bytes: Vec::new(),
+ total: Some(total),
+ }),
+ None => Err(format!("Apple answered {status}")),
+ };
+ }
let bytes = r.bytes().await.map_err(|e| e.to_string())?;
- // A server that ignores the range sends the whole file: the part is cut from it.
+ // A server that ignores the range sends the whole file: the part is cut from it,
+ // and its length is the file's.
return Ok(if whole {
let from = usize::try_from(offset)
.unwrap_or(usize::MAX)
@@ -222,9 +244,15 @@ impl Remote for AppRemote {
let to = usize::try_from(offset + len)
.unwrap_or(usize::MAX)
.min(bytes.len());
- bytes[from..to].to_vec()
+ quince_vfs::Part {
+ bytes: bytes[from..to].to_vec(),
+ total: Some(bytes.len() as u64),
+ }
} else {
- bytes.to_vec()
+ quince_vfs::Part {
+ bytes: bytes.to_vec(),
+ total,
+ }
});
}
Err("Apple would not send the file".into())
@@ -232,6 +260,19 @@ impl Remote for AppRemote {
}
}
+/// The whole length a `Content-Range` gives: `bytes 0-9/52311` or `bytes */52311`. `None` for
+/// one that says it is not known (`/*`) or cannot be read.
+fn served_total(range: &str) -> Option<u64> {
+ range
+ .trim()
+ .strip_prefix("bytes")?
+ .rsplit_once('/')?
+ .1
+ .trim()
+ .parse()
+ .ok()
+}
+
struct AppStore {
app: AppHandle,
/// The folder's own read-only connection on the library: what it lists never waits on the
@@ -781,6 +822,15 @@ pub fn drive_keep(drivewsid: String, on: bool) -> Result<Vec<String>, String> {
mod stale_tests {
use super::*;
+ /// The length a file is served at, from a range answer or a 416 past its end.
+ #[test]
+ fn a_served_length_is_read_from_content_range() {
+ assert_eq!(served_total("bytes 0-0/52311"), Some(52_311));
+ assert_eq!(served_total("bytes */52311"), Some(52_311));
+ assert_eq!(served_total("bytes 0-9/*"), None);
+ assert_eq!(served_total("items 0-9/10"), None);
+ }
+
/// A dead mount is let go of before a new one is made over it; a folder that answers, or
/// is not there, is left as it is.
#[test]