Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 65 additions & 19 deletions core/crates/storage/src/upgrade_copies.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Why a file an older build wrote is copied before its first write in a newer format: core ADR 0012.

use std::io::ErrorKind;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

Expand Down Expand Up @@ -95,30 +97,19 @@ pub(crate) async fn keep_database(pool: &SqlitePool, path: &Path, version: u64)
remove_leftovers_blocking(path)?;
let copy = vacant_name(path, version, SystemTime::now())?;
let partial = partial_name(&copy);
// VACUUM INTO refuses a file that is not empty, and creates a missing
// one from the umask: made here, it is 0600 like the database.
crate::fs::create_private_file_if_missing(&partial)?;
let target = partial.to_str().ok_or_else(|| StorageError::Write {
path: partial.clone(),
source: std::io::Error::new(ErrorKind::InvalidInput, "the path is not UTF-8"),
})?;
if let Err(error) = sqlx::query("VACUUM INTO ?").bind(target).execute(pool).await {
let _ = remove_with_sidecars(&partial);
return Err(StorageError::Write {
path: copy,
source: database_io_error(&error),
});
}
let write_error = |source| StorageError::Write {
path: copy.clone(),
source,
};
// SQLite creates the copy from the umask, so only the owner may reach the folder before it is 0600.
crate::fs::ensure_private_dir(parent_of(&partial))?;
vacuum_into_new_file(pool, &partial).await.map_err(write_error)?;
if let Err(error) = crate::query_history::scrub_copy(&partial).await {
let _ = remove_with_sidecars(&partial);
return Err(error);
}
// Renamed into place only once on disk, so a copy cut short by a
// power cut never carries a name that says it is whole.
let write_error = |source| StorageError::Write {
path: copy.clone(),
source,
};
// Windows flushes only a handle opened for writing.
let landed = std::fs::OpenOptions::new()
.write(true)
Expand All @@ -136,6 +127,29 @@ pub(crate) async fn keep_database(pool: &SqlitePool, path: &Path, version: u64)
Ok(copy)
}

// The SQLite documentation allows a target that exists if it is empty, and macOS 27's SQLite refuses
// one even then, so SQLite only ever gets a name that is free. A file already there is not ours to remove.
async fn vacuum_into_new_file(pool: &SqlitePool, target: &Path) -> std::io::Result<()> {
match std::fs::symlink_metadata(target) {
Err(error) if error.kind() == ErrorKind::NotFound => {}
Ok(_) => return Err(std::io::Error::from(ErrorKind::AlreadyExists)),
Err(error) => return Err(error),
}
let name = target
.to_str()
.ok_or_else(|| std::io::Error::new(ErrorKind::InvalidInput, "the path is not UTF-8"))?;
if let Err(error) = sqlx::query("VACUUM INTO ?").bind(name).execute(pool).await {
let _ = remove_with_sidecars(target);
return Err(database_io_error(&error));
}
#[cfg(unix)]
if let Err(error) = std::fs::set_permissions(target, std::fs::Permissions::from_mode(0o600)) {
let _ = remove_with_sidecars(target);
return Err(error);
}
Ok(())
}

// A copy of the connection list keeps no connection the list no longer has, so a deleted one is gone
// from it too; a copy that cannot be read that way is deleted rather than kept with it.
pub fn trim_connection_copies_blocking(
Expand Down Expand Up @@ -376,7 +390,6 @@ fn database_io_error(error: &sqlx::Error) -> std::io::Error {
#[cfg(test)]
#[cfg(unix)]
mod tests {
use std::os::unix::fs::PermissionsExt;
use std::time::{Duration, UNIX_EPOCH};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -528,6 +541,39 @@ mod tests {
);
}

// The empty 0600 file the copy step once made first: macOS 27's SQLite refuses it, and upstream
// SQLite writes into it, so the step refuses it itself whatever SQLite is linked.
#[tokio::test]
async fn the_copy_step_takes_only_a_name_that_is_free() {
let root = tempfile::tempdir().expect("tempdir");
let path = root.path().join("items.db");
let pool = database(&path).await;
base().run(&pool).await.expect("the older schema");
let taken = root.path().join(".items.db.v1-100.partial");
crate::fs::create_private_file_if_missing(&taken).expect("an empty file");

let refused = vacuum_into_new_file(&pool, &taken)
.await
.expect_err("a copy into a file already there");
let free = root.path().join(".items.db.v1-101.partial");
vacuum_into_new_file(&pool, &free)
.await
.expect("a copy into a free name");

assert_eq!(refused.kind(), ErrorKind::AlreadyExists);
assert_eq!(std::fs::metadata(&taken).expect("still there").len(), 0);
assert_eq!(
std::fs::metadata(&free).expect("the copy").permissions().mode() & 0o777,
0o600
);
let copied = database(&free).await;
let tables: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE name = 'items'")
.fetch_one(&copied)
.await
.expect("count");
assert_eq!(tables, 1);
}

#[tokio::test]
async fn a_copy_a_crash_left_unfinished_is_removed_before_the_next() {
let root = tempfile::tempdir().expect("tempdir");
Expand Down
6 changes: 4 additions & 2 deletions linux/docs/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,10 @@ that replaces it with a newer format ([core ADR 0012](../../core/docs/decisions/
- `tablepro_storage::upgrade_copies` makes them. A JSON document's copy is its
bytes as read, written 0600 by `write_private_blocking`; a copy whose bytes
match the newest one is not made again. The history's is `VACUUM INTO` through
the open connection, so it holds what the WAL holds too, into a 0600
`.partial` file. Its full-text indexes are then optimized with `secure_delete`
the open connection, so it holds what the WAL holds too, into a `.partial`
name that is free: macOS 27's SQLite refuses any file already there, even an
empty one. SQLite creates it from the umask in the 0700 folder, and it is set
to 0600 right after. Its full-text indexes are then optimized with `secure_delete`
on, since `VACUUM INTO` copies the words of entries deleted before, and only
then is it synced and renamed into place.
- Each file keeps its newest 3 copies (`KEPT_PER_FILE`); a new one deletes the
Expand Down
Loading