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 in Windows File Explorer, through Cloud Files
Yashwanth Kamireddif34aabbd0a6f
diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.ymlindex cc3bc09..8f94093 100644--- a/.github/workflows/windows.yml+++ b/.github/workflows/windows.yml@@ -98,6 +98,12 @@ jobs:"VCPKGRS_TRIPLET=x64-windows-static" >> $env:GITHUB_ENV# The window's files must exist for the app crate to compile.- run: npm run build+ # iCloud Drive in File Explorer on this Windows: a cloud folder registered, listed and+ # read by another process, its output shown (whether the shell took the registration).+ - 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- name: Tests, on Windowsif: ${{ inputs.test_first }}# In release mode, so the installer build below reuses what this compiled. The probediff --git a/Cargo.lock b/Cargo.lockindex ce03cea..16cad89 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4197,6 +4197,8 @@ dependencies = ["serde_json","tempfile","tokio",+ "windows 0.62.2",+ "windows-future 0.3.2",][[package]]diff --git a/crates/quince-vfs/Cargo.toml b/crates/quince-vfs/Cargo.tomlindex 7a7e268..c680deb 100644--- a/crates/quince-vfs/Cargo.toml+++ b/crates/quince-vfs/Cargo.toml@@ -17,6 +17,24 @@ serde_json = { workspace = true }[target.'cfg(target_os = "linux")'.dependencies]fuser = { version = "0.18", default-features = false }+# iCloud Drive in File Explorer (cloud.rs): the Cloud Files API and the shell's registration.+[target.'cfg(windows)'.dependencies]+windows = { version = "0.62", features = [+ "Foundation",+ "Storage",+ "Storage_Provider",+ "Storage_Search",+ "Win32_Foundation",+ "Win32_Storage_CloudFilters",+ "Win32_Storage_FileSystem",+ "Win32_System_CorrelationVector",+ "Win32_System_IO",+ "Win32_Security",+ "Win32_Security_Authorization",+ "Win32_System_Threading",+] }+windows-future = "0.3"+[dev-dependencies]image = { version = "0.25", default-features = false, features = ["jpeg", "png"] }tempfile = "3"diff --git a/crates/quince-vfs/src/cloud.rs b/crates/quince-vfs/src/cloud.rsnew file mode 100644index 0000000..9641545--- /dev/null+++ b/crates/quince-vfs/src/cloud.rs@@ -0,0 +1,1033 @@+//! iCloud Drive in Windows File Explorer, through the Cloud Files API — the system OneDrive is+//! built on. Every file and folder is a placeholder made from the library, with no network; a+//! file's bytes come from Apple the first time something opens it, and from then on it is a+//! plain NTFS file at full speed. No driver of ours, nothing in the kernel, nothing to install.+//!+//! How it is kept from the bugs other clients shipped (docs/windows-drive.md):+//! - Windows' callbacks only copy what they are told and hand the work to the runtime; nothing+//! is ever waited on inside one (a callback that blocked deadlocked Nextcloud, PR #2778).+//! - Each request is answered exactly once, whole: every byte asked for in pieces Windows+//! accepts (`transfer::pieces`), or one failure for the whole range. A request said to be+//! done before its last byte left a small file waiting for good (Nextcloud #10845).+//! - A long fetch from Apple reports progress, which keeps Windows' 60 s limit from cutting it.+//! - A folder's placeholders are made one per call: batches of them came back "done" and then+//! would not open (ERROR_CLOUD_FILE_METADATA_CORRUPT, Microsoft Q&A 6057324).+//! - Names Windows can't hold are shown as their look-alikes (`named_on`), never dropped.+//! - Every callback is caught: a panic fails its request rather than taking Explorer's request,+//! and Skykeep, down with it (Nextcloud #8608).+//! - Population is FULL (never PARTIAL, which Microsoft says is not supported), and a folder+//! already listed is brought up to date when the library changes, never left stale (#10871).++use crate::{named_on, served_size_differs, transfer, Names, Remote, Store, CHUNK, ROOT};+use quince_core::drive::DriveItem;+use std::collections::HashMap;+use std::ffi::c_void;+use std::path::{Path, PathBuf};+use std::sync::Arc;+use windows::core::{HSTRING, PCWSTR};+use windows::Win32::Foundation::{+ NTSTATUS, STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE, STATUS_CLOUD_FILE_UNSUCCESSFUL, STATUS_SUCCESS,+};+use windows::Win32::Storage::CloudFilters::*;+use windows::Win32::Storage::FileSystem::{+ FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_BASIC_INFO,+};++/// What Explorer's folder is made from and fetched with, and where its requests are answered.+pub struct Provider {+ pub remote: Arc<dyn Remote>,+ pub store: Arc<dyn Store>,+ /// The app's async runtime: every request is answered there, never on Windows' thread.+ pub rt: tokio::runtime::Handle,+ /// Where a file saved here is copied before it is sent (never inside the folder itself).+ pub staging: PathBuf,+ /// 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>>>,+}++impl Provider {+ pub fn new(+ remote: Arc<dyn Remote>,+ store: Arc<dyn Store>,+ rt: tokio::runtime::Handle,+ staging: PathBuf,+ ) -> Provider {+ Provider {+ remote,+ store,+ rt,+ staging,+ listed: Arc::default(),+ }+ }+}++/// The folder, connected: its requests come in until it is dropped.+pub struct CloudFolder {+ key: CF_CONNECTION_KEY,+ provider: *const Provider,+ pub root: PathBuf,+ /// Registered with the shell (in Explorer's navigation pane), or as the bare filter.+ pub shell: bool,+ /// The watcher sending what changes here, and how to stop it.+ watcher: Option<std::thread::JoinHandle<()>>,+ stop: Arc<std::sync::atomic::AtomicBool>,+}++// SAFETY: the connection key is a plain handle Windows hands out for any thread to use, and the+// provider behind the pointer is Send + Sync, owned here until the drop disconnects.+unsafe impl Send for CloudFolder {}+unsafe impl Sync for CloudFolder {}++impl Drop for CloudFolder {+ fn drop(&mut self) {+ // The watcher first: told to stop, and woken from its wait for changes.+ self.stop.store(true, std::sync::atomic::Ordering::Relaxed);+ if let Some(watcher) = self.watcher.take() {+ use std::os::windows::io::AsRawHandle;+ // SAFETY: the thread's own handle, alive until joined.+ unsafe {+ let _ = windows::Win32::System::IO::CancelSynchronousIo(+ windows::Win32::Foundation::HANDLE(watcher.as_raw_handle()),+ );+ }+ let _ = watcher.join();+ }+ // SAFETY: the key is this folder's; once disconnected no callback holds the provider.+ unsafe {+ let _ = CfDisconnectSyncRoot(self.key);+ drop(Box::from_raw(self.provider.cast_mut()));+ }+ }+}++impl CloudFolder {+ /// 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+ /// from Apple goes from here. A file changed here too is never touched: it goes to Apple as+ /// a conflicted copy (cloud_local). Nothing is ever sent to Apple as a delete for being+ /// missing here (Nextcloud #3444), and a folder once listed is never left stale (#10871).+ pub fn refresh(&self, folder: &str) {+ // SAFETY: the provider lives as long as this folder.+ let provider = unsafe { &*self.provider };+ let Some(dir) = provider+ .listed+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .get(folder)+ .cloned()+ else {+ return;+ };+ let Some(items) = provider.store.children(folder) else {+ return;+ };+ 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()) {+ here.insert(item.drivewsid, entry.path());+ }+ }+ let in_sync =+ |p: &Path| placeholder_state(p).is_some_and(|(s, _)| s == CF_IN_SYNC_STATE_IN_SYNC);+ let named = named_on(&items, Names::Windows);+ let at_apple: std::collections::HashSet<&str> =+ named.iter().map(|(_, i)| i.drivewsid.as_str()).collect();+ for (name, item) in &named {+ let want = dir.join(name);+ match here.get(&item.drivewsid) {+ None if !want.exists() => {+ let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);+ let made = Placeholder::of(name, item, size);+ let mut one = [made.info()];+ // SAFETY: the name and identity are alive for the call.+ let created = unsafe {+ CfCreatePlaceholders(+ &HSTRING::from(dir.as_os_str()),+ &mut one,+ CF_CREATE_FLAG_NONE,+ None,+ )+ };+ if let Err(e) = created {+ eprintln!("quince: {name} in {}: {e}", dir.display());+ }+ }+ None => {}+ Some(path) if in_sync(path) => {+ let mut path = path.clone();+ if path != want && !want.exists() && std::fs::rename(&path, &want).is_ok() {+ path = want;+ }+ let known = identity_of(&path);+ if known.is_some_and(|k| k.etag != item.etag || k.size != item.size) {+ if let Err(e) = renew(&path, item) {+ // Open in a program now: brought up to date on the next change.+ eprintln!("quince: {} not yet updated: {e}", path.display());+ }+ }+ }+ Some(_) => {}+ }+ }+ for (id, path) in &here {+ // Gone from Apple (deleted, or moved to a folder of its own here): gone from here,+ // unless it holds a change of ours not yet sent.+ if !at_apple.contains(id.as_str()) && in_sync(path) {+ let _ = if path.is_dir() {+ std::fs::remove_dir_all(path)+ } else {+ std::fs::remove_file(path)+ };+ }+ }+ }+}++/// 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.+fn renew(path: &Path, item: &DriveItem) -> windows::core::Result<()> {+ use windows::Win32::Foundation::GENERIC_ACCESS_RIGHTS;+ use windows::Win32::Storage::FileSystem::*;+ // SAFETY: a path string alive for the call.+ let handle = Handle(unsafe {+ CreateFileW(+ &HSTRING::from(path.as_os_str()),+ (FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES).0 | GENERIC_ACCESS_RIGHTS(0x4000_0000).0,+ FILE_SHARE_NONE,+ None,+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ None,+ )+ }?);+ let size = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);+ let made = Placeholder::of("", item, size);+ let flags = if item.is_container() {+ CF_UPDATE_FLAG_MARK_IN_SYNC+ } else {+ CF_UPDATE_FLAG_MARK_IN_SYNC | CF_UPDATE_FLAG_DEHYDRATE+ };+ // SAFETY: the metadata and identity are alive for the call.+ unsafe {+ CfUpdatePlaceholder(+ handle.0,+ Some(&made.meta),+ Some(made.identity.as_ptr().cast()),+ made.identity.len() as u32,+ None,+ flags,+ None,+ None,+ )+ }+}++/// How Explorer shows the folder: its name in the navigation pane, its icon, and the id it is+/// registered under (`Skykeep!<user's SID>!<account>`, at most 174 characters).+pub struct Shown<'a> {+ pub id: &'a str,+ pub name: &'a str,+ /// `C:\path\skykeep.exe,0`+ pub icon: &'a str,+}++/// Register `root` as a cloud folder (Explorer's navigation pane through the shell's own+/// 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) {+ Ok(()) => true,+ Err(e) => {+ // Refused by the shell (an unpackaged copy, where Windows wants package identity):+ // the folder still works, without its entry in the navigation pane. The shell route+ // is tried again every start.+ eprintln!("quince: Explorer registration: {e}; registering the folder alone");+ register_bare(root, shown)?;+ false+ }+ };+ let (remote, staging, rt) = (+ Arc::clone(&provider.remote),+ provider.staging.clone(),+ provider.rt.clone(),+ );+ provider+ .listed+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(ROOT.to_string(), root.to_path_buf());+ let mut folder = connect(root, provider)?;+ folder.shell = shell;+ // What changed here while Skykeep was closed, then what changes from now on.+ folder.watcher = crate::cloud_local::watch(+ root.to_path_buf(),+ remote,+ staging,+ rt,+ Arc::clone(&folder.stop),+ );+ Ok(folder)+}++/// The shell's registration: the folder in Explorer's navigation pane, with its name and icon,+/// "Free up space" and "Always keep on this device" on every file.+fn register_with_shell(root: &Path, shown: &Shown) -> Result<(), String> {+ use windows::Storage::Provider::*;+ use windows::Storage::StorageFolder;+ let e = |e: windows::core::Error| e.to_string();+ let folder = StorageFolder::GetFolderFromPathAsync(&HSTRING::from(root.as_os_str()))+ .and_then(|op| op.join())+ .map_err(e)?;+ let info = StorageProviderSyncRootInfo::new().map_err(e)?;+ info.SetId(&HSTRING::from(shown.id)).map_err(e)?;+ info.SetPath(&folder).map_err(e)?;+ info.SetDisplayNameResource(&HSTRING::from(shown.name))+ .map_err(e)?;+ info.SetIconResource(&HSTRING::from(shown.icon))+ .map_err(e)?;+ info.SetHydrationPolicy(StorageProviderHydrationPolicy::Full)+ .map_err(e)?;+ info.SetHydrationPolicyModifier(StorageProviderHydrationPolicyModifier::None)+ .map_err(e)?;+ info.SetPopulationPolicy(StorageProviderPopulationPolicy::Full)+ .map_err(e)?;+ info.SetInSyncPolicy(+ StorageProviderInSyncPolicy::FileCreationTime+ | StorageProviderInSyncPolicy::DirectoryCreationTime,+ )+ .map_err(e)?;+ info.SetHardlinkPolicy(StorageProviderHardlinkPolicy::None)+ .map_err(e)?;+ info.SetShowSiblingsAsGroup(false).map_err(e)?;+ info.SetVersion(&HSTRING::from(env!("CARGO_PKG_VERSION")))+ .map_err(e)?;+ StorageProviderSyncRootManager::Register(&info).map_err(e)+}++/// The filter alone: the folder's files download on open, but it has no navigation-pane entry.+fn register_bare(root: &Path, shown: &Shown) -> Result<(), String> {+ let name = HSTRING::from(shown.name);+ let version = HSTRING::from(env!("CARGO_PKG_VERSION"));+ let identity = shown.id.as_bytes();+ let registration = CF_SYNC_REGISTRATION {+ StructSize: size_of::<CF_SYNC_REGISTRATION>() as u32,+ ProviderName: PCWSTR(name.as_ptr()),+ ProviderVersion: PCWSTR(version.as_ptr()),+ SyncRootIdentity: identity.as_ptr().cast(),+ SyncRootIdentityLength: identity.len() as u32,+ ..Default::default()+ };+ let policies = CF_SYNC_POLICIES {+ StructSize: size_of::<CF_SYNC_POLICIES>() as u32,+ Hydration: CF_HYDRATION_POLICY {+ Primary: CF_HYDRATION_POLICY_FULL,+ Modifier: CF_HYDRATION_POLICY_MODIFIER_NONE,+ },+ Population: CF_POPULATION_POLICY {+ Primary: CF_POPULATION_POLICY_FULL,+ Modifier: CF_POPULATION_POLICY_MODIFIER_NONE,+ },+ InSync: CF_INSYNC_POLICY_TRACK_ALL,+ HardLink: CF_HARDLINK_POLICY_NONE,+ PlaceholderManagement: CF_PLACEHOLDER_MANAGEMENT_POLICY_DEFAULT,+ };+ let path = HSTRING::from(root.as_os_str());+ // SAFETY: every pointer is to a value alive for the call.+ unsafe { CfRegisterSyncRoot(&path, ®istration, &policies, CF_REGISTER_FLAG_UPDATE) }+ .map_err(|e| e.to_string())+}++fn connect(root: &Path, provider: Provider) -> Result<CloudFolder, String> {+ let table = [+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_FETCH_DATA,+ Callback: Some(on_fetch_data),+ },+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS,+ Callback: Some(on_fetch_placeholders),+ },+ // A delete or rename in Explorer waits on these: answered at once, so no program ever+ // waits on Skykeep; Apple is told only once Windows has done it (the completions).+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_NOTIFY_DELETE,+ Callback: Some(on_delete),+ },+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_NOTIFY_RENAME,+ Callback: Some(on_rename),+ },+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_NOTIFY_DELETE_COMPLETION,+ Callback: Some(on_deleted),+ },+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_NOTIFY_RENAME_COMPLETION,+ Callback: Some(on_renamed),+ },+ CF_CALLBACK_REGISTRATION {+ Type: CF_CALLBACK_TYPE_NONE,+ Callback: None,+ },+ ];+ let provider = Box::into_raw(Box::new(provider));+ let path = HSTRING::from(root.as_os_str());+ // Our own reads of a placeholder (an indexer of ours, a copy) never fetch it: blocked here,+ // they can't wait on themselves.+ let flags =+ CF_CONNECT_FLAG_BLOCK_SELF_IMPLICIT_HYDRATION | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH;+ // SAFETY: the table lives for the call (Windows copies it); the provider lives until the+ // CloudFolder's drop disconnects.+ match unsafe { CfConnectSyncRoot(&path, table.as_ptr(), Some(provider.cast()), flags) } {+ Ok(key) => Ok(CloudFolder {+ key,+ provider,+ root: root.to_path_buf(),+ shell: false,+ watcher: None,+ stop: Arc::new(std::sync::atomic::AtomicBool::new(false)),+ }),+ Err(e) => {+ // SAFETY: not handed to Windows after all.+ drop(unsafe { Box::from_raw(provider) });+ Err(e.to_string())+ }+ }+}++/// The id Windows wants a cloud folder registered under: `Skykeep!<this user's SID>!<account>`,+/// at most 174 characters (Microsoft's limit; longer is refused with ERROR_INSUFFICIENT_BUFFER).+pub fn sync_root_id(account: &str) -> Result<String, String> {+ use windows::Win32::Foundation::{CloseHandle, LocalFree, HANDLE, HLOCAL};+ use windows::Win32::Security::Authorization::ConvertSidToStringSidW;+ use windows::Win32::Security::{GetTokenInformation, TokenUser, TOKEN_QUERY, TOKEN_USER};+ use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};+ // SAFETY: the token is closed and the SID string freed before returning; the buffer is+ // sized by the first call.+ let sid = unsafe {+ let mut token = HANDLE::default();+ OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token)+ .map_err(|e| e.to_string())?;+ let mut needed = 0u32;+ let _ = GetTokenInformation(token, TokenUser, None, 0, &mut needed);+ let mut buf = vec![0u8; needed as usize];+ let got = GetTokenInformation(+ token,+ TokenUser,+ Some(buf.as_mut_ptr().cast()),+ needed,+ &mut needed,+ );+ let _ = CloseHandle(token);+ got.map_err(|e| e.to_string())?;+ let user = &*buf.as_ptr().cast::<TOKEN_USER>();+ let mut text = windows::core::PWSTR::null();+ ConvertSidToStringSidW(user.User.Sid, &mut text).map_err(|e| e.to_string())?;+ let sid = text.to_string().map_err(|e| e.to_string());+ let _ = LocalFree(Some(HLOCAL(text.0.cast())));+ sid?+ };+ // The account, as letters and digits only: `!` separates the parts.+ let account: String = account+ .chars()+ .filter(char::is_ascii_alphanumeric)+ .take(40)+ .collect();+ let id = format!("Skykeep!{sid}!{account}");+ Ok(id.chars().take(174).collect())+}++/// Stop being the folder's provider for good: its placeholders go back to plain files the+/// shell no longer fetches. Only at sign-out or when the person turns the folder off.+pub fn unregister(root: &Path, id: &str) {+ let _ =+ windows::Storage::Provider::StorageProviderSyncRootManager::Unregister(&HSTRING::from(id));+ // SAFETY: a path string alive for the call.+ let _ = unsafe { CfUnregisterSyncRoot(&HSTRING::from(root.as_os_str())) };+}++/// What a request is for, copied out of Windows' callback so it can be answered elsewhere.+struct Request {+ connection: CF_CONNECTION_KEY,+ transfer: i64,+ request: i64,+ item: Option<DriveItem>,+ size: u64,+ /// The file's full path now ("C:\\Users\\…"), from the volume and Windows' path for it.+ path: PathBuf,+}++// SAFETY: plain values; the connection key is usable from any thread.+unsafe impl Send for Request {}++/// The callback's request, and our provider: read whole before the callback returns.+unsafe fn request(info: *const CF_CALLBACK_INFO) -> Option<(Request, &'static Provider)> {+ let info = unsafe { info.as_ref()? };+ let provider = unsafe { info.CallbackContext.cast::<Provider>().as_ref()? };+ let identity = if info.FileIdentity.is_null() || info.FileIdentityLength == 0 {+ None+ } else {+ // SAFETY: Windows gives the identity we stored, for the length it says.+ let bytes = unsafe {+ std::slice::from_raw_parts(+ info.FileIdentity.cast::<u8>(),+ info.FileIdentityLength as usize,+ )+ };+ serde_json::from_slice::<DriveItem>(bytes).ok()+ };+ Some((+ Request {+ connection: info.ConnectionKey,+ transfer: info.TransferKey,+ request: info.RequestKey,+ item: identity,+ size: u64::try_from(info.FileSize).unwrap_or(0),+ // SAFETY: Windows' strings, valid for this call (copied here).+ path: unsafe { full_path(info.VolumeDosName, info.NormalizedPath) },+ },+ provider,+ ))+}++unsafe extern "system" fn on_fetch_data(+ info: *const CF_CALLBACK_INFO,+ params: *const CF_CALLBACK_PARAMETERS,+) {+ let caught = std::panic::catch_unwind(|| {+ // SAFETY: Windows' pointers, valid for this call; everything needed is copied out.+ let Some((req, provider)) = (unsafe { request(info) }) else {+ return;+ };+ let fetch = unsafe { (*params).Anonymous.FetchData };+ let offset = u64::try_from(fetch.RequiredFileOffset).unwrap_or(0);+ let len = u64::try_from(fetch.RequiredLength).unwrap_or(0);+ let (remote, rt) = (Arc::clone(&provider.remote), provider.rt.clone());+ rt.spawn(async move { fetch_data(remote.as_ref(), req, offset, len).await });+ });+ if caught.is_err() {+ eprintln!("quince: a file request failed inside Skykeep; it is answered as failed");+ }+}++/// A file's bytes, `len` from `offset`, from Apple to Windows: every piece in order, then done;+/// or the whole range failed, once.+async fn fetch_data(remote: &dyn Remote, req: Request, offset: u64, len: u64) {+ let Some(item) = req.item.clone() else {+ fail_data(&req, offset, len, STATUS_CLOUD_FILE_UNSUCCESSFUL);+ return;+ };+ let pieces = transfer::pieces(req.size, offset, len, CHUNK);+ let Some(&(start, _)) = pieces.first() else {+ fail_data(&req, offset, len, STATUS_CLOUD_FILE_UNSUCCESSFUL);+ return;+ };+ let end = pieces.last().map_or(start, |&(at, n)| at + n);+ 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);+ let bytes = if n == 0 {+ Vec::new()+ } else {+ match remote.read_part(&item, at, n).await {+ Ok(part) if part.bytes.len() as u64 == n => part.bytes,+ Ok(part) => {+ eprintln!(+ "quince: {} answered {} of {n} bytes at {at}",+ item.drivewsid,+ part.bytes.len()+ );+ fail_data(&req, at, end - at, STATUS_CLOUD_FILE_UNSUCCESSFUL);+ return;+ }+ Err(e) => {+ eprintln!("quince: fetching {}: {e}", item.drivewsid);+ fail_data(&req, at, end - at, STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE);+ return;+ }+ }+ };+ if let Err(e) = transfer_data(&req, at, &bytes, STATUS_SUCCESS) {+ // Windows took the request back (the reader closed it, it timed out): nothing more+ // is sent for it.+ eprintln!("quince: handing {} to Windows: {e}", item.drivewsid);+ return;+ }+ }+}++fn report(req: &Request, total: u64, done: u64) {+ // SAFETY: a plain call with keys Windows gave us.+ let _ = unsafe {+ CfReportProviderProgress(req.connection, req.transfer, total as i64, done as i64)+ };+}++/// One piece handed to Windows, or with a failure status the range it fails.+fn transfer_data(+ req: &Request,+ offset: u64,+ bytes: &[u8],+ status: NTSTATUS,+) -> windows::core::Result<()> {+ transfer_range(+ req,+ offset,+ bytes.len() as u64,+ bytes.as_ptr().cast(),+ status,+ )+}++fn fail_data(req: &Request, offset: u64, len: u64, status: NTSTATUS) {+ // Failed whole, over the very range the pieces would have covered: a short or empty failure+ // can be refused, leaving the reader waiting out the timeout (cloud-filter-rs notes).+ let whole = u64::MAX / transfer::BLOCK * transfer::BLOCK;+ let (start, n) = transfer::pieces(req.size, offset, len, whole)+ .first()+ .copied()+ .unwrap_or((offset / transfer::BLOCK * transfer::BLOCK, transfer::BLOCK));+ let _ = transfer_range(req, start, n, std::ptr::null(), status);+}++fn transfer_range(+ req: &Request,+ offset: u64,+ len: u64,+ buffer: *const c_void,+ status: NTSTATUS,+) -> windows::core::Result<()> {+ let info = CF_OPERATION_INFO {+ StructSize: size_of::<CF_OPERATION_INFO>() as u32,+ Type: CF_OPERATION_TYPE_TRANSFER_DATA,+ ConnectionKey: req.connection,+ TransferKey: req.transfer,+ RequestKey: req.request,+ ..Default::default()+ };+ let mut params = CF_OPERATION_PARAMETERS {+ ParamSize: size_of::<CF_OPERATION_PARAMETERS>() as u32,+ Anonymous: CF_OPERATION_PARAMETERS_0 {+ TransferData: CF_OPERATION_PARAMETERS_0_0 {+ Flags: CF_OPERATION_TRANSFER_DATA_FLAG_NONE,+ CompletionStatus: status,+ Buffer: buffer,+ Offset: offset as i64,+ Length: len as i64,+ },+ },+ };+ // SAFETY: the buffer (or null, on failure) is alive for the call.+ unsafe { CfExecute(&info, &mut params) }+}++unsafe extern "system" fn on_fetch_placeholders(+ info: *const CF_CALLBACK_INFO,+ _params: *const CF_CALLBACK_PARAMETERS,+) {+ let caught = std::panic::catch_unwind(|| {+ // SAFETY: as in on_fetch_data.+ let Some((req, provider)) = (unsafe { request(info) }) else {+ return;+ };+ let (remote, store, rt) = (+ Arc::clone(&provider.remote),+ Arc::clone(&provider.store),+ provider.rt.clone(),+ );+ let folder = req+ .item+ .as_ref()+ .map_or(ROOT.to_string(), |i| i.drivewsid.clone());+ provider+ .listed+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .insert(folder, req.path.clone());+ rt.spawn(async move { fetch_placeholders(remote.as_ref(), store.as_ref(), req).await });+ });+ if caught.is_err() {+ eprintln!("quince: a folder request failed inside Skykeep; it is answered as failed");+ }+}++/// A folder's items as placeholders, from the library (from Apple first when the folder was+/// never listed), one per call, the last marking the folder whole.+async fn fetch_placeholders(remote: &dyn Remote, store: &dyn Store, req: Request) {+ let folder = req+ .item+ .as_ref()+ .map_or(ROOT.to_string(), |i| i.drivewsid.clone());+ let items = match store.children(&folder) {+ Some(items) => items,+ None => match remote.list(&folder).await {+ Ok(items) => items,+ Err(e) => {+ eprintln!("quince: listing {folder}: {e}");+ let _ = transfer_placeholders(+ &req,+ &mut [],+ 0,+ true,+ STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE,+ );+ return;+ }+ },+ };+ let named = named_on(&items, Names::Windows);+ let total = named.len() as i64;+ if named.is_empty() {+ let _ = transfer_placeholders(&req, &mut [], 0, true, STATUS_SUCCESS);+ return;+ }+ let last = named.len() - 1;+ for (i, (name, item)) in named.into_iter().enumerate() {+ let size = placeholder_size(remote, &item).await;+ let made = Placeholder::of(&name, &item, size);+ let mut one = [made.info()];+ if let Err(e) = transfer_placeholders(&req, &mut one, total, i == last, STATUS_SUCCESS) {+ eprintln!("quince: {name} in {folder}: {e}");+ return;+ }+ }+}++/// The size a placeholder is made at: the listing's, except where Apple serves another (Pages,+/// Numbers and Keynote packages), which is asked once — a placeholder at the wrong size gives+/// the reader a broken file.+async fn placeholder_size(remote: &dyn Remote, item: &DriveItem) -> u64 {+ let listed = u64::try_from(item.size.unwrap_or(0)).unwrap_or(0);+ if !served_size_differs(item) {+ return listed;+ }+ match remote.read_part(item, 0, 1).await {+ Ok(part) => part.total.unwrap_or(listed),+ Err(_) => listed,+ }+}++fn transfer_placeholders(+ req: &Request,+ array: &mut [CF_PLACEHOLDER_CREATE_INFO],+ total: i64,+ last: bool,+ status: NTSTATUS,+) -> windows::core::Result<()> {+ let info = CF_OPERATION_INFO {+ StructSize: size_of::<CF_OPERATION_INFO>() as u32,+ Type: CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS,+ ConnectionKey: req.connection,+ TransferKey: req.transfer,+ RequestKey: req.request,+ ..Default::default()+ };+ let mut params = CF_OPERATION_PARAMETERS {+ ParamSize: size_of::<CF_OPERATION_PARAMETERS>() as u32,+ Anonymous: CF_OPERATION_PARAMETERS_0 {+ TransferPlaceholders: CF_OPERATION_PARAMETERS_0_4 {+ // The last says the folder is whole: no more requests for it, its updates come+ // from us (`refresh`).+ Flags: if last {+ CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_DISABLE_ON_DEMAND_POPULATION+ } else {+ CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_NONE+ },+ CompletionStatus: status,+ PlaceholderTotalCount: total,+ PlaceholderArray: array.as_mut_ptr(),+ PlaceholderCount: array.len() as u32,+ EntriesProcessed: 0,+ },+ },+ };+ // SAFETY: the array and the names and identities it points at are alive for the call.+ unsafe { CfExecute(&info, &mut params) }+}++/// A path Windows gives as a volume ("C:") and a path on it ("\\Users\\…"), joined.+unsafe fn full_path(volume: PCWSTR, path: PCWSTR) -> PathBuf {+ let text = |p: PCWSTR| {+ if p.is_null() {+ String::new()+ } else {+ unsafe { p.to_string() }.unwrap_or_default()+ }+ };+ 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) {+ let info = CF_OPERATION_INFO {+ StructSize: size_of::<CF_OPERATION_INFO>() as u32,+ Type: kind,+ ConnectionKey: req.connection,+ TransferKey: req.transfer,+ RequestKey: req.request,+ ..Default::default()+ };+ let mut params = CF_OPERATION_PARAMETERS {+ ParamSize: size_of::<CF_OPERATION_PARAMETERS>() as u32,+ ..Default::default()+ };+ // 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!(+ "quince: answering Windows about {}: {e}",+ req.path.display()+ );+ }+}++unsafe extern "system" fn on_delete(+ info: *const CF_CALLBACK_INFO,+ _params: *const CF_CALLBACK_PARAMETERS,+) {+ 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);+ }+ });+}++unsafe extern "system" fn on_rename(+ info: *const CF_CALLBACK_INFO,+ _params: *const CF_CALLBACK_PARAMETERS,+) {+ 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);+ }+ });+}++/// Deleted in Explorer, and done: into Apple's Recently Deleted, where it can be put back for+/// 30 days. Only for this notice from Windows, a person's own delete: Skykeep never deletes at+/// Apple because a file is missing here (a client that did lost people's files, Nextcloud #3444).+unsafe extern "system" fn on_deleted(+ info: *const CF_CALLBACK_INFO,+ _params: *const CF_CALLBACK_PARAMETERS,+) {+ let _ = std::panic::catch_unwind(|| {+ // SAFETY: as in on_fetch_data.+ let Some((req, provider)) = (unsafe { request(info) }) else {+ return;+ };+ let Some(item) = req.item.clone() else { return };+ let (remote, rt) = (Arc::clone(&provider.remote), provider.rt.clone());+ rt.spawn(async move {+ // Done means gone: a delete that was undone before it was done leaves the file.+ if req.path.exists() {+ return;+ }+ for wait in [0u64, 2, 8, 30] {+ tokio::time::sleep(std::time::Duration::from_secs(wait)).await;+ match remote.delete(&item).await {+ Ok(()) => return,+ Err(e) => eprintln!("quince: deleting {} at Apple: {e}", item.drivewsid),+ }+ }+ });+ });+}++/// Renamed or moved in Explorer, and done: the same at Apple — moved into the folder it is in+/// now, then given its new name, each only if it changed.+unsafe extern "system" fn on_renamed(+ info: *const CF_CALLBACK_INFO,+ params: *const CF_CALLBACK_PARAMETERS,+) {+ let _ = std::panic::catch_unwind(|| {+ // SAFETY: as in on_fetch_data.+ let Some((req, provider)) = (unsafe { request(info) }) else {+ return;+ };+ let Some(item) = req.item.clone() else { return };+ let _ = params;+ let (remote, rt) = (Arc::clone(&provider.remote), provider.rt.clone());+ rt.spawn(async move { renamed(remote.as_ref(), item, &req.path).await });+ });+}++async fn renamed(remote: &dyn Remote, item: DriveItem, now: &Path) {+ let Some(name) = now.file_name().and_then(|n| n.to_str()) else {+ return;+ };+ let Some(parent) = now.parent().and_then(folder_item) else {+ // Moved out of the folder Skykeep keeps: nothing at Apple changes until it is deleted.+ return;+ };+ let moved = item.parent_id.as_deref() != Some(parent.drivewsid.as_str());+ for wait in [0u64, 2, 8, 30] {+ tokio::time::sleep(std::time::Duration::from_secs(wait)).await;+ let step = async {+ if moved {+ remote.move_to(&item, &parent).await?;+ }+ if item.display_name() != name {+ remote.rename(&item, name).await?;+ }+ Ok::<(), String>(())+ };+ match step.await {+ Ok(()) => return,+ Err(e) => eprintln!("quince: renaming {} at Apple: {e}", item.drivewsid),+ }+ }+}++/// The Apple folder a folder here stands for: its placeholder's identity, or the root's (whose+/// registration carries none). `None` for a folder that is not one of ours.+pub(crate) fn folder_item(dir: &Path) -> Option<DriveItem> {+ match identity_of(dir) {+ Some(item) => Some(item),+ None if is_sync_root(dir) => Some(DriveItem {+ drivewsid: ROOT.to_string(),+ parent_id: None,+ name: String::new(),+ extension: None,+ kind: "FOLDER".into(),+ size: None,+ child_count: None,+ created_at: None,+ modified_at: None,+ etag: None,+ }),+ None => None,+ }+}++/// A placeholder's identity (the Apple item it was made from), read without fetching anything.+pub(crate) fn identity_of(path: &Path) -> Option<DriveItem> {+ let (state, identity) = placeholder_state(path)?;+ let _ = state;+ serde_json::from_slice(&identity?).ok()+}++/// Whether `path` is the cloud folder itself.+fn is_sync_root(path: &Path) -> bool {+ use windows::Storage::Provider::StorageProviderSyncRootManager;+ use windows::Storage::StorageFolder;+ StorageFolder::GetFolderFromPathAsync(&HSTRING::from(path.as_os_str()))+ .and_then(|op| op.join())+ .and_then(|f| StorageProviderSyncRootManager::GetSyncRootInformationForFolder(&f))+ .and_then(|info| info.Path())+ .and_then(|p| p.Path())+ .is_ok_and(|p| std::path::Path::new(&p.to_string_lossy()) == path)+}++/// A file or folder's placeholder state (whether it is in sync) and identity; `None` for a+/// plain file that is not a placeholder. Opened for its attributes only: nothing is fetched.+pub(crate) fn placeholder_state(path: &Path) -> Option<(CF_IN_SYNC_STATE, Option<Vec<u8>>)> {+ let handle = open_attributes(path).ok()?;+ let mut buf = vec![0u8; size_of::<CF_PLACEHOLDER_BASIC_INFO>() + 4096];+ let mut got = 0u32;+ // SAFETY: the buffer is as long as said; the handle is closed after.+ let read = unsafe {+ CfGetPlaceholderInfo(+ handle.0,+ CF_PLACEHOLDER_INFO_BASIC,+ buf.as_mut_ptr().cast(),+ buf.len() as u32,+ Some(&mut got),+ )+ };+ if read.is_err() {+ return None;+ }+ // SAFETY: Windows wrote a CF_PLACEHOLDER_BASIC_INFO, its identity following it.+ let info = unsafe { &*buf.as_ptr().cast::<CF_PLACEHOLDER_BASIC_INFO>() };+ let len = info.FileIdentityLength as usize;+ let at = std::mem::offset_of!(CF_PLACEHOLDER_BASIC_INFO, FileIdentity);+ let identity = (len > 0 && at + len <= got as usize).then(|| buf[at..at + len].to_vec());+ Some((info.InSyncState, identity))+}++/// A handle that only reads attributes (folders too), closed when dropped.+pub(crate) struct Handle(pub windows::Win32::Foundation::HANDLE);++impl Drop for Handle {+ fn drop(&mut self) {+ // SAFETY: our own handle, closed once.+ unsafe {+ let _ = windows::Win32::Foundation::CloseHandle(self.0);+ }+ }+}++pub(crate) fn open_attributes(path: &Path) -> windows::core::Result<Handle> {+ use windows::Win32::Storage::FileSystem::*;+ // SAFETY: a path string alive for the call.+ unsafe {+ CreateFileW(+ &HSTRING::from(path.as_os_str()),+ FILE_READ_ATTRIBUTES.0,+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,+ None,+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ None,+ )+ }+ .map(Handle)+}++/// One placeholder's name, identity and metadata, kept alive while Windows reads them.+struct Placeholder {+ name: Vec<u16>,+ identity: Vec<u8>,+ meta: CF_FS_METADATA,+}++impl Placeholder {+ fn of(name: &str, item: &DriveItem, size: u64) -> Placeholder {+ let folder = item.is_container();+ let time = |secs: Option<i64>| secs.map_or(0, filetime);+ let modified = time(item.modified_at.or(item.created_at));+ Placeholder {+ name: name.encode_utf16().chain(Some(0)).collect(),+ // The item itself: a request for it is answered from this, with no lookup.+ identity: serde_json::to_vec(item).unwrap_or_default(),+ meta: CF_FS_METADATA {+ BasicInfo: FILE_BASIC_INFO {+ CreationTime: time(item.created_at.or(item.modified_at)),+ LastAccessTime: modified,+ LastWriteTime: modified,+ ChangeTime: modified,+ FileAttributes: if folder {+ FILE_ATTRIBUTE_DIRECTORY.0+ } else {+ FILE_ATTRIBUTE_NORMAL.0+ },+ },+ FileSize: if folder { 0 } else { size as i64 },+ },+ }+ }++ fn info(&self) -> CF_PLACEHOLDER_CREATE_INFO {+ CF_PLACEHOLDER_CREATE_INFO {+ RelativeFileName: PCWSTR(self.name.as_ptr()),+ FsMetadata: self.meta,+ FileIdentity: self.identity.as_ptr().cast(),+ FileIdentityLength: self.identity.len() as u32,+ // In sync as made. A folder is filled when it is first opened (FETCH_PLACEHOLDERS),+ // so it is never made as already populated.+ Flags: CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC,+ ..Default::default()+ }+ }+}++/// Seconds since 1970 as Windows' FILETIME: 100 ns ticks since 1601.+fn filetime(secs: i64) -> i64 {+ (secs + 11_644_473_600).saturating_mul(10_000_000)+}diff --git a/crates/quince-vfs/src/cloud_local.rs b/crates/quince-vfs/src/cloud_local.rsnew file mode 100644index 0000000..b8e5b10--- /dev/null+++ b/crates/quince-vfs/src/cloud_local.rs@@ -0,0 +1,434 @@+//! Changes made in File Explorer's iCloud Drive, sent to Apple: new files and folders, and+//! files saved over. (Deletes and renames come as Windows' own notices, in `cloud.rs`.)+//!+//! What is waiting is the files themselves: Windows keeps each placeholder's "in sync" mark on+//! disk, so a file saved here is out of sync until Apple has it, and a file never sent is no+//! placeholder at all. A scan at start finds whatever changed while Skykeep was closed, and+//! nothing else has to be remembered — no second record that could disagree with the disk.+//!+//! Each file is sent once it has been left alone a moment (a save is several writes), from a+//! copy taken first, so a program still writing can never send half a file. It is marked in+//! sync only if it is still exactly what was sent; otherwise it goes again. A file changed at+//! Apple meanwhile is kept beside it as a conflicted copy, never written over (`writes::send`).++use crate::cloud::{folder_item, identity_of, placeholder_state};+use crate::writes::{send, stays_local, SETTLE};+use crate::Remote;+use quince_core::drive::DriveItem;+use std::collections::HashMap;+use std::path::{Path, PathBuf};+use std::sync::Arc;+use std::time::{Instant, SystemTime};+use windows::core::HSTRING;+use windows::Win32::Storage::CloudFilters::*;++/// The files Explorer and Windows keep for themselves in every folder, never sent: left "syncing"+/// for ever if they stay unmarked (Nextcloud #10995), so they are marked in sync where they are.+fn windows_own(name: &str) -> bool {+ name.eq_ignore_ascii_case("desktop.ini") || name.eq_ignore_ascii_case("thumbs.db")+}++/// Never sent, never marked: editors' scratch files and Office's "~$" locks.+fn scratch(name: &str) -> bool {+ stays_local(name) || name.starts_with("~$")+}++/// Watch `root` and send what changes there, until `stop` is set. Starts with a scan.+pub fn watch(+ root: PathBuf,+ remote: Arc<dyn Remote>,+ staging: PathBuf,+ rt: tokio::runtime::Handle,+ stop: Arc<std::sync::atomic::AtomicBool>,+) -> 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));+ let watched = root.clone();+ std::thread::Builder::new()+ .name("quince:cloud-watch".into())+ .spawn(move || listen(&watched, &tx, &stop))+ .ok()+}++/// Every file and folder under `root` that is not in sync, without opening a folder never+/// listed (that would fetch it) or reading any file.+fn scan(root: &Path) -> Vec<PathBuf> {+ let mut out = Vec::new();+ let mut dirs = vec![root.to_path_buf()];+ while let Some(dir) = dirs.pop() {+ for entry in std::fs::read_dir(&dir).into_iter().flatten().flatten() {+ let path = entry.path();+ let Ok(meta) = entry.metadata() else { continue };+ let pending = match placeholder_state(&path) {+ None => true,+ Some((state, _)) => state != CF_IN_SYNC_STATE_IN_SYNC,+ };+ if pending {+ out.push(path.clone());+ }+ // A folder placeholder still to be listed (recall on open) is all Apple's: nothing+ // of ours can be in it.+ use std::os::windows::fs::MetadataExt;+ const RECALL_ON_OPEN: u32 = 0x0004_0000;+ if meta.is_dir() && meta.file_attributes() & RECALL_ON_OPEN == 0 {+ dirs.push(path);+ }+ }+ }+ out+}++/// Windows' change notices for everything under `root`, as paths, until `stop`.+fn listen(+ root: &Path,+ tx: &tokio::sync::mpsc::UnboundedSender<PathBuf>,+ stop: &std::sync::atomic::AtomicBool,+) {+ use windows::Win32::Storage::FileSystem::*;+ // SAFETY: a path string alive for the call; the handle is closed when this returns.+ let Ok(dir) = (unsafe {+ CreateFileW(+ &HSTRING::from(root.as_os_str()),+ FILE_LIST_DIRECTORY.0,+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,+ None,+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ None,+ )+ }) else {+ eprintln!("quince: watching {} for changes failed", root.display());+ return;+ };+ let _close = crate::cloud::Handle(dir);+ let mut buf = vec![0u32; 16 * 1024];+ while !stop.load(std::sync::atomic::Ordering::Relaxed) {+ let mut got = 0u32;+ // SAFETY: the buffer is ours and as long as said; the call waits for changes.+ let read = unsafe {+ ReadDirectoryChangesW(+ dir,+ buf.as_mut_ptr().cast(),+ (buf.len() * 4) as u32,+ true,+ FILE_NOTIFY_CHANGE_FILE_NAME+ | FILE_NOTIFY_CHANGE_DIR_NAME+ | FILE_NOTIFY_CHANGE_LAST_WRITE+ | FILE_NOTIFY_CHANGE_SIZE+ | FILE_NOTIFY_CHANGE_ATTRIBUTES,+ Some(&mut got),+ None,+ None,+ )+ };+ if read.is_err() {+ if stop.load(std::sync::atomic::Ordering::Relaxed) {+ return;+ }+ std::thread::sleep(std::time::Duration::from_secs(1));+ continue;+ }+ if got == 0 {+ // More changed than fit: everything is looked at again.+ for path in scan(root) {+ let _ = tx.send(path);+ }+ continue;+ }+ let bytes = buf.as_ptr().cast::<u8>();+ let mut at = 0usize;+ loop {+ // SAFETY: Windows wrote a chain of FILE_NOTIFY_INFORMATION within `got` bytes.+ let info = unsafe { &*bytes.add(at).cast::<FILE_NOTIFY_INFORMATION>() };+ let len = info.FileNameLength as usize / 2;+ // SAFETY: the name follows the header, `len` UTF-16 units long.+ let name = unsafe { std::slice::from_raw_parts(info.FileName.as_ptr(), len) };+ let path = root.join(String::from_utf16_lossy(name));+ if info.Action != FILE_ACTION_REMOVED && info.Action != FILE_ACTION_RENAMED_OLD_NAME {+ let _ = tx.send(path);+ }+ if info.NextEntryOffset == 0 {+ break;+ }+ at += info.NextEntryOffset as usize;+ }+ }+}++/// Each path sent once nothing has touched it for SETTLE, one at a time, tried again later if+/// Apple could not take it.+async fn settle_and_send(+ mut rx: tokio::sync::mpsc::UnboundedReceiver<PathBuf>,+ remote: Arc<dyn Remote>,+ staging: PathBuf,+) {+ let mut touched: HashMap<PathBuf, Instant> = HashMap::new();+ let mut tick = tokio::time::interval(std::time::Duration::from_millis(500));+ loop {+ tokio::select! {+ got = rx.recv() => match got {+ Some(path) => { touched.insert(path, Instant::now()); }+ None => return,+ },+ _ = tick.tick() => {+ let due: Vec<PathBuf> = touched+ .iter()+ .filter(|(_, at)| at.elapsed() >= SETTLE)+ .map(|(p, _)| p.clone())+ .collect();+ for path in due {+ touched.remove(&path);+ if let Err(e) = sync_path(remote.as_ref(), &staging, &path).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));+ }+ }+ }+ }+ }+}++/// 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> {+ let Some(name) = path+ .file_name()+ .and_then(|n| n.to_str())+ .map(str::to_string)+ else {+ return Ok(());+ };+ if scratch(&name) || !path.exists() {+ return Ok(());+ }+ let state = placeholder_state(path);+ if windows_own(&name) {+ if state.is_none() {+ let _ = convert(path, None);+ }+ return Ok(());+ }+ let Some(parent) = path.parent() else {+ return Ok(());+ };+ let folder = match folder_item(parent) {+ Some(f) => f,+ // Its folder isn't at Apple yet: it is sent with that folder's contents once it is.+ None => return Ok(()),+ };+ if path.is_dir() {+ if state.is_some() {+ // A folder of ours whose own attributes changed: kept or freed, with what is in it.+ let _ = mark_in_sync(path);+ keep_or_free(path).await;+ 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?;+ }+ 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.+ keep_or_free(path).await;+ return Ok(());+ }+ Some(_) => identity_of(path),+ None => None,+ };+ // A copy to send from, so bytes still being written never reach Apple half done.+ let before = stamp(path).ok_or("the file went away")?;+ let dir = staging.join(format!("{}-{}", std::process::id(), stamp_key(&before)));+ 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 _ = std::fs::remove_dir_all(&dir);+ let sent = sent?;+ if stamp(path) != Some(before) {+ // Changed while it was being sent: it goes again, as it is now.+ return Err("changed while sending; sending again".into());+ }+ let mut here = path.to_path_buf();+ if sent.conflicted {+ // Apple's file had changed elsewhere: ours went beside it, under its conflicted name,+ // and is named so here too; Apple's comes down in its place with the next listing.+ here = path.with_file_name(sent.item.display_name());+ std::fs::rename(path, &here).map_err(|e| e.to_string())?;+ }+ if state.is_some() && !sent.conflicted {+ update(&here, &sent.item).map_err(|e| e.to_string())+ } else {+ convert(&here, Some(&sent.item)).map_err(|e| e.to_string())+ }+}++/// When and how long: what tells a file changed under us.+#[derive(PartialEq, Eq, Clone, Copy)]+struct Stamp(u64, u128);++fn stamp(path: &Path) -> Option<Stamp> {+ let m = std::fs::metadata(path).ok()?;+ let at = m+ .modified()+ .ok()?+ .duration_since(SystemTime::UNIX_EPOCH)+ .ok()?+ .as_nanos();+ Some(Stamp(m.len(), at))+}++fn stamp_key(s: &Stamp) -> String {+ format!("{:x}{:x}", s.0, s.1)+}++fn open_for_update(path: &Path) -> windows::core::Result<crate::cloud::Handle> {+ use windows::Win32::Foundation::GENERIC_ACCESS_RIGHTS;+ use windows::Win32::Storage::FileSystem::*;+ // SAFETY: a path string alive for the call.+ unsafe {+ CreateFileW(+ &HSTRING::from(path.as_os_str()),+ (FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES).0 | GENERIC_ACCESS_RIGHTS(0x4000_0000).0,+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,+ None,+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ None,+ )+ }+ .map(crate::cloud::Handle)+}++/// A file or folder of ours from now on: a placeholder for `item`, in sync (`None`: one that+/// stays here, marked in sync so Explorer never shows it waiting).+fn convert(path: &Path, item: Option<&DriveItem>) -> windows::core::Result<()> {+ let handle = open_for_update(path)?;+ let identity = item+ .map(|i| serde_json::to_vec(i).unwrap_or_default())+ .unwrap_or_default();+ // SAFETY: the identity is alive for the call; the handle is ours.+ unsafe {+ CfConvertToPlaceholder(+ handle.0,+ (!identity.is_empty()).then(|| identity.as_ptr().cast()),+ identity.len() as u32,+ CF_CONVERT_FLAG_MARK_IN_SYNC,+ None,+ None,+ )+ }+}++/// A placeholder now standing for `item` (its new version at Apple), in sync.+fn update(path: &Path, item: &DriveItem) -> windows::core::Result<()> {+ let handle = open_for_update(path)?;+ let identity = serde_json::to_vec(item).unwrap_or_default();+ // SAFETY: as in convert.+ unsafe {+ CfUpdatePlaceholder(+ handle.0,+ None,+ Some(identity.as_ptr().cast()),+ identity.len() as u32,+ None,+ CF_UPDATE_FLAG_MARK_IN_SYNC,+ None,+ None,+ )+ }+}++fn mark_in_sync(path: &Path) -> windows::core::Result<()> {+ let handle = open_for_update(path)?;+ // SAFETY: our handle.+ unsafe {+ CfSetInSyncState(+ handle.0,+ CF_IN_SYNC_STATE_IN_SYNC,+ CF_SET_IN_SYNC_FLAG_NONE,+ None,+ )+ }+}++const PINNED: u32 = 0x0008_0000;+const UNPINNED: u32 = 0x0010_0000;+const NOT_ALL_HERE: u32 = 0x0040_0000; // FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS++/// "Always keep on this device" (pinned): every byte fetched now and kept. "Free up space"+/// (unpinned): the bytes let go, the file still listed and in iCloud. Windows only marks the+/// file; doing it is ours (Microsoft's hydration-policy notes). A folder's choice is its files'+/// too, for the folders already listed in it; a file of a pinned folder made later is pinned+/// by Windows itself.+async fn keep_or_free(path: &Path) {+ use std::os::windows::fs::MetadataExt;+ let Ok(meta) = std::fs::metadata(path) else {+ return;+ };+ let attrs = meta.file_attributes();+ if meta.is_dir() {+ if attrs & (PINNED | UNPINNED) == 0 {+ return;+ }+ for entry in std::fs::read_dir(path).into_iter().flatten().flatten() {+ let child = entry.path();+ if entry+ .metadata()+ .is_ok_and(|m| !m.is_dir() || m.file_attributes() & 0x0004_0000 == 0)+ {+ Box::pin(keep_or_free(&child)).await;+ }+ }+ return;+ }+ let here = attrs & NOT_ALL_HERE == 0;+ if attrs & PINNED != 0 && !here {+ let file = path.to_path_buf();+ // Fetched through Skykeep's own requests: waited on off the runtime's threads, which+ // answer them.+ let fetched = tokio::task::spawn_blocking(move || hydrate(&file)).await;+ if let Ok(Err(e)) = fetched {+ eprintln!("quince: keeping {} on this device: {e}", path.display());+ }+ } else if attrs & UNPINNED != 0 && here {+ if let Err(e) = dehydrate(path) {+ // Open in a program now: freed once it is closed and touched again.+ eprintln!("quince: freeing {}: {e}", path.display());+ }+ }+}++fn hydrate(path: &Path) -> windows::core::Result<()> {+ let handle = crate::cloud::open_attributes(path)?;+ // SAFETY: our handle; the whole file (length -1).+ unsafe { CfHydratePlaceholder(handle.0, 0, -1, CF_HYDRATE_FLAG_NONE, None) }+}++/// The file's bytes let go, never under a program that has it open (opened alone).+fn dehydrate(path: &Path) -> windows::core::Result<()> {+ use windows::Win32::Foundation::GENERIC_ACCESS_RIGHTS;+ use windows::Win32::Storage::FileSystem::*;+ // SAFETY: a path string alive for the call; the handle is closed when dropped.+ let handle = crate::cloud::Handle(unsafe {+ CreateFileW(+ &HSTRING::from(path.as_os_str()),+ (FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES).0 | GENERIC_ACCESS_RIGHTS(0x4000_0000).0,+ FILE_SHARE_NONE,+ None,+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ None,+ )+ }?);+ // SAFETY: our handle; the whole file.+ unsafe { CfDehydratePlaceholder(handle.0, 0, -1, CF_DEHYDRATE_FLAG_NONE, None) }+}diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rsindex 7ac41d8..189fb5a 100644--- a/crates/quince-vfs/src/lib.rs+++ b/crates/quince-vfs/src/lib.rs@@ -6,8 +6,13 @@//! own front end ([`fuse`] on Linux) asks it and never waits on the network for what the//! library already knows. Design: docs/mount-design.md.+#[cfg(windows)]+pub mod cloud;+#[cfg(windows)]+mod cloud_local;#[cfg(target_os = "linux")]pub mod fuse;+pub mod transfer;pub mod writes;use quince_core::drive::DriveItem;@@ -262,39 +267,113 @@ impl Inodes {}}+/// Which file system a name is made for: the folder on Linux (FUSE), or Windows Explorer's+/// (Cloud Files on NTFS), whose names forbid more and ignore case.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum Names {+ Linux,+ Windows,+}++impl Names {+ /// This computer's.+ pub const HERE: Names = if cfg!(windows) {+ Names::Windows+ } else {+ Names::Linux+ };+}+/// A folder's items as names a file manager shows: the name with its extension, NFC as Apple/// keeps them. Two that would share a name (Apple's names differ by case or by a character a/// path cannot hold) are told apart, never one hiding the other.#[must_use]pub fn named(items: &[DriveItem]) -> Vec<(String, DriveItem)> {- let base_of = |item: &DriveItem| {- // "/" cannot be in a name on Linux, and NUL nowhere: Apple allows the first.- let base: String = item- .display_name()- .chars()- .map(|c| match c {- '/' => '∕',- '\0' => '�',- c => c,- })- .collect();- if base == "." || base == ".." || base.is_empty() {- format!("{base}_")- } else {- base+ named_on(items, Names::HERE)+}++/// One of Apple's names as `on` can hold it. Linux forbids only "/" and NUL. Windows also+/// forbids `< > : " \ | ? *` and control characters, reserves device names (CON, NUL, COM1…,+/// with any extension) and drops a trailing dot or space: each is shown as its look-alike, as+/// rclone's encoding does, so the file is there under a name that reads the same. A name+/// Windows can't hold, left as it is, failed the whole folder in Nextcloud (#9524) — every+/// file in it unreachable until a restart.+#[must_use]+pub fn shown_name(name: &str, on: Names) -> String {+ let mut base: String = name+ .chars()+ .map(|c| match (on, c) {+ (Names::Linux, '/') => '∕',+ (Names::Linux, '\0') => '�',+ (Names::Windows, '\0') => '\u{2400}',+ (Names::Linux, c) => c,+ (Names::Windows, '<') => '<',+ (Names::Windows, '>') => '>',+ (Names::Windows, ':') => ':',+ (Names::Windows, '"') => '"',+ (Names::Windows, '/') => '/',+ (Names::Windows, '\\') => '\',+ (Names::Windows, '|') => '|',+ (Names::Windows, '?') => '?',+ (Names::Windows, '*') => '*',+ // Control characters as their pictures (␁…␟).+ (Names::Windows, c) if (c as u32) < 0x20 => {+ char::from_u32(0x2400 + c as u32).unwrap_or('�')+ }+ (Names::Windows, c) => c,+ })+ .collect();+ if base == "." || base == ".." || base.is_empty() {+ return format!("{base}_");+ }+ if on == Names::Windows {+ // A trailing dot or space is dropped by Windows itself: the name would change under us.+ if base.ends_with('.') {+ base.pop();+ base.push('.');+ } else if base.ends_with(' ') {+ base.pop();+ base.push('␠');}+ // CON, PRN, AUX, NUL, COM0–9, LPT0–9 — alone or before an extension — open a device.+ let stem = base+ .split('.')+ .next()+ .unwrap_or("")+ .trim_end()+ .to_ascii_uppercase();+ let device = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")+ || ((stem.starts_with("COM") || stem.starts_with("LPT"))+ && stem.len() == 4+ && stem.as_bytes()[3].is_ascii_digit());+ if device {+ let cut = base.find('.').unwrap_or(base.len());+ base.insert(cut, '_');+ }+ }+ base+}++/// [`named`] for the names `on` holds. On Windows two names that differ only in case are one+/// name (NTFS ignores case), so they are told apart like any two the same (Nextcloud #9736).+#[must_use]+pub fn named_on(items: &[DriveItem], on: Names) -> Vec<(String, DriveItem)> {+ let base_of = |item: &DriveItem| shown_name(&item.display_name(), on);+ let key = |name: &str| match on {+ Names::Linux => name.to_string(),+ Names::Windows => name.to_uppercase(),};// Every name the folder holds as it is, so a number given to tell two apart never lands on// a third that is really called that ("Report (2).pdf" beside two "Report.pdf"s).- let real: std::collections::HashSet<String> = items.iter().map(base_of).collect();+ let real: std::collections::HashSet<String> = items.iter().map(|i| key(&base_of(i))).collect();let mut given: std::collections::HashSet<String> = std::collections::HashSet::new();let mut taken: HashMap<String, usize> = HashMap::new();let mut out = Vec::with_capacity(items.len());for item in items {let base = base_of(item);- let n = taken.entry(base.clone()).or_insert(0);+ let n = taken.entry(key(&base)).or_insert(0);*n += 1;- let name = if *n == 1 && given.insert(base.clone()) {+ let name = if *n == 1 && given.insert(key(&base)) {base} else {// "Report (2).pdf": the second of two with the same name.@@ -306,7 +385,7 @@ pub fn named(items: &[DriveItem]) -> Vec<(String, DriveItem)> {}_ => format!("{base} ({n})"),};- if !real.contains(&candidate) && given.insert(candidate.clone()) {+ if !real.contains(&key(&candidate)) && given.insert(key(&candidate)) {break candidate;}*n += 1;@@ -942,7 +1021,10 @@ mod tests {item("c", "a/b", None, "FOLDER", 0, "e"),item("d", "..", None, "FOLDER", 0, "e"),];- let names: Vec<String> = named(&items).into_iter().map(|(n, _)| n).collect();+ let names: Vec<String> = named_on(&items, Names::Linux)+ .into_iter()+ .map(|(n, _)| n)+ .collect();assert_eq!(names, ["Report.pdf", "Report (2).pdf", "a∕b", ".._"]);}@@ -958,6 +1040,35 @@ mod tests {assert_eq!(names, ["Report.pdf", "Report (3).pdf", "Report (2).pdf"]);}+ /// Windows: forbidden characters, device names, a trailing dot or space, and names that+ /// differ only in case all shown, each under its own name (Nextcloud #9524, #9736).+ #[test]+ fn windows_names_are_all_shown() {+ let items = [+ item("a", "a:b?", Some("txt"), "FILE", 1, "e"),+ item("b", "CON", Some("txt"), "FILE", 1, "e"),+ item("c", "notes.", None, "FOLDER", 0, "e"),+ item("d", "Report", Some("pdf"), "FILE", 1, "e"),+ item("e", "report", Some("PDF"), "FILE", 1, "e"),+ ];+ let names: Vec<String> = named_on(&items, Names::Windows)+ .into_iter()+ .map(|(n, _)| n)+ .collect();+ assert_eq!(+ names,+ [+ "a:b?.txt",+ "CON_.txt",+ "notes.",+ "Report.pdf",+ "report (2).PDF"+ ]+ );+ // Linux keeps its own rules.+ assert_eq!(shown_name("a:b?", Names::Linux), "a:b?");+ }+#[test]fn inodes_are_stable() {let mut inodes = Inodes::new();diff --git a/crates/quince-vfs/src/transfer.rs b/crates/quince-vfs/src/transfer.rsnew file mode 100644index 0000000..cd7f82b--- /dev/null+++ b/crates/quince-vfs/src/transfer.rs@@ -0,0 +1,93 @@+//! How a request for a file's bytes is answered on Windows (Cloud Files): the pieces handed to+//! `CfExecute(TRANSFER_DATA)`. Windows takes a piece only if it starts on a 4 KB boundary and is+//! a whole number of 4 KB blocks, except one that ends at the end of the file. Pure arithmetic,+//! kept apart from the Windows calls so it is proved here for every awkward size.++/// The block Windows transfers in.+pub const BLOCK: u64 = 4096;++/// The pieces that answer a request for `len` bytes at `offset` of a file `size` long, each at+/// most `most` bytes (a multiple of [`BLOCK`]): from `offset` rounded down to a block, to the+/// request's end rounded up to a block or the file's end, whichever comes first. Every byte+/// asked for is in exactly one piece, so the request is done when the last piece is sent —+/// never before, which is how a short file was left waiting in Nextcloud (#10845: "done" was+/// said before every byte had gone through). A file of no bytes is one empty piece at its end.+/// Nothing for a request that starts past the end: the caller fails it, whole.+#[must_use]+pub fn pieces(size: u64, offset: u64, len: u64, most: u64) -> Vec<(u64, u64)> {+ debug_assert!(most >= BLOCK && most % BLOCK == 0);+ if size == 0 {+ return if offset == 0 {+ vec![(0, 0)]+ } else {+ Vec::new()+ };+ }+ if offset >= size {+ return Vec::new();+ }+ let start = offset / BLOCK * BLOCK;+ let wanted_end = offset.saturating_add(len.max(1)).min(size);+ let end = wanted_end.div_ceil(BLOCK).saturating_mul(BLOCK).min(size);+ let mut out = Vec::new();+ let mut at = start;+ while at < end {+ let to = (at + most).min(end);+ out.push((at, to - at));+ at = to;+ }+ out+}++#[cfg(test)]+mod tests {+ use super::*;++ /// Every piece starts on a block and is whole blocks unless it ends the file, the pieces+ /// meet end to end, and together they hold every byte asked for — at the sizes and+ /// offsets around a block that broke other clients.+ #[test]+ fn every_request_is_answered_whole_and_aligned() {+ let most = 4 * BLOCK;+ for size in [+ 1,+ 2462,+ 4095,+ 4096,+ 4097,+ 8191,+ 8192,+ 8193,+ 5 * BLOCK + 7,+ 64 * BLOCK,+ ] {+ for offset in [0, 1, 4095, 4096, 4097, size.saturating_sub(1)] {+ if offset >= size {+ continue;+ }+ for len in [1, 4095, 4096, size] {+ let p = pieces(size, offset, len, most);+ let (first, last) = (p[0], p[p.len() - 1]);+ assert_eq!(first.0 % BLOCK, 0);+ assert!(first.0 <= offset);+ assert!(+ last.0 + last.1 >= (offset + len).min(size),+ "{size} {offset} {len}"+ );+ for w in p.windows(2) {+ assert_eq!(w[0].0 + w[0].1, w[1].0);+ }+ for &(at, n) in &p {+ assert!(n <= most && n > 0);+ assert!(+ n % BLOCK == 0 || at + n == size,+ "{size} {offset} {len}: {at}+{n}"+ );+ }+ }+ }+ }+ assert_eq!(pieces(0, 0, 10, most), [(0, 0)]);+ assert!(pieces(100, 100, 1, most).is_empty());+ }+}diff --git a/crates/quince-vfs/tests/cloud_windows.rs b/crates/quince-vfs/tests/cloud_windows.rsnew file mode 100644index 0000000..7356181--- /dev/null+++ b/crates/quince-vfs/tests/cloud_windows.rs@@ -0,0 +1,284 @@+//! iCloud Drive in File Explorer, on a real Windows: a cloud folder registered under the user's+//! profile with a stand-in for Apple behind it, listed and read by another process, as Explorer+//! and every other app reach it. The sizes are the ones that broke other clients: none, one byte,+//! either side of a 4 KB block (Nextcloud #10845), and across our 4 MB pieces. A name Windows+//! can't hold must be there under its look-alike (#9524).+#![cfg(windows)]++use quince_core::drive::DriveItem;+use quince_vfs::cloud::{self, Provider, Shown};+use quince_vfs::{Fut, Remote, Store, ROOT};+use std::collections::HashMap;+use std::path::{Path, PathBuf};+use std::process::Command;+use std::sync::Arc;++fn byte(i: u64) -> u8 {+ ((i * 31 + 7) % 251) as u8+}++fn file(id: &str, name: &str, ext: &str, size: i64) -> DriveItem {+ DriveItem {+ drivewsid: format!("FILE::com.apple.CloudDocs::{id}"),+ parent_id: Some(ROOT.into()),+ name: name.into(),+ extension: Some(ext.into()),+ kind: "FILE".into(),+ size: Some(size),+ child_count: None,+ created_at: Some(1_790_000_000),+ modified_at: Some(1_790_000_000),+ etag: Some("e1".into()),+ }+}++struct Apple(+ HashMap<String, Vec<DriveItem>>,+ Arc<std::sync::Mutex<Vec<String>>>,+);++impl Apple {+ fn said(&self, what: String) {+ self.1.lock().unwrap().push(what);+ }+}++impl Remote for Apple {+ fn mkdir(&self, parent: &DriveItem, name: &str) -> Fut<DriveItem> {+ self.said(format!("mkdir {name}"));+ let mut made = file(&format!("dir-{name}"), name, "", 0);+ made.drivewsid = format!("FOLDER::com.apple.CloudDocs::dir-{name}");+ made.kind = "FOLDER".into();+ made.extension = None;+ made.parent_id = Some(parent.drivewsid.clone());+ Box::pin(async move { Ok(made) })+ }+ fn upload(&self, folder: &DriveItem, path: &Path) -> Fut<DriveItem> {+ let name = path.file_name().unwrap().to_string_lossy().to_string();+ let len = std::fs::metadata(path).map(|m| m.len() as i64).unwrap_or(0);+ self.said(format!("upload {name} {len}"));+ let (stem, ext) = name.rsplit_once('.').unwrap_or((&name, ""));+ let mut sent = file(&format!("up-{name}"), stem, ext, len);+ sent.parent_id = Some(folder.drivewsid.clone());+ Box::pin(async move { Ok(sent) })+ }+ fn rename(&self, item: &DriveItem, name: &str) -> Fut<()> {+ self.said(format!("rename {} {name}", item.display_name()));+ Box::pin(async { Ok(()) })+ }+ fn move_to(&self, item: &DriveItem, folder: &DriveItem) -> Fut<()> {+ self.said(format!("move {} {}", item.display_name(), folder.name));+ Box::pin(async { Ok(()) })+ }+ fn delete(&self, item: &DriveItem) -> Fut<()> {+ self.said(format!("delete {}", item.display_name()));+ Box::pin(async { Ok(()) })+ }+ fn list(&self, folder: &str) -> Fut<Vec<DriveItem>> {+ let items = self.0.get(folder).cloned().unwrap_or_default();+ Box::pin(async move { Ok(items) })+ }+ fn read(&self, item: &DriveItem, offset: u64, len: u64) -> Fut<Vec<u8>> {+ let size = item.size.unwrap_or(0) as u64;+ let end = (offset + len).min(size);+ Box::pin(async move {+ // As a network would: the bytes arrive a moment later, on another thread.+ tokio::time::sleep(std::time::Duration::from_millis(20)).await;+ Ok((offset..end).map(byte).collect())+ })+ }+}++/// 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>>);++impl Store for Library {+ fn children(&self, folder: &str) -> Option<Vec<DriveItem>> {+ (folder == ROOT).then(|| self.0.get(folder).cloned().unwrap_or_default())+ }+}++/// `cmd` as another process: what it lists and what it copies out are what any app gets.+fn dir(path: &Path) -> Vec<String> {+ let out = Command::new("cmd")+ .args(["/c", "dir", "/b"])+ .arg(path)+ .output()+ .unwrap();+ String::from_utf8_lossy(&out.stdout)+ .lines()+ .map(str::to_string)+ .collect()+}++fn copy_out(from: &Path, to: &Path) {+ let status = Command::new("cmd")+ .args(["/c", "copy", "/b", "/y"])+ .arg(from)+ .arg(to)+ .status()+ .unwrap();+ assert!(status.success(), "copying {} out failed", from.display());+}++#[test]+fn explorer_lists_and_opens_icloud_drive() {+ let sizes: [i64; 7] = [0, 1, 4095, 4096, 4097, 8193, 9 * 1024 * 1024 + 123];+ let mut root: Vec<DriveItem> = sizes+ .iter()+ .map(|&n| file(&format!("f{n}"), &format!("size {n}"), "bin", n))+ .collect();+ root.push(file("odd", "a:b?", "txt", 5));+ root.push(file("keep", "keep", "bin", 50_000));+ let mut sub = DriveItem {+ drivewsid: "FOLDER::com.apple.CloudDocs::sub".into(),+ kind: "FOLDER".into(),+ name: "Trip".into(),+ extension: None,+ size: None,+ ..file("x", "x", "x", 0)+ };+ sub.parent_id = Some(ROOT.into());+ root.push(sub.clone());+ let inner = vec![file("in", "inside", "bin", 70_000)];+ let tree: HashMap<String, Vec<DriveItem>> =+ [(ROOT.to_string(), root), (sub.drivewsid.clone(), inner)].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 test {}", std::process::id()));+ 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 id = cloud::sync_root_id(&format!("test{}", std::process::id())).unwrap();+ let shown = Shown {+ id: &id,+ name: "Skykeep test",+ icon: "%SystemRoot%\\system32\\imageres.dll,-1043",+ };+ let folder = cloud::start(+ &place,+ &shown,+ Provider::new(+ Arc::new(Apple(tree.clone(), Arc::clone(&log))),+ Arc::new(Library(tree)),+ rt.handle().clone(),+ out.join("sending"),+ ),+ )+ .expect("the cloud folder registers and connects");+ println!("registered {} (shell: {})", id, folder.shell);++ let listed = dir(&place);+ for n in sizes {+ assert!(+ listed.contains(&format!("size {n}.bin")),+ "size {n}.bin missing from {listed:?}"+ );+ }+ assert!(+ listed.contains(&"a:b?.txt".to_string()),+ "the odd name is missing from {listed:?}"+ );+ assert!(listed.contains(&"Trip".to_string()));++ for n in sizes {+ let copy = out.join(format!("{n}.bin"));+ copy_out(&place.join(format!("size {n}.bin")), ©);+ let got = std::fs::read(©).unwrap();+ assert_eq!(got.len() as i64, n, "size {n}: wrong length");+ assert!(+ got.iter().enumerate().all(|(i, &b)| b == byte(i as u64)),+ "size {n}: wrong bytes"+ );+ }+ // A folder never listed: from Apple, then its file opens.+ assert_eq!(dir(&place.join("Trip")), ["inside.bin"]);+ let copy = out.join("inside.bin");+ copy_out(&place.join("Trip").join("inside.bin"), ©);+ assert_eq!(std::fs::read(©).unwrap().len(), 70_000);++ // Changes made here, as a person makes them, reach Apple: each waited for (a save settles+ // for two seconds before it is sent).+ 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| {+ // Verbatim: cmd reads quotes its own way, and an argument escaped for C (\\") is not it.+ use std::os::windows::process::CommandExt;+ let ok = Command::new("cmd")+ .arg("/c")+ .raw_arg(line)+ .current_dir(&place)+ .status()+ .unwrap()+ .success();+ assert!(ok, "{line}");+ };+ // "Free up space" on a file already here, and "Always keep on this device" on one never+ // opened: the marks Explorer's menu sets, carried out (bytes let go; every byte fetched).+ use std::os::windows::fs::MetadataExt;+ let not_all_here = |name: &str| {+ std::fs::metadata(place.join(name))+ .unwrap()+ .file_attributes()+ & 0x0040_0000+ != 0+ };+ let wait_for = |what: &str, done: &dyn Fn() -> bool| {+ for _ in 0..150 {+ if done() {+ return;+ }+ std::thread::sleep(std::time::Duration::from_millis(200));+ }+ panic!("{what} never happened");+ };+ assert!(+ !not_all_here("size 8193.bin"),+ "size 8193.bin was read, so it is here"+ );+ cmd("attrib +U -P \"size 8193.bin\"");+ wait_for("freeing size 8193.bin", &|| not_all_here("size 8193.bin"));+ assert!(not_all_here("keep.bin"), "keep.bin was never opened");+ cmd("attrib +P -U keep.bin");+ wait_for("keeping keep.bin", &|| !not_all_here("keep.bin"));++ cmd("echo hello> new.txt");+ until("upload new.txt");+ cmd("mkdir NewFolder && echo inside> NewFolder\\a.txt");+ until("mkdir NewFolder");+ until("upload a.txt");+ cmd("echo more>> \"size 4096.bin\"");+ until("upload size 4096.bin");+ until("delete size 4096.bin");+ cmd("ren \"size 1.bin\" renamed.bin");+ until("rename size 1.bin renamed.bin");+ cmd("del \"size 4095.bin\"");+ until("delete size 4095.bin");+ // Nothing a person didn't do: no delete of a file that is still there.+ assert!(+ !log.lock().unwrap().iter().any(|l| l == "delete size 0.bin"),+ "{:?}",+ log.lock().unwrap()+ );+ 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/packaging/windows/installer-hooks.nsh b/packaging/windows/installer-hooks.nshindex 7c739da..d003d41 100644--- a/packaging/windows/installer-hooks.nsh+++ b/packaging/windows/installer-hooks.nsh@@ -95,6 +95,15 @@!macroend; After Tauri has taken "Skykeep" away, and like it, kept on an update.+; iCloud Drive in File Explorer gone with Skykeep: no longer a cloud folder in Explorer's+; navigation pane (Nextcloud's stayed registered after uninstall, #3562). Before the program+; is removed, since it is what unregisters it; never on an update, which keeps the folder.+!macro NSIS_HOOK_PREUNINSTALL+ ${If} $UpdateMode <> 1+ ExecWait '"$INSTDIR\${MAINBINARYNAME}.exe" --forget-cloud-folders'+ ${EndIf}+!macroend+!macro NSIS_HOOK_POSTUNINSTALL${If} $UpdateMode <> 1!insertmacro SKYKEEP_MENUdiff --git a/packaging/windows/msix/AppxManifest.xml b/packaging/windows/msix/AppxManifest.xmlindex ea46ad2..521b93a 100644--- a/packaging/windows/msix/AppxManifest.xml+++ b/packaging/windows/msix/AppxManifest.xml@@ -16,8 +16,9 @@xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"xmlns:uap10="http://schemas.microsoft.com/appx/manifest/uap/windows10/10"xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"+ xmlns:desktop3="http://schemas.microsoft.com/appx/manifest/desktop/windows10/3"xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"- IgnorableNamespaces="uap uap10 desktop rescap">+ IgnorableNamespaces="uap uap10 desktop desktop3 rescap"><IdentityName="__IDENTITY_NAME__"@@ -52,6 +53,12 @@<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" Square71x71Logo="Assets\Square71x71Logo.png" Square310x310Logo="Assets\Square310x310Logo.png" /></uap:VisualElements><Extensions>+ <!-- iCloud Drive in File Explorer (crates/quince-vfs/src/cloud.rs): a cloud folder of the+ package's own, which Windows removes with it — undeclared, it stayed registered+ after uninstall (Microsoft Q&A 5551232). -->+ <desktop3:Extension Category="windows.cloudFiles">+ <desktop3:CloudFiles />+ </desktop3:Extension><!-- Open at Login: off until turned on in Settings (src-tauri/src/login.rs), thenlisted in Task Manager's Startup apps. Started hidden, as the Run key starts it. --><desktop:Extension Category="windows.startupTask" Executable="skykeep.exe" EntryPoint="Windows.FullTrustApplication" uap10:Parameters="--background">diff --git a/src-tauri/src/drive_watch.rs b/src-tauri/src/drive_watch.rsindex 6e1cec3..b452c65 100644--- a/src-tauri/src/drive_watch.rs+++ b/src-tauri/src/drive_watch.rs@@ -225,6 +225,9 @@ async fn read_zones(if !listed.is_empty() {quiet!("drive watch: {} folder(s) changed", listed.len());app.emit(CHANGED, &listed).ok();+ // File Explorer's folders too (Windows), off the async threads: it touches the disk.+ let changed = listed.clone();+ tauri::async_runtime::spawn_blocking(move || crate::mount::drive_changed(&changed));}failed.map_or(Ok(()), Err)}diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rsindex 852be27..e887126 100644--- a/src-tauri/src/main.rs+++ b/src-tauri/src/main.rs@@ -2043,7 +2043,9 @@ async fn auth_sign_out(keep: bool, app: tauri::AppHandle, auth: State<'_, Auth>)LIVE.store(false, Ordering::SeqCst);// A notes password typed this session goes with the session.notes_cmds::forget_passwords();- tauri::async_runtime::spawn_blocking(mount::release)+ // Gone with the account: on Windows no longer in File Explorer either (mount::forget).+ let leaving = app.clone();+ tauri::async_runtime::spawn_blocking(move || mount::forget(&leaving)).await.map_err(|e| e.to_string())?;let mut client = auth.client.lock().await;@@ -6793,6 +6795,13 @@ fn build_tray(app: &tauri::App) -> tauri::Result<()> {fn main() {// First, before any thread: how freed memory goes back to the system (#11).return_freed_memory();+ // The uninstaller's last word (packaging/windows/installer-hooks.nsh): iCloud Drive no+ // longer a cloud folder in File Explorer. Nothing else is started.+ #[cfg(windows)]+ if std::env::args().any(|a| a == "--forget-cloud-folders") {+ mount::forget_on_uninstall();+ return;+ }// Started by an update: the version it replaced finishes exiting first, so this launch is// not handed to it as a second one (single instance, below).updates::after_update();diff --git a/src-tauri/src/mount.rs b/src-tauri/src/mount.rsindex 1791a4e..eec35a8 100644--- a/src-tauri/src/mount.rs+++ b/src-tauri/src/mount.rs@@ -553,16 +553,142 @@ fn stop(which: Which) {}}-#[cfg(not(target_os = "linux"))]+/// The folders in File Explorer (Cloud Files): each connected while Skykeep runs. Registered+/// once and kept registered across restarts, so Explorer keeps them and every file already+/// downloaded stays a plain file; unregistered only when turned off or at sign-out (`forget`).+#[cfg(windows)]+static CLOUD: Mutex<Vec<(Which, PathBuf, quince_vfs::cloud::CloudFolder)>> = Mutex::new(Vec::new());++/// Explorer's id for a folder, per Windows user (its SID) and kind.+#[cfg(windows)]+fn cloud_id(which: Which) -> Result<String, String> {+ quince_vfs::cloud::sync_root_id(match which {+ Which::Drive => "iCloudDrive",+ Which::Photos => "iCloudPhotos",+ })+}++#[cfg(windows)]+fn start(app: &AppHandle, which: Which, at: &std::path::Path) -> Result<(), String> {+ 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 folder = quince_vfs::cloud::start(+ at,+ &quince_vfs::cloud::Shown {+ id: &id,+ name: "iCloud Drive",+ 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"),+ ),+ )+ .map_err(|e| format!("iCloud Drive could not be added to File Explorer: {e}"))?;+ CLOUD+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .push((which, at.to_path_buf(), folder));+ Ok(())+}++/// Disconnected (quit, a library handed over): Explorer keeps the folder, and its files.+#[cfg(windows)]+fn stop(which: Which) {+ CLOUD+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .retain(|(w, ..)| *w != which);+}++/// Skykeep being uninstalled: its cloud folders unregistered, from where they are by default+/// (the shell's registration is by id; the bare one, where the shell refused, by path).+#[cfg(windows)]+pub fn forget_on_uninstall() {+ let home = std::env::var_os("USERPROFILE")+ .map(PathBuf::from)+ .unwrap_or_default();+ for which in [Which::Drive, Which::Photos] {+ if let Ok(id) = cloud_id(which) {+ quince_vfs::cloud::unregister(&home.join(which.folder()), &id);+ }+ }+}++/// Turned off, or signed out: no longer a cloud folder in Explorer at all.+#[cfg(windows)]+fn forget_folder(app: &AppHandle, which: Which) {+ stop(which);+ if let Ok(id) = cloud_id(which) {+ quince_vfs::cloud::unregister(&place(app, which), &id);+ }+}++#[cfg(windows)]+fn mounted(which: Which) -> Option<PathBuf> {+ CLOUD+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .iter()+ .find(|(w, ..)| *w == which)+ .map(|(_, at, _)| at.clone())+}++#[cfg(not(any(target_os = "linux", windows)))]fn start(_app: &AppHandle, _which: Which, _at: &std::path::Path) -> Result<(), String> {Err("These folders are not on this system yet.".into())}-#[cfg(not(target_os = "linux"))]+#[cfg(not(any(target_os = "linux", windows)))]fn stop(_which: Which) {}-#[cfg(not(target_os = "linux"))]+#[cfg(not(any(target_os = "linux", windows)))]fn mounted(_which: Which) -> Option<PathBuf> {None}+#[cfg(not(windows))]+fn forget_folder(_app: &AppHandle, which: Which) {+ stop(which);+}++/// Drive folders Apple changed (the change feed's): brought up to date in File Explorer. On+/// Linux the mount reads the library afresh by itself.+pub fn drive_changed(folders: &[String]) {+ #[cfg(windows)]+ {+ let cloud = CLOUD+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ for (which, _, folder) in cloud.iter() {+ if *which == Which::Drive {+ for id in folders {+ folder.refresh(id);+ }+ }+ }+ }+ #[cfg(not(windows))]+ let _ = folders;+}++/// 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);+ forget_folder(app, Which::Photos);+}fn status(app: &AppHandle, which: Which, problem: Option<String>) -> FolderStatus {let at = mounted(which).unwrap_or_else(|| place(app, which));@@ -581,7 +707,9 @@ fn status(app: &AppHandle, which: Which, problem: Option<String>) -> FolderStatupath: at.to_string_lossy().into_owned(),problem,waiting,- available: cfg!(target_os = "linux") && !in_flatpak(),+ // 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),}}@@ -718,7 +846,7 @@ async fn set(app: AppHandle, which: Which, on: bool) -> Result<FolderStatus, Strlet problem = if on {start(&app, which, &place(&app, which)).err()} else {- stop(which);+ forget_folder(&app, which);None};let data = app.state::<Data>();diff --git a/src/screens/SettingsView.tsx b/src/screens/SettingsView.tsxindex e088251..e439d06 100644--- a/src/screens/SettingsView.tsx+++ b/src/screens/SettingsView.tsx@@ -1,4 +1,5 @@import { MOVE_CHECKED_KEY } from "./notes/model";+import { onWindows } from "../lib/platform";import { ConfirmSheet } from "../components/Confirm";import { toast, toastSaved, useErrorToast } from "../components/Toast";import { Progress } from "../components/Spinner";@@ -653,7 +654,7 @@ export function SettingsView({ onSignOut }: { onSignOut: (keep: boolean) => Prom<FolderRowapp="drivews"label="Drive folder"- off="Your iCloud Drive in Files and every other app"+ off={onWindows ? "Your iCloud Drive in File Explorer and every other app" : "Your iCloud Drive in Files and every other app"}folder={folder}busy={folderBusy}line={folderState}