diff --git a/core/crates/core/src/error.rs b/core/crates/core/src/error.rs index 6312d587fc..0d0f3a275e 100644 --- a/core/crates/core/src/error.rs +++ b/core/crates/core/src/error.rs @@ -117,6 +117,10 @@ pub enum DriverError { #[error("no permission to open {}", .path.display())] FileAccessDenied { path: PathBuf }, + // A new database is made only where nothing is, so a file another program made there is kept. + #[error("a file is already at {}", .path.display())] + FileExists { path: PathBuf }, + #[error("{} is not a database file", .path.display())] NotADatabase { path: PathBuf }, @@ -172,6 +176,7 @@ impl DriverError { Self::FileNotFound { .. } => "driver.file_not_found", Self::DatabaseNotFound { .. } => "driver.database_not_found", Self::FileAccessDenied { .. } => "driver.file_access_denied", + Self::FileExists { .. } => "driver.file_exists", Self::NotADatabase { .. } => "driver.not_a_database", Self::FileIo { .. } => "driver.file_io", Self::Busy(..) => "driver.busy", @@ -217,6 +222,7 @@ impl DriverError { | Self::PartiallyApplied { .. } => ErrorCategory::WriteOutcome, Self::FileNotFound { .. } | Self::FileAccessDenied { .. } + | Self::FileExists { .. } | Self::NotADatabase { .. } | Self::FileIo { .. } => ErrorCategory::File, Self::Busy(_) | Self::EditorSessionBusy => ErrorCategory::Busy, @@ -274,6 +280,17 @@ mod tests { ); } + #[test] + fn a_file_already_there_is_a_file_problem_with_its_own_code() { + let there = DriverError::FileExists { + path: PathBuf::from("/data/app.sqlite3"), + }; + + assert_eq!(there.category(), ErrorCategory::File); + assert_eq!(there.code(), "driver.file_exists"); + assert!(there.to_string().contains("/data/app.sqlite3"), "{there}"); + } + #[test] fn a_saves_cause_is_found_under_its_outcome() { let lost = DriverError::ConnectionLost { diff --git a/core/crates/core/src/file_endpoint.rs b/core/crates/core/src/file_endpoint.rs index b253333d75..0b378968d6 100644 --- a/core/crates/core/src/file_endpoint.rs +++ b/core/crates/core/src/file_endpoint.rs @@ -17,6 +17,10 @@ impl FileEndpoint { Self::with_mode(path, FileOpenMode::CreateNew) } + pub fn replace(path: PathBuf) -> Result { + Self::with_mode(path, FileOpenMode::Replace) + } + fn with_mode(path: PathBuf, mode: FileOpenMode) -> Result { if !path.is_absolute() { return Err(ConfigError::RelativePath); @@ -70,13 +74,24 @@ mod tests { let created = FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap(); assert_eq!(created.mode(), FileOpenMode::CreateNew); + + let replaced = FileEndpoint::replace(absolute("/srv/old.sqlite")).unwrap(); + assert_eq!(replaced.mode(), FileOpenMode::Replace); + assert_eq!( + FileEndpoint::replace(PathBuf::from("old.sqlite")), + Err(ConfigError::RelativePath) + ); } #[test] fn for_reopen_is_open_existing_for_the_same_path() { - let created = FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap(); - let reopened = created.for_reopen(); - assert_eq!(reopened.path(), created.path()); - assert_eq!(reopened.mode(), FileOpenMode::OpenExisting); + for created in [ + FileEndpoint::create_new(absolute("/srv/new.sqlite")).unwrap(), + FileEndpoint::replace(absolute("/srv/new.sqlite")).unwrap(), + ] { + let reopened = created.for_reopen(); + assert_eq!(reopened.path(), created.path()); + assert_eq!(reopened.mode(), FileOpenMode::OpenExisting); + } } } diff --git a/core/crates/core/src/file_open_mode.rs b/core/crates/core/src/file_open_mode.rs index eea24e127b..3db936032c 100644 --- a/core/crates/core/src/file_open_mode.rs +++ b/core/crates/core/src/file_open_mode.rs @@ -1,5 +1,8 @@ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum FileOpenMode { OpenExisting, + // Only where nothing is; a file there is refused, never replaced. CreateNew, + // Over a file the user agreed to replace. + Replace, } diff --git a/core/crates/drivers/duckdb/src/create_file.rs b/core/crates/drivers/duckdb/src/create_file.rs index c659b20776..19cb9fe7dc 100644 --- a/core/crates/drivers/duckdb/src/create_file.rs +++ b/core/crates/drivers/duckdb/src/create_file.rs @@ -9,21 +9,79 @@ use tokio_util::sync::CancellationToken; use crate::errors::map_error; use crate::instance::{config, file_error}; -/// The write-ahead log DuckDB keeps beside a database. One left from the -/// database being replaced would be replayed into the new one. +// A log left beside an earlier database at the path would be replayed into the new one. const WAL_SUFFIX: &str = ".wal"; -/// Put an empty database at `path`, replacing whatever is there. -/// -/// The save dialog has already asked before replacing a file. An empty -/// file is no DuckDB database, so the new one is written beside the -/// target first, then renamed over it, which is atomic: a reader sees the -/// old file or the new one, never half of one. -/// -/// `given_up` is the connect's stop, read again once the new database is -/// built: building it takes a while, and a connect given up meanwhile -/// leaves the old file where it is. -pub(crate) fn create_or_replace(path: &Path, given_up: &CancellationToken) -> Result<(), DriverError> { +// Built beside the path, as an empty file is no DuckDB database; a connect given up meanwhile leaves the path alone. +pub(crate) fn create_new(path: &Path, given_up: &CancellationToken) -> Result<(), DriverError> { + let (parent, name) = target(path)?; + let temporary = beside(parent, name, "tmp"); + let made = build_empty(&temporary, path) + .and_then(|()| match given_up.is_cancelled() { + true => Err(DriverError::Cancelled), + false => Ok(()), + }) + .and_then(|()| publish(&temporary, path)); + // Nothing half-built is left beside the user's files. + let _ = std::fs::remove_file(&temporary); + let _ = std::fs::remove_file(wal_of(&temporary)); + made?; + tablepro_core::sync_directory(parent).map_err(|error| file_error(error.kind(), path)) +} + +// A link fails on anything at the path, so another program's file there is never touched, and appears whole. +fn publish(temporary: &Path, path: &Path) -> Result<(), DriverError> { + match std::fs::hard_link(temporary, path) { + Ok(()) => {} + Err(error) if error.kind() == ErrorKind::AlreadyExists => return Err(already_there(path)), + // A file system without links, such as FAT or the document portal's. + Err(_) => copy_into_new(temporary, path)?, + } + match std::fs::remove_file(wal_of(path)) { + Err(error) if error.kind() != ErrorKind::NotFound => Err(file_error(error.kind(), path)), + _ => Ok(()), + } +} + +// Written only through its exclusive handle, and kept on a failure, when the path may name another file. +fn copy_into_new(temporary: &Path, path: &Path) -> Result<(), DriverError> { + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); + let mut file = options.open(path).map_err(|error| match error.kind() { + ErrorKind::AlreadyExists => already_there(path), + kind => file_error(kind, path), + })?; + File::open(temporary) + .and_then(|mut built| std::io::copy(&mut built, &mut file)) + .and_then(|_| file.sync_all()) + .map_err(|error| file_error(error.kind(), path)) +} + +fn already_there(path: &Path) -> DriverError { + DriverError::FileExists { + path: path.to_path_buf(), + } +} + +// The save dialog asked first. A rename puts the database in place, so a reader sees the old file or the new one. +pub(crate) fn replace(path: &Path, given_up: &CancellationToken) -> Result<(), DriverError> { + let (parent, name) = target(path)?; + let temporary = beside(parent, name, "tmp"); + let placed = build_empty(&temporary, path).and_then(|()| match given_up.is_cancelled() { + true => Err(DriverError::Cancelled), + false => place(&temporary, path, parent, name), + }); + if placed.is_err() { + let _ = std::fs::remove_file(&temporary); + let _ = std::fs::remove_file(wal_of(&temporary)); + return placed; + } + tablepro_core::sync_directory(parent).map_err(|error| file_error(error.kind(), path)) +} + +fn target(path: &Path) -> Result<(&Path, &OsStr), DriverError> { let parent = match path.parent() { Some(parent) if !parent.as_os_str().is_empty() => parent, _ => { @@ -42,21 +100,9 @@ pub(crate) fn create_or_replace(path: &Path, given_up: &CancellationToken) -> Re path: path.to_path_buf(), }); } - let temporary = beside(parent, name, "tmp"); - let placed = build_empty(&temporary, path).and_then(|()| match given_up.is_cancelled() { - true => Err(DriverError::Cancelled), - false => place(&temporary, path, parent, name), - }); - if placed.is_err() { - // Nothing half-built is left beside the user's files. - let _ = std::fs::remove_file(&temporary); - let _ = std::fs::remove_file(wal_of(&temporary)); - return placed; - } - tablepro_core::sync_directory(parent).map_err(|error| file_error(error.kind(), path)) + Ok((parent, name)) } -/// A hidden name beside the user's file, unique to this call. fn beside(parent: &Path, name: &OsStr, suffix: &str) -> PathBuf { parent.join(format!( ".{}.{}.{suffix}", @@ -65,8 +111,7 @@ fn beside(parent: &Path, name: &OsStr, suffix: &str) -> PathBuf { )) } -/// An empty database, checkpointed into the file so no log goes with -/// it, readable by its owner only. +// Checkpointed, so no log goes with the file. fn build_empty(temporary: &Path, target: &Path) -> Result<(), DriverError> { let connection = duckdb::Connection::open_with_flags(temporary, config(false)?).map_err(map_error)?; connection.execute_batch("CHECKPOINT").map_err(map_error)?; @@ -86,9 +131,7 @@ fn build_empty(temporary: &Path, target: &Path) -> Result<(), DriverError> { .map_err(|error| file_error(error.kind(), target)) } -/// Rename the new database over `path`. The old file's log is moved -/// aside first and put back if the rename fails, so a database that -/// stays keeps the changes only its log holds. +// The old file's log is put back if the rename fails, so a database that stays keeps the changes only its log holds. fn place(temporary: &Path, path: &Path, parent: &Path, name: &OsStr) -> Result<(), DriverError> { let _held = lock_replaced(path)?; let wal = wal_of(path); @@ -110,10 +153,7 @@ fn place(temporary: &Path, path: &Path, parent: &Path, name: &OsStr) -> Result<( Ok(()) } -/// Lock the file being replaced as DuckDB locks a database it opens, and -/// hold the lock through the rename. Another process with the database -/// open holds that lock, and would go on writing a log beside the new -/// file under the same name. +// Held through the rename: a process with the database open holds it, and would go on logging beside the new file. fn lock_replaced(path: &Path) -> Result, DriverError> { match std::fs::metadata(path) { Ok(metadata) if metadata.is_file() => {} @@ -122,16 +162,13 @@ fn lock_replaced(path: &Path) -> Result, DriverError> { Err(error) if error.kind() == ErrorKind::NotFound => return Ok(None), Err(error) => return Err(file_error(error.kind(), path)), } - // A POSIX lock belongs to the process, and closing any descriptor on - // the file drops all of them, so a file this process holds open is - // not opened again here. + // Closing any descriptor drops the process's POSIX locks on the file, so one this process holds is not opened. if tablepro_core::held_by_this_process(path) { return Err(DriverError::Busy(BusyReason::FileLocked)); } let (file, exclusive) = match OpenOptions::new().read(true).write(true).open(path) { Ok(file) => (file, true), - // A write lock needs the file open for writing. A shared lock - // still meets the writer's. + // A write lock needs the file open for writing; a shared lock still meets the writer's. Err(error) if error.kind() == ErrorKind::PermissionDenied || error.kind() == ErrorKind::ReadOnlyFilesystem => { (File::open(path).map_err(|error| file_error(error.kind(), path))?, false) } @@ -146,12 +183,11 @@ fn lock_replaced(path: &Path) -> Result, DriverError> { enum LockOutcome { Held, Busy, - /// A file system without locks: DuckDB could not have locked it - /// either. + // A file system without locks: DuckDB could not have locked it either. Unsupported, } -/// A POSIX record lock, the kind DuckDB takes on Unix. +// A POSIX record lock, the kind DuckDB takes on Unix. #[cfg(unix)] fn lock_as_duckdb_does(file: &File, exclusive: bool) -> LockOutcome { use rustix::fs::FlockOperation; @@ -168,8 +204,7 @@ fn lock_as_duckdb_does(file: &File, exclusive: bool) -> LockOutcome { } } -/// LockFileEx, the kind DuckDB takes on Windows, which std's file locks -/// are. +// LockFileEx, the kind DuckDB takes on Windows, which std's file locks are. #[cfg(windows)] fn lock_as_duckdb_does(file: &File, exclusive: bool) -> LockOutcome { let locked = match exclusive { @@ -200,21 +235,110 @@ mod tests { .unwrap() } + fn names_in(directory: &Path) -> Vec { + let mut names: Vec<_> = std::fs::read_dir(directory) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + names.sort(); + names + } + #[test] fn a_new_database_is_made_where_no_file_was() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("new.duckdb"); - create_or_replace(&path, &CancellationToken::new()).unwrap(); + create_new(&path, &CancellationToken::new()).unwrap(); - let made: Vec<_> = std::fs::read_dir(directory.path()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - assert_eq!(made, [std::ffi::OsString::from("new.duckdb")]); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + let metadata = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + } + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("new.duckdb")]); + assert_eq!(tables_in(&path), 0); + } + + // The app found the path free, then another program made a file there before the create. + #[test] + fn a_file_made_after_the_path_was_checked_is_never_replaced() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("data.duckdb"); + std::fs::write(&path, b"another program's data").unwrap(); + std::fs::write(wal_of(&path), b"its log").unwrap(); + + assert_eq!( + create_new(&path, &CancellationToken::new()), + Err(DriverError::FileExists { path: path.clone() }) + ); + + assert_eq!(std::fs::read(&path).unwrap(), b"another program's data"); + assert_eq!(std::fs::read(wal_of(&path)).unwrap(), b"its log"); + assert_eq!( + names_in(directory.path()), + [ + std::ffi::OsString::from("data.duckdb"), + std::ffi::OsString::from("data.duckdb.wal") + ] + ); + } + + #[test] + fn a_new_database_drops_the_log_a_deleted_one_left() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("data.duckdb"); + std::fs::write(wal_of(&path), b"left from a database that is gone").unwrap(); + + create_new(&path, &CancellationToken::new()).unwrap(); + + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("data.duckdb")]); + assert_eq!(tables_in(&path), 0); + } + + #[test] + fn without_links_the_database_is_copied_only_where_nothing_is() { + let directory = tempfile::tempdir().unwrap(); + let built = directory.path().join(".built.tmp"); + build_empty(&built, &built).unwrap(); + let path = directory.path().join("copied.duckdb"); + let taken = directory.path().join("taken.duckdb"); + std::fs::write(&taken, b"another program's data").unwrap(); + + copy_into_new(&built, &path).unwrap(); + + assert_eq!( + copy_into_new(&built, &taken), + Err(DriverError::FileExists { path: taken.clone() }) + ); + assert_eq!(std::fs::read(&taken).unwrap(), b"another program's data"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + let metadata = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + } assert_eq!(tables_in(&path), 0); } + #[cfg(unix)] + #[test] + fn a_link_at_the_path_is_not_followed() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("link.duckdb"); + let elsewhere = directory.path().join("elsewhere.duckdb"); + std::os::unix::fs::symlink(&elsewhere, &path).unwrap(); + + assert_eq!( + create_new(&path, &CancellationToken::new()), + Err(DriverError::FileExists { path }) + ); + assert!(!elsewhere.exists(), "the create followed the link"); + } + #[test] fn an_existing_file_and_its_log_are_replaced_by_an_empty_private_database() { let directory = tempfile::tempdir().unwrap(); @@ -222,7 +346,7 @@ mod tests { std::fs::write(&path, b"old contents").unwrap(); std::fs::write(wal_of(&path), b"old log").unwrap(); - create_or_replace(&path, &CancellationToken::new()).unwrap(); + replace(&path, &CancellationToken::new()).unwrap(); #[cfg(unix)] { @@ -235,11 +359,7 @@ mod tests { !wal_of(&path).exists(), "the old log would be replayed into the new database" ); - let leftovers: Vec<_> = std::fs::read_dir(directory.path()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - assert_eq!(leftovers, [std::ffi::OsString::from("data.duckdb")]); + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("data.duckdb")]); assert_eq!(tables_in(&path), 0); } @@ -252,16 +372,12 @@ mod tests { std::fs::write(path.join("inside"), b"keeps the directory from being replaced").unwrap(); std::fs::write(wal_of(&path), b"committed changes").unwrap(); - assert!(create_or_replace(&path, &CancellationToken::new()).is_err()); + assert!(replace(&path, &CancellationToken::new()).is_err()); + assert!(create_new(&path, &CancellationToken::new()).is_err()); assert_eq!(std::fs::read(wal_of(&path)).unwrap(), b"committed changes"); - let mut leftovers: Vec<_> = std::fs::read_dir(directory.path()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - leftovers.sort(); assert_eq!( - leftovers, + names_in(directory.path()), [ std::ffi::OsString::from("data.duckdb"), std::ffi::OsString::from("data.duckdb.wal") @@ -279,31 +395,28 @@ mod tests { let open = File::open(&path).unwrap(); assert_eq!( - create_or_replace(&path, &CancellationToken::new()), + replace(&path, &CancellationToken::new()), Err(DriverError::Busy(BusyReason::FileLocked)) ); assert_eq!(std::fs::read(&path).unwrap(), b"open elsewhere in this process"); drop(open); } - /// A connect given up while the new database was built leaves the - /// old file, and nothing half-built beside it. + // A connect given up while the database was built leaves the path as it was, with nothing half-built beside it. #[test] - fn a_create_given_up_while_building_keeps_the_old_file() { + fn a_create_given_up_while_building_leaves_the_path_as_it_was() { let directory = tempfile::tempdir().unwrap(); - let path = directory.path().join("data.duckdb"); - std::fs::write(&path, b"what the user had").unwrap(); + let kept = directory.path().join("data.duckdb"); + std::fs::write(&kept, b"what the user had").unwrap(); + let free = directory.path().join("new.duckdb"); let given_up = CancellationToken::new(); given_up.cancel(); - assert_eq!(create_or_replace(&path, &given_up), Err(DriverError::Cancelled)); + assert_eq!(replace(&kept, &given_up), Err(DriverError::Cancelled)); + assert_eq!(create_new(&free, &given_up), Err(DriverError::Cancelled)); - assert_eq!(std::fs::read(&path).unwrap(), b"what the user had"); - let left: Vec<_> = std::fs::read_dir(directory.path()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - assert_eq!(left, [std::ffi::OsString::from("data.duckdb")]); + assert_eq!(std::fs::read(&kept).unwrap(), b"what the user had"); + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("data.duckdb")]); } #[test] @@ -312,7 +425,11 @@ mod tests { let path = directory.path().join("missing").join("data.duckdb"); assert_eq!( - create_or_replace(&path, &CancellationToken::new()), + create_new(&path, &CancellationToken::new()), + Err(DriverError::FileNotFound { path: path.clone() }) + ); + assert_eq!( + replace(&path, &CancellationToken::new()), Err(DriverError::FileNotFound { path }) ); } diff --git a/core/crates/drivers/duckdb/src/instance.rs b/core/crates/drivers/duckdb/src/instance.rs index 89a3a8f4de..56a9d845af 100644 --- a/core/crates/drivers/duckdb/src/instance.rs +++ b/core/crates/drivers/duckdb/src/instance.rs @@ -21,7 +21,7 @@ use duckdb::{AccessMode, Config, InterruptHandle}; use tablepro_core::{BusyReason, DriverError, FileClaim, FileEndpoint, FileIdentity, FileOpenMode, LossPhase}; use tokio_util::sync::CancellationToken; -use crate::create_file::create_or_replace; +use crate::create_file::{create_new, replace}; use crate::driver::ENGINE; use crate::errors::{map_error, map_open_error}; @@ -155,12 +155,7 @@ impl Link { } impl Instance { - /// Open the file the endpoint names, or join the instance this - /// process already has on it. Blocks: DuckDB reads the file and - /// replays its log here, and a file still closing is waited for. - /// `given_up` is the connect's own stop: the blocking open outlives a - /// caller that went while it waited, and must not then replace the - /// file. + // `given_up` is the connect's own stop: this blocking open outlives a caller that went while it waited. pub(crate) fn open( endpoint: &FileEndpoint, read_only: bool, @@ -183,15 +178,22 @@ impl Instance { } // Replacing a file this process has open would pull it // out from under the windows using it. - (Some(true), FileOpenMode::CreateNew) => return Err(DriverError::Busy(BusyReason::FileLocked)), - (None, FileOpenMode::CreateNew) => { + (Some(true), FileOpenMode::Replace) => return Err(DriverError::Busy(BusyReason::FileLocked)), + (None, FileOpenMode::Replace) => { if FileIdentity::of(path).is_some_and(|id| attached(&open, id, None)) { return Err(DriverError::Busy(BusyReason::FileLocked)); } if given_up.is_cancelled() { return Err(DriverError::Cancelled); } - create_or_replace(path, given_up)?; + replace(path, given_up)?; + } + // A file there, open in this process or not, is refused by the create itself. + (_, FileOpenMode::CreateNew) => { + if given_up.is_cancelled() { + return Err(DriverError::Cancelled); + } + create_new(path, given_up)?; } (_, FileOpenMode::OpenExisting) => {} } @@ -500,7 +502,7 @@ mod tests { assert!(weak.upgrade().is_none()); assert!( Instance::open( - &created(directory.path(), "kept.duckdb"), + &FileEndpoint::replace(directory.path().join("kept.duckdb")).unwrap(), false, &CancellationToken::new() ) @@ -509,8 +511,7 @@ mod tests { ); } - /// A connect given up while its open waited does not go on to - /// replace the file. + // A connect given up while its open waited does not go on to replace the file. #[test] fn a_given_up_create_leaves_the_file_alone() { let directory = tempfile::tempdir().unwrap(); @@ -519,7 +520,7 @@ mod tests { let given_up = CancellationToken::new(); given_up.cancel(); - let opened = Instance::open(&FileEndpoint::create_new(path.clone()).unwrap(), false, &given_up); + let opened = Instance::open(&FileEndpoint::replace(path.clone()).unwrap(), false, &given_up); assert_eq!(opened.unwrap_err(), DriverError::Cancelled); assert_eq!(std::fs::read(&path).unwrap(), b"what the user had"); @@ -551,6 +552,17 @@ mod tests { assert_eq!( Instance::open(&endpoint, false, &CancellationToken::new()).unwrap_err(), + DriverError::FileExists { + path: endpoint.path().to_path_buf() + } + ); + assert_eq!( + Instance::open( + &FileEndpoint::replace(endpoint.path().to_path_buf()).unwrap(), + false, + &CancellationToken::new() + ) + .unwrap_err(), DriverError::Busy(BusyReason::FileLocked) ); } diff --git a/core/crates/drivers/duckdb/tests/integration/open_semantics.rs b/core/crates/drivers/duckdb/tests/integration/open_semantics.rs index 8fe9948cb4..ed919b0402 100644 --- a/core/crates/drivers/duckdb/tests/integration/open_semantics.rs +++ b/core/crates/drivers/duckdb/tests/integration/open_semantics.rs @@ -1,5 +1,6 @@ -//! What opening a path does: open what is there, create only when asked, -//! replace atomically, and keep one database per file in the process. +//! What opening a path does: open what is there, create only where nothing +//! is, replace atomically when asked, and keep one database per file in the +//! process. use std::io::{BufRead, BufReader, Write}; use std::os::unix::fs::PermissionsExt; @@ -28,7 +29,31 @@ async fn a_missing_file_is_not_found_and_not_created() { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn a_new_database_replaces_the_file_and_its_log() { +async fn a_new_database_keeps_a_database_already_there() { + let file = TestFile::new(); + let old = file.open().await; + apply(old.as_ref(), &["CREATE TABLE old_table (a INTEGER)"]) + .await + .unwrap(); + old.close().await.unwrap(); + drop(old); + + let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()).await; + + assert_eq!( + created.err(), + Some(DriverError::FileExists { + path: file.path.clone() + }) + ); + let kept = file.open().await; + let tables = query(kept.as_ref(), "SELECT count(*) FROM duckdb_tables()").await; + assert_eq!(tables.rows[0][0], Value::Int(1)); + kept.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_replace_replaces_the_file_and_its_log() { let file = TestFile::new(); let old = file.open().await; apply(old.as_ref(), &["CREATE TABLE old_table (a INTEGER)"]) @@ -39,7 +64,7 @@ async fn a_new_database_replaces_the_file_and_its_log() { let wal = format!("{}.wal", file.path.display()); std::fs::write(&wal, b"a log left from the replaced database").unwrap(); - let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()) + let created = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()) .await .unwrap(); @@ -129,7 +154,7 @@ async fn an_editor_session_outlives_its_workspace() { assert_eq!(editor.state(), EditorSessionState::Lost); drop(editor); - let replaced = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()).await; + let replaced = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()).await; assert!(replaced.is_ok(), "the file was still held: {:?}", replaced.err()); } @@ -139,7 +164,7 @@ async fn an_editor_session_outlives_its_workspace() { async fn a_new_or_attached_database_stays_readable_by_duckdb_1() { let file = TestFile::new(); let attached = file.path.with_extension("side.duckdb"); - let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()) + let created = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()) .await .unwrap(); apply(created.as_ref(), &["CREATE TABLE facts (a INTEGER)"]) @@ -499,7 +524,7 @@ async fn a_file_another_process_has_open_is_not_replaced() { ); } - let replaced = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()).await; + let replaced = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()).await; drop(holder.stdin.take()); let _ = std::io::copy(&mut output, &mut std::io::sink()); diff --git a/core/crates/drivers/sqlite/src/connection.rs b/core/crates/drivers/sqlite/src/connection.rs index 2ae404648a..2951ebee80 100644 --- a/core/crates/drivers/sqlite/src/connection.rs +++ b/core/crates/drivers/sqlite/src/connection.rs @@ -23,7 +23,7 @@ use tokio::time::Instant; use tokio_util::sync::{CancellationToken, WaitForCancellationFutureOwned}; use crate::catalog::{SqliteVersion, check_runtime_version}; -use crate::create_file::create_or_replace; +use crate::create_file::make_file; use crate::deadline; use crate::dialect::SqliteDialect; use crate::driver::WORKSPACE_POOL_MAX_SIZE; @@ -56,8 +56,6 @@ pub(crate) struct SqliteConnection { } impl SqliteConnection { - /// Open the file the endpoint names, creating it first when the - /// user asked for a new database. pub(crate) async fn open( endpoint: &FileEndpoint, settings: WorkspaceSettings, @@ -72,9 +70,10 @@ impl SqliteConnection { if tablepro_core::claimed_by_another(&path, crate::driver::ENGINE) { return Err(DriverError::Busy(tablepro_core::BusyReason::FileLocked)); } - if endpoint.mode() == FileOpenMode::CreateNew { + let mode = endpoint.mode(); + if mode != FileOpenMode::OpenExisting { let target = path.clone(); - tokio::task::spawn_blocking(move || create_or_replace(&target)) + tokio::task::spawn_blocking(move || make_file(&target, mode)) .await .map_err(|error| DriverError::Protocol(format!("creating the database file stopped: {error}")))??; } diff --git a/core/crates/drivers/sqlite/src/create_file.rs b/core/crates/drivers/sqlite/src/create_file.rs index 8283c0f09b..075a0f8476 100644 --- a/core/crates/drivers/sqlite/src/create_file.rs +++ b/core/crates/drivers/sqlite/src/create_file.rs @@ -1,20 +1,61 @@ +use std::ffi::OsStr; use std::fs::OpenOptions; use std::io::ErrorKind; use std::path::{Path, PathBuf}; -use tablepro_core::{BusyReason, DriverError}; +use tablepro_core::{BusyReason, DriverError, FileOpenMode}; -/// The journal and WAL files SQLite keeps beside a database. One left -/// from the database being replaced would be replayed into the new one. +// A journal or log left beside the path would be replayed into the new database. const SIDECARS: [&str; 3] = ["-wal", "-shm", "-journal"]; -/// Put an empty database at `path`, replacing whatever is there. -/// -/// The save dialog has already asked the user before replacing a file, -/// so an existing file is not an error. It is replaced by a rename, -/// which is atomic: a reader sees the old file or the new one, never a -/// half-written one. An empty file is a valid empty database. -pub(crate) fn create_or_replace(path: &Path) -> Result<(), DriverError> { +pub(crate) fn make_file(path: &Path, mode: FileOpenMode) -> Result<(), DriverError> { + match mode { + FileOpenMode::OpenExisting => Ok(()), + FileOpenMode::CreateNew => create_new(path), + FileOpenMode::Replace => replace(path), + } +} + +// Exclusive, so another program's file at the path is never replaced, nor removed on a failure. Empty is a database. +pub(crate) fn create_new(path: &Path) -> Result<(), DriverError> { + let (parent, _) = target(path)?; + let file = private_file().open(path).map_err(|error| match error.kind() { + ErrorKind::AlreadyExists => DriverError::FileExists { + path: path.to_path_buf(), + }, + kind => file_error(kind, path), + })?; + remove_sidecars(path)?; + file.sync_all().map_err(|error| file_error(error.kind(), path))?; + sync_folder(parent, path) +} + +// The save dialog asked first. A rename puts the file in place, so a reader sees the old file or the new one. +pub(crate) fn replace(path: &Path) -> Result<(), DriverError> { + let (parent, name) = target(path)?; + // A file another window or the other engine has open would be pulled out from under it. + if tablepro_core::held_by_this_process(path) { + return Err(DriverError::Busy(BusyReason::FileLocked)); + } + let temporary = parent.join(format!( + ".{}.{}.tmp", + name.to_string_lossy(), + uuid::Uuid::new_v4().simple() + )); + let created = private_file().open(&temporary).and_then(|file| file.sync_all()); + if let Err(error) = created { + let _ = std::fs::remove_file(&temporary); + return Err(file_error(error.kind(), path)); + } + if let Err(error) = std::fs::rename(&temporary, path) { + let _ = std::fs::remove_file(&temporary); + return Err(file_error(error.kind(), path)); + } + remove_sidecars(path)?; + sync_folder(parent, path) +} + +fn target(path: &Path) -> Result<(&Path, &OsStr), DriverError> { let parent = match path.parent() { Some(parent) if !parent.as_os_str().is_empty() => parent, _ => { @@ -28,32 +69,18 @@ pub(crate) fn create_or_replace(path: &Path) -> Result<(), DriverError> { path: path.to_path_buf(), }); }; - // A file another window or the other engine has open would be pulled - // out from under it, and its writes would go to a file no name finds. - if tablepro_core::held_by_this_process(path) { - return Err(DriverError::Busy(BusyReason::FileLocked)); - } - let temporary = parent.join(format!( - ".{}.{}.tmp", - name.to_string_lossy(), - uuid::Uuid::new_v4().simple() - )); + Ok((parent, name)) +} +fn private_file() -> OpenOptions { let mut options = OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); - let created = options.open(&temporary).and_then(|file| file.sync_all()); - if let Err(error) = created { - // A file that was created and then failed to sync is not left - // behind beside the user's files. - let _ = std::fs::remove_file(&temporary); - return Err(file_error(error.kind(), path)); - } - if let Err(error) = std::fs::rename(&temporary, path) { - let _ = std::fs::remove_file(&temporary); - return Err(file_error(error.kind(), path)); - } + options +} + +fn remove_sidecars(path: &Path) -> Result<(), DriverError> { for suffix in SIDECARS { let mut sidecar = path.as_os_str().to_owned(); sidecar.push(suffix); @@ -62,6 +89,10 @@ pub(crate) fn create_or_replace(path: &Path) -> Result<(), DriverError> { _ => {} } } + Ok(()) +} + +fn sync_folder(parent: &Path, path: &Path) -> Result<(), DriverError> { tablepro_core::sync_directory(parent).map_err(|error| file_error(error.kind(), path)) } @@ -79,13 +110,98 @@ fn file_error(kind: ErrorKind, path: &Path) -> DriverError { mod tests { use super::*; + fn names_in(directory: &Path) -> Vec { + let mut names: Vec<_> = std::fs::read_dir(directory) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + names.sort(); + names + } + + fn sidecars_of(path: &Path) -> Vec { + SIDECARS + .iter() + .map(|suffix| { + let mut sidecar = path.as_os_str().to_owned(); + sidecar.push(suffix); + PathBuf::from(sidecar) + }) + .collect() + } + + #[test] + fn a_new_file_is_an_empty_private_one_where_nothing_was() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("new.sqlite3"); + + create_new(&path).unwrap(); + + let metadata = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.len(), 0); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + + assert_eq!(metadata.permissions().mode() & 0o777, 0o600); + } + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("new.sqlite3")]); + } + + // The app found the path free, then another program made a file there before the create. + #[test] + fn a_file_made_after_the_path_was_checked_is_never_replaced() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("data.sqlite3"); + std::fs::write(&path, b"another program's data").unwrap(); + let journal = sidecars_of(&path).remove(2); + std::fs::write(&journal, b"its journal").unwrap(); + + assert_eq!(create_new(&path), Err(DriverError::FileExists { path: path.clone() })); + + assert_eq!(std::fs::read(&path).unwrap(), b"another program's data"); + assert_eq!(std::fs::read(&journal).unwrap(), b"its journal"); + assert_eq!( + names_in(directory.path()), + [ + std::ffi::OsString::from("data.sqlite3"), + std::ffi::OsString::from("data.sqlite3-journal") + ] + ); + } + #[test] - fn an_existing_file_is_replaced_by_an_empty_private_one() { + fn a_new_file_removes_the_logs_a_deleted_database_left() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("data.sqlite3"); + for sidecar in sidecars_of(&path) { + std::fs::write(sidecar, b"left from a database that is gone").unwrap(); + } + + create_new(&path).unwrap(); + + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("data.sqlite3")]); + } + + #[cfg(unix)] + #[test] + fn a_link_at_the_path_is_not_followed() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("link.sqlite3"); + let elsewhere = directory.path().join("elsewhere.sqlite3"); + std::os::unix::fs::symlink(&elsewhere, &path).unwrap(); + + assert_eq!(create_new(&path), Err(DriverError::FileExists { path })); + assert!(!elsewhere.exists(), "the create followed the link"); + } + + #[test] + fn a_replaced_file_becomes_an_empty_private_one() { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("data.sqlite3"); std::fs::write(&path, b"old contents").unwrap(); - create_or_replace(&path).unwrap(); + replace(&path).unwrap(); let metadata = std::fs::metadata(&path).unwrap(); assert_eq!(metadata.len(), 0); @@ -95,11 +211,7 @@ mod tests { assert_eq!(metadata.permissions().mode() & 0o777, 0o600); } - let leftovers: Vec<_> = std::fs::read_dir(directory.path()) - .unwrap() - .map(|entry| entry.unwrap().file_name()) - .collect(); - assert_eq!(leftovers, [std::ffi::OsString::from("data.sqlite3")]); + assert_eq!(names_in(directory.path()), [std::ffi::OsString::from("data.sqlite3")]); } #[test] @@ -131,6 +243,7 @@ mod tests { let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("missing").join("data.sqlite3"); - assert_eq!(create_or_replace(&path), Err(DriverError::FileNotFound { path })); + assert_eq!(create_new(&path), Err(DriverError::FileNotFound { path: path.clone() })); + assert_eq!(replace(&path), Err(DriverError::FileNotFound { path })); } } diff --git a/core/crates/drivers/sqlite/tests/integration/open_semantics.rs b/core/crates/drivers/sqlite/tests/integration/open_semantics.rs index e5cb02bf1a..6735770a2d 100644 --- a/core/crates/drivers/sqlite/tests/integration/open_semantics.rs +++ b/core/crates/drivers/sqlite/tests/integration/open_semantics.rs @@ -1,5 +1,5 @@ -//! What opening a path does: open what is there, create only when -//! asked, and replace atomically. +//! What opening a path does: open what is there, create only where +//! nothing is, and replace atomically when asked. use std::os::unix::fs::PermissionsExt; use std::path::PathBuf; @@ -26,13 +26,34 @@ async fn open_existing_missing_path_file_not_found_no_file_created() { } #[tokio::test] -async fn create_new_replaces_existing_database_atomically() { +async fn create_new_keeps_a_database_already_there() { let file = TestFile::new().await; let old = file.open().await; apply(old.as_ref(), &["CREATE TABLE old_table (a)"]).await.unwrap(); old.close().await.unwrap(); - let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()) + let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()).await; + + assert_eq!( + created.err(), + Some(DriverError::FileExists { + path: file.path.clone() + }) + ); + let kept = file.open().await; + let tables = query(kept.as_ref(), "SELECT name FROM sqlite_schema").await; + assert_eq!(tables.rows, vec![vec![Value::Text("old_table".to_owned())]]); + kept.close().await.unwrap(); +} + +#[tokio::test] +async fn replace_replaces_existing_database_atomically() { + let file = TestFile::new().await; + let old = file.open().await; + apply(old.as_ref(), &["CREATE TABLE old_table (a)"]).await.unwrap(); + old.close().await.unwrap(); + + let created = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()) .await .unwrap(); @@ -44,7 +65,7 @@ async fn create_new_replaces_existing_database_atomically() { } #[tokio::test] -async fn create_new_removes_stale_wal_and_shm_sidecars() { +async fn replace_removes_stale_wal_and_shm_sidecars() { let file = TestFile::new().await; let sidecars: Vec = ["-wal", "-shm", "-journal"] .iter() @@ -54,7 +75,7 @@ async fn create_new_removes_stale_wal_and_shm_sidecars() { std::fs::write(sidecar, b"stale journal from the replaced database").unwrap(); } - let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()) + let created = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()) .await .unwrap(); @@ -242,12 +263,12 @@ async fn a_file_duckdb_has_open_is_not_opened() { /// A new database over a file this process has open would pull it out /// from under the window using it. #[tokio::test] -async fn create_new_refuses_a_file_this_process_holds() { +async fn replace_refuses_a_file_this_process_holds() { let file = TestFile::new().await; let open = file.open().await; apply(open.as_ref(), &["CREATE TABLE kept (a)"]).await.unwrap(); - let created = connect(FileEndpoint::create_new(file.path.clone()).unwrap(), writable()).await; + let created = connect(FileEndpoint::replace(file.path.clone()).unwrap(), writable()).await; assert_eq!( created.err(), diff --git a/core/crates/ffi/src/error.rs b/core/crates/ffi/src/error.rs index 04221c5410..6b85dc1ac9 100644 --- a/core/crates/ffi/src/error.rs +++ b/core/crates/ffi/src/error.rs @@ -248,6 +248,7 @@ fn file_path(error: &DriverError) -> Option<&Path> { match error { DriverError::FileNotFound { path } | DriverError::FileAccessDenied { path } + | DriverError::FileExists { path } | DriverError::NotADatabase { path } | DriverError::FileIo { path, .. } => Some(path), _ => None, diff --git a/core/crates/ffi/tests/sqlite_path.rs b/core/crates/ffi/tests/sqlite_path.rs index f9e7d8066a..a854190712 100644 --- a/core/crates/ffi/tests/sqlite_path.rs +++ b/core/crates/ffi/tests/sqlite_path.rs @@ -288,6 +288,38 @@ async fn errors_carry_the_codes_the_app_words_them_by() { assert!(info.server.unwrap().message.contains("no such table")); } +// The app found the path free, then another program made a file there before the create. +#[tokio::test] +async fn a_new_file_never_replaces_one_made_after_the_path_was_checked() { + let dir = tempfile::tempdir().unwrap(); + let core = core(); + + for (engine, name) in [("sqlite", "made.sqlite"), ("duckdb", "made.duckdb")] { + let path = dir.path().join(name); + std::fs::write(&path, b"another program's data").unwrap(); + + let error = core + .open_file( + FileOpenRequest { + path: path.display().to_string(), + driver_id: Some(engine.to_owned()), + read_only: false, + create: true, + }, + None, + ) + .await + .unwrap_err(); + + let CoreError::Failed { info } = error else { + panic!("{engine}: {error:?}") + }; + assert_eq!(info.codes, ["connect.driver", "driver.file_exists"], "{engine}"); + assert!(info.path.unwrap().ends_with(name), "{engine}"); + assert_eq!(std::fs::read(&path).unwrap(), b"another program's data", "{engine}"); + } +} + #[tokio::test] async fn a_cancelled_open_is_a_cancel() { let dir = tempfile::tempdir().unwrap(); diff --git a/linux/changelog.d/4316.fixed.md b/linux/changelog.d/4316.fixed.md new file mode 100644 index 0000000000..0f684c5e45 --- /dev/null +++ b/linux/changelog.d/4316.fixed.md @@ -0,0 +1 @@ +New Database no longer replaces a file another program made at the chosen name before you connected. diff --git a/linux/crates/app/src/services/file_location.rs b/linux/crates/app/src/services/file_location.rs index 0e9bcfb09d..4eeadd9989 100644 --- a/linux/crates/app/src/services/file_location.rs +++ b/linux/crates/app/src/services/file_location.rs @@ -106,6 +106,11 @@ fn mount_kind_blocking(path: &Path) -> FileLocationKind { } } +// A dangling link counts, since a create where nothing is refuses it too. +pub fn is_taken_blocking(path: &Path) -> bool { + std::fs::symlink_metadata(path).is_ok() +} + /// The file a document portal path stands for, when the portal says. pub fn portal_host_path_blocking(path: &Path) -> Option { let info = gio::File::for_path(path) diff --git a/linux/crates/app/src/ui/connect_dialog/database_file_row.rs b/linux/crates/app/src/ui/connect_dialog/database_file_row.rs index b5ec3c6405..8449cee8ce 100644 --- a/linux/crates/app/src/ui/connect_dialog/database_file_row.rs +++ b/linux/crates/app/src/ui/connect_dialog/database_file_row.rs @@ -143,6 +143,7 @@ impl DatabaseFileRow { let endpoint = match mode { FileOpenMode::OpenExisting => FileEndpoint::open_existing(path), FileOpenMode::CreateNew => FileEndpoint::create_new(path), + FileOpenMode::Replace => FileEndpoint::replace(path), }; match endpoint { Ok(endpoint) => self.set_endpoint(Some(endpoint)), @@ -169,15 +170,27 @@ impl DatabaseFileRow { } } - /// The save chooser asks before replacing a file that exists, and - /// the driver then replaces it whole, so no second question here. async fn choose_new(&self) { let files = self.imp().files.borrow().clone(); let dialog = database_file_dialog(&crate::i18n::gettext("New Database File"), files.as_ref()); dialog.set_initial_name(Some(&new_file_name(files.as_ref()))); let chosen = dialog.save_future(self.parent_window().as_ref()).await; if let Some(path) = chosen.ok().and_then(|file| file.path()) { - self.adopt_choice(path, FileOpenMode::CreateNew); + let mode = self.new_file_mode(&path).await; + self.adopt_choice(path, mode); + } + } + + // GTK does not say whether the chooser asked to replace, so a file there when it closes is taken as asked about. + pub(super) async fn new_file_mode(&self, path: &Path) -> FileOpenMode { + let Some(tasks) = self.imp().tasks.get() else { + return FileOpenMode::CreateNew; + }; + let probe = path.to_path_buf(); + let taken = tasks.spawn_blocking_task(move || file_location::is_taken_blocking(&probe)); + match taken.await { + Ok(true) => FileOpenMode::Replace, + _ => FileOpenMode::CreateNew, } } @@ -194,7 +207,9 @@ fn subtitle(endpoint: Option<&FileEndpoint>, host_path: Option<&Path>) -> String }; let shown = file_location::display_path(file.path(), host_path); match file.mode() { - FileOpenMode::CreateNew => crate::i18n::gettext_f("{path}, created when you connect", &[("path", &shown)]), + FileOpenMode::CreateNew | FileOpenMode::Replace => { + crate::i18n::gettext_f("{path}, created when you connect", &[("path", &shown)]) + } FileOpenMode::OpenExisting => shown, } } @@ -290,6 +305,11 @@ mod tests { "/srv/data/app.db" ); assert_eq!(subtitle(Some(&created), None), "new.db, created when you connect"); + let replaced = FileEndpoint::replace(PathBuf::from("/home/me/old.db")).expect("absolute"); + assert_eq!( + subtitle(Some(&replaced), None), + "/home/me/old.db, created when you connect" + ); assert_eq!(subtitle(Some(&local), None), "/home/me/app.db"); assert_eq!(subtitle(None, None), "No file chosen"); } diff --git a/linux/crates/app/src/ui/connect_dialog/mod.rs b/linux/crates/app/src/ui/connect_dialog/mod.rs index 830c8d6826..2e9f3bb1e6 100644 --- a/linux/crates/app/src/ui/connect_dialog/mod.rs +++ b/linux/crates/app/src/ui/connect_dialog/mod.rs @@ -28,12 +28,14 @@ use secrecy::SecretString; use tablepro_core::connection_url::ConnectionUrl; use tablepro_core::credentials::CredentialInteraction; use tablepro_core::{ - AiPolicy, AuthMode, ConfigError, DatabaseDriver, DriverCapabilities, DriverRegistry, ExternalAccess, LocalSocket, - NetworkOption, NetworkOptions, QueryTimeout, SafeModeLevel, TlsConfig, TlsMode, TransportClass, WorkspaceSettings, - is_local_socket_host, + AiPolicy, AuthMode, ConfigError, DatabaseDriver, DriverCapabilities, DriverRegistry, ExternalAccess, FileOpenMode, + LocalSocket, NetworkOption, NetworkOptions, QueryTimeout, SafeModeLevel, TlsConfig, TlsMode, TransportClass, + WorkspaceSettings, is_local_socket_host, }; use tablepro_session::runtime::{ProcessHold, Tasks}; -use tablepro_session::{ConnectError, ConnectRequest, ConnectionMetadata, Connector, CredentialSource, PendingSave}; +use tablepro_session::{ + ConnectError, ConnectRequest, ConnectionMetadata, Connector, CredentialSource, PendingSave, RequestTarget, +}; use tablepro_storage::{ConnectionTag, CredentialDemand, SavedConnection}; use uuid::Uuid; @@ -1236,6 +1238,11 @@ impl ConnectDialog { // A test entry is never saved, so it gets an id of its own. let mut request = request; request.metadata.id = Uuid::new_v4(); + // A test of a new file makes it, so Connect then opens that file rather than refusing it. + let made = match &request.target { + RequestTarget::File(file) if file.mode() != FileOpenMode::OpenExisting => Some(file.clone()), + _ => None, + }; let connector = services.connector.clone(); let interaction = services.interaction.clone(); let cancel = ticket.cancel().clone(); @@ -1269,7 +1276,14 @@ impl ConnectDialog { } dialog.stop_test(); match outcome { - Ok(Ok(())) => dialog.show_toast(&crate::i18n::gettext("Connection works"), None), + Ok(Ok(())) => { + if let Some(made) = made + && imp.file_row.endpoint().as_ref() == Some(&made) + { + imp.file_row.set_endpoint(Some(made.for_reopen())); + } + dialog.show_toast(&crate::i18n::gettext("Connection works"), None); + } Ok(Err(ConnectError::Cancelled)) => {} Ok(Err(error)) => dialog.show_toast( &crate::ui::error_text::connect_error_message(&error), diff --git a/linux/crates/app/src/ui/connect_dialog/tests.rs b/linux/crates/app/src/ui/connect_dialog/tests.rs index d56555fbf9..de53e45448 100644 --- a/linux/crates/app/src/ui/connect_dialog/tests.rs +++ b/linux/crates/app/src/ui/connect_dialog/tests.rs @@ -492,6 +492,51 @@ fn new_database_sets_create_new_endpoint() { assert!(harness.dialog.imp().connect_button.is_sensitive()); } +// The save chooser asked only about a file there when it closed; a free name must not replace one made later. +#[gtk4::test] +fn a_new_name_replaces_only_a_file_the_chooser_asked_about() { + let harness = dialog(); + select(&harness.dialog, "sqlite"); + let root = tempfile::tempdir().expect("tempdir"); + let there = root.path().join("there.sqlite3"); + std::fs::write(&there, b"").expect("a file"); + let free = root.path().join("free.sqlite3"); + let modes = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); + + let found = modes.clone(); + let asked = harness.dialog.imp().file_row.get(); + glib::spawn_future_local(async move { + for path in [there, free] { + let mode = asked.new_file_mode(&path).await; + found.borrow_mut().push(mode); + } + }); + wait_until(WAIT, || modes.borrow().len() == 2).expect("both answers"); + + assert_eq!(*modes.borrow(), [FileOpenMode::Replace, FileOpenMode::CreateNew]); +} + +#[gtk4::test] +fn a_tested_new_file_is_opened_by_connect() { + let harness = open(Options { + connector: FakeConnector::new([FakeEstablish::Connect(ConnectionScript::default())]), + ..Options::default() + }); + select(&harness.dialog, "sqlite"); + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("tested.sqlite3"); + let imp = harness.dialog.imp(); + imp.file_row.adopt_choice(path.clone(), FileOpenMode::CreateNew); + + imp.test_button.emit_clicked(); + wait_until(WAIT, || !imp.toasts.borrow().is_empty()).expect("the test result"); + + assert_eq!(imp.toasts.borrow().as_slice(), ["Connection works"]); + let file = imp.file_row.endpoint().expect("a file"); + assert_eq!(file.path(), path); + assert_eq!(file.mode(), FileOpenMode::OpenExisting); +} + #[gtk4::test] fn mysql_requires_database() { let harness = dialog(); diff --git a/linux/crates/app/src/ui/error_text/driver.rs b/linux/crates/app/src/ui/error_text/driver.rs index 21921d1081..e1f0640ab6 100644 --- a/linux/crates/app/src/ui/error_text/driver.rs +++ b/linux/crates/app/src/ui/error_text/driver.rs @@ -317,6 +317,10 @@ fn file_message(error: &DriverError) -> String { "No permission to open {path}.", &[("path", &path.display().to_string())], ), + DriverError::FileExists { path } => crate::i18n::gettext_f( + "A file is already at {path}, so it was left as it is. Use Choose… to open it, or New Database… to replace it.", + &[("path", &path.display().to_string())], + ), DriverError::NotADatabase { path } => crate::i18n::gettext_f( "{path} is not a database file.", &[("path", &path.display().to_string())], diff --git a/linux/crates/app/src/ui/error_text/tests.rs b/linux/crates/app/src/ui/error_text/tests.rs index 5dcecba876..b28148caa0 100644 --- a/linux/crates/app/src/ui/error_text/tests.rs +++ b/linux/crates/app/src/ui/error_text/tests.rs @@ -279,6 +279,16 @@ fn file_not_found_names_path() { assert!(message.contains("/srv/app.db"), "{message}"); } +#[test] +fn a_file_already_there_names_it_and_says_it_was_kept() { + let message = driver_message(&DriverError::FileExists { + path: PathBuf::from("/srv/new.db"), + }); + + assert!(message.contains("/srv/new.db"), "{message}"); + assert!(message.contains("left as it is"), "{message}"); +} + #[test] fn timeout_phase_login_vs_statement_differ() { let timeout = |phase| { diff --git a/linux/docs/drivers/duckdb.md b/linux/docs/drivers/duckdb.md index 6c163e20a8..4cf7f6b681 100644 --- a/linux/docs/drivers/duckdb.md +++ b/linux/docs/drivers/duckdb.md @@ -21,10 +21,11 @@ The connect reads `version()`, which also proves the file opened. The app names ### New databases -`FileOpenMode::CreateNew` builds an empty database next to the target, checkpoints and closes it, sets mode 0600, renames it over the path and syncs the folder. A reader sees the old file or the new one. +Both modes build an empty database next to the target, checkpoint and close it, set mode 0600, put it at the path and sync the folder. -- The driver takes the lock DuckDB takes on the file it replaces and holds it through the rename. A file another process has open, or this one, is `Busy(FileLocked)`: its writer would go on logging to `.wal` beside the new file. -- The old file's `.wal` moves aside before the rename and is removed after it, since DuckDB would replay it into the new database. When the rename fails it goes back, so the old database keeps the changes only its log holds. +- `FileOpenMode::CreateNew` hard links it to the path, which fails on anything there, so a file another program made after the app looked is never replaced: that is `FileExists`, a symbolic link included. A file system without links, such as FAT or the Flatpak document portal, gets a copy through a file made with `create_new` (`O_EXCL`) instead. A `.wal` a deleted database left is removed once the name is taken. A failure never removes the file at the path, which may by then be another program's. +- `FileOpenMode::Replace`, for a file the save dialog asked about replacing, renames it over the path, so a reader sees the old file or the new one. It takes the lock DuckDB takes on the file it replaces and holds it through the rename. A file another process has open, or this one, is `Busy(FileLocked)`: its writer would go on logging to `.wal` beside the new file. +- On a replace, the old file's `.wal` moves aside before the rename and is removed after it, since DuckDB would replay it into the new database. When the rename fails it goes back, so the old database keeps the changes only its log holds. ### Where the file lives diff --git a/linux/docs/drivers/sqlite.md b/linux/docs/drivers/sqlite.md index 3d3edcb8aa..da6496b9dd 100644 --- a/linux/docs/drivers/sqlite.md +++ b/linux/docs/drivers/sqlite.md @@ -21,7 +21,9 @@ The connect reads `sqlite_version()` and counts `sqlite_schema` once, so a file ### New databases -`FileOpenMode::CreateNew` is what the New Database flow sends after the save dialog has asked about replacing a file. The driver writes an empty file next to the target with mode 0600, syncs it, renames it over the path, removes any `-wal`, `-shm` or `-journal` file left beside the old database (it would be replayed into the new one), and syncs the folder so the rename is durable. A reader sees the old file or the new one, never half of one. An existing file is replaced, not refused, unless this process has it open: the rename would pull it out from under the window or the DuckDB database using it, so that is `Busy(FileLocked)`. +`FileOpenMode::CreateNew` makes the file only where nothing is: an empty file with mode 0600, opened with `create_new` (`O_EXCL`), so a file another program made after the app looked is never replaced. Anything at the path, a symbolic link included, is `FileExists`. The driver then removes any `-wal`, `-shm` or `-journal` file a deleted database left (it would be replayed into the new one), syncs the file and syncs the folder. A failure never removes the file, which may by then be another program's. + +`FileOpenMode::Replace` is what the New Database flow sends for a file the save dialog asked about replacing. The driver writes an empty file next to the target with mode 0600, syncs it, renames it over the path, removes the old database's sidecars and syncs the folder. A reader sees the old file or the new one, never half of one. A file this process has open is `Busy(FileLocked)`: the rename would pull it out from under the window or the DuckDB database using it. A reconnect uses `FileEndpoint::for_reopen`, which is always `OpenExisting`, so reconnecting never empties a database the user just created. diff --git a/linux/docs/error-handling.md b/linux/docs/error-handling.md index 6eaa427cf5..00cf8d9cf9 100644 --- a/linux/docs/error-handling.md +++ b/linux/docs/error-handling.md @@ -34,6 +34,7 @@ Errors are typed enums, built with `thiserror` wherever they cross a crate bound | `RolledBack`, `RowGuardFailed`, `CommitOutcomeUnknown`, `PartiallyApplied` | What a failed save left in the database ([adding-drivers.md](adding-drivers.md#5-writes-and-the-writeledger)). | | `GuardUnsupported` | The engine reports no row count, so a guarded write cannot run. | | `FileNotFound`, `FileAccessDenied`, `NotADatabase`, `FileIo` | A file engine's file. | +| `FileExists` | A new database file was asked for where a file already is, which is kept as it is. | | `Busy(BusyReason)` | Something was busy: `NoFreeConnection` (the server has no room for another connection, or the pool had no free slot in time) or `FileLocked` (another connection held the database file's lock past the lock timeout). | | `EditorSessionBusy` | The tab's session is already running a statement. | | `Config(ConfigError)` | The settings cannot be used: a bad host, port, path, TLS option, auth mode or missing database name. | diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs index bbaef01de6..60575628b1 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs @@ -762,15 +762,18 @@ private async Task SaveAsync(bool connect) private async Task CreateFileAsync(ConnectionDraft draft) { var path = draft.Database.Trim(); - if (File.Exists(path)) + try { - _createFile = false; - return; + var session = await _workbench.Core.OpenFileAsync(new FileOpenOptions(path, draft.EngineId, Create: true)).ConfigureAwait(true); + await session.DisposeAsync().ConfigureAwait(true); + SetStatus(Strings.Format("FileCreated", Path.GetFileName(path)), true); + } + catch (TableProException exception) when (exception.Code == "driver.file_exists") + { + // A file made there since it was named opens as it is, as one there when it was named does. + SetStatus(Strings.Get("NewFileExists"), null); } - var session = await _workbench.Core.OpenFileAsync(new FileOpenOptions(path, draft.EngineId, Create: true)).ConfigureAwait(true); - await session.DisposeAsync().ConfigureAwait(true); _createFile = false; - SetStatus(Strings.Format("FileCreated", Path.GetFileName(path)), true); } } diff --git a/windows/tests/TablePro.App.UITests/NewFileTests.cs b/windows/tests/TablePro.App.UITests/NewFileTests.cs index 6bdb82b5d3..049501a178 100644 --- a/windows/tests/TablePro.App.UITests/NewFileTests.cs +++ b/windows/tests/TablePro.App.UITests/NewFileTests.cs @@ -74,6 +74,30 @@ public void AnExistingFileNamedForANewOneOpensAsItIsWithoutAReplaceQuestion(stri app.FindByName("Album", app.Find("ObjectTree")); } + [Theory] + [MemberData(nameof(Themes))] + public void AFileMadeAfterTheNewNameWasChosenOpensAsItIs(string theme) + { + using var app = AppSession.Launch(theme); + var file = Path.Combine(app.DataFolder, "chinook-copy.sqlite"); + app.Click(app.Find("NewConnectionCard"), () => app.HasWindow("New connection")); + var form = app.WaitForWindow("New connection"); + app.Click(app.Find("Engine_sqlite", form), () => app.TryFind("FileBox", form) is not null, form); + app.Find("NameBox", form).AsTextBox().Text = "Made elsewhere"; + app.Click(app.Find("NewFileButton", form), () => SaveDialog(app, form) is not null, form); + SaveInFileDialog(app, form, file); + Assert.Equal( + "The file is made when you connect", + AppSession.WaitForText(app.Find("FormStatus", form), text => text.Length > 0)); + + // Another program makes the file between the form's check and Connect. + Assert.Equal(file, AppSession.SampleDatabase(app.DataFolder)); + app.Find("ConnectButton", form).AsButton().Invoke(); + + app.WaitForClose("New connection"); + app.FindByName("Album", app.Find("ObjectTree")); + } + // The form owns its picker, so UI Automation lists it under the form, the app's window or the desktop. private static Window? SaveDialog(AppSession app, Window form) => form.FindFirstChild(condition => condition.ByClassName("#32770"))?.AsWindow() ?? FileDialog(app); diff --git a/windows/tests/TablePro.Interop.Tests/NewFileTests.cs b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs index 76b132b94f..80bce6f563 100644 --- a/windows/tests/TablePro.Interop.Tests/NewFileTests.cs +++ b/windows/tests/TablePro.Interop.Tests/NewFileTests.cs @@ -39,4 +39,23 @@ public async Task ANewFileIsMadeEmptyAndOpensAgainForWriting(string engine, stri using var run = editor.Run("CREATE TABLE notes (body TEXT)", cancellationToken: cancellationToken); Assert.IsType(await run.Completion); } + + // The form found the path free, then another program made a file there before Connect. + [Theory] + [InlineData("duckdb", "taken.duckdb")] + [InlineData("sqlite", "taken.sqlite")] + public async Task AFileMadeAfterThePathWasCheckedIsNeverReplaced(string engine, string name) + { + var cancellationToken = TestContext.Current.CancellationToken; + var path = Path.Combine(_directory, name); + await File.WriteAllTextAsync(path, "another program's data", cancellationToken); + + var error = await Assert.ThrowsAsync( + () => _core.OpenFileAsync(new FileOpenOptions(path, engine, Create: true), cancellationToken)); + + Assert.Equal("driver.file_exists", error.Code); + Assert.Equal(name, Path.GetFileName(error.Error.Path)); + Assert.Equal("another program's data", await File.ReadAllTextAsync(path, cancellationToken)); + Assert.Equal(path, Assert.Single(Directory.GetFiles(_directory))); + } }