Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Avoid race condition (TOCTOU) is multiple files have the same pretty name
Ilya2f5a267fa451
diff --git a/apps/desktop/src-tauri/src/update_project_names.rs b/apps/desktop/src-tauri/src/update_project_names.rsindex 88cd0994f..46a3c3552 100644--- a/apps/desktop/src-tauri/src/update_project_names.rs+++ b/apps/desktop/src-tauri/src/update_project_names.rs@@ -1,7 +1,11 @@-use std::path::{Path, PathBuf};+use std::{+ collections::HashSet,+ path::{Path, PathBuf},+};use cap_project::RecordingMeta;use futures::StreamExt;+use lazy_static::lazy_static;use tauri::AppHandle;use tokio::fs;@@ -9,6 +13,11 @@ use crate::recordings_path;const STORE_KEY: &str = "uuid_projects_migrated";+lazy_static! {+ static ref IN_FLIGHT_BASES: tokio::sync::Mutex<HashSet<String>> =+ tokio::sync::Mutex::new(HashSet::new());+}+pub fn migrate_if_needed(app: &AppHandle) -> Result<(), String> {use tauri_plugin_store::StoreExt;@@ -186,7 +195,39 @@ async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult,}};- match migrate_project_filename_async(&path, &meta).await {+ // Lock on the base sanitized name to prevent concurrent migrations with same target+ let base_name = sanitize_filename::sanitize(meta.pretty_name.replace(":", "."));+ {+ let mut in_flight = IN_FLIGHT_BASES.lock().await;+ let mut wait_count = 0;+ while !in_flight.insert(base_name.clone()) {+ wait_count += 1;+ if wait_count == 1 {+ tracing::debug!(+ "Project {} waiting for concurrent migration of base name \"{}\"",+ filename,+ base_name+ );+ }+ drop(in_flight);+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;+ in_flight = IN_FLIGHT_BASES.lock().await;+ }+ if wait_count > 0 {+ tracing::debug!(+ "Project {} acquired lock for \"{}\" after {} waits",+ filename,+ base_name,+ wait_count+ );+ }+ }++ let result = migrate_project_filename_async(&path, &meta).await;++ IN_FLIGHT_BASES.lock().await.remove(&base_name);++ match result {Ok(new_path) => {if new_path != path {let new_name = new_path.file_name().unwrap().to_string_lossy();diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rsindex 9c7d828f1..f65765cf5 100644--- a/crates/utils/src/lib.rs+++ b/crates/utils/src/lib.rs@@ -35,16 +35,25 @@ pub fn ensure_dir(path: &PathBuf) -> Result<PathBuf, std::io::Error> {/// # Example////// ```rust-/// let unique_name = ensure_unique_filename("My Recording.cap", &recordings_dir);+/// let unique_name = ensure_unique_filename("My Recording.cap", &recordings_dir,);/// // If "My Recording.cap" exists, returns "My Recording (1).cap"/// // If that exists too, returns "My Recording (2).cap", etc.////// let unique_name = ensure_unique_filename("document.pdf", &documents_dir);/// // If "document.pdf" exists, returns "document (1).pdf"/// ```+#[inline]pub fn ensure_unique_filename(base_filename: &str,parent_dir: &std::path::Path,+) -> Result<String, String> {+ ensure_unique_filename_with_attempts(base_filename, parent_dir, 20)+}++pub fn ensure_unique_filename_with_attempts(+ base_filename: &str,+ parent_dir: &std::path::Path,+ attempts: i32,) -> Result<String, String> {let initial_path = parent_dir.join(base_filename);@@ -82,7 +91,7 @@ pub fn ensure_unique_filename(counter += 1;// prevent infinite loop- if counter > 1000 {+ if counter > attempts {return Err("Too many filename conflicts, unable to create unique filename".to_string(),);