Skip to content

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

fix(desktop): fix clipboard race, updater platform gating, audio status, and download limits

saivinay18058e43f94d514a
diff --git a/desktop/app/cpp/app_helpers.cpp b/desktop/app/cpp/app_helpers.cpp
index 36ba75b..0611bd7 100644
--- a/desktop/app/cpp/app_helpers.cpp
+++ b/desktop/app/cpp/app_helpers.cpp
@@ -379,31 +379,51 @@ rust::String mac_clipboard_read_text(bool &is_private)
if (!pboard)
return rust::String();
- id types = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend)(pboard, sel_registerName("types"));
id strClass = reinterpret_cast<id>(objc_getClass("NSString"));
- if (types && strClass) {
- id concealed = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
- strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.ConcealedType");
- id transient = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
- strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.TransientType");
- id autoGen = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
- strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.AutoGeneratedType");
- if ((concealed && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), concealed))
- || (transient && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), transient))
- || (autoGen && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), autoGen))) {
- is_private = true;
- return rust::String();
- }
- }
+ if (!strClass)
+ return rust::String();
+ id concealed = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
+ strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.ConcealedType");
+ id transient = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
+ strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.TransientType");
+ id autoGen = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
+ strClass, sel_registerName("stringWithUTF8String:"), "org.nspasteboard.AutoGeneratedType");
id plainTextType = reinterpret_cast<id (*)(id, SEL, const char *)>(objc_msgSend)(
strClass, sel_registerName("stringWithUTF8String:"), "public.utf8-plain-text");
- id str = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend)(pboard, sel_registerName("stringForType:"), plainTextType);
- if (str) {
- const char *utf8 = reinterpret_cast<const char * (*)(id, SEL)>(objc_msgSend)(str, sel_registerName("UTF8String"));
- if (utf8)
- return rust::String(utf8);
+
+ for (int attempt = 0; attempt < 2; ++attempt) {
+ int64_t countBefore = reinterpret_cast<int64_t (*)(id, SEL)>(objc_msgSend)(pboard, sel_registerName("changeCount"));
+
+ id types = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend)(pboard, sel_registerName("types"));
+ if (types) {
+ if ((concealed && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), concealed))
+ || (transient && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), transient))
+ || (autoGen && reinterpret_cast<bool (*)(id, SEL, id)>(objc_msgSend)(types, sel_registerName("containsObject:"), autoGen))) {
+ int64_t countAfter = reinterpret_cast<int64_t (*)(id, SEL)>(objc_msgSend)(pboard, sel_registerName("changeCount"));
+ if (countBefore == countAfter) {
+ is_private = true;
+ return rust::String();
+ }
+ continue;
+ }
+ }
+
+ id str = reinterpret_cast<id (*)(id, SEL, id)>(objc_msgSend)(pboard, sel_registerName("stringForType:"), plainTextType);
+ int64_t countAfter = reinterpret_cast<int64_t (*)(id, SEL)>(objc_msgSend)(pboard, sel_registerName("changeCount"));
+ if (countBefore != countAfter) {
+ continue;
+ }
+
+ if (str) {
+ const char *utf8 = reinterpret_cast<const char * (*)(id, SEL)>(objc_msgSend)(str, sel_registerName("UTF8String"));
+ if (utf8)
+ return rust::String(utf8);
+ }
+ return rust::String();
}
+
+ is_private = false;
return rust::String();
}
@@ -446,8 +466,10 @@ bool mac_clipboard_write_text(rust::Str text, bool is_private)
arrClass, sel_registerName("arrayWithObjects:count:"), items, 3);
reinterpret_cast<void (*)(id, SEL, id, id)>(objc_msgSend)(
pboard, sel_registerName("declareTypes:owner:"), types, nullptr);
- reinterpret_cast<bool (*)(id, SEL, id, id)>(objc_msgSend)(
+ bool ok = reinterpret_cast<bool (*)(id, SEL, id, id)>(objc_msgSend)(
pboard, sel_registerName("setString:forType:"), nsStr, plainTextType);
+ if (!ok)
+ return false;
id dataClass = reinterpret_cast<id>(objc_getClass("NSData"));
if (dataClass) {
id emptyData = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend)(dataClass, sel_registerName("data"));
@@ -456,16 +478,16 @@ bool mac_clipboard_write_text(rust::Str text, bool is_private)
reinterpret_cast<bool (*)(id, SEL, id, id)>(objc_msgSend)(
pboard, sel_registerName("setData:forType:"), emptyData, transient);
}
+ return true;
} else {
const id items[] = { plainTextType };
id types = reinterpret_cast<id (*)(id, SEL, const id *, unsigned long)>(objc_msgSend)(
arrClass, sel_registerName("arrayWithObjects:count:"), items, 1);
reinterpret_cast<void (*)(id, SEL, id, id)>(objc_msgSend)(
pboard, sel_registerName("declareTypes:owner:"), types, nullptr);
- reinterpret_cast<bool (*)(id, SEL, id, id)>(objc_msgSend)(
+ return reinterpret_cast<bool (*)(id, SEL, id, id)>(objc_msgSend)(
pboard, sel_registerName("setString:forType:"), nsStr, plainTextType);
}
- return true;
}
#else
int64_t mac_clipboard_change_count()
diff --git a/desktop/app/src/mac/audio.rs b/desktop/app/src/mac/audio.rs
index 37ec625..f608b31 100644
--- a/desktop/app/src/mac/audio.rs
+++ b/desktop/app/src/mac/audio.rs
@@ -24,16 +24,27 @@ pub fn speaker_state() -> Option<(u8, bool)> {
parse_speaker_state(&String::from_utf8_lossy(&output.stdout))
}
-pub fn set_speaker_volume(level: u8) -> Result<(), String> {
- let script = format!("set volume output volume {level}");
- std::process::Command::new("osascript").args(["-e", &script]).output().map_err(|e| e.to_string())?;
+fn run_osascript(script: &str) -> Result<(), String> {
+ let output =
+ std::process::Command::new("osascript").args(["-e", script]).output().map_err(|e| e.to_string())?;
+ if !output.status.success() {
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ let msg = stderr.trim();
+ return Err(if msg.is_empty() {
+ format!("osascript failed with exit code {:?}", output.status.code())
+ } else {
+ msg.to_string()
+ });
+ }
Ok(())
}
+pub fn set_speaker_volume(level: u8) -> Result<(), String> {
+ run_osascript(&format!("set volume output volume {level}"))
+}
+
pub fn set_speaker_mute(muted: bool) -> Result<(), String> {
- let script = format!("set volume output muted {muted}");
- std::process::Command::new("osascript").args(["-e", &script]).output().map_err(|e| e.to_string())?;
- Ok(())
+ run_osascript(&format!("set volume output muted {muted}"))
}
pub fn toggle_speaker_mute() -> Result<bool, String> {
@@ -64,6 +75,9 @@ pub fn mic_volume() -> Option<u8> {
.args(["-e", "input volume of (get volume settings)"])
.output()
.ok()?;
+ if !output.status.success() {
+ return None;
+ }
let text = String::from_utf8_lossy(&output.stdout);
text.trim().parse().ok()
}
@@ -73,7 +87,7 @@ pub fn mic_muted() -> Option<bool> {
}
pub fn toggle_mic_mute() -> Result<bool, String> {
- let current_vol = mic_volume().unwrap_or(0);
+ let current_vol = mic_volume().ok_or_else(|| "Couldn't read microphone volume".to_string())?;
let muted = current_vol == 0;
let next = !muted;
let vol = if next {
@@ -86,7 +100,7 @@ pub fn toggle_mic_mute() -> Result<bool, String> {
if saved > 0 { saved } else { 75 }
};
let script = format!("set volume input volume {vol}");
- std::process::Command::new("osascript").args(["-e", &script]).output().map_err(|e| e.to_string())?;
+ run_osascript(&script)?;
Ok(next)
}
diff --git a/desktop/app/src/mac/http.rs b/desktop/app/src/mac/http.rs
index 92c9c0b..7668646 100644
--- a/desktop/app/src/mac/http.rs
+++ b/desktop/app/src/mac/http.rs
@@ -30,17 +30,30 @@ pub fn get_retrying(url: &str, limit: usize) -> Result<Vec<u8>, String> {
pub fn download(
url: &str,
file: &Path,
- _limit: u64,
+ limit: u64,
mut progress: impl FnMut(u64, u64),
) -> Result<(), String> {
+ let have = file.metadata().map(|m| m.len()).unwrap_or(0);
+ if have > limit {
+ return Err("too large".into());
+ }
+ let limit_str = limit.to_string();
let status = std::process::Command::new("curl")
- .args(["-s", "-L", "-f", "-C", "-", "-o", &file.to_string_lossy(), url])
+ .args(["-s", "-L", "-f", "-C", "-", "--max-filesize", &limit_str, "-o", &file.to_string_lossy(), url])
.status()
.map_err(|e| e.to_string())?;
+ let len = file.metadata().map(|m| m.len()).unwrap_or(0);
+ if len > limit {
+ let _ = std::fs::remove_file(file);
+ return Err("too large".into());
+ }
if !status.success() {
+ if status.code() == Some(63) {
+ let _ = std::fs::remove_file(file);
+ return Err("too large".into());
+ }
return Err("download failed".into());
}
- let len = file.metadata().map(|m| m.len()).unwrap_or(0);
progress(len, len);
Ok(())
}
diff --git a/desktop/app/src/updater.rs b/desktop/app/src/updater.rs
index 723d00b..c894168 100644
--- a/desktop/app/src/updater.rs
+++ b/desktop/app/src/updater.rs
@@ -40,10 +40,12 @@ use std::{
use serde::Deserialize;
use sha2::{Digest, Sha256};
+#[cfg(windows)]
+use crate::win::autostart;
use crate::{
core_host,
state::Changes,
- win::{autostart, http, toast},
+ win::{http, toast},
};
const LATEST: &str = "https://api.github.com/repos/thtbee/nectarlink/releases/latest";
@@ -104,6 +106,10 @@ pub fn set_auto_install(on: bool) {
/// Whether this copy updates itself (an installed one, not a build run
/// from its folder).
pub fn can_update() -> bool {
+ #[cfg(not(windows))]
+ return false;
+
+ #[cfg(windows)]
std::env::current_exe().is_ok_and(|exe| autostart::is_installed(&exe))
}
@@ -221,6 +227,9 @@ const INSTALLER_SUFFIX: &str =
/// The release as an update, if it's a newer published one with an
/// installer for this architecture and its sums.
fn newer_release(release: Release, current: &str) -> Option<Update> {
+ if !cfg!(windows) {
+ return None;
+ }
newer_release_for(release, current, INSTALLER_SUFFIX)
}
@@ -331,20 +340,28 @@ fn task_registered() -> bool {
/// Blocking. Returns what went wrong, in words for the user. On success the
/// app is about to quit for the installer.
pub fn install(progress: impl FnMut(u64, u64)) -> Result<(), String> {
- let update = available().ok_or("There's no update to install.")?;
- let staged = stage(&update, progress).map_err(|e| {
- tracing::warn!(error = %e, "update download failed");
- e.for_user()
- })?;
- if task_registered() {
- match install_with_task(true) {
- Ok(()) => return Ok(()),
- Err(e) => {
- tracing::warn!(error = e, "the update task didn't start; asking for permission instead")
+ #[cfg(not(windows))]
+ {
+ let _ = progress;
+ Err("Updates are not supported on this platform.".into())
+ }
+ #[cfg(windows)]
+ {
+ let update = available().ok_or("There's no update to install.")?;
+ let staged = stage(&update, progress).map_err(|e| {
+ tracing::warn!(error = %e, "update download failed");
+ e.for_user()
+ })?;
+ if task_registered() {
+ match install_with_task(true) {
+ Ok(()) => return Ok(()),
+ Err(e) => {
+ tracing::warn!(error = e, "the update task didn't start; asking for permission instead")
+ }
}
}
+ install_with_prompt(&update, &staged)
}
- install_with_prompt(&update, &staged)
}
/// Why a download didn't produce a checked installer.
@@ -667,6 +684,7 @@ fn download_verified(update: &Update, dir: &Path) -> Result<PathBuf, String> {
/// The installer with Windows' administrator prompt (no update task): a
/// fresh copy of the download in this user's temp folder, checked again, so
/// what runs is what was checked.
+#[allow(dead_code)]
fn install_with_prompt(update: &Update, staged: &Path) -> Result<(), String> {
let failed = |e: String| {
tracing::warn!(error = e, "update failed");