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.

iCloud Photos in Windows File Explorer, beside iCloud Drive

Yashwanth Kamireddi89438794df9b
diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml
index 8f94093..95e74c1 100644
--- a/.github/workflows/windows.yml
+++ b/.github/workflows/windows.yml
@@ -103,7 +103,7 @@ jobs:
- name: iCloud Drive in File Explorer
env:
RUSTFLAGS: --remap-path-prefix=${{ github.workspace }}=skykeep -C target-feature=+crt-static
- run: cargo test --release -p quince-vfs --test cloud_windows --locked -- --nocapture
+ run: cargo test --release -p quince-vfs --test cloud_windows --locked -- --nocapture --test-threads=1
- name: Tests, on Windows
if: ${{ inputs.test_first }}
# In release mode, so the installer build below reuses what this compiled. The probe
diff --git a/crates/quince-vfs/src/cloud.rs b/crates/quince-vfs/src/cloud.rs
index 9641545..59993ea 100644
--- a/crates/quince-vfs/src/cloud.rs
+++ b/crates/quince-vfs/src/cloud.rs
@@ -44,9 +44,23 @@ pub struct Provider {
/// Each folder Explorer has listed, by Apple's id: where it is, so a change at Apple is
/// brought to it (`CloudFolder::refresh`).
listed: Arc<std::sync::Mutex<HashMap<String, PathBuf>>>,
+ /// Only adding and deleting (iCloud Photos): a photo has no name or folder of its own to
+ /// change, so renames are declined and an added photo is never taken for a replacement of
+ /// one with the same name (iPhones reuse IMG_ numbers).
+ add_only: bool,
+ /// What each listed folder last showed, as a fingerprint: a folder whose items at Apple are
+ /// the same is not looked at again, so one new photo touches its month and nothing else.
+ shown: std::sync::Mutex<HashMap<String, u64>>,
}
impl Provider {
+ /// Only adding and deleting, as iCloud Photos is.
+ #[must_use]
+ pub fn add_only(mut self) -> Provider {
+ self.add_only = true;
+ self
+ }
+
pub fn new(
remote: Arc<dyn Remote>,
store: Arc<dyn Store>,
@@ -59,6 +73,8 @@ impl Provider {
rt,
staging,
listed: Arc::default(),
+ add_only: false,
+ shown: std::sync::Mutex::default(),
}
}
}
@@ -103,6 +119,23 @@ impl Drop for CloudFolder {
}
impl CloudFolder {
+ /// Every folder Explorer has listed brought up to date: for a tree made of views (Photos'
+ /// years, months and albums), where one change at Apple can touch any of them.
+ pub fn refresh_all(&self) {
+ // SAFETY: the provider lives as long as this folder.
+ let provider = unsafe { &*self.provider };
+ let listed: Vec<String> = provider
+ .listed
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .keys()
+ .cloned()
+ .collect();
+ for folder in listed {
+ self.refresh(&folder);
+ }
+ }
+
/// A folder Apple changed, brought up to date here if Explorer has listed it (one never
/// listed is made fresh when it is first opened): what is new appears, what changed at
/// Apple is updated and its old bytes let go, what was renamed is renamed, and what is gone
@@ -124,6 +157,16 @@ impl CloudFolder {
let Some(items) = provider.store.children(folder) else {
return;
};
+ let print = fingerprint(&items);
+ let mut shown = provider
+ .shown
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if shown.get(folder) == Some(&print) {
+ return;
+ }
+ shown.insert(folder.to_string(), print);
+ drop(shown);
let mut here: HashMap<String, PathBuf> = HashMap::new();
for entry in std::fs::read_dir(&dir).into_iter().flatten().flatten() {
if let Some(item) = identity_of(&entry.path()) {
@@ -186,6 +229,16 @@ impl CloudFolder {
}
}
+/// What a folder's items are, in one number: their ids, names, sizes and versions.
+fn fingerprint(items: &[DriveItem]) -> u64 {
+ use std::hash::{Hash, Hasher};
+ let mut h = std::collections::hash_map::DefaultHasher::new();
+ for i in items {
+ (&i.drivewsid, &i.name, &i.extension, i.size, &i.etag).hash(&mut h);
+ }
+ h.finish()
+}
+
/// A placeholder made to stand for `item`'s new version at Apple: its size and dates, its
/// identity, and its old bytes let go so the next open fetches the new ones. Opened alone (no
/// sharing): a file open in a program is left until it is closed, never changed under it.
@@ -239,7 +292,12 @@ pub struct Shown<'a> {
/// registration, or the bare filter where that is refused) and connect to it.
pub fn start(root: &Path, shown: &Shown, provider: Provider) -> Result<CloudFolder, String> {
std::fs::create_dir_all(root).map_err(|e| e.to_string())?;
- let shell = match register_with_shell(root, shown) {
+ // Tried twice: the shell's registry of cloud folders can refuse one made the moment after
+ // another (Element not found, seen registering iCloud Photos right after iCloud Drive).
+ let shell = match register_with_shell(root, shown).or_else(|_| {
+ std::thread::sleep(std::time::Duration::from_millis(800));
+ register_with_shell(root, shown)
+ }) {
Ok(()) => true,
Err(e) => {
// Refused by the shell (an unpackaged copy, where Windows wants package identity):
@@ -263,12 +321,15 @@ pub fn start(root: &Path, shown: &Shown, provider: Provider) -> Result<CloudFold
let mut folder = connect(root, provider)?;
folder.shell = shell;
// What changed here while Skykeep was closed, then what changes from now on.
+ // SAFETY: the provider lives as long as the folder.
+ let add_only = unsafe { (*folder.provider).add_only };
folder.watcher = crate::cloud_local::watch(
root.to_path_buf(),
remote,
staging,
rt,
Arc::clone(&folder.stop),
+ add_only,
);
Ok(folder)
}
@@ -527,6 +588,39 @@ async fn fetch_data(remote: &dyn Remote, req: Request, offset: u64, len: u64) {
return;
};
let end = pieces.last().map_or(start, |&(at, n)| at + n);
+ // Kept whole by the app already (a photo in its photo cache): read from there, fetched
+ // once if need be, never through Apple in pieces.
+ if let Some(whole) = remote.file(&item) {
+ report(&req, end - start, 0);
+ let path = match whole.await {
+ Ok(path) => path,
+ Err(e) => {
+ eprintln!("quince: fetching {}: {e}", item.drivewsid);
+ fail_data(
+ &req,
+ start,
+ end - start,
+ STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE,
+ );
+ return;
+ }
+ };
+ for (at, n) in pieces {
+ let bytes = read_at(&path, at, n);
+ match bytes {
+ Some(bytes) if bytes.len() as u64 == n => {
+ if transfer_data(&req, at, &bytes, STATUS_SUCCESS).is_err() {
+ return;
+ }
+ }
+ _ => {
+ fail_data(&req, at, end - at, STATUS_CLOUD_FILE_UNSUCCESSFUL);
+ return;
+ }
+ }
+ }
+ return;
+ }
for (at, n) in pieces {
// Each piece read takes seconds on a slow line: progress keeps Windows' 60 s limit off.
report(&req, end - start, at - start);
@@ -560,6 +654,16 @@ async fn fetch_data(remote: &dyn Remote, req: Request, offset: u64, len: u64) {
}
}
+/// `n` bytes of a file kept on this computer, from `at`.
+fn read_at(path: &Path, at: u64, n: u64) -> Option<Vec<u8>> {
+ use std::io::{Read, Seek, SeekFrom};
+ let mut f = std::fs::File::open(path).ok()?;
+ f.seek(SeekFrom::Start(at)).ok()?;
+ let mut buf = vec![0u8; usize::try_from(n).ok()?];
+ f.read_exact(&mut buf).ok()?;
+ Some(buf)
+}
+
fn report(req: &Request, total: u64, done: u64) {
// SAFETY: a plain call with keys Windows gave us.
let _ = unsafe {
@@ -761,8 +865,9 @@ unsafe fn full_path(volume: PCWSTR, path: PCWSTR) -> PathBuf {
PathBuf::from(format!("{}{}", text(volume), text(path)))
}
-/// Answers a delete or rename notice: allowed. Nothing is waited on here.
-fn ack(req: &Request, kind: CF_OPERATION_TYPE) {
+/// Answers a delete or rename notice: allowed, or (`status` not success) declined. Nothing is
+/// waited on here.
+fn ack(req: &Request, kind: CF_OPERATION_TYPE, status: NTSTATUS) {
let info = CF_OPERATION_INFO {
StructSize: size_of::<CF_OPERATION_INFO>() as u32,
Type: kind,
@@ -773,9 +878,14 @@ fn ack(req: &Request, kind: CF_OPERATION_TYPE) {
};
let mut params = CF_OPERATION_PARAMETERS {
ParamSize: size_of::<CF_OPERATION_PARAMETERS>() as u32,
- ..Default::default()
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ // AckDelete and AckRename share their layout: flags, then the status.
+ AckRename: CF_OPERATION_PARAMETERS_0_6 {
+ Flags: CF_OPERATION_ACK_RENAME_FLAG_NONE,
+ CompletionStatus: status,
+ },
+ },
};
- // AckDelete and AckRename share their layout: flags, then the status (success, zero).
// SAFETY: a plain call with keys Windows gave us.
if let Err(e) = unsafe { CfExecute(&info, &mut params) } {
eprintln!(
@@ -792,7 +902,7 @@ unsafe extern "system" fn on_delete(
let _ = std::panic::catch_unwind(|| {
// SAFETY: as in on_fetch_data.
if let Some((req, _)) = unsafe { request(info) } {
- ack(&req, CF_OPERATION_TYPE_ACK_DELETE);
+ ack(&req, CF_OPERATION_TYPE_ACK_DELETE, STATUS_SUCCESS);
}
});
}
@@ -803,8 +913,15 @@ unsafe extern "system" fn on_rename(
) {
let _ = std::panic::catch_unwind(|| {
// SAFETY: as in on_fetch_data.
- if let Some((req, _)) = unsafe { request(info) } {
- ack(&req, CF_OPERATION_TYPE_ACK_RENAME);
+ if let Some((req, provider)) = unsafe { request(info) } {
+ // A photo has no name of its own to change: declined, as the Photos folder on
+ // Linux declines it, and the file stays as it was.
+ let status = if provider.add_only {
+ windows::Win32::Foundation::STATUS_CLOUD_FILE_NOT_SUPPORTED
+ } else {
+ STATUS_SUCCESS
+ };
+ ack(&req, CF_OPERATION_TYPE_ACK_RENAME, status);
}
});
}
diff --git a/crates/quince-vfs/src/cloud_local.rs b/crates/quince-vfs/src/cloud_local.rs
index b8e5b10..38dd35c 100644
--- a/crates/quince-vfs/src/cloud_local.rs
+++ b/crates/quince-vfs/src/cloud_local.rs
@@ -40,13 +40,14 @@ pub fn watch(
staging: PathBuf,
rt: tokio::runtime::Handle,
stop: Arc<std::sync::atomic::AtomicBool>,
+ add_only: bool,
) -> Option<std::thread::JoinHandle<()>> {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<PathBuf>();
// Whatever changed while Skykeep was closed.
for path in scan(&root) {
let _ = tx.send(path);
}
- rt.spawn(settle_and_send(rx, Arc::clone(&remote), staging));
+ rt.spawn(settle_and_send(rx, Arc::clone(&remote), staging, add_only));
let watched = root.clone();
std::thread::Builder::new()
.name("quince:cloud-watch".into())
@@ -165,6 +166,7 @@ async fn settle_and_send(
mut rx: tokio::sync::mpsc::UnboundedReceiver<PathBuf>,
remote: Arc<dyn Remote>,
staging: PathBuf,
+ add_only: bool,
) {
let mut touched: HashMap<PathBuf, Instant> = HashMap::new();
let mut tick = tokio::time::interval(std::time::Duration::from_millis(500));
@@ -182,7 +184,7 @@ async fn settle_and_send(
.collect();
for path in due {
touched.remove(&path);
- if let Err(e) = sync_path(remote.as_ref(), &staging, &path).await {
+ if let Err(e) = sync_path(remote.as_ref(), &staging, &path, add_only).await {
eprintln!("quince: sending {}: {e}", path.display());
// Tried again in a while; the file itself stays as it is.
touched.insert(path, Instant::now() + std::time::Duration::from_secs(25));
@@ -194,7 +196,12 @@ async fn settle_and_send(
}
/// One path made Apple's: a new folder made there, a new or saved file sent.
-async fn sync_path(remote: &dyn Remote, staging: &Path, path: &Path) -> Result<(), String> {
+async fn sync_path(
+ remote: &dyn Remote,
+ staging: &Path,
+ path: &Path,
+ add_only: bool,
+) -> Result<(), String> {
let Some(name) = path
.file_name()
.and_then(|n| n.to_str())
@@ -227,14 +234,25 @@ async fn sync_path(remote: &dyn Remote, staging: &Path, path: &Path) -> Result<(
keep_or_free(path).await;
return Ok(());
}
+ if add_only {
+ // Photos has no folders to make: it stays here, marked so it never shows waiting.
+ let _ = convert(path, None);
+ return Ok(());
+ }
let made = remote.mkdir(&folder, &name).await?;
convert(path, Some(&made)).map_err(|e| e.to_string())?;
// What was copied in with it.
for entry in std::fs::read_dir(path).into_iter().flatten().flatten() {
- Box::pin(sync_path(remote, staging, &entry.path())).await?;
+ Box::pin(sync_path(remote, staging, &entry.path(), add_only)).await?;
}
return Ok(());
}
+ let edited_here = matches!(&state, Some((st, _)) if *st != CF_IN_SYNC_STATE_IN_SYNC);
+ if add_only && (edited_here || (state.is_none() && !remote.accepts(&folder))) {
+ // Photos: a photo saved over is not sent as a change (a photo has no versions to
+ // replace), and a view (Favorites, Videos) is no place to add one. Left as it is.
+ return Ok(());
+ }
let base = match &state {
Some((s, _)) if *s == CF_IN_SYNC_STATE_IN_SYNC => {
// In sync: only "Free up space" or "Always keep on this device" can have changed.
@@ -250,7 +268,18 @@ async fn sync_path(remote: &dyn Remote, staging: &Path, path: &Path) -> Result<(
std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
let staged = dir.join(&name);
std::fs::copy(path, &staged).map_err(|e| e.to_string())?;
- let sent = send(remote, &folder, &staged, base.as_ref(), base.as_ref()).await;
+ let sent = if add_only {
+ // Only ever an addition: never a replacement of a photo found under the same name.
+ remote
+ .upload(&folder, &staged)
+ .await
+ .map(|item| crate::writes::Sent {
+ item,
+ conflicted: false,
+ })
+ } else {
+ send(remote, &folder, &staged, base.as_ref(), base.as_ref()).await
+ };
let _ = std::fs::remove_dir_all(&dir);
let sent = sent?;
if stamp(path) != Some(before) {
diff --git a/crates/quince-vfs/tests/cloud_windows.rs b/crates/quince-vfs/tests/cloud_windows.rs
index 7356181..c64314a 100644
--- a/crates/quince-vfs/tests/cloud_windows.rs
+++ b/crates/quince-vfs/tests/cloud_windows.rs
@@ -91,11 +91,18 @@ impl Remote for Apple {
/// The library knows the root (as after a sync); the subfolder only Apple knows, so opening it
/// lists it from Apple, as a folder never opened before does.
-struct Library(HashMap<String, Vec<DriveItem>>);
+struct Library(Arc<std::sync::Mutex<HashMap<String, Vec<DriveItem>>>>);
impl Store for Library {
fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {
- (folder == ROOT).then(|| self.0.get(folder).cloned().unwrap_or_default())
+ (folder == ROOT).then(|| {
+ self.0
+ .lock()
+ .unwrap()
+ .get(folder)
+ .cloned()
+ .unwrap_or_default()
+ })
}
}
@@ -154,6 +161,7 @@ fn explorer_lists_and_opens_icloud_drive() {
let out = std::env::temp_dir().join(format!("skykeep-out-{}", std::process::id()));
std::fs::create_dir_all(&out).unwrap();
let log = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
+ let library = Arc::new(std::sync::Mutex::new(tree.clone()));
let id = cloud::sync_root_id(&format!("test{}", std::process::id())).unwrap();
let shown = Shown {
id: &id,
@@ -165,7 +173,7 @@ fn explorer_lists_and_opens_icloud_drive() {
&shown,
Provider::new(
Arc::new(Apple(tree.clone(), Arc::clone(&log))),
- Arc::new(Library(tree)),
+ Arc::new(Library(Arc::clone(&library))),
rt.handle().clone(),
out.join("sending"),
),
@@ -257,6 +265,42 @@ fn explorer_lists_and_opens_icloud_drive() {
cmd("attrib +P -U keep.bin");
wait_for("keeping keep.bin", &|| !not_all_here("keep.bin"));
+ // Changes made at Apple reach a folder Explorer has listed: a new file appears, one gone
+ // from Apple goes from here, one changed lets its old bytes go (never opened stale).
+ {
+ let mut lib = library.lock().unwrap();
+ let root = lib.get_mut(ROOT).unwrap();
+ root.retain(|i| i.name != "size 0");
+ root.push(file("fresh", "fresh", "bin", 10));
+ let changed = root.iter_mut().find(|i| i.name == "size 4097").unwrap();
+ changed.etag = Some("e2".into());
+ changed.size = Some(5000);
+ }
+ assert!(
+ !not_all_here("size 4097.bin"),
+ "size 4097.bin was read, so it is here"
+ );
+ folder.refresh(ROOT);
+ let listed = dir(&place);
+ assert!(
+ listed.contains(&"fresh.bin".to_string()),
+ "fresh.bin missing from {listed:?}"
+ );
+ assert!(
+ !listed.contains(&"size 0.bin".to_string()),
+ "size 0.bin still in {listed:?}"
+ );
+ assert!(
+ not_all_here("size 4097.bin"),
+ "the changed file kept its old bytes"
+ );
+ assert_eq!(
+ std::fs::metadata(place.join("size 4097.bin"))
+ .unwrap()
+ .len(),
+ 5000
+ );
+
cmd("echo hello> new.txt");
until("upload new.txt");
cmd("mkdir NewFolder && echo inside> NewFolder\\a.txt");
@@ -282,3 +326,212 @@ fn explorer_lists_and_opens_icloud_drive() {
let _ = std::fs::remove_dir_all(&place);
let _ = std::fs::remove_dir_all(&out);
}
+
+/// A 1×1 JPEG: a real picture, so a thumbnail could be drawn from it if it were fetched.
+const JPEG: &[u8] = &[
+ 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
+ 0x00, 0x01, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x08, 0x06, 0x06, 0x07, 0x06, 0x05, 0x08,
+ 0x07, 0x07, 0x07, 0x09, 0x09, 0x08, 0x0A, 0x0C, 0x14, 0x0D, 0x0C, 0x0B, 0x0B, 0x0C, 0x19, 0x12,
+ 0x13, 0x0F, 0x14, 0x1D, 0x1A, 0x1F, 0x1E, 0x1D, 0x1A, 0x1C, 0x1C, 0x20, 0x24, 0x2E, 0x27, 0x20,
+ 0x22, 0x2C, 0x23, 0x1C, 0x1C, 0x28, 0x37, 0x29, 0x2C, 0x30, 0x31, 0x34, 0x34, 0x34, 0x1F, 0x27,
+ 0x39, 0x3D, 0x38, 0x32, 0x3C, 0x2E, 0x33, 0x34, 0x32, 0xFF, 0xC0, 0x00, 0x0B, 0x08, 0x00, 0x01,
+ 0x00, 0x01, 0x01, 0x01, 0x11, 0x00, 0xFF, 0xC4, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0xFF, 0xC4, 0x00, 0x14,
+ 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3F, 0x00, 0x54, 0xDF, 0xFF, 0xD9,
+];
+
+/// iCloud Photos as the Photos folder makes it (Library, then photos), each read whole from the
+/// app's photo cache, only ever added to or deleted from.
+struct Photos(
+ Arc<std::sync::Mutex<Vec<String>>>,
+ PathBuf,
+ HashMap<String, Vec<DriveItem>>,
+);
+
+impl Remote for Photos {
+ fn list(&self, folder: &str) -> Fut<Vec<DriveItem>> {
+ let items = self.2.get(folder).cloned().unwrap_or_default();
+ Box::pin(async move { Ok(items) })
+ }
+ fn read(&self, _: &DriveItem, _: u64, _: u64) -> Fut<Vec<u8>> {
+ Box::pin(async { Err("read through the photo cache".to_string()) })
+ }
+ fn file(&self, item: &DriveItem) -> Option<Fut<PathBuf>> {
+ self.0
+ .lock()
+ .unwrap()
+ .push(format!("fetch {}", item.display_name()));
+ let path = self.1.clone();
+ Some(Box::pin(async move { Ok(path) }))
+ }
+ fn accepts(&self, folder: &DriveItem) -> bool {
+ folder.drivewsid == "PH::library"
+ }
+ fn upload(&self, folder: &DriveItem, path: &Path) -> Fut<DriveItem> {
+ let name = path.file_name().unwrap().to_string_lossy().to_string();
+ self.0.lock().unwrap().push(format!("upload {name}"));
+ let (stem, ext) = name.rsplit_once('.').unwrap_or((&name, ""));
+ let mut sent = file(&format!("up-{name}"), stem, ext, JPEG.len() as i64);
+ sent.drivewsid = format!("PHOTO::up-{name}");
+ sent.parent_id = Some(folder.drivewsid.clone());
+ Box::pin(async move { Ok(sent) })
+ }
+ fn delete(&self, item: &DriveItem) -> Fut<()> {
+ self.0
+ .lock()
+ .unwrap()
+ .push(format!("delete {}", item.display_name()));
+ Box::pin(async { Ok(()) })
+ }
+}
+
+#[test]
+fn explorer_shows_icloud_photos() {
+ let photo = |id: &str, name: &str| {
+ let mut p = file(id, name, "JPG", JPEG.len() as i64);
+ p.drivewsid = format!("PHOTO::{id}");
+ p.parent_id = Some("PH::library".into());
+ p
+ };
+ let mut library = DriveItem {
+ drivewsid: "PH::library".into(),
+ kind: "FOLDER".into(),
+ name: "Library".into(),
+ extension: None,
+ size: None,
+ ..file("x", "x", "x", 0)
+ };
+ library.parent_id = Some(ROOT.into());
+ let tree: HashMap<String, Vec<DriveItem>> = [
+ (ROOT.to_string(), vec![library.clone()]),
+ (
+ "PH::library".to_string(),
+ vec![
+ photo("a", "IMG_0001"),
+ photo("b", "IMG_0002"),
+ photo("c", "IMG_0003"),
+ ],
+ ),
+ ]
+ .into();
+
+ let rt = tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let base = PathBuf::from(std::env::var("USERPROFILE").unwrap());
+ let place = base.join(format!("Skykeep photos test {}", std::process::id()));
+ let out = std::env::temp_dir().join(format!("skykeep-photos-{}", std::process::id()));
+ std::fs::create_dir_all(&out).unwrap();
+ let cached = out.join("cached.jpg");
+ std::fs::write(&cached, JPEG).unwrap();
+ let log = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
+ let id = cloud::sync_root_id(&format!("photos{}", std::process::id())).unwrap();
+ let shown = Shown {
+ id: &id,
+ name: "Skykeep photos test",
+ icon: "%SystemRoot%\\system32\\imageres.dll,-1043",
+ };
+ let folder = cloud::start(
+ &place,
+ &shown,
+ Provider::new(
+ Arc::new(Photos(Arc::clone(&log), cached, tree.clone())),
+ Arc::new(Library(Arc::new(std::sync::Mutex::new(tree)))),
+ rt.handle().clone(),
+ out.join("sending"),
+ )
+ .add_only(),
+ )
+ .expect("the photos folder registers and connects");
+ println!("registered {} (shell: {})", id, folder.shell);
+
+ let lib = place.join("Library");
+ assert_eq!(dir(&place), ["Library"]);
+ assert_eq!(dir(&lib), ["IMG_0001.JPG", "IMG_0002.JPG", "IMG_0003.JPG"]);
+ let copy = out.join("one.jpg");
+ copy_out(&lib.join("IMG_0001.JPG"), &copy);
+ assert_eq!(std::fs::read(&copy).unwrap(), JPEG);
+
+ // Windows' own thumbnail of a photo never opened, asked by another process as Explorer asks:
+ // whether that fetches the photo is printed, the fact the thumbnail provider is built on.
+ let thumb = format!(
+ r#"Add-Type -TypeDefinition @'
+using System; using System.Runtime.InteropServices;
+[ComImport, Guid("bcc18b79-ba16-442f-80c4-8a59c30c463b"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+public interface IShellItemImageFactory {{ [PreserveSig] int GetImage(SIZE size, int flags, out IntPtr bitmap); }}
+[StructLayout(LayoutKind.Sequential)] public struct SIZE {{ public int cx; public int cy; }}
+public static class Thumb {{
+ [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
+ static extern void SHCreateItemFromParsingName(string path, IntPtr bc, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.Interface)] out IShellItemImageFactory item);
+ public static int Of(string path) {{ IShellItemImageFactory f; SHCreateItemFromParsingName(path, IntPtr.Zero, typeof(IShellItemImageFactory).GUID, out f); IntPtr h; return f.GetImage(new SIZE {{ cx = 96, cy = 96 }}, 8, out h); }}
+}}
+'@
+[Thumb]::Of('{}')"#,
+ lib.join("IMG_0002.JPG").display()
+ );
+ let before = log
+ .lock()
+ .unwrap()
+ .iter()
+ .filter(|l| l.contains("IMG_0002"))
+ .count();
+ let asked = Command::new("powershell")
+ .args(["-NoProfile", "-Command", &thumb])
+ .output()
+ .unwrap();
+ std::thread::sleep(std::time::Duration::from_secs(2));
+ let fetched = log
+ .lock()
+ .unwrap()
+ .iter()
+ .filter(|l| l.contains("IMG_0002"))
+ .count()
+ > before;
+ println!(
+ "thumbnail of a photo never opened: answer {:?}, fetched the photo: {fetched}",
+ String::from_utf8_lossy(&asked.stdout).trim()
+ );
+
+ let until = |what: &str| {
+ for _ in 0..150 {
+ if log.lock().unwrap().iter().any(|l| l.starts_with(what)) {
+ return;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ }
+ panic!(
+ "never reached Apple: {what}; Apple heard {:?}",
+ log.lock().unwrap()
+ );
+ };
+ let cmd = |line: &str| {
+ use std::os::windows::process::CommandExt;
+ Command::new("cmd")
+ .arg("/c")
+ .raw_arg(line)
+ .current_dir(&lib)
+ .status()
+ .unwrap()
+ .success()
+ };
+ std::fs::write(out.join("new.jpg"), JPEG).unwrap();
+ assert!(cmd(&format!(
+ "copy /b /y \"{}\" new.jpg",
+ out.join("new.jpg").display()
+ )));
+ until("upload new.jpg");
+ // A photo has no name of its own: the rename is declined and nothing reaches Apple.
+ assert!(!cmd("ren IMG_0003.JPG renamed.JPG"), "a photo was renamed");
+ assert!(lib.join("IMG_0003.JPG").exists());
+ assert!(cmd("del IMG_0003.JPG"));
+ until("delete IMG_0003.JPG");
+ assert!(!log.lock().unwrap().iter().any(|l| l.starts_with("rename")));
+ println!("Apple heard: {:?}", log.lock().unwrap());
+
+ drop(folder);
+ cloud::unregister(&place, &id);
+ let _ = std::fs::remove_dir_all(&place);
+ let _ = std::fs::remove_dir_all(&out);
+}
diff --git a/src-tauri/src/apps.rs b/src-tauri/src/apps.rs
index d02f7a0..317656b 100644
--- a/src-tauri/src/apps.rs
+++ b/src-tauri/src/apps.rs
@@ -161,6 +161,7 @@ pub fn new_event(app: &AppHandle) {
/// The app ID a window of `key` is known by: the app's own launcher entry's name, as the
/// desktop matches them (`<identifier>.Notes.desktop`).
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn app_id(app: &AppHandle, key: &str) -> String {
let name = APPS
.iter()
diff --git a/src-tauri/src/library_lock.rs b/src-tauri/src/library_lock.rs
index 43c39d4..c3bee77 100644
--- a/src-tauri/src/library_lock.rs
+++ b/src-tauri/src/library_lock.rs
@@ -138,6 +138,7 @@ impl LibraryLock {
Held { guard, _turn: turn }
}
+ #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn try_lock(&self) -> Result<Held<'_>, tokio::sync::TryLockError> {
let turn = Turn::take(&self.others);
let guard = self.library.try_lock()?;
diff --git a/src-tauri/src/login.rs b/src-tauri/src/login.rs
index 522e8c9..19b4acc 100644
--- a/src-tauri/src/login.rs
+++ b/src-tauri/src/login.rs
@@ -60,6 +60,7 @@ pub fn shown() {
}
/// Whether any window has been shown this run.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn was_shown() -> bool {
SHOWN.load(Ordering::SeqCst)
}
@@ -83,10 +84,12 @@ fn hides(search_only: bool, needs_you: bool, tray: impl FnOnce() -> bool) -> boo
/// How long a Skykeep started for a search goes on with no window shown and no search asked
/// before it ends: the person quit it, and a search does not change that.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub const SEARCH_IDLE: std::time::Duration = std::time::Duration::from_secs(10 * 60);
/// Whether a run started for a search is done: no window shown, nothing asked for a while.
#[must_use]
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn search_done(shown: bool, idle: std::time::Duration) -> bool {
!shown && idle >= SEARCH_IDLE
}
@@ -159,6 +162,7 @@ fn uses_startup_task(package_identity: bool) -> bool {
}
/// Running inside a Flatpak.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn in_flatpak() -> bool {
std::env::var_os("FLATPAK_ID").is_some()
}
diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rs
index eec35a8..a1aea8b 100644
--- a/src-tauri/src/mount.rs
+++ b/src-tauri/src/mount.rs
@@ -573,33 +573,48 @@ fn start(app: &AppHandle, which: Which, at: &std::path::Path) -> Result<(), Stri
if mounted(which).is_some() {
return Ok(());
}
- if which == Which::Photos {
- return Err("Photos in File Explorer comes next; iCloud Drive is there now.".into());
- }
- let remote = Arc::new(AppRemote {
- app: app.clone(),
- links: Arc::new(Mutex::new(HashMap::new())),
- });
let id = cloud_id(which)?;
let icon = std::env::current_exe()
.map(|exe| format!("{},0", exe.display()))
.unwrap_or_default();
+ let rt = tauri::async_runtime::handle().inner().clone();
+ let (provider, name) = match which {
+ Which::Drive => (
+ quince_vfs::cloud::Provider::new(
+ Arc::new(AppRemote {
+ app: app.clone(),
+ links: Arc::new(Mutex::new(HashMap::new())),
+ }),
+ Arc::new(AppStore::new(app.clone())),
+ rt,
+ // Saved files are copied here before they are sent, beside the library.
+ crate::state_dir(app).join("drive-sending"),
+ ),
+ "iCloud Drive",
+ ),
+ // The same tree as the Photos folder on Linux (mount_photos): read through the app's
+ // own photo cache, and only ever added to or deleted from.
+ Which::Photos => (
+ quince_vfs::cloud::Provider::new(
+ Arc::new(crate::mount_photos::PhotosRemote(app.clone())),
+ Arc::new(crate::mount_photos::PhotosStore(app.clone())),
+ rt,
+ crate::state_dir(app).join("photos-sending"),
+ )
+ .add_only(),
+ "iCloud Photos",
+ ),
+ };
let folder = quince_vfs::cloud::start(
at,
&quince_vfs::cloud::Shown {
id: &id,
- name: "iCloud Drive",
+ name,
icon: &icon,
},
- quince_vfs::cloud::Provider::new(
- remote,
- Arc::new(AppStore::new(app.clone())),
- tauri::async_runtime::handle().inner().clone(),
- // Saved files are copied here before they are sent, beside the library (owner-only).
- crate::state_dir(app).join("drive-sending"),
- ),
+ provider,
)
- .map_err(|e| format!("iCloud Drive could not be added to File Explorer: {e}"))?;
+ .map_err(|e| format!("{name} could not be added to File Explorer: {e}"))?;
CLOUD
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
@@ -684,6 +699,23 @@ pub fn drive_changed(folders: &[String]) {
let _ = folders;
}
+/// Photos changed at Apple: every Photos folder File Explorer has listed brought up to date
+/// (Windows). Its folders are views of the library (years, months, albums), so all of them are
+/// looked at again, not one named folder.
+pub fn photos_changed() {
+ #[cfg(windows)]
+ {
+ let cloud = CLOUD
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ for (which, _, folder) in cloud.iter() {
+ if *which == Which::Photos {
+ folder.refresh_all();
+ }
+ }
+ }
+}
+
/// At sign-out: each folder gone with the account — on Windows no longer in Explorer either.
pub fn forget(app: &AppHandle) {
forget_folder(app, Which::Drive);
@@ -707,9 +739,8 @@ fn status(app: &AppHandle, which: Which, problem: Option<String>) -> FolderStatu
path: at.to_string_lossy().into_owned(),
problem,
waiting,
- // Linux (not in the Flatpak, which has no FUSE), and Windows for iCloud Drive.
- available: (cfg!(target_os = "linux") && !in_flatpak())
- || (cfg!(windows) && which == Which::Drive),
+ // Linux (not in the Flatpak, which has no FUSE), and Windows.
+ available: (cfg!(target_os = "linux") && !in_flatpak()) || cfg!(windows),
}
}
diff --git a/src-tauri/src/outbox.rs b/src-tauri/src/outbox.rs
index 9597b9e..0938616 100644
--- a/src-tauri/src/outbox.rs
+++ b/src-tauri/src/outbox.rs
@@ -2072,6 +2072,7 @@ async fn send_event(
/// Whether a change in connectivity is the network coming back: online now, and not known to
/// be online before. `was` is updated to now.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn came_online(was: &mut Option<bool>, online: bool) -> bool {
let back = online && *was != Some(true);
*was = Some(online);
@@ -2080,11 +2081,13 @@ fn came_online(was: &mut Option<bool>, online: bool) -> bool {
/// NetworkManager's `NMState`: 60 is reaching the local site, 70 the internet. Apple may be
/// reachable at either (a captive portal clears to 70), so both count.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn nm_online(state: u32) -> bool {
state >= 60
}
/// connman's `State`: "ready" is connected, "online" connected and checked.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn connman_online(state: &str) -> bool {
matches!(state, "ready" | "online")
}
diff --git a/src-tauri/src/refresh.rs b/src-tauri/src/refresh.rs
index 9df6f60..4b940a8 100644
--- a/src-tauri/src/refresh.rs
+++ b/src-tauri/src/refresh.rs
@@ -715,11 +715,16 @@ async fn run(client: &Arc<Mutex<Client>>, data: &LibraryLock, services: &[&str])
.await
.ok()
.flatten();
- let changed = reports
+ let changed: Vec<_> = reports
.iter()
.filter(|r| r.changed)
.map(|r| r.service)
.collect();
+ // New or changed photos reach the Photos folder in File Explorer too (Windows), whether or
+ // not a window is open; off the async threads, as it touches the disk.
+ if changed.contains(&"photos") {
+ tauri::async_runtime::spawn_blocking(crate::mount::photos_changed);
+ }
let items = reports
.iter()
.filter(|r| r.changed)