Harden v1.3.6 data safety and editor lifecycle

Protect file mutations, backup restoration, and WebDAV synchronization from data loss. Preserve editor drafts across concurrent saves, external changes, and window or vault transitions. Integrate toolbar customization inline and extend regression coverage.
This commit is contained in:
Yuri Karamian committed 2026-09-29 14:05:46 +02:00
1 parent 806c63aa65
commit e1f1ef0b02
26 files changed
+4292 -1284

No files matched your search

+2
View File
@@ -1782,9 +1782,11 @@ dependencies = [
"arboard",
"chrono",
"dirs",
"fs4",
"futures",
"gray_matter",
"image",
"libc",
"log",
"notify",
"objc2-app-kit",
+4
View File
@@ -51,10 +51,14 @@ rayon = "1"
quick-xml = "0.41"
urlencoding = "2"
sha2 = "0.10"
fs4 = { version = "0.8", default-features = false }
rustls = { version = "0.23", default-features = false, features = ["ring", "logging", "std", "tls12"] }
[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = "0.3.2"
[target.'cfg(target_os = "android")'.dependencies]
libc = "0.2"
[target.'cfg(target_os = "ios")'.dependencies]
tauri-plugin-ios-vault-access = { path = "plugins/ios-vault-access" }
@@ -35,6 +35,16 @@ class MainActivity : TauriActivity() {
webView.settings.layoutAlgorithm = android.webkit.WebSettings.LayoutAlgorithm.NORMAL
}
override fun onPause() {
// Give the editor a chance to persist its current document before Android
// suspends the WebView. The save remains best-effort when the process is killed.
webView?.evaluateJavascript(
"window.dispatchEvent(new Event('app-lifecycle-flush'))",
null
)
super.onPause()
}
override fun onResume() {
super.onResume()
if (hasStoragePermission()) {
+967 -172
View File
File diff suppressed because it is too large. Load diff
+306 -145
View File
@@ -2,10 +2,10 @@ use crate::asset_scope;
use crate::search::SearchIndex;
use crate::state::AppState;
use crate::types::*;
use crate::vault::{operations, watcher};
use crate::vault::operations;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tauri::{AppHandle, Manager, Runtime, State};
use tauri::{AppHandle, Emitter, Manager, Runtime, State};
use tauri_plugin_fs::FsExt;
fn index_note_bg(search: Option<Arc<SearchIndex>>, path: &str) {
@@ -27,6 +27,45 @@ fn remove_note_bg(search: Option<Arc<SearchIndex>>, path: &str) {
}
}
#[derive(Clone, serde::Serialize)]
struct PathMovedEvent<'a> {
vault_path: &'a str,
old_path: &'a str,
new_path: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
old_raw: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
new_raw: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
old_title: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
new_title: Option<&'a str>,
}
fn emit_path_move<R: Runtime>(
app: &AppHandle<R>,
vault: &str,
old: &str,
new: &str,
renamed: Option<&operations::RenamedNote>,
) {
// Called before releasing note_save; watcher reconciliation takes that lock too.
if let Err(error) = app.emit(
"vault-path-moved",
PathMovedEvent {
vault_path: vault,
old_path: old,
new_path: new,
old_raw: renamed.map(|note| note.old_raw.as_str()),
new_raw: renamed.map(|note| note.new_raw.as_str()),
old_title: renamed.map(|note| note.old_title.as_str()),
new_title: renamed.map(|note| note.new_title.as_str()),
},
) {
log::error!("Could not notify note windows about a moved path: {error}");
}
}
// ── Vault Management ──
#[cfg(target_os = "ios")]
@@ -62,13 +101,12 @@ fn open_vault_path(
// Stage the search index and watcher before replacing the active runtime.
let search = state.search_index_for_vault(&path)?;
let new_watcher = watcher::start_watcher(app.clone(), path.clone())?;
asset_scope::allow_vault_assets(&app, Path::new(&path))?;
state.ensure_vault_watcher(&app, &path)?;
// Update config. External vaults use the bookmark as their stable identity;
// the resolved path is refreshed whenever the vault opens.
let mut search_slot = state.search_index.lock().map_err(|e| e.to_string())?;
let mut watcher_slot = state.watcher.lock().map_err(|e| e.to_string())?;
let mut config = state.config.lock().map_err(|e| e.to_string())?;
let mut next = config.clone();
let active_bookmark_id = external
@@ -108,16 +146,18 @@ fn open_vault_path(
}
next.active_vault = Some(path);
next.active_bookmark_id = active_bookmark_id;
save_app_config(&next)?;
if let Err(error) = save_app_config(&next) {
drop(config);
drop(search_slot);
let _ = state.retain_vault_watchers();
return Err(error);
}
let old_search = search_slot.replace(search.clone());
let old_watcher = watcher_slot.replace(new_watcher);
*config = next;
drop(config);
drop(watcher_slot);
drop(search_slot);
// Watcher shutdown and index disposal may block; never do either while a
// global state slot is locked.
drop(old_watcher);
// Index disposal and watcher shutdown must not hold global state slots.
state.retain_vault_watchers()?;
drop(old_search);
Ok(search)
@@ -286,7 +326,6 @@ fn remove_vault_path<R: Runtime>(
.search_index
.lock()
.map_err(|error| error.to_string())?;
let mut watcher_slot = state.watcher.lock().map_err(|error| error.to_string())?;
let mut config = state.config.lock().map_err(|error| error.to_string())?;
let target = if let Some(bookmark_id) = bookmark_id.as_deref() {
config
@@ -312,6 +351,8 @@ fn remove_vault_path<R: Runtime>(
let Some((target_path, target_bookmark, is_active)) = target else {
return Ok(());
};
#[cfg(desktop)]
AppState::ensure_no_detached_windows(app, &target_path)?;
let old = config.clone();
let mut next = old.clone();
@@ -343,12 +384,10 @@ fn remove_vault_path<R: Runtime>(
let _ = (app, &target_bookmark);
let old_search = if is_active { search_slot.take() } else { None };
let old_watcher = if is_active { watcher_slot.take() } else { None };
*config = next;
drop(config);
drop(watcher_slot);
drop(search_slot);
drop(old_watcher);
state.retain_vault_watchers()?;
drop(old_search);
Ok(())
@@ -361,9 +400,26 @@ pub fn get_app_config(state: State<'_, AppState>) -> Result<AppConfig, String> {
}
#[tauri::command]
pub fn get_pending_open_file(state: State<'_, AppState>) -> Result<Option<String>, String> {
let mut pending = state.pending_open_file.lock().map_err(|e| e.to_string())?;
Ok(pending.take())
pub fn get_pending_open_file(
state: State<'_, AppState>,
) -> Result<Option<crate::state::PendingOpenFile>, String> {
state.peek_pending_open_file()
}
#[tauri::command]
pub fn ack_pending_open_file(state: State<'_, AppState>, id: u64) -> Result<(), String> {
if !state.ack_pending_open_file(id)? {
return Err("File-open request is no longer pending".to_string());
}
Ok(())
}
#[cfg(desktop)]
#[tauri::command]
pub fn cancel_window_close(state: State<'_, AppState>) {
state
.quitting
.store(false, std::sync::atomic::Ordering::Relaxed);
}
/// Finish a desktop close request only after the frontend has flushed its editor.
@@ -637,7 +693,8 @@ pub async fn create_notebook(
}
#[tauri::command]
pub async fn rename_notebook(
pub async fn rename_notebook<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
vault_path: String,
path: String,
@@ -646,6 +703,7 @@ pub async fn rename_notebook(
let search = state.vault_snapshot(&vault_path)?;
let _write_guard = state.note_save.lock().await;
let renamed = operations::rename_notebook(&vault_path, &path, &new_name)?;
emit_path_move(&app, &vault_path, &path, &renamed, None);
if let Some(search) = search {
if let Err(error) = search.queue_vault_refresh(&vault_path) {
log::error!("Failed to queue renamed notebook search refresh: {error}");
@@ -655,7 +713,8 @@ pub async fn rename_notebook(
}
#[tauri::command]
pub async fn move_notebook(
pub async fn move_notebook<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
vault_path: String,
notebook_path: String,
@@ -665,32 +724,8 @@ pub async fn move_notebook(
let _write_guard = state.note_save.lock().await;
let vault_path = vault_path.as_str();
let old_relative = Path::new(&notebook_path)
.strip_prefix(vault_path)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default();
let new_full_path = operations::move_notebook(vault_path, &notebook_path, &dest_parent)?;
let new_relative = Path::new(&new_full_path)
.strip_prefix(vault_path)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default();
// Update Quick Access paths for all notes inside the moved notebook
if let Ok(mut qa) = operations::load_quick_access(vault_path) {
let old_prefix = format!("{}/", old_relative);
let mut changed = false;
for path in qa.iter_mut() {
if path.starts_with(&old_prefix) {
*path = format!("{}/{}", new_relative, &path[old_prefix.len()..]);
changed = true;
}
}
if changed {
let _ = operations::save_quick_access(vault_path, &qa);
}
}
emit_path_move(&app, vault_path, &notebook_path, &new_full_path, None);
// Reconcile renamed paths without blocking the move on a full rebuild.
if let Some(search) = search {
@@ -759,7 +794,8 @@ pub async fn save_note(
path: String,
meta: NoteMeta,
body: String,
) -> Result<(), String> {
expected_raw: String,
) -> Result<NoteContent, String> {
let search = state.vault_snapshot(&vault_path)?;
let max_versions = state
.config
@@ -773,7 +809,8 @@ pub async fn save_note(
tauri::async_runtime::spawn_blocking(move || {
// Keep ordering even if the calling window closes while its save runs.
let _save_guard = save_guard;
if let Some(saved) = operations::save_note(&vault_path, &path, &meta, &body)? {
if let Some(saved) = operations::save_note(&vault_path, &path, &meta, &body, &expected_raw)?
{
crate::history::maybe_snapshot(
&vault_path,
&saved.id,
@@ -782,7 +819,7 @@ pub async fn save_note(
);
index_note_bg(search, &path);
}
Ok(())
operations::read_vault_note(&vault_path, &path)
})
.await
.map_err(|error| error.to_string())?
@@ -840,7 +877,8 @@ pub async fn create_daily_note(
}
#[tauri::command]
pub async fn rename_note(
pub async fn rename_note<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
vault_path: String,
path: String,
@@ -849,12 +887,13 @@ pub async fn rename_note(
let search = state.vault_snapshot(&vault_path)?;
let _write_guard = state.note_save.lock().await;
let renamed = operations::rename_note(&path, &new_title, &vault_path)?;
emit_path_move(&app, &vault_path, &path, &renamed.path, Some(&renamed));
if let Some(search) = search {
if let Err(error) = search.queue_vault_refresh(&vault_path) {
log::error!("Failed to queue renamed note search refresh: {error}");
}
}
Ok(renamed)
Ok(renamed.path)
}
#[tauri::command]
@@ -874,7 +913,8 @@ pub async fn delete_note(
}
#[tauri::command]
pub async fn move_note(
pub async fn move_note<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
vault_path: String,
note_path: String,
@@ -884,29 +924,9 @@ pub async fn move_note(
let _write_guard = state.note_save.lock().await;
let vault_path = vault_path.as_str();
// Compute old relative path before move
let old_relative = Path::new(&note_path)
.strip_prefix(vault_path)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default();
let new_full_path = operations::move_note(vault_path, &note_path, &dest_notebook)?;
emit_path_move(&app, vault_path, &note_path, &new_full_path, None);
// Update quick access if the moved note was in it
if !old_relative.is_empty() {
if let Ok(mut qa) = operations::load_quick_access(vault_path) {
if let Some(pos) = qa.iter().position(|p| *p == old_relative) {
let new_relative = Path::new(&new_full_path)
.strip_prefix(vault_path)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default();
if !new_relative.is_empty() {
qa[pos] = new_relative;
let _ = operations::save_quick_access(vault_path, &qa);
}
}
}
}
index_note_bg(search.clone(), &note_path);
index_note_bg(search, &new_full_path);
@@ -1394,6 +1414,7 @@ pub async fn set_task_done(
let NoteContent {
meta,
content: body,
raw,
..
} = operations::read_vault_note(&vault_path, &note_path)?;
let mut lines: Vec<String> = body.lines().map(|l| l.to_string()).collect();
@@ -1416,7 +1437,7 @@ pub async fn set_task_done(
if body.ends_with('\n') && !new_body.ends_with('\n') {
new_body.push('\n');
}
if operations::save_note(&vault_path, &note_path, &meta, &new_body)?.is_some() {
if operations::save_note(&vault_path, &note_path, &meta, &new_body, &raw)?.is_some() {
index_note_bg(search, &note_path);
}
Ok(())
@@ -1455,6 +1476,7 @@ pub async fn set_task_priority(
let NoteContent {
meta,
content: body,
raw,
..
} = operations::read_vault_note(&vault_path, &note_path)?;
let mut lines: Vec<String> = body.lines().map(|l| l.to_string()).collect();
@@ -1476,7 +1498,7 @@ pub async fn set_task_priority(
if body.ends_with('\n') && !new_body.ends_with('\n') {
new_body.push('\n');
}
if operations::save_note(&vault_path, &note_path, &meta, &new_body)?.is_some() {
if operations::save_note(&vault_path, &note_path, &meta, &new_body, &raw)?.is_some() {
index_note_bg(search, &note_path);
}
Ok(())
@@ -1514,6 +1536,7 @@ pub async fn set_task_due(
let NoteContent {
meta,
content: body,
raw,
..
} = operations::read_vault_note(&vault_path, &note_path)?;
let mut lines: Vec<String> = body.lines().map(|l| l.to_string()).collect();
@@ -1535,7 +1558,7 @@ pub async fn set_task_due(
if body.ends_with('\n') && !new_body.ends_with('\n') {
new_body.push('\n');
}
if operations::save_note(&vault_path, &note_path, &meta, &new_body)?.is_some() {
if operations::save_note(&vault_path, &note_path, &meta, &new_body, &raw)?.is_some() {
index_note_bg(search, &note_path);
}
Ok(())
@@ -2393,6 +2416,7 @@ pub fn import_obsidian(app: AppHandle) -> Result<(), String> {
fn do_import_obsidian(app: AppHandle, vault_path: &str) -> Result<ImportResult, String> {
let state = app.state::<AppState>();
let _maintenance_guard = state.vault_maintenance.blocking_lock();
let _write_guard = state.note_save.blocking_lock();
ensure_cleanup_idle(&state)?;
state
@@ -3057,89 +3081,67 @@ pub fn write_bytes_to(app: AppHandle, destination: String, data: Vec<u8>) -> Res
// ── Backup ──
#[tauri::command]
pub fn create_backup(app: AppHandle) -> Result<(), String> {
let (vault_path, backup_dir, include_attachments, max_count) = {
pub fn create_backup(app: AppHandle, vault_path: String) -> Result<(), String> {
let (backup_dir, include_attachments, max_count, bookmark_id) = {
let state = app.state::<AppState>();
state.validate_vault(&vault_path)?;
let config = state.config.lock().map_err(|e| e.to_string())?;
let vault_path = config.active_vault.clone().ok_or("No active vault")?;
let backup_dir = crate::backup::get_backup_dir(&config.backup_location)?;
let bookmark_id = config
.vaults
.iter()
.find(|vault| vault.path == vault_path)
.and_then(|vault| vault.bookmark_id.clone());
(
vault_path,
backup_dir,
crate::backup::get_backup_dir(&config.backup_location)?,
config.backup_include_attachments,
config.backup_max_count,
bookmark_id,
)
};
std::thread::spawn(move || {
use tauri::Emitter;
match crate::backup::create_backup(&vault_path, &backup_dir, include_attachments) {
Ok(entry) => {
// Update last backup time
let state = app.state::<AppState>();
if let Ok(mut config) = state.config.lock() {
config.last_backup_time = Some(entry.created.clone());
let _ = save_app_config(&config);
}
// Cleanup old backups
let _ = crate::backup::cleanup_old_backups(&backup_dir, max_count);
let _ = app.emit(
"backup-done",
serde_json::json!({
"success": true,
"entry": entry,
}),
);
}
Err(e) => {
let _ = app.emit(
"backup-done",
serde_json::json!({
"success": false,
"error": e,
}),
);
}
}
});
Ok(())
}
#[tauri::command]
pub fn list_backups(state: State<'_, AppState>) -> Result<Vec<BackupEntry>, String> {
let config = state.config.lock().map_err(|e| e.to_string())?;
let backup_dir = crate::backup::get_backup_dir(&config.backup_location)?;
crate::backup::list_backups(&backup_dir)
}
#[tauri::command]
pub fn restore_backup(app: AppHandle, backup_path: String) -> Result<(), String> {
let (vault_path, backup_dir) = {
let state = app.state::<AppState>();
let config = state.config.lock().map_err(|e| e.to_string())?;
(
config.active_vault.clone().ok_or("No active vault")?,
crate::backup::get_backup_dir(&config.backup_location)?,
)
};
std::thread::spawn(move || {
use tauri::Emitter;
match crate::backup::restore_backup(&vault_path, &backup_dir, &backup_path) {
Ok(()) => {
let _maintenance = state.vault_maintenance.blocking_lock();
let result = {
let _writes = state.note_save.blocking_lock();
crate::backup::create_backup(&vault_path, &backup_dir, include_attachments)
};
match result {
Ok(entry) => {
if let Ok(mut config) = state.config.lock() {
let mut next = config.clone();
if let Some(vault) = next.vaults.iter_mut().find(|candidate| {
vault_matches_identity(candidate, &vault_path, bookmark_id.as_deref())
}) {
vault.last_backup_time = Some(entry.created.clone());
match save_app_config(&next) {
Ok(()) => *config = next,
Err(error) => {
log::error!("Could not persist backup timestamp: {error}")
}
}
}
}
if let Err(error) =
crate::backup::cleanup_old_backups(&backup_dir, &vault_path, max_count)
{
log::error!("Could not prune completed backups: {error}");
}
let _ = app.emit(
"restore-done",
"backup-done",
serde_json::json!({
"success": true,
"vault_path": vault_path, "bookmark_id": bookmark_id,
"success": true, "entry": entry,
}),
);
}
Err(e) => {
Err(error) => {
let _ = app.emit(
"restore-done",
"backup-done",
serde_json::json!({
"success": false,
"error": e,
"vault_path": vault_path, "bookmark_id": bookmark_id,
"success": false, "error": error,
}),
);
}
@@ -3149,9 +3151,98 @@ pub fn restore_backup(app: AppHandle, backup_path: String) -> Result<(), String>
}
#[tauri::command]
pub fn delete_backup(state: State<'_, AppState>, backup_path: String) -> Result<(), String> {
let config = state.config.lock().map_err(|error| error.to_string())?;
let backup_dir = crate::backup::get_backup_dir(&config.backup_location)?;
pub async fn list_backups(
state: State<'_, AppState>,
vault_path: String,
) -> Result<Vec<BackupEntry>, String> {
state.validate_vault(&vault_path)?;
let backup_dir = {
let config = state.config.lock().map_err(|e| e.to_string())?;
crate::backup::get_backup_dir(&config.backup_location)?
};
tauri::async_runtime::spawn_blocking(move || {
crate::backup::list_backups(&backup_dir, &vault_path)
})
.await
.map_err(|error| error.to_string())?
}
#[tauri::command]
pub fn restore_backup(
app: AppHandle,
vault_path: String,
backup_path: String,
) -> Result<(), String> {
let backup_dir = {
let state = app.state::<AppState>();
state.validate_vault(&vault_path)?;
let config = state.config.lock().map_err(|e| e.to_string())?;
crate::backup::get_backup_dir(&config.backup_location)?
};
std::thread::spawn(move || {
use std::sync::atomic::Ordering;
use tauri::Emitter;
let state = app.state::<AppState>();
let _maintenance = state.vault_maintenance.blocking_lock();
let result = {
let _writes = state.note_save.blocking_lock();
state.importing.store(true, Ordering::Release);
let result = crate::backup::restore_backup(&vault_path, &backup_dir, &backup_path);
state.importing.store(false, Ordering::Release);
result
};
match result {
Ok(()) => {
if let Ok(Some(search)) = state.vault_snapshot(&vault_path) {
if let Err(error) = search.queue_vault_refresh(&vault_path) {
log::error!("Could not refresh restored vault search: {error}");
}
}
let _ = app.emit(
"file-changed",
FileEvent {
vault_path: vault_path.clone(),
event_type: "modify".into(),
path: vault_path.clone(),
note: None,
},
);
let _ = app.emit(
"vault-restored",
serde_json::json!({ "vault_path": vault_path }),
);
let _ = app.emit(
"restore-done",
serde_json::json!({
"vault_path": vault_path, "success": true,
}),
);
}
Err(error) => {
let _ = app.emit(
"restore-done",
serde_json::json!({
"vault_path": vault_path, "success": false, "error": error,
}),
);
}
}
});
Ok(())
}
#[tauri::command]
pub fn delete_backup(
state: State<'_, AppState>,
vault_path: String,
backup_path: String,
) -> Result<(), String> {
state.validate_vault(&vault_path)?;
let backup_dir = {
let config = state.config.lock().map_err(|error| error.to_string())?;
crate::backup::get_backup_dir(&config.backup_location)?
};
crate::backup::delete_backup(&backup_dir, &backup_path)
}
@@ -3499,13 +3590,13 @@ pub fn sync_now(app: AppHandle) -> Result<(), String> {
}
let _ = app.emit(
"sync-done",
serde_json::json!({ "success": true, "summary": summary, "last_sync_time": ts }),
serde_json::json!({ "vault_path": vault, "bookmark_id": bookmark_id, "success": true, "summary": summary, "last_sync_time": ts }),
);
}
Err(e) => {
let _ = app.emit(
"sync-error",
serde_json::json!({ "success": false, "error": e }),
serde_json::json!({ "vault_path": vault, "success": false, "error": e }),
);
}
}
@@ -3933,6 +4024,62 @@ mod mutation_order_tests {
use super::*;
use futures::FutureExt;
#[tokio::test]
async fn concurrent_editors_reject_stale_baselines_and_accept_the_saved_revision() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path().to_string_lossy().into_owned();
operations::ensure_vault_structure(&root).unwrap();
let entry = operations::create_note(&root, None, "Shared").unwrap();
let initial = operations::read_note(&root, &entry.path).unwrap();
let app = tauri::test::mock_builder()
.manage(AppState::new(AppConfig {
active_vault: Some(root.clone()),
..AppConfig::default()
}))
.build(tauri::test::mock_context(tauri::test::noop_assets()))
.unwrap();
let state = app.state::<AppState>();
let held = state.note_save.lock().await;
let mut first = Box::pin(save_note(
app.state(),
root.clone(),
entry.path.clone(),
initial.meta.clone(),
"# Shared\n\nFirst editor's content".into(),
initial.raw.clone(),
));
let mut stale = Box::pin(save_note(
app.state(),
root.clone(),
entry.path.clone(),
initial.meta.clone(),
"# Shared\n\nStale editor's content".into(),
initial.raw,
));
assert!(first.as_mut().now_or_never().is_none());
assert!(stale.as_mut().now_or_never().is_none());
drop(held);
let accepted = first.await.unwrap();
assert!(stale.await.is_err());
assert_eq!(std::fs::read_to_string(&entry.path).unwrap(), accepted.raw);
assert_eq!(accepted.content, "# Shared\n\nFirst editor's content");
let subsequent = save_note(
app.state(),
root.clone(),
entry.path.clone(),
accepted.meta,
"# Shared\n\nNext accepted edit".into(),
accepted.raw,
)
.await
.unwrap();
assert_eq!(subsequent.content, "# Shared\n\nNext accepted edit");
assert_eq!(
std::fs::read_to_string(&entry.path).unwrap(),
subsequent.raw
);
}
#[tokio::test]
async fn detached_history_quick_access_titles_and_graph_keep_their_origin() {
let first = tempfile::tempdir().unwrap();
@@ -4094,6 +4241,7 @@ mod mutation_order_tests {
alpha.path.clone(),
alpha.meta.clone(),
"# Shared\n\nQueued save".into(),
std::fs::read_to_string(&alpha.path).unwrap(),
));
assert!(creating.as_mut().now_or_never().is_none());
assert!(saving.as_mut().now_or_never().is_none());
@@ -4126,6 +4274,7 @@ mod mutation_order_tests {
alpha.path.clone(),
alpha.meta,
"# Shared\n\nDetached save".into(),
std::fs::read_to_string(&alpha.path).unwrap(),
)
.await
.unwrap();
@@ -4222,6 +4371,7 @@ mod mutation_order_tests {
Some("builtin:briefcase".into()),
));
let mut renaming = Box::pin(rename_notebook(
app.handle().clone(),
app.state(),
path.clone(),
vault.path().join("Projects").to_string_lossy().into_owned(),
@@ -4233,6 +4383,7 @@ mod mutation_order_tests {
changing.await.unwrap();
let renamed = renaming.await.unwrap();
move_notebook(
app.handle().clone(),
app.state(),
path.clone(),
renamed,
@@ -4298,6 +4449,7 @@ mod mutation_order_tests {
note.path.clone(),
note.meta,
"# Draft\n\nSaved before search is ready.".into(),
std::fs::read_to_string(&note.path).unwrap(),
)
.await
.unwrap();
@@ -4324,13 +4476,21 @@ mod mutation_order_tests {
let saving = state.note_save.lock().await;
let mut moving = Box::pin(move_note(
app.handle().clone(),
app.state(),
vault_path.clone(),
entry.path.clone(),
archive.to_string_lossy().into_owned(),
));
assert!(moving.as_mut().now_or_never().is_none());
operations::save_note(&vault_path, &entry.path, &entry.meta, "Latest saved text").unwrap();
operations::save_note(
&vault_path,
&entry.path,
&entry.meta,
"Latest saved text",
&std::fs::read_to_string(&entry.path).unwrap(),
)
.unwrap();
drop(saving);
let moved_path = moving.await.unwrap();
assert!(!Path::new(&entry.path).exists());
@@ -4349,6 +4509,7 @@ mod mutation_order_tests {
&moved_path,
&moved.meta,
"Latest text before trash",
&moved.raw,
)
.unwrap();
drop(saving);
+136 -53
View File
@@ -24,6 +24,63 @@ fn history_dir(vault_path: &str, note_id: &str) -> Result<PathBuf, String> {
.join(safe_path_component(note_id, "note ID")?))
}
fn snapshot_path(dir: &Path, raw_content: &str) -> Result<(), String> {
use std::io::Write;
let mut temporary = tempfile::Builder::new()
.prefix(".helixnotes-history-")
.tempfile_in(dir)
.map_err(|error| error.to_string())?;
temporary
.write_all(raw_content.as_bytes())
.map_err(|error| error.to_string())?;
temporary
.as_file()
.sync_all()
.map_err(|error| error.to_string())?;
let mut instant = Utc::now();
loop {
// Fractional seconds keep timestamps valid RFC3339 for the UI while
// allowing multiple versions within one second.
let timestamp = instant.format("%Y-%m-%dT%H-%M-%S%.3f");
let path = dir.join(format!("{timestamp}.md"));
match temporary.persist_noclobber(&path) {
Ok(_) => return Ok(()),
Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => {
temporary = error.file;
instant += chrono::Duration::milliseconds(1);
}
Err(error) => return Err(error.error.to_string()),
}
}
}
fn version_timestamp(filename: &str) -> String {
match (filename.get(..13), filename.get(14..16), filename.get(17..)) {
(Some(hour), Some(minute), Some(rest))
if filename.as_bytes().get(10) == Some(&b'T')
&& filename.as_bytes().get(13) == Some(&b'-')
&& filename.as_bytes().get(16) == Some(&b'-') =>
{
format!("{hour}:{minute}:{rest}Z")
}
_ => filename.to_string(),
}
}
fn snapshot_filename(timestamp: &str) -> String {
let time = timestamp.strip_suffix('Z').unwrap_or(timestamp);
match (time.get(..13), time.get(14..16), time.get(17..)) {
(Some(hour), Some(minute), Some(rest))
if time.as_bytes().get(10) == Some(&b'T')
&& time.as_bytes().get(13) == Some(&b':')
&& time.as_bytes().get(16) == Some(&b':') =>
{
format!("{hour}-{minute}-{rest}.md")
}
_ => format!("{timestamp}.md"),
}
}
/// Save a version snapshot if enough time has passed since the last one.
/// Minimum interval: 5 minutes between snapshots.
pub fn maybe_snapshot(vault_path: &str, note_id: &str, raw_content: &str, max_versions: u32) {
@@ -32,31 +89,34 @@ pub fn maybe_snapshot(vault_path: &str, note_id: &str, raw_content: &str, max_ve
return;
};
// Check if we should create a snapshot (5 min cooldown)
// Old second-resolution and new fractional-second names share the first
// 19 ASCII bytes. Ignore arbitrary files (including malformed Unicode).
if let Ok(entries) = fs::read_dir(&dir) {
let mut files: Vec<String> = entries
.filter_map(|e| e.ok())
.filter_map(|e| e.file_name().to_str().map(|s| s.to_string()))
.filter(|n| n.ends_with(".md"))
.collect();
files.sort();
if let Some(last) = files.last() {
if let Some(ts) = last.strip_suffix(".md") {
// Parse timestamp: 2026-02-08T18-30-00.md
if let Some(t_pos) = ts.find('T') {
let date_part = &ts[..t_pos];
let time_part = &ts[t_pos + 1..];
let time_colons = time_part.replace('-', ":");
let iso = format!("{}T{}Z", date_part, time_colons);
if let Ok(last_time) = iso.parse::<DateTime<Utc>>() {
let elapsed = Utc::now() - last_time;
if elapsed.num_minutes() < 5 {
return; // Too soon, skip
}
}
let latest = entries
.filter_map(|entry| {
let name = entry.ok()?.file_name();
let name = name.to_str()?;
if !name.ends_with(".md") {
return None;
}
}
let (Some(hour), Some(minute), Some(second)) =
(name.get(..13), name.get(14..16), name.get(17..19))
else {
return None;
};
if name.as_bytes().get(10) != Some(&b'T')
|| name.as_bytes().get(13) != Some(&b'-')
|| name.as_bytes().get(16) != Some(&b'-')
{
return None;
}
format!("{hour}:{minute}:{second}Z")
.parse::<DateTime<Utc>>()
.ok()
})
.max();
if latest.is_some_and(|last| (Utc::now() - last).num_minutes() < 5) {
return;
}
}
@@ -66,12 +126,8 @@ pub fn maybe_snapshot(vault_path: &str, note_id: &str, raw_content: &str, max_ve
return;
}
let timestamp = Utc::now().format("%Y-%m-%dT%H-%M-%S").to_string();
let filename = format!("{}.md", timestamp);
let path = dir.join(&filename);
if let Err(e) = fs::write(&path, raw_content) {
eprintln!("Failed to write version snapshot: {}", e);
if let Err(error) = snapshot_path(&dir, raw_content) {
eprintln!("Failed to write version snapshot: {error}");
return;
}
@@ -91,11 +147,7 @@ pub fn force_snapshot(
let dir = history_dir(vault_path, note_id)?;
fs::create_dir_all(&dir).map_err(|error| error.to_string())?;
let timestamp = Utc::now().format("%Y-%m-%dT%H-%M-%S").to_string();
let filename = format!("{}.md", timestamp);
let path = dir.join(&filename);
fs::write(&path, raw_content).map_err(|error| error.to_string())?;
snapshot_path(&dir, raw_content)?;
if max_versions > 0 {
prune_versions(&dir, max_versions)?;
@@ -123,16 +175,7 @@ pub fn list_versions(vault_path: &str, note_id: &str) -> Result<Vec<VersionEntry
.to_string();
let meta = fs::metadata(&path).map_err(|e| e.to_string())?;
// Parse timestamp from filename
let timestamp = if let Some(t_pos) = filename.find('T') {
let date_part = &filename[..t_pos];
let time_part = &filename[t_pos + 1..];
let time_colons = time_part.replace('-', ":");
format!("{}T{}Z", date_part, time_colons)
} else {
filename.clone()
};
let timestamp = version_timestamp(&filename);
entries.push(VersionEntry {
timestamp,
size: meta.len(),
@@ -148,15 +191,8 @@ pub fn list_versions(vault_path: &str, note_id: &str) -> Result<Vec<VersionEntry
/// Get the raw content of a specific version.
pub fn get_version(vault_path: &str, note_id: &str, timestamp: &str) -> Result<String, String> {
safe_path_component(timestamp, "version timestamp")?;
// Convert ISO timestamp back to filename: 2026-02-08T18:30:00Z → 2026-02-08T18-30-00.md
let filename = if let Some(t_pos) = timestamp.find('T') {
let date_part = &timestamp[..t_pos];
let time_part = timestamp[t_pos + 1..].trim_end_matches('Z');
let time_dashes = time_part.replace(':', "-");
format!("{date_part}T{time_dashes}.md")
} else {
format!("{}.md", timestamp)
};
// Accept both legacy second-resolution names and unique suffixes.
let filename = snapshot_filename(timestamp);
let path = history_dir(vault_path, note_id)?.join(&filename);
fs::read_to_string(&path).map_err(|e| format!("Version not found: {}", e))
@@ -206,4 +242,51 @@ mod tests {
fs::remove_dir_all(vault).unwrap();
}
#[test]
fn rapid_forced_versions_are_individually_retrievable_and_prunable() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path().to_str().unwrap();
for index in 0..200 {
super::force_snapshot(root, "note", &format!("snapshot {index}"), 0).unwrap();
}
let versions = list_versions(root, "note").unwrap();
assert_eq!(versions.len(), 200);
let contents: std::collections::HashSet<_> = versions
.iter()
.map(|version| get_version(root, "note", &version.timestamp).unwrap())
.collect();
assert_eq!(contents.len(), 200);
assert!(contents.contains("snapshot 0"));
assert!(contents.contains("snapshot 199"));
super::force_snapshot(root, "note", "latest", 10).unwrap();
let retained = list_versions(root, "note").unwrap();
assert_eq!(retained.len(), 10);
assert_eq!(
get_version(root, "note", &retained[0].timestamp).unwrap(),
"latest"
);
}
#[test]
fn legacy_versions_remain_readable_and_recent_unique_versions_observe_cooldown() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path().to_str().unwrap();
let history = vault.path().join(".helixnotes/history/note");
fs::create_dir_all(&history).unwrap();
fs::write(history.join("2026-01-01T00-00-00.md"), "legacy").unwrap();
fs::write(history.join("2026-01-01T1é-00-00.md"), "unusual filename").unwrap();
super::force_snapshot(root, "note", "new", 0).unwrap();
super::maybe_snapshot(root, "note", "too soon", 0);
let versions = list_versions(root, "note").unwrap();
assert_eq!(versions.len(), 3);
assert_eq!(
get_version(root, "note", "2026-01-01T00:00:00Z").unwrap(),
"legacy"
);
assert_eq!(
get_version(root, "note", &versions[0].timestamp).unwrap(),
"new"
);
}
}
+14 -4
View File
@@ -146,9 +146,9 @@ pub fn run() {
asset_scope::allow_external_note_assets(app.handle(), &resolved);
}
let app_state = app.state::<AppState>();
let _ = app_state.pending_open_file.lock().map(|mut p| {
*p = Some(resolved_str.to_string());
});
if let Err(error) = app_state.enqueue_open_file(resolved_str.to_string()) {
log::error!("Could not queue startup file: {error}");
}
}
}
}
@@ -253,6 +253,9 @@ pub fn run() {
commands::get_install_type,
commands::is_mobile_platform,
commands::get_pending_open_file,
commands::ack_pending_open_file,
#[cfg(desktop)]
commands::cancel_window_close,
])
.register_asynchronous_uri_scheme_protocol("imgproxy", |_ctx, request, responder| {
let path = request.uri().path().to_string();
@@ -307,7 +310,14 @@ pub fn run() {
}
let _ = asset_scope::allow_external_note_assets(app, &resolved);
}
let _ = app.emit("open-file", resolved_str.to_string());
if let Err(error) = app
.state::<AppState>()
.enqueue_open_file(resolved_str.to_string())
{
log::error!("Could not queue open file: {error}");
} else {
let _ = app.emit("open-file-pending", ());
}
}
}
}));
+157 -5
View File
@@ -1,30 +1,145 @@
use crate::search::{SearchIndex, SearchIndexShutdown};
use crate::types::AppConfig;
use crate::vault::watcher::VaultWatcher;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex, Weak};
use tauri::AppHandle;
#[cfg(desktop)]
use tauri::{Manager, Runtime, Url};
#[cfg(desktop)]
fn is_detached_window_for_vault(label: &str, url: &Url, target_path: &str) -> bool {
label.starts_with("note-")
&& url
.query_pairs()
.any(|(key, value)| key == "vault" && value == target_path)
}
struct CachedSearchIndex {
index: Weak<SearchIndex>,
shutdown: Arc<SearchIndexShutdown>,
}
#[derive(Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PendingOpenFile {
pub id: u64,
pub path: String,
}
#[derive(Default)]
struct PendingOpenFiles {
next_id: u64,
requests: VecDeque<PendingOpenFile>,
}
pub struct AppState {
pub config: Mutex<AppConfig>,
pub search_index: Mutex<Option<Arc<SearchIndex>>>,
search_indexes: Mutex<HashMap<String, CachedSearchIndex>>,
pub watcher: Mutex<Option<VaultWatcher>>,
pub watchers: Mutex<HashMap<String, VaultWatcher>>,
pub vault_transition: tokio::sync::Mutex<()>,
pub vault_maintenance: Arc<tokio::sync::Mutex<()>>,
pub note_save: Arc<tokio::sync::Mutex<()>>,
pub importing: AtomicBool,
pub syncing: AtomicBool,
#[cfg(desktop)]
pub quitting: AtomicBool,
pub pending_open_file: Mutex<Option<String>>,
pending_open_files: Mutex<PendingOpenFiles>,
}
impl AppState {
pub fn enqueue_open_file(&self, path: String) -> Result<(), String> {
let mut pending = self
.pending_open_files
.lock()
.map_err(|error| error.to_string())?;
pending.next_id += 1;
let id = pending.next_id;
pending.requests.push_back(PendingOpenFile { id, path });
Ok(())
}
pub fn peek_pending_open_file(&self) -> Result<Option<PendingOpenFile>, String> {
let pending = self
.pending_open_files
.lock()
.map_err(|error| error.to_string())?;
Ok(pending.requests.front().cloned())
}
pub fn ack_pending_open_file(&self, id: u64) -> Result<bool, String> {
let mut pending = self
.pending_open_files
.lock()
.map_err(|error| error.to_string())?;
if pending
.requests
.front()
.is_some_and(|request| request.id == id)
{
pending.requests.pop_front();
Ok(true)
} else {
Ok(false)
}
}
pub fn ensure_vault_watcher(&self, app: &AppHandle, vault_path: &str) -> Result<(), String> {
let mut watchers = self.watchers.lock().map_err(|error| error.to_string())?;
if !watchers.contains_key(vault_path) {
let watcher = crate::vault::watcher::start_watcher(app.clone(), vault_path.to_owned())?;
watchers.insert(vault_path.to_owned(), watcher);
}
Ok(())
}
pub fn retain_vault_watchers(&self) -> Result<(), String> {
let configured: HashSet<String> = {
let config = self.config.lock().map_err(|error| error.to_string())?;
config
.vaults
.iter()
.map(|vault| vault.path.clone())
.chain(config.active_vault.clone())
.collect()
};
let removed = {
let mut watchers = self.watchers.lock().map_err(|error| error.to_string())?;
let removed: Vec<String> = watchers
.keys()
.filter(|path| !configured.contains(*path))
.cloned()
.collect();
removed
.into_iter()
.filter_map(|path| watchers.remove(&path))
.collect::<Vec<_>>()
};
drop(removed);
Ok(())
}
#[cfg(desktop)]
pub fn ensure_no_detached_windows<R: Runtime>(
app: &AppHandle<R>,
target_path: &str,
) -> Result<(), String> {
for (label, window) in app.webview_windows() {
if is_detached_window_for_vault(
&label,
&window.url().map_err(|error| error.to_string())?,
target_path,
) {
return Err(
"Close this vault's detached note windows before forgetting it.".into(),
);
}
}
Ok(())
}
// A refresh worker may outlive a vault switch. Reuse its writer when opening
// that vault again instead of competing for Tantivy's exclusive writer lock.
pub fn search_index_for_vault(&self, vault_path: &str) -> Result<Arc<SearchIndex>, String> {
@@ -118,14 +233,15 @@ impl AppState {
config: Mutex::new(config),
search_index: Mutex::new(None),
search_indexes: Mutex::new(HashMap::new()),
watcher: Mutex::new(None),
watchers: Mutex::new(HashMap::new()),
vault_transition: tokio::sync::Mutex::new(()),
vault_maintenance: Arc::new(tokio::sync::Mutex::new(())),
note_save: Arc::new(tokio::sync::Mutex::new(())),
importing: AtomicBool::new(false),
syncing: AtomicBool::new(false),
#[cfg(desktop)]
quitting: AtomicBool::new(false),
pending_open_file: Mutex::new(None),
pending_open_files: Mutex::new(PendingOpenFiles::default()),
}
}
}
@@ -134,6 +250,42 @@ impl AppState {
mod tests {
use super::*;
#[test]
fn pending_open_requests_survive_reads_and_only_acknowledge_head() {
let state = AppState::new(AppConfig::default());
state.enqueue_open_file("one.md".into()).unwrap();
state.enqueue_open_file("two.md".into()).unwrap();
let first = state.peek_pending_open_file().unwrap().unwrap();
assert_eq!(first.path, "one.md");
assert_eq!(
state.peek_pending_open_file().unwrap().unwrap().id,
first.id
);
assert!(!state.ack_pending_open_file(first.id + 1).unwrap());
assert_eq!(
state.peek_pending_open_file().unwrap().unwrap().id,
first.id
);
assert!(state.ack_pending_open_file(first.id).unwrap());
let second = state.peek_pending_open_file().unwrap().unwrap();
assert_eq!(second.path, "two.md");
assert!(!state.ack_pending_open_file(first.id).unwrap());
assert!(state.ack_pending_open_file(second.id).unwrap());
assert!(state.peek_pending_open_file().unwrap().is_none());
}
#[test]
fn detached_window_owner_matches_encoded_vault_path_only() {
let url = Url::parse("tauri://localhost/?note=a.md&vault=%2Fvaults%2FA%26B").unwrap();
assert!(is_detached_window_for_vault(
"note-123",
&url,
"/vaults/A&B"
));
assert!(!is_detached_window_for_vault("note-123", &url, "/vaults/A"));
assert!(!is_detached_window_for_vault("main", &url, "/vaults/A&B"));
}
#[test]
fn close_policy_respects_live_settings_window_type_and_explicit_quit() {
let mut state = AppState::new(AppConfig {
+580 -226
View File
File diff suppressed because it is too large. Load diff
+2 -3
View File
@@ -104,6 +104,8 @@ pub struct VaultConfig {
pub sync_interval_minutes: u32,
#[serde(default)]
pub last_sync_time: Option<String>,
#[serde(default)]
pub last_backup_time: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -220,8 +222,6 @@ pub struct AppConfig {
#[serde(default)]
pub backup_location: Option<String>,
#[serde(default)]
pub last_backup_time: Option<String>,
#[serde(default)]
pub backup_include_attachments: bool,
#[serde(default = "default_max_versions")]
pub max_versions_per_note: u32,
@@ -366,7 +366,6 @@ impl Default for AppConfig {
backup_frequency: "24h".to_string(),
backup_max_count: 10,
backup_location: None,
last_backup_time: None,
backup_include_attachments: false,
max_versions_per_note: 20,
ai_provider: None,
+334 -108
View File
@@ -1,5 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::fs::OpenOptions;
use std::io;
use std::path::{Path, PathBuf};
use chrono::Utc;
use gray_matter::engine::YAML;
@@ -9,23 +11,16 @@ use uuid::Uuid;
use crate::types::{ImportResult, NoteMeta};
use crate::vault::frontmatter;
use crate::vault::operations::replace_file_atomically;
pub fn import(vault_path: &str) -> Result<ImportResult, String> {
let vault = Path::new(vault_path);
let file_index = build_file_index(vault);
let file_index = build_file_index(vault)?;
let md_files: Vec<_> = walkdir::WalkDir::new(vault)
let md_files: Vec<_> = scanned_files(vault)?
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
let p = e.path();
p.is_file()
&& p.extension().and_then(|x| x.to_str()) == Some("md")
&& !p.to_string_lossy().contains("/.helixnotes/")
&& !p.to_string_lossy().contains("/.trash/")
})
.map(|e| e.path().to_path_buf())
.filter(|p| p.extension().and_then(|x| x.to_str()) == Some("md"))
.collect();
let wiki_embed_re =
@@ -46,10 +41,8 @@ pub fn import(vault_path: &str) -> Result<ImportResult, String> {
};
for path in &md_files {
let raw = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => continue,
};
let raw = std::fs::read_to_string(path)
.map_err(|e| format!("Reading {}: {e}", path.display()))?;
let note_dir = path.parent().unwrap_or(Path::new(""));
let mut content = raw.clone();
@@ -193,7 +186,8 @@ pub fn import(vault_path: &str) -> Result<ImportResult, String> {
}
if changed {
let _ = std::fs::write(path, &content);
replace_file_atomically(path, content.as_bytes())
.map_err(|e| format!("Writing {}: {e}", path.display()))?;
result.files_converted += 1;
}
}
@@ -203,30 +197,65 @@ pub fn import(vault_path: &str) -> Result<ImportResult, String> {
if !moved.is_empty() {
rewrite_attachment_refs(vault, &moved)?;
for old_rel in moved.keys() {
let src_path = vault.join(old_rel);
std::fs::remove_file(&src_path)
.map_err(|e| format!("Removing {} after copying: {e}", src_path.display()))?;
}
}
cleanup_empty_dirs(vault);
cleanup_empty_dirs(vault)?;
Ok(result)
}
fn build_file_index(vault: &Path) -> HashMap<String, String> {
let mut index = HashMap::new();
for entry in walkdir::WalkDir::new(vault)
fn is_excluded_dir(path: &Path) -> bool {
matches!(
path.file_name().and_then(|name| name.to_str()),
Some(".helixnotes" | ".obsidian" | ".trash" | ".stfolder" | ".claude")
)
}
fn vault_entries(
vault: &Path,
) -> impl Iterator<Item = Result<walkdir::DirEntry, walkdir::Error>> + '_ {
walkdir::WalkDir::new(vault)
.into_iter()
.filter_map(|e| e.ok())
{
let path = entry.path();
if !path.is_file() {
.filter_entry(|entry| {
entry.depth() == 0 || !entry.file_type().is_dir() || !is_excluded_dir(entry.path())
})
}
fn scanned_files(vault: &Path) -> Result<Vec<PathBuf>, String> {
vault_entries(vault)
.filter_map(|entry| match entry {
Ok(entry) if entry.file_type().is_file() => Some(Ok(entry.into_path())),
Ok(_) => None,
Err(e) => Some(Err(e)),
})
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Scanning {}: {e}", vault.display()))
}
fn vault_relative(path: &Path, vault: &Path) -> Result<String, String> {
path.strip_prefix(vault)
.map(|rel| rel.to_string_lossy().replace('\\', "/"))
.map_err(|e| format!("Relativizing {}: {e}", path.display()))
}
fn build_file_index(vault: &Path) -> Result<HashMap<String, String>, String> {
let mut index = HashMap::new();
for entry in vault_entries(vault) {
let entry = entry.map_err(|e| format!("Scanning {}: {e}", vault.display()))?;
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if let Ok(rel) = path.strip_prefix(vault) {
index.insert(name.to_string(), rel.to_string_lossy().to_string());
}
index.insert(name.to_string(), vault_relative(path, vault)?);
}
}
index
Ok(index)
}
fn rename_deprecated_properties(raw: &str) -> String {
@@ -565,52 +594,102 @@ fn fix_md_link_refs(
.to_string()
}
fn ensure_attachment_parent(
vault: &Path,
canonical_vault: &Path,
parent: &Path,
) -> Result<(), String> {
let relative = parent
.strip_prefix(vault)
.map_err(|e| format!("Invalid attachment destination {}: {e}", parent.display()))?;
let mut current = vault.to_path_buf();
let mut relative_path = PathBuf::new();
for component in relative.components() {
current.push(component);
relative_path.push(component);
match std::fs::create_dir(&current) {
Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(format!("Creating {}: {e}", current.display())),
}
let canonical = std::fs::canonicalize(&current)
.map_err(|e| format!("Resolving {}: {e}", current.display()))?;
if !current.is_dir() || canonical != canonical_vault.join(&relative_path) {
return Err(format!(
"Attachment destination escapes vault: {}",
current.display()
));
}
}
Ok(())
}
fn move_attachments(vault: &Path) -> Result<HashMap<String, String>, String> {
let attachments_dir = vault.join(".helixnotes").join("attachments");
let _ = std::fs::create_dir_all(&attachments_dir);
let attachment_files: Vec<_> = walkdir::WalkDir::new(vault)
let attachment_files: Vec<_> = scanned_files(vault)?
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
let p = e.path();
if !p.is_file() {
return false;
}
if p.extension().and_then(|x| x.to_str()) == Some("md") {
return false;
}
let path_str = p.to_string_lossy();
!path_str.contains("/.helixnotes/")
&& !path_str.contains("/.obsidian/")
&& !path_str.contains("/.trash/")
&& !path_str.contains("/.stfolder")
&& !path_str.contains("/.claude/")
})
.map(|e| e.path().to_path_buf())
.filter(|p| p.extension().and_then(|x| x.to_str()) != Some("md"))
.collect();
let canonical_vault =
std::fs::canonicalize(vault).map_err(|e| format!("Resolving {}: {e}", vault.display()))?;
let mut moved: HashMap<String, String> = HashMap::new();
let mut moved = HashMap::new();
for src_path in attachment_files {
let old_rel = vault_relative(&src_path, vault)?;
let preferred = attachments_dir.join(&old_rel);
let parent = preferred.parent().expect("attachment has a parent");
ensure_attachment_parent(vault, &canonical_vault, parent)?;
for src_path in &attachment_files {
let old_rel = match src_path.strip_prefix(vault) {
Ok(r) => r.to_string_lossy().to_string(),
Err(_) => continue,
// Reserve the destination atomically: neither rename nor std::fs::copy protects
// a pre-existing attachment (or one created concurrently) from replacement.
let mut suffix = 0u64;
let (dest_path, mut dest) = loop {
let candidate = if suffix == 0 {
preferred.clone()
} else {
let stem = preferred.file_stem().unwrap_or_default().to_string_lossy();
let ext = preferred
.extension()
.map(|ext| format!(".{}", ext.to_string_lossy()))
.unwrap_or_default();
preferred.with_file_name(format!("{stem}-{suffix}{ext}"))
};
match OpenOptions::new()
.write(true)
.create_new(true)
.open(&candidate)
{
Ok(file) => break (candidate, file),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
suffix = suffix
.checked_add(1)
.ok_or("Too many attachment collisions")?;
}
Err(e) => return Err(format!("Creating {}: {e}", candidate.display())),
}
};
let new_rel = format!(".helixnotes/attachments/{}", old_rel);
let dest_path = vault.join(&new_rel);
if let Some(parent) = dest_path.parent() {
let _ = std::fs::create_dir_all(parent);
}
if std::fs::rename(src_path, &dest_path).is_ok()
|| std::fs::copy(src_path, &dest_path)
.and_then(|_| std::fs::remove_file(src_path))
.is_ok()
{
moved.insert(old_rel, new_rel);
let copied = (|| -> io::Result<()> {
let mut src = std::fs::File::open(&src_path)?;
io::copy(&mut src, &mut dest)?;
dest.sync_all()?;
Ok(())
})();
drop(dest);
if let Err(e) = copied {
std::fs::remove_file(&dest_path).map_err(|cleanup| {
format!(
"Copying {} to {}: {e}; removing incomplete copy: {cleanup}",
src_path.display(),
dest_path.display()
)
})?;
return Err(format!(
"Copying {} to {}: {e}",
src_path.display(),
dest_path.display()
));
}
moved.insert(old_rel, vault_relative(&dest_path, vault)?);
}
Ok(moved)
}
@@ -618,24 +697,14 @@ fn move_attachments(vault: &Path) -> Result<HashMap<String, String>, String> {
fn rewrite_attachment_refs(vault: &Path, moved: &HashMap<String, String>) -> Result<(), String> {
let md_ref = Regex::new(r"(!?\[[^\]]*\])\(([^)]+)\)").map_err(|e| e.to_string())?;
let md_files: Vec<_> = walkdir::WalkDir::new(vault)
let md_files: Vec<_> = scanned_files(vault)?
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
let p = e.path();
p.is_file()
&& p.extension().and_then(|x| x.to_str()) == Some("md")
&& !p.to_string_lossy().contains("/.helixnotes/")
&& !p.to_string_lossy().contains("/.trash/")
})
.map(|e| e.path().to_path_buf())
.filter(|p| p.extension().and_then(|x| x.to_str()) == Some("md"))
.collect();
for path in &md_files {
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => continue,
};
let content = std::fs::read_to_string(path)
.map_err(|e| format!("Reading {}: {e}", path.display()))?;
let note_dir = path.parent().unwrap_or(Path::new(""));
let new_content = md_ref
@@ -643,28 +712,29 @@ fn rewrite_attachment_refs(vault: &Path, moved: &HashMap<String, String>) -> Res
let prefix = &caps[1];
let href = &caps[2];
let decoded_href = percent_decode(href);
let (reference, suffix) = decoded_href
.find(['#', '?'])
.map(|index| decoded_href.split_at(index))
.unwrap_or((&decoded_href, ""));
if decoded_href.starts_with("http") || decoded_href.starts_with("data:") {
if reference.starts_with("http")
|| reference.starts_with("data:")
|| reference.starts_with('/')
{
return format!("{}({})", prefix, href);
}
let abs_from_note = note_dir.join(&decoded_href);
let rel_from_note = abs_from_note
let rel_from_note = note_dir
.strip_prefix(vault)
.ok()
.map(|r| r.to_string_lossy().to_string());
let rel_from_vault = decoded_href.clone();
.and_then(|base| resolve_relative_ref(base, reference));
let old_vault_rel = rel_from_note
.as_ref()
.filter(|r| moved.contains_key(r.as_str()))
.cloned()
.or_else(|| {
if moved.contains_key(&rel_from_vault) {
Some(rel_from_vault)
} else {
None
}
resolve_relative_ref(Path::new(""), reference)
.filter(|r| moved.contains_key(r.as_str()))
});
if let Some(old_rel) = old_vault_rel {
@@ -675,7 +745,12 @@ fn rewrite_attachment_refs(vault: &Path, moved: &HashMap<String, String>) -> Res
note_dir.to_str().unwrap_or(""),
)
.unwrap_or(new_rel.clone());
return format!("{}({})", prefix, new_note_rel.replace(' ', "%20"));
return format!(
"{}({}{})",
prefix,
new_note_rel.replace('\\', "/").replace(' ', "%20"),
suffix
);
}
}
format!("{}({})", prefix, href)
@@ -683,33 +758,59 @@ fn rewrite_attachment_refs(vault: &Path, moved: &HashMap<String, String>) -> Res
.to_string();
if new_content != content {
let _ = std::fs::write(path, new_content);
replace_file_atomically(path, new_content.as_bytes())
.map_err(|e| format!("Writing {}: {e}", path.display()))?;
}
}
Ok(())
}
fn cleanup_empty_dirs(root: &Path) {
let mut dirs: Vec<_> = walkdir::WalkDir::new(root)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().is_dir())
.map(|e| e.path().to_path_buf())
.collect();
fn resolve_relative_ref(base: &Path, reference: &str) -> Option<String> {
let mut rel = base.to_path_buf();
for component in Path::new(&reference.replace('\\', "/")).components() {
match component {
std::path::Component::Normal(name) => rel.push(name),
std::path::Component::ParentDir => {
if !rel.pop() {
return None;
}
}
std::path::Component::CurDir => {}
_ => return None,
}
}
Some(rel.to_string_lossy().replace('\\', "/"))
}
fn cleanup_empty_dirs(root: &Path) -> Result<(), String> {
let mut dirs: Vec<_> = vault_entries(root)
.filter_map(|entry| match entry {
Ok(entry) if entry.file_type().is_dir() => Some(Ok(entry.into_path())),
Ok(_) => None,
Err(e) => Some(Err(e)),
})
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Scanning {}: {e}", root.display()))?;
dirs.sort_by_key(|path| std::cmp::Reverse(path.components().count()));
for dir in dirs {
let dir_str = dir.to_string_lossy();
if dir_str.contains("/.helixnotes") || dir_str.contains("/.obsidian") || dir == root {
if dir == root {
continue;
}
if let Ok(mut entries) = std::fs::read_dir(&dir) {
if entries.next().is_none() {
let _ = std::fs::remove_dir(&dir);
}
let mut entries =
std::fs::read_dir(&dir).map_err(|e| format!("Reading {}: {e}", dir.display()))?;
if entries
.next()
.transpose()
.map_err(|e| format!("Reading {}: {e}", dir.display()))?
.is_none()
{
std::fs::remove_dir(&dir)
.map_err(|e| format!("Removing empty directory {}: {e}", dir.display()))?;
}
}
Ok(())
}
fn split_anchor(reference: &str) -> (&str, Option<&str>) {
@@ -778,7 +879,7 @@ fn pathdiff(target: &str, base: &str) -> Result<String, ()> {
for component in &target_components[common..] {
result.push(component);
}
Ok(result.to_string_lossy().to_string())
Ok(result.to_string_lossy().replace('\\', "/"))
}
fn percent_decode(s: &str) -> String {
@@ -1013,6 +1114,131 @@ mod tests {
assert_eq!(result, raw);
}
#[test]
fn import_keeps_colliding_attachments_and_rewrites_note_relative_links() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
let note_dir = root.join("Notes");
let stored = root.join(".helixnotes/attachments/Notes");
std::fs::create_dir_all(&note_dir).unwrap();
std::fs::create_dir_all(&stored).unwrap();
std::fs::write(note_dir.join("pic.png"), b"NEW").unwrap();
std::fs::write(stored.join("pic.png"), b"OLD").unwrap();
std::fs::write(stored.join("pic-1.png"), b"OLDER").unwrap();
std::fs::write(
note_dir.join("Entry.md"),
"# Entry\n\n![picture](pic.png) [picture](./pic.png#preview)",
)
.unwrap();
let result = import(root.to_str().unwrap()).unwrap();
assert_eq!(result.attachments_moved, 1);
assert_eq!(std::fs::read(stored.join("pic.png")).unwrap(), b"OLD");
assert_eq!(std::fs::read(stored.join("pic-1.png")).unwrap(), b"OLDER");
assert_eq!(std::fs::read(stored.join("pic-2.png")).unwrap(), b"NEW");
assert!(!note_dir.join("pic.png").exists());
let note = std::fs::read_to_string(note_dir.join("Entry.md")).unwrap();
assert!(note.contains("![picture](../.helixnotes/attachments/Notes/pic-2.png)"));
assert!(note.contains("[picture](../.helixnotes/attachments/Notes/pic-2.png#preview)"));
}
#[test]
fn import_prunes_metadata_directories_at_every_depth() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
for hidden in [".helixnotes", ".obsidian", ".trash", ".stfolder", ".claude"] {
for base in [root.to_path_buf(), root.join("Notebook")] {
let hidden_dir = base.join(hidden).join("history");
std::fs::create_dir_all(&hidden_dir).unwrap();
std::fs::write(hidden_dir.join("Secret.md"), "==untouched==").unwrap();
std::fs::write(hidden_dir.join("asset.bin"), b"untouched").unwrap();
}
}
let visible_dir = root.join("Notebook/stfolder-visible");
std::fs::create_dir_all(&visible_dir).unwrap();
std::fs::write(visible_dir.join("asset.bin"), b"visible").unwrap();
std::fs::write(
root.join("Note.md"),
"# Note\n\n![file](Notebook/stfolder-visible/asset.bin)",
)
.unwrap();
let result = import(root.to_str().unwrap()).unwrap();
assert_eq!(result.attachments_moved, 1);
assert_eq!(
std::fs::read(root.join(".helixnotes/attachments/Notebook/stfolder-visible/asset.bin"))
.unwrap(),
b"visible"
);
for hidden in [".helixnotes", ".obsidian", ".trash", ".stfolder", ".claude"] {
for base in [root.to_path_buf(), root.join("Notebook")] {
let hidden_dir = base.join(hidden).join("history");
assert_eq!(
std::fs::read_to_string(hidden_dir.join("Secret.md")).unwrap(),
"==untouched=="
);
assert_eq!(
std::fs::read(hidden_dir.join("asset.bin")).unwrap(),
b"untouched"
);
}
}
}
#[test]
fn destination_io_failure_leaves_original_attachment_in_place() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
std::fs::create_dir(root.join(".helixnotes")).unwrap();
std::fs::write(root.join(".helixnotes/attachments"), b"existing metadata").unwrap();
std::fs::write(root.join("asset.bin"), b"original").unwrap();
assert!(import(root.to_str().unwrap()).is_err());
assert_eq!(std::fs::read(root.join("asset.bin")).unwrap(), b"original");
assert_eq!(
std::fs::read(root.join(".helixnotes/attachments")).unwrap(),
b"existing metadata"
);
}
#[test]
fn readonly_note_does_not_lose_original_or_attachment() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
let note = root.join("Note.md");
let original = "# Note\n\n![asset](asset.bin)";
std::fs::write(&note, original).unwrap();
std::fs::write(root.join("asset.bin"), b"original").unwrap();
let original_permissions = std::fs::metadata(&note).unwrap().permissions();
let mut permissions = original_permissions.clone();
permissions.set_readonly(true);
std::fs::set_permissions(&note, permissions).unwrap();
let result = import(root.to_str().unwrap());
assert!(result.is_err());
assert_eq!(std::fs::read_to_string(&note).unwrap(), original);
assert_eq!(std::fs::read(root.join("asset.bin")).unwrap(), b"original");
std::fs::set_permissions(&note, original_permissions).unwrap();
}
#[cfg(unix)]
#[test]
fn attachment_directory_symlink_cannot_escape_vault() {
let vault = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let root = vault.path();
std::fs::create_dir(root.join(".helixnotes")).unwrap();
std::os::unix::fs::symlink(outside.path(), root.join(".helixnotes/attachments")).unwrap();
std::fs::write(root.join("asset.bin"), b"original").unwrap();
assert!(import(root.to_str().unwrap()).is_err());
assert_eq!(std::fs::read(root.join("asset.bin")).unwrap(), b"original");
assert!(!outside.path().join("asset.bin").exists());
}
#[test]
fn test_split_anchor_same_note() {
let (file, anchor) = split_anchor("#Heading");
File diff suppressed because it is too large. Load diff
+19 -15
View File
@@ -133,6 +133,7 @@ pub fn start_watcher(app: AppHandle, vault_path: String) -> Result<VaultWatcher,
std::thread::spawn(move || {
let vault_root = Path::new(&vault_path);
let mut changes = HashMap::new();
let mut search = None;
loop {
let first = if changes.is_empty() {
match rx.recv() {
@@ -162,27 +163,30 @@ pub fn start_watcher(app: AppHandle, vault_path: String) -> Result<VaultWatcher,
next = rx.recv_timeout(remaining).ok();
}
let state = app.state::<AppState>();
// Native note mutations hold note_save through their path event.
// Read and emit only after they finish, so the watcher cannot
// publish an intermediate rename or stale note contents.
let _save_guard = state.note_save.blocking_lock();
// Keep the batch while muted, rather than losing concurrent external
// edits. Nothing is indexed or emitted until local imports finish.
if state.importing.load(std::sync::atomic::Ordering::Relaxed) {
continue;
}
let search = {
// Match the runtime commit's lock order so an old watcher cannot
// send its final batch into the newly selected vault's index.
let Ok(search) = state.search_index.lock() else {
continue;
};
let active = state
.config
.lock()
.ok()
.is_some_and(|config| config.active_vault.as_deref() == Some(&vault_path));
if !active {
continue;
// Each watcher belongs to its own vault. A detached note window may
// still need changes from a vault that is no longer active; never
// index those changes into the main window's active search index.
if state.validate_vault(&vault_path).is_err() {
continue;
}
if search.is_none() {
match state.vault_snapshot(&vault_path) {
Ok(index) => search = index,
Err(error) => {
log::error!("Could not open search index for watched vault: {error}");
continue;
}
}
search.clone()
};
}
let mut refresh_vault = false;
for (path, event_type) in changes.drain() {
if path.extension().and_then(|extension| extension.to_str()) == Some("md")