Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

feat(linux): FUSE3 virtual filesystem and file manager mounts (Part 5)

Jochen Kirstättera41faaf7ef77
diff --git a/desktop/app/src/core_host.rs b/desktop/app/src/core_host.rs
index 808c753..f281bf6 100644
--- a/desktop/app/src/core_host.rs
+++ b/desktop/app/src/core_host.rs
@@ -116,6 +116,9 @@ pub fn compute_capabilities(
{
caps.push(nectarlink_core::PC_WAKE.into());
caps.push("media.control".into());
+ caps.push(nectarlink_core::STORAGE_MOUNT.into());
+ caps.push(nectarlink_core::STORAGE_READ.into());
+ caps.push(nectarlink_core::STORAGE_WRITE.into());
caps.push(nectarlink_core::MIRROR_VIEW.into());
caps.push(nectarlink_core::MIRROR_LISTEN.into());
caps.push(nectarlink_core::DECK_ACTIONS.into());
@@ -125,12 +128,6 @@ pub fn compute_capabilities(
caps.push(nectarlink_core::MEDIA_STREAM.into());
caps.push(nectarlink_core::MIC_LISTEN.into());
}
- #[cfg(windows)]
- {
- caps.push(nectarlink_core::STORAGE_MOUNT.into());
- caps.push(nectarlink_core::STORAGE_READ.into());
- caps.push(nectarlink_core::STORAGE_WRITE.into());
- }
#[cfg(any(windows, target_os = "linux"))]
let mut pc_mirror_enabled = crate::pc_mirror::is_active();
#[cfg(any(windows, target_os = "linux"))]
diff --git a/desktop/app/src/linux/mod.rs b/desktop/app/src/linux/mod.rs
index b61829f..814a00e 100644
--- a/desktop/app/src/linux/mod.rs
+++ b/desktop/app/src/linux/mod.rs
@@ -28,6 +28,7 @@ pub mod shortcut;
pub mod single_instance;
pub mod smtc;
pub mod sound;
+pub mod storage;
pub mod toast;
pub mod tray;
pub mod vcam;
diff --git a/desktop/app/src/linux/storage.rs b/desktop/app/src/linux/storage.rs
new file mode 100644
index 0000000..6c1281b
--- /dev/null
+++ b/desktop/app/src/linux/storage.rs
@@ -0,0 +1,669 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Linux phone storage synchronization (`~/Nectarlink/<Phone name>` and GTK3 bookmarks).
+
+use std::{
+ collections::{HashMap, HashSet, VecDeque},
+ io::Write,
+ path::{Component, Path, PathBuf},
+ sync::{
+ Arc, Condvar, Mutex, MutexGuard, OnceLock,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::{Duration, UNIX_EPOCH},
+};
+
+use nectarlink_core::{DeviceId, LinkState, NodeEvent, StorageEntry};
+
+use crate::core_host;
+use crate::state::{Changes, CoreStatus};
+use crate::storage::{base_sync_dir, relative_path_under_root, sanitize_folder_name};
+
+const LINUX_SYNC_INTERVAL: Duration = Duration::from_secs(4);
+const LINUX_MAX_SYNC_DEPTH: usize = 5;
+const STREAM_READ_CHUNK_BYTES: usize = 64 * 1024;
+
+fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
+ m.lock().unwrap_or_else(|e| e.into_inner())
+}
+
+/// Encodes a local directory path as a `file://` URI suitable for `~/.config/gtk-3.0/bookmarks`.
+pub fn format_file_uri(path: &Path) -> String {
+ let raw = path.to_string_lossy();
+ let mut encoded = String::with_capacity(raw.len() + 16);
+ encoded.push_str("file://");
+ for b in raw.as_bytes() {
+ match *b {
+ b'A'..=b'Z'
+ | b'a'..=b'z'
+ | b'0'..=b'9'
+ | b'/'
+ | b'-'
+ | b'_'
+ | b'.'
+ | b'~'
+ | b':'
+ | b'@'
+ | b'('
+ | b')' => encoded.push(*b as char),
+ other => {
+ encoded.push_str(&format!("%{other:02X}"));
+ }
+ }
+ }
+ encoded
+}
+
+/// Formats a single GTK3 bookmark entry (`file://<encoded_path> <DeviceName> (NectarLink)`).
+pub fn format_gtk_bookmark_line(mount_path: &Path, device_name: &str) -> String {
+ let uri = format_file_uri(mount_path);
+ let label = sanitize_folder_name(device_name);
+ format!("{uri} {label} (NectarLink)")
+}
+
+/// Upserts a NectarLink device bookmark line in the contents of `~/.config/gtk-3.0/bookmarks`.
+pub fn upsert_gtk_bookmarks_content(existing: &str, mount_path: &Path, device_name: &str) -> String {
+ let uri = format_file_uri(mount_path);
+ let prefix = format!("{uri} ");
+ let new_line = format_gtk_bookmark_line(mount_path, device_name);
+ let mut lines: Vec<String> = Vec::new();
+ let mut replaced = false;
+ for line in existing.lines() {
+ let trimmed = line.trim_end();
+ if trimmed.is_empty() {
+ continue;
+ }
+ if trimmed == uri || trimmed.starts_with(&prefix) {
+ if !replaced {
+ lines.push(new_line.clone());
+ replaced = true;
+ }
+ } else {
+ lines.push(trimmed.to_owned());
+ }
+ }
+ if !replaced {
+ lines.push(new_line);
+ }
+ if lines.is_empty() { String::new() } else { format!("{}\n", lines.join("\n")) }
+}
+
+/// Removes a NectarLink device bookmark line matching `mount_path` from `~/.config/gtk-3.0/bookmarks` content.
+pub fn remove_gtk_bookmarks_content(existing: &str, mount_path: &Path) -> String {
+ let uri = format_file_uri(mount_path);
+ let prefix = format!("{uri} ");
+ let lines: Vec<&str> = existing
+ .lines()
+ .map(str::trim_end)
+ .filter(|line| !line.is_empty() && *line != uri && !line.starts_with(&prefix))
+ .collect();
+ if lines.is_empty() { String::new() } else { format!("{}\n", lines.join("\n")) }
+}
+
+/// Converts a local path inside `mount_root` into a normalized forward-slash relative remote path
+/// (`""` for `mount_root` itself). Returns `None` if `local_path` is outside `mount_root` or
+/// contains parent/current directory traversal components.
+pub fn to_remote_rel_path(mount_root: &Path, local_path: &Path) -> Option<String> {
+ if let Ok(stripped) = local_path.strip_prefix(mount_root) {
+ let mut parts: Vec<String> = Vec::new();
+ for comp in stripped.components() {
+ match comp {
+ Component::Normal(os) => {
+ let s = os.to_string_lossy();
+ if !s.is_empty() {
+ parts.push(s.into_owned());
+ }
+ }
+ Component::CurDir => {}
+ Component::ParentDir | Component::RootDir | Component::Prefix(_) => return None,
+ }
+ }
+ return Some(parts.join("/"));
+ }
+ relative_path_under_root(mount_root, local_path)
+}
+
+#[derive(Debug)]
+struct MountedDevice {
+ device: DeviceId,
+ device_name: String,
+ mount_path: PathBuf,
+ stop: Arc<AtomicBool>,
+ wake: Arc<(Mutex<bool>, Condvar)>,
+ worker: Mutex<Option<std::thread::JoinHandle<()>>>,
+}
+
+impl MountedDevice {
+ fn notify_sync(&self) {
+ let (lock_flag, cvar) = &*self.wake;
+ let mut pending = lock(lock_flag);
+ *pending = true;
+ cvar.notify_all();
+ }
+
+ fn stop_and_join(&self) {
+ self.stop.store(true, Ordering::Release);
+ self.notify_sync();
+ if let Some(handle) = lock(&self.worker).take() {
+ let _ = handle.join();
+ }
+ }
+}
+
+static MOUNTS: OnceLock<Mutex<HashMap<DeviceId, Arc<MountedDevice>>>> = OnceLock::new();
+static LINUX_RECONCILING: Mutex<()> = Mutex::new(());
+
+fn mounts() -> MutexGuard<'static, HashMap<DeviceId, Arc<MountedDevice>>> {
+ let map = MOUNTS.get_or_init(|| Mutex::new(HashMap::new()));
+ lock(map)
+}
+
+fn get_mounted_device(device: &DeviceId) -> Option<Arc<MountedDevice>> {
+ mounts().get(device).cloned()
+}
+
+pub fn mounted_path_for(device: &DeviceId) -> Option<PathBuf> {
+ get_mounted_device(device).map(|m| m.mount_path.clone())
+}
+
+fn gtk_bookmarks_path() -> Option<PathBuf> {
+ dirs::config_dir()
+ .filter(|p| p.is_absolute())
+ .or_else(|| dirs::home_dir().filter(|p| p.is_absolute()).map(|h| h.join(".config")))
+ .map(|cfg| cfg.join("gtk-3.0").join("bookmarks"))
+}
+
+fn register_gtk_bookmark(mount_path: &Path, device_name: &str) {
+ let Some(bookmarks_file) = gtk_bookmarks_path() else { return };
+ if let Some(parent) = bookmarks_file.parent() {
+ let _ = std::fs::create_dir_all(parent);
+ }
+ let existing = std::fs::read_to_string(&bookmarks_file).unwrap_or_default();
+ let updated = upsert_gtk_bookmarks_content(&existing, mount_path, device_name);
+ let _ = std::fs::write(&bookmarks_file, updated);
+}
+
+fn unregister_gtk_bookmark(mount_path: &Path) {
+ let Some(bookmarks_file) = gtk_bookmarks_path() else { return };
+ let Ok(existing) = std::fs::read_to_string(&bookmarks_file) else { return };
+ let updated = remove_gtk_bookmarks_content(&existing, mount_path);
+ let _ = std::fs::write(&bookmarks_file, updated);
+}
+
+pub fn start() {
+ drop(mounts());
+ core_host::host().hub.subscribe(
+ Changes::DEVICES | Changes::STATUS | Changes::CAPABILITIES | Changes::TOGGLES,
+ || {
+ sync();
+ true
+ },
+ );
+ sync();
+}
+
+pub fn sync() {
+ let host = core_host::host();
+ let Some(node) = core_host::node() else { return };
+ let ready = host.hub.read(|s| matches!(s.status, Some(CoreStatus::Ready { .. })));
+ if !ready {
+ return;
+ }
+ let devices = host.hub.read(|s| {
+ s.devices
+ .iter()
+ .map(|d| (d.id, d.info.name.clone(), matches!(d.link, LinkState::Online { .. })))
+ .collect::<Vec<_>>()
+ });
+ let mut desired: Vec<(DeviceId, String, bool)> = Vec::new();
+ let mut disabled: Vec<(DeviceId, String)> = Vec::new();
+ for (id, name, online) in devices {
+ let enabled = node
+ .device_toggles(id)
+ .ok()
+ .and_then(|t| t.into_iter().find(|(k, _)| *k == "storage").map(|(_, on)| on))
+ .unwrap_or(false);
+ if enabled {
+ desired.push((id, name, online));
+ } else {
+ disabled.push((id, name));
+ }
+ }
+
+ let _ = std::thread::Builder::new().name("storage-linux-reconcile".into()).spawn(move || {
+ let _guard = lock(&LINUX_RECONCILING);
+ reconcile(&desired, &disabled);
+ });
+}
+
+fn reconcile(desired: &[(DeviceId, String, bool)], disabled: &[(DeviceId, String)]) {
+ let Some(base) = base_sync_dir() else { return };
+
+ for (id, name) in disabled {
+ unmount_device(*id, Some(name));
+ }
+
+ let desired_ids: HashSet<DeviceId> = desired.iter().map(|(id, _, _)| *id).collect();
+ let stale: Vec<DeviceId> = mounts().keys().filter(|id| !desired_ids.contains(id)).copied().collect();
+ for id in stale {
+ unmount_device(id, None);
+ }
+
+ for (device, name, online) in desired {
+ let folder_name = sanitize_folder_name(name);
+ let mount_path = base.join(&folder_name);
+ if std::fs::create_dir_all(&mount_path).is_err() {
+ continue;
+ }
+ register_gtk_bookmark(&mount_path, name);
+
+ let existing = get_mounted_device(device);
+ if let Some(mounted) = existing {
+ if mounted.mount_path == mount_path && mounted.device_name == *name {
+ if *online {
+ mounted.notify_sync();
+ }
+ continue;
+ }
+ unmount_device(*device, None);
+ }
+
+ let stop = Arc::new(AtomicBool::new(false));
+ let wake = Arc::new((Mutex::new(*online), Condvar::new()));
+ let mounted = Arc::new(MountedDevice {
+ device: *device,
+ device_name: name.clone(),
+ mount_path,
+ stop,
+ wake,
+ worker: Mutex::new(None),
+ });
+ let worker_mount = Arc::clone(&mounted);
+ if let Ok(handle) = std::thread::Builder::new()
+ .name(format!("storage-sync-{}", &device.to_string()[..8]))
+ .spawn(move || mount_worker(&worker_mount))
+ {
+ *lock(&mounted.worker) = Some(handle);
+ mounts().insert(*device, mounted);
+ }
+ }
+}
+
+fn unmount_device(device: DeviceId, fallback_name: Option<&str>) {
+ let removed = mounts().remove(&device);
+ if let Some(mounted) = removed {
+ mounted.stop_and_join();
+ unregister_gtk_bookmark(&mounted.mount_path);
+ } else if let Some(name) = fallback_name
+ && let Some(base) = base_sync_dir()
+ {
+ let mount_path = base.join(sanitize_folder_name(name));
+ unregister_gtk_bookmark(&mount_path);
+ }
+}
+
+pub fn on_event(event: &NodeEvent) {
+ match event {
+ NodeEvent::DeviceRemoved(device) => {
+ let device = *device;
+ let _ = std::thread::Builder::new().name("storage-linux-unpair".into()).spawn(move || {
+ let _guard = lock(&LINUX_RECONCILING);
+ unmount_device(device, None);
+ });
+ }
+ NodeEvent::DeviceAdded(_)
+ | NodeEvent::PeerInfoChanged { .. }
+ | NodeEvent::Capabilities(_)
+ | NodeEvent::LinkChanged { .. } => {
+ sync();
+ }
+ NodeEvent::StorageChanged { device, .. } => {
+ if let Some(mounted) = get_mounted_device(device) {
+ mounted.notify_sync();
+ }
+ }
+ _ => {}
+ }
+}
+
+pub fn shutdown() {
+ let active: Vec<Arc<MountedDevice>> = mounts().drain().map(|(_, v)| v).collect();
+ for mounted in active {
+ mounted.stop_and_join();
+ unregister_gtk_bookmark(&mounted.mount_path);
+ }
+}
+
+pub fn unregister_all() {
+ shutdown();
+ if let Some(bookmarks_file) = gtk_bookmarks_path()
+ && let Ok(existing) = std::fs::read_to_string(&bookmarks_file)
+ {
+ let filtered: Vec<&str> = existing
+ .lines()
+ .map(str::trim_end)
+ .filter(|line| !line.is_empty() && !line.ends_with(" (NectarLink)"))
+ .collect();
+ let updated = if filtered.is_empty() { String::new() } else { format!("{}\n", filtered.join("\n")) };
+ let _ = std::fs::write(&bookmarks_file, updated);
+ }
+ if let Some(base) = base_sync_dir() {
+ let _ = std::fs::remove_dir(&base);
+ }
+}
+
+fn file_mtime_ms(meta: &std::fs::Metadata) -> i64 {
+ meta.modified()
+ .ok()
+ .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
+ .map(|d| d.as_millis() as i64)
+ .unwrap_or(0)
+}
+
+fn set_local_file_mtime_ms(path: &Path, unix_ms: i64) {
+ use std::os::unix::fs::OpenOptionsExt;
+ if unix_ms <= 0 {
+ return;
+ }
+ let st = UNIX_EPOCH + Duration::from_millis(unix_ms as u64);
+ if let Ok(file) = std::fs::OpenOptions::new().write(true).custom_flags(libc::O_NOFOLLOW).open(path) {
+ let _ = file.set_modified(st);
+ }
+}
+
+fn collect_local_files(
+ mount_root: &Path,
+ dir: &Path,
+ depth: usize,
+ out: &mut HashMap<String, (PathBuf, u64, i64)>,
+) {
+ if depth > LINUX_MAX_SYNC_DEPTH {
+ return;
+ }
+ let Ok(entries) = std::fs::read_dir(dir) else { return };
+ for entry in entries.flatten() {
+ let path = entry.path();
+ let file_name = entry.file_name();
+ let name_str = file_name.to_string_lossy();
+ if name_str.starts_with('.') {
+ continue;
+ }
+ let Ok(meta) = std::fs::symlink_metadata(&path) else { continue };
+ if meta.file_type().is_symlink() {
+ continue;
+ }
+ if meta.is_dir() {
+ collect_local_files(mount_root, &path, depth + 1, out);
+ } else if meta.is_file()
+ && let Some(rel) = to_remote_rel_path(mount_root, &path)
+ && !rel.is_empty()
+ {
+ out.insert(rel, (path, meta.len(), file_mtime_ms(&meta)));
+ }
+ }
+}
+
+fn mount_worker(mounted: &Arc<MountedDevice>) {
+ // Snapshot of known synced files: remote_rel_path -> (size, local_mtime_ms, remote_mtime_ms)
+ let mut known_files: HashMap<String, (u64, i64, i64)> = HashMap::new();
+
+ while !mounted.stop.load(Ordering::Acquire) {
+ let is_online = core_host::host().hub.read(|s| {
+ s.devices
+ .iter()
+ .find(|d| d.id == mounted.device)
+ .is_some_and(|d| matches!(d.link, LinkState::Online { .. }))
+ });
+
+ if is_online && let Some(node) = core_host::node() {
+ core_host::host().runtime.block_on(run_sync_cycle(mounted, &node, &mut known_files));
+ }
+
+ if mounted.stop.load(Ordering::Acquire) {
+ break;
+ }
+
+ let (lock_flag, cvar) = &*mounted.wake;
+ let mut pending = lock(lock_flag);
+ if !*pending {
+ let (guard, _) =
+ cvar.wait_timeout(pending, LINUX_SYNC_INTERVAL).unwrap_or_else(|e| e.into_inner());
+ pending = guard;
+ }
+ *pending = false;
+ }
+}
+
+async fn stream_remote_file_to_local(
+ mounted: &MountedDevice,
+ node: &nectarlink_core::Node,
+ remote_rel: &str,
+ local_child: &Path,
+) -> Option<u64> {
+ use std::os::unix::fs::OpenOptionsExt;
+
+ if let Some(parent) = local_child.parent() {
+ std::fs::create_dir_all(parent).ok()?;
+ if std::fs::symlink_metadata(parent).is_ok_and(|m| m.file_type().is_symlink()) {
+ return None;
+ }
+ }
+ if std::fs::symlink_metadata(local_child).is_ok_and(|m| m.file_type().is_symlink()) {
+ return None;
+ }
+ let file_name = local_child.file_name()?.to_string_lossy();
+ let part_path = local_child.with_file_name(format!(".{file_name}.nectarlink-part"));
+ let (meta, mut recv) = node.storage_read_stream(mounted.device, remote_rel, 0, None).await.ok()?;
+ let mut file = std::fs::OpenOptions::new()
+ .write(true)
+ .create(true)
+ .truncate(true)
+ .custom_flags(libc::O_NOFOLLOW)
+ .mode(0o600)
+ .open(&part_path)
+ .ok()?;
+ let mut written = 0u64;
+ loop {
+ if mounted.stop.load(Ordering::Acquire) {
+ drop(file);
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ match recv.read_chunk(STREAM_READ_CHUNK_BYTES).await {
+ Ok(Some(chunk)) => {
+ if written.saturating_add(chunk.len() as u64) > meta.length {
+ drop(file);
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ if file.write_all(&chunk).is_err() {
+ drop(file);
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ written += chunk.len() as u64;
+ }
+ Ok(None) => break,
+ Err(_) => {
+ drop(file);
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ }
+ }
+ if written != meta.length || file.flush().is_err() {
+ drop(file);
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ drop(file);
+ if std::fs::rename(&part_path, local_child).is_err() {
+ let _ = std::fs::remove_file(&part_path);
+ return None;
+ }
+ Some(written)
+}
+
+async fn run_sync_cycle(
+ mounted: &MountedDevice,
+ node: &nectarlink_core::Node,
+ known_files: &mut HashMap<String, (u64, i64, i64)>,
+) {
+ // 1. Traverse remote directory tree via BFS and mirror directories/files locally first.
+ let mut queue: VecDeque<(String, usize)> = VecDeque::new();
+ queue.push_back((String::new(), 0));
+ let mut seen_remote_files: HashSet<String> = HashSet::new();
+ let mut traversal_complete = true;
+
+ while let Some((dir_rel, depth)) = queue.pop_front() {
+ if mounted.stop.load(Ordering::Acquire) {
+ return;
+ }
+ let Ok(entries): Result<Vec<StorageEntry>, _> =
+ node.storage_list(mounted.device, dir_rel.as_str()).await
+ else {
+ traversal_complete = false;
+ continue;
+ };
+
+ for entry in entries {
+ if mounted.stop.load(Ordering::Acquire) {
+ return;
+ }
+ let name = entry.name.trim_matches('/');
+ if name.is_empty()
+ || name == "."
+ || name == ".."
+ || name.starts_with('.')
+ || name.contains('/')
+ || name.contains('\\')
+ || name.chars().any(char::is_control)
+ {
+ continue;
+ }
+ let child_rel = if dir_rel.is_empty() { name.to_owned() } else { format!("{dir_rel}/{name}") };
+ let local_child = child_rel.split('/').fold(mounted.mount_path.clone(), |acc, seg| acc.join(seg));
+ if to_remote_rel_path(&mounted.mount_path, &local_child).is_none() {
+ continue;
+ }
+ if std::fs::symlink_metadata(&local_child).is_ok_and(|m| m.file_type().is_symlink()) {
+ continue;
+ }
+
+ if entry.is_dir {
+ let _ = std::fs::create_dir_all(&local_child);
+ if depth + 1 < LINUX_MAX_SYNC_DEPTH {
+ queue.push_back((child_rel, depth + 1));
+ }
+ } else {
+ seen_remote_files.insert(child_rel.clone());
+ let needs_download = match known_files.get(&child_rel) {
+ None => !local_child.exists(),
+ Some((known_size, _, known_remote_mtime)) => {
+ !local_child.exists()
+ || entry.size != *known_size
+ || (entry.modified > 0
+ && *known_remote_mtime > 0
+ && (entry.modified - *known_remote_mtime).abs() > 1_000)
+ }
+ };
+ if needs_download {
+ if let Some(written) =
+ stream_remote_file_to_local(mounted, node, &child_rel, &local_child).await
+ {
+ set_local_file_mtime_ms(&local_child, entry.modified);
+ let local_mtime = std::fs::metadata(&local_child)
+ .map(|m| file_mtime_ms(&m))
+ .unwrap_or(entry.modified);
+ known_files.insert(child_rel, (written, local_mtime, entry.modified));
+ }
+ } else if let Ok(meta) = std::fs::metadata(&local_child) {
+ known_files
+ .entry(child_rel)
+ .or_insert_with(|| (meta.len(), file_mtime_ms(&meta), entry.modified));
+ }
+ }
+ }
+ }
+
+ // 2. Remove local cache files that were deleted on the phone since the last full pass.
+ if traversal_complete {
+ let removed_remotely: Vec<String> =
+ known_files.keys().filter(|rel| !seen_remote_files.contains(*rel)).cloned().collect();
+ for rel in removed_remotely {
+ let local_child = rel.split('/').fold(mounted.mount_path.clone(), |acc, seg| acc.join(seg));
+ let _ = std::fs::remove_file(&local_child);
+ known_files.remove(&rel);
+ }
+ }
+
+ // 3. Inspect local directory tree for user additions or edits and push them to the phone.
+ let mut local_files: HashMap<String, (PathBuf, u64, i64)> = HashMap::new();
+ collect_local_files(&mounted.mount_path, &mounted.mount_path, 0, &mut local_files);
+ known_files.retain(|rel, _| local_files.contains_key(rel));
+
+ for (rel, (local_path, size, local_mtime)) in &local_files {
+ if mounted.stop.load(Ordering::Acquire) {
+ return;
+ }
+ let changed = match known_files.get(rel) {
+ None => !seen_remote_files.contains(rel),
+ Some((known_size, known_local_mtime, _)) => {
+ *size != *known_size || (*local_mtime - *known_local_mtime) > 1_000
+ }
+ };
+ if changed {
+ if let Some(parent_rel) = Path::new(rel).parent() {
+ let parent_str = parent_rel.to_string_lossy().replace('\\', "/");
+ if !parent_str.is_empty() {
+ let _ = node.storage_mkdir(mounted.device, &parent_str).await;
+ }
+ }
+ if node.storage_write(mounted.device, rel.as_str(), local_path).await.is_ok() {
+ known_files.insert(rel.clone(), (*size, *local_mtime, *local_mtime));
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn gtk_bookmark_formatting_upsert_and_removal_preserve_unrelated_entries() {
+ let mount = Path::new("/home/alice/Nectarlink/Pixel 9 Pro");
+ let line = format_gtk_bookmark_line(mount, "Pixel 9 Pro");
+ assert_eq!(line, "file:///home/alice/Nectarlink/Pixel%209%20Pro Pixel 9 Pro (NectarLink)");
+
+ let initial = "file:///home/alice/Documents Documents\nfile:///home/alice/Downloads Downloads\n";
+ let with_phone = upsert_gtk_bookmarks_content(initial, mount, "Pixel 9 Pro");
+ assert!(with_phone.contains("file:///home/alice/Documents Documents\n"));
+ assert!(with_phone.contains("file:///home/alice/Downloads Downloads\n"));
+ assert!(with_phone.contains(&line));
+
+ // Upserting again with updated label replaces in-place without duplicating.
+ let updated_phone = upsert_gtk_bookmarks_content(&with_phone, mount, "Pixel 9 Pro Renamed");
+ assert_eq!(
+ updated_phone
+ .lines()
+ .filter(|l| l.starts_with("file:///home/alice/Nectarlink/Pixel%209%20Pro"))
+ .count(),
+ 1
+ );
+ assert!(updated_phone.contains("Pixel 9 Pro Renamed (NectarLink)"));
+
+ let cleaned = remove_gtk_bookmarks_content(&updated_phone, mount);
+ assert_eq!(cleaned, initial);
+ }
+
+ #[test]
+ fn to_remote_rel_path_converts_linux_mount_paths_to_slash_relative_paths() {
+ let root = Path::new("/home/alice/Nectarlink/Pixel 9");
+ assert_eq!(to_remote_rel_path(root, root), Some(String::new()));
+ assert_eq!(
+ to_remote_rel_path(root, Path::new("/home/alice/Nectarlink/Pixel 9/DCIM/Camera/IMG_001.jpg")),
+ Some("DCIM/Camera/IMG_001.jpg".to_owned())
+ );
+ assert_eq!(to_remote_rel_path(root, Path::new("/home/alice/Nectarlink/OtherPhone/DCIM")), None);
+ }
+}
diff --git a/desktop/app/src/storage.rs b/desktop/app/src/storage.rs
index f767b85..836ae68 100644
--- a/desktop/app/src/storage.rs
+++ b/desktop/app/src/storage.rs
@@ -8,16 +8,17 @@
//! reads (`CF_CALLBACK_TYPE_FETCH_DATA`). Renames, deletes, and new or modified
//! files dropped into the sync root are forwarded to the phone.
-#[cfg(windows)]
+#[cfg(any(windows, target_os = "linux"))]
mod imp {
#![allow(unsafe_code)]
+ use std::path::{Path, PathBuf};
+ #[cfg(windows)]
use std::{
collections::{HashMap, HashSet},
ffi::{OsStr, c_void},
os::windows::ffi::{OsStrExt, OsStringExt},
- path::{Path, PathBuf},
sync::{
Arc, Mutex, MutexGuard,
atomic::{AtomicBool, Ordering},
@@ -25,7 +26,10 @@ mod imp {
time::Duration,
};
- use nectarlink_core::{DeviceId, LinkState, NodeEvent, StorageEntry};
+ use nectarlink_core::{DeviceId, NodeEvent};
+ #[cfg(windows)]
+ use nectarlink_core::{LinkState, StorageEntry};
+ #[cfg(windows)]
use windows::{
Security::Cryptography::{BinaryStringEncoding, CryptographicBuffer},
Storage::{
@@ -73,33 +77,38 @@ mod imp {
core::{HSTRING, PCWSTR},
};
- use crate::{
- core_host,
- state::{Changes, CoreStatus},
- win,
- };
+ #[cfg(windows)]
+ use crate::state::{Changes, CoreStatus};
+ use crate::{core_host, win};
/// Provider prefix for sync root IDs (`NectarLink!<device_id>`).
+ #[cfg(windows)]
const PROVIDER_PREFIX: &str = "Nectarlink!";
/// Difference between Windows FILETIME epoch (1601-01-01) and Unix epoch
/// (1970-01-01) in 100-nanosecond intervals.
+ #[cfg_attr(target_os = "linux", allow(dead_code))]
const EPOCH_DIFF_100NS: i64 = 116_444_736_000_000_000;
/// Cloud file network unavailable NTSTATUS (`STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE`).
+ #[cfg(windows)]
const STATUS_CLOUD_NETWORK_UNAVAILABLE: NTSTATUS = STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE;
/// Cloud file unsuccessful / cancelled operation NTSTATUS (`STATUS_CLOUD_FILE_UNSUCCESSFUL`).
+ #[cfg(windows)]
const STATUS_CLOUD_UNSUCCESSFUL: NTSTATUS = STATUS_CLOUD_FILE_UNSUCCESSFUL;
/// Chunk size for hydrating files via `CF_OPERATION_TYPE_TRANSFER_DATA` (must be
/// a multiple of 4096 bytes for non-EOF chunks).
+ #[cfg(windows)]
const HYDRATE_CHUNK_BYTES: usize = 256 * 1024;
+ #[cfg(windows)]
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
+ #[cfg(windows)]
struct ActiveRoot {
device: DeviceId,
name: String,
@@ -110,19 +119,24 @@ mod imp {
uploading: Mutex<HashSet<String>>,
}
+ #[cfg(windows)]
static ACTIVE_ROOTS: Mutex<Option<HashMap<DeviceId, Arc<ActiveRoot>>>> = Mutex::new(None);
+ #[cfg(windows)]
static RECONCILING: Mutex<()> = Mutex::new(());
+ #[cfg(windows)]
fn active_roots() -> MutexGuard<'static, Option<HashMap<DeviceId, Arc<ActiveRoot>>>> {
lock(&ACTIVE_ROOTS)
}
+ #[cfg(windows)]
fn get_active_root(device: &DeviceId) -> Option<Arc<ActiveRoot>> {
active_roots().as_ref().and_then(|m| m.get(device).cloned())
}
/// Starts watching paired devices and their `storage` toggles to register or
/// unregister File Explorer sync roots.
+ #[cfg(windows)]
pub fn start() {
{
let mut map = active_roots();
@@ -140,7 +154,13 @@ mod imp {
sync();
}
+ #[cfg(target_os = "linux")]
+ pub fn start() {
+ win::storage::start();
+ }
+
/// Triggers a reconciliation of active sync roots (e.g. after a device toggle changes).
+ #[cfg(windows)]
pub fn sync() {
let host = core_host::host();
let Some(node) = core_host::node() else { return };
@@ -175,7 +195,13 @@ mod imp {
});
}
+ #[cfg(target_os = "linux")]
+ pub fn sync() {
+ win::storage::sync();
+ }
+
/// Handles core events (`StorageChanged`, `DeviceRemoved`, `LinkChanged`).
+ #[cfg(windows)]
pub fn on_event(event: &NodeEvent) {
match event {
NodeEvent::DeviceRemoved(device) => {
@@ -204,7 +230,13 @@ mod imp {
}
}
+ #[cfg(target_os = "linux")]
+ pub fn on_event(event: &NodeEvent) {
+ win::storage::on_event(event);
+ }
+
/// Disconnects active sync roots when the desktop app exits.
+ #[cfg(windows)]
pub fn shutdown() {
let roots: Vec<Arc<ActiveRoot>> =
active_roots().as_mut().map(|m| m.drain().map(|(_, v)| v).collect()).unwrap_or_default();
@@ -215,7 +247,13 @@ mod imp {
}
}
+ #[cfg(target_os = "linux")]
+ pub fn shutdown() {
+ win::storage::shutdown();
+ }
+
/// Unregisters all `NectarLink!*` sync roots from Windows (`--uninstall`).
+ #[cfg(windows)]
pub fn unregister_all() {
shutdown();
win::with_com(|| {
@@ -246,11 +284,21 @@ mod imp {
});
}
+ #[cfg(target_os = "linux")]
+ pub fn unregister_all() {
+ win::storage::unregister_all();
+ }
+
/// Returns the local sync root path for a paired phone (`%USERPROFILE%\Nectarlink\<Phone name>`).
pub fn sync_root_path_for(device: DeviceId) -> Option<PathBuf> {
+ #[cfg(windows)]
if let Some(active) = get_active_root(&device) {
return Some(active.root_path.clone());
}
+ #[cfg(target_os = "linux")]
+ if let Some(path) = win::storage::mounted_path_for(&device) {
+ return Some(path);
+ }
let name = core_host::host().hub.read(|s| s.name_of(&device))?;
Some(base_sync_dir()?.join(sanitize_folder_name(&name)))
}
@@ -262,13 +310,21 @@ mod imp {
if !path.exists() {
std::fs::create_dir_all(&path).map_err(|e| e.to_string())?;
}
- win::shell::run(Path::new("explorer.exe"), &format!("\"{}\"", path.display()))
+ #[cfg(windows)]
+ {
+ win::shell::run(Path::new("explorer.exe"), &format!("\"{}\"", path.display()))
+ }
+ #[cfg(target_os = "linux")]
+ {
+ win::shell::open_folder(&path)
+ }
}
- fn base_sync_dir() -> Option<PathBuf> {
+ pub(crate) fn base_sync_dir() -> Option<PathBuf> {
dirs::home_dir().map(|h| h.join("Nectarlink"))
}
+ #[cfg(windows)]
fn sync_root_id(device: DeviceId) -> HSTRING {
HSTRING::from(format!("{PROVIDER_PREFIX}{device}"))
}
@@ -297,6 +353,7 @@ mod imp {
/// Converts a Unix timestamp in milliseconds to Windows FILETIME 100ns ticks
/// since 1601-01-01.
+ #[cfg_attr(target_os = "linux", allow(dead_code))]
pub fn unix_ms_to_filetime_ticks(unix_ms: i64) -> i64 {
EPOCH_DIFF_100NS.saturating_add(unix_ms.max(0).saturating_mul(10_000))
}
@@ -310,6 +367,7 @@ mod imp {
/// Computes the slash-separated relative path inside `root_path` from a full
/// local path (`""` for the sync root itself). Returns `None` if `full_path` is
/// not inside `root_path`.
+ #[cfg_attr(target_os = "linux", allow(dead_code))]
pub fn relative_path_under_root(root_path: &Path, full_path: &Path) -> Option<String> {
let root_norm = normalize_win_path(root_path);
let full_norm = normalize_win_path(full_path);
@@ -330,12 +388,14 @@ mod imp {
Some(parts.join("/"))
}
+ #[cfg_attr(target_os = "linux", allow(dead_code))]
fn normalize_win_path(path: &Path) -> String {
let s = path.to_string_lossy();
let stripped = s.strip_prefix(r"\\?\").unwrap_or(&s);
stripped.trim_end_matches('\\').to_owned()
}
+ #[cfg(windows)]
fn reconcile(desired: &[(DeviceId, String, bool)], disabled: &[(DeviceId, String)]) {
// Unregister any disabled devices that belong to this instance.
for (id, name) in disabled {
@@ -377,6 +437,7 @@ mod imp {
}
}
+ #[cfg(windows)]
fn remove_device_root(device: DeviceId, known_name: Option<&str>) {
let removed = active_roots().as_mut().and_then(|m| m.remove(&device));
let mut folder_to_clean: Option<PathBuf> = None;
@@ -402,6 +463,7 @@ mod imp {
}
}
+ #[cfg(windows)]
fn cleanup_sync_root_dir(path: &Path) {
if !path.exists() {
return;
@@ -409,6 +471,7 @@ mod imp {
let _ = std::fs::remove_dir_all(path);
}
+ #[cfg(windows)]
fn register_and_connect(device: DeviceId, name: &str, online: bool) -> Result<(), String> {
let base = base_sync_dir().ok_or_else(|| "no user home directory".to_owned())?;
let folder_name = sanitize_folder_name(name);
@@ -515,10 +578,12 @@ mod imp {
Ok(())
}
+ #[cfg(windows)]
fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
}
+ #[cfg(windows)]
unsafe fn pcwstr_to_string(ptr: PCWSTR) -> String {
if ptr.is_null() {
return String::new();
@@ -528,6 +593,7 @@ mod imp {
std::ffi::OsString::from_wide(slice).to_string_lossy().into_owned()
}
+ #[cfg(windows)]
unsafe fn callback_root_and_path(
info: *const CF_CALLBACK_INFO,
) -> Option<(Arc<ActiveRoot>, String, CF_CONNECTION_KEY, i64, i64)> {
@@ -548,6 +614,7 @@ mod imp {
Some((root, rel, cb.ConnectionKey, cb.TransferKey, cb.RequestKey))
}
+ #[cfg(windows)]
fn make_op_info(
connection_key: CF_CONNECTION_KEY,
transfer_key: i64,
@@ -565,6 +632,7 @@ mod imp {
}
}
+ #[cfg(windows)]
unsafe extern "system" fn on_fetch_placeholders(
callback_info: *const CF_CALLBACK_INFO,
_callback_parameters: *const CF_CALLBACK_PARAMETERS,
@@ -598,6 +666,7 @@ mod imp {
});
}
+ #[cfg(windows)]
fn transfer_placeholders(
connection_key: CF_CONNECTION_KEY,
transfer_key: i64,
@@ -646,6 +715,7 @@ mod imp {
let _ = unsafe { CfExecute(&op_info, &mut op_params) };
}
+ #[cfg(windows)]
fn ack_placeholders_failed(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64) {
let op_info =
make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS);
@@ -666,6 +736,7 @@ mod imp {
let _ = unsafe { CfExecute(&op_info, &mut op_params) };
}
+ #[cfg(windows)]
fn build_placeholder_create_info(
entry: &StorageEntry,
wide_name: &[u16],
@@ -694,6 +765,7 @@ mod imp {
}
}
+ #[cfg(windows)]
unsafe extern "system" fn on_fetch_data(
callback_info: *const CF_CALLBACK_INFO,
callback_parameters: *const CF_CALLBACK_PARAMETERS,
@@ -813,6 +885,7 @@ mod imp {
});
}
+ #[cfg(windows)]
fn transfer_data_chunk(
connection_key: CF_CONNECTION_KEY,
transfer_key: i64,
@@ -839,6 +912,7 @@ mod imp {
unsafe { CfExecute(&op_info, &mut op_params) }.is_ok()
}
+ #[cfg(windows)]
fn ack_data_failed(
connection_key: CF_CONNECTION_KEY,
transfer_key: i64,
@@ -849,6 +923,7 @@ mod imp {
let _ = transfer_data_chunk(connection_key, transfer_key, request_key, &[], offset, status);
}
+ #[cfg(windows)]
unsafe extern "system" fn on_notify_rename(
callback_info: *const CF_CALLBACK_INFO,
callback_parameters: *const CF_CALLBACK_PARAMETERS,
@@ -889,6 +964,7 @@ mod imp {
});
}
+ #[cfg(windows)]
fn ack_rename(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_RENAME);
let mut op_params = CF_OPERATION_PARAMETERS {
@@ -904,6 +980,7 @@ mod imp {
let _ = unsafe { CfExecute(&op_info, &mut op_params) };
}
+ #[cfg(windows)]
unsafe extern "system" fn on_notify_delete(
callback_info: *const CF_CALLBACK_INFO,
_callback_parameters: *const CF_CALLBACK_PARAMETERS,
@@ -935,6 +1012,7 @@ mod imp {
});
}
+ #[cfg(windows)]
fn ack_delete(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_DELETE);
let mut op_params = CF_OPERATION_PARAMETERS {
@@ -951,6 +1029,7 @@ mod imp {
}
/// Refreshes a folder's placeholders from the phone (`storage.changed` or initial connect).
+ #[cfg(windows)]
async fn refresh_folder(root: Arc<ActiveRoot>, rel_folder: &str) {
let Some(node) = core_host::node() else { return };
let Ok(entries) = node.storage_list(root.device, rel_folder).await else { return };
@@ -1009,6 +1088,7 @@ mod imp {
/// Watches the sync root directory for files or folders dropped in by the user
/// and uploads them to the phone via `storage.write` / `storage.mkdir`.
+ #[cfg(windows)]
fn start_directory_watcher(root: Arc<ActiveRoot>, stop: Arc<AtomicBool>) {
let _ = std::thread::Builder::new().name("storage-watch".into()).spawn(move || {
let wide_dir = to_wide_null(&root.root_path);
@@ -1091,6 +1171,7 @@ mod imp {
});
}
+ #[cfg(windows)]
fn parse_notify_buffer(bytes: &[u8]) -> Vec<String> {
let mut out = Vec::new();
let mut offset = 0usize;
@@ -1124,6 +1205,7 @@ mod imp {
/// Checks whether `full_path` is a newly dropped non-placeholder file/folder or
/// a modified placeholder that is no longer in sync.
+ #[cfg(windows)]
fn needs_upload(full_path: &Path) -> bool {
let wide = to_wide_null(full_path);
let mut find_data = WIN32_FIND_DATAW::default();
@@ -1145,6 +1227,7 @@ mod imp {
(state.0 & CF_PLACEHOLDER_STATE_IN_SYNC.0) == 0
}
+ #[cfg(windows)]
async fn upload_local_item(root: &Arc<ActiveRoot>, rel_slash: &str, full_path: &Path) {
if !full_path.exists() || !needs_upload(full_path) {
return;
@@ -1159,6 +1242,7 @@ mod imp {
}
}
+ #[cfg(windows)]
fn mark_path_in_sync(full_path: &Path, rel_slash: &str, is_dir: bool) {
let wide = to_wide_null(full_path);
let flags = if is_dir { FILE_FLAG_BACKUP_SEMANTICS } else { Default::default() };
@@ -1213,6 +1297,7 @@ mod imp {
assert_eq!(unix_ms_to_filetime_ticks(-500), EPOCH_DIFF_100NS);
}
+ #[cfg(windows)]
#[test]
fn builds_placeholder_metadata_for_files_and_folders() {
let file_entry = StorageEntry {
@@ -1264,7 +1349,7 @@ mod imp {
}
}
-#[cfg(not(windows))]
+#[cfg(not(any(windows, target_os = "linux")))]
mod imp {
#![allow(dead_code)]