use std::fs;
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail, ensure};
use rusqlite::{Connection, params};
use super::SqliteStore;
use super::compaction::{SPACE_MARGIN, VacuumSpace};
use super::pool::{self, open_connection};
use crate::keys::StoreKey;
const PLAIN_HEADER: &[u8; 16] = b"SQLite format 3\0";
const REWRITE_SUFFIX: &str = "enc";
const READERS_WAIT: Duration = Duration::from_secs(30);
const PROBE_BUSY: Duration = Duration::from_secs(1);
pub const PLAINTEXT_COPY_PREFIX: &str = "brain.db.plaintext-";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FileState {
Absent,
Plaintext,
Encrypted,
}
pub fn db_file_state(path: &Path) -> Result<FileState> {
let mut file = match fs::File::open(path) {
Ok(file) => file,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(FileState::Absent),
Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
};
let mut header = [0u8; 16];
let mut filled = 0;
while filled < header.len() {
let read = file
.read(&mut header[filled..])
.with_context(|| format!("reading {}", path.display()))?;
if read == 0 {
break;
}
filled += read;
}
Ok(match filled {
0 => FileState::Absent,
16 if &header == PLAIN_HEADER => FileState::Plaintext,
_ => FileState::Encrypted,
})
}
pub fn rewrite_path(db: &Path) -> PathBuf {
let mut name = db.file_name().unwrap_or_default().to_os_string();
name.push(".");
name.push(REWRITE_SUFFIX);
db.with_file_name(name)
}
pub fn remove_interrupted_rewrite(db: &Path) -> Result<bool> {
let copy = rewrite_path(db);
let mut removed = false;
let sidecars = ["-journal", "-wal", "-shm"].map(|suffix| sidecar(©, suffix));
for path in std::iter::once(copy.clone()).chain(sidecars) {
match fs::remove_file(&path) {
Ok(()) => removed = true,
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => return Err(e).with_context(|| format!("removing {}", path.display())),
}
}
Ok(removed)
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum RewriteTo<'a> {
Key(&'a StoreKey),
CurrentKey,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rewrite {
pub bytes_before: u64,
pub bytes_after: u64,
pub pause: Duration,
}
#[derive(Debug)]
pub struct NotReopened(pub anyhow::Error);
impl std::fmt::Display for NotReopened {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"the store was rewritten but did not reopen; restart to open it: {:#}",
self.0
)
}
}
impl std::error::Error for NotReopened {}
#[derive(Debug)]
pub struct ReadsHeld(pub Duration);
impl std::fmt::Display for ReadsHeld {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"a read held a store connection for over {}s; the rewrite runs again later",
self.0.as_secs()
)
}
}
impl std::error::Error for ReadsHeld {}
impl SqliteStore {
pub fn is_encrypted(&self) -> bool {
self.store_key().is_some()
}
pub(crate) fn store_key(&self) -> Option<Arc<StoreKey>> {
match &self.readers {
super::ReadBackend::Pooled(pool) => pool.key(),
super::ReadBackend::SameAsWriter => None,
}
}
pub fn rewrite_under_key(&self, to: RewriteTo<'_>) -> Result<Rewrite> {
self.rewrite_waiting(to, READERS_WAIT)
}
pub(crate) fn rewrite_waiting(
&self,
to: RewriteTo<'_>,
readers_wait: Duration,
) -> Result<Rewrite> {
let super::ReadBackend::Pooled(readers) = &self.readers else {
bail!("an in-memory store has no file to rewrite");
};
let db = self
.file_path()?
.ok_or_else(|| anyhow!("the store has no file to rewrite"))?;
let copy = rewrite_path(&db);
let mut writer = self.conn();
remove_interrupted_rewrite(&db)?;
let old_key = self.store_key();
let key = match to {
RewriteTo::Key(key) => Some(Arc::new(key.duplicate())),
RewriteTo::CurrentKey => old_key.clone(),
};
let started = Instant::now();
let bytes_before = file_len(&db)?;
let exported = export(&writer, ©, key.as_deref(), old_key.is_none())
.and_then(|()| check_copy(&writer, ©, key.as_deref()));
if let Err(e) = exported {
remove_interrupted_rewrite(&db)?;
return Err(e);
}
let Some(mut exclusive) = readers.exclusive(readers_wait) else {
remove_interrupted_rewrite(&db)?;
return Err(ReadsHeld(readers_wait).into());
};
let placeholder = pool::unusable_connection()?;
exclusive.close_all();
drop(std::mem::replace(&mut *writer, placeholder));
let swapped =
prove_alone(&db, old_key.as_deref()).and_then(|()| swap_into_place(&db, ©));
if let Err(e) = swapped {
let restored = reopen_store(&mut writer, exclusive, &db, old_key);
if let Err(cleanup) = remove_interrupted_rewrite(&db) {
tracing::warn!("cannot remove the rewrite copy: {cleanup:#}");
}
return match restored {
Ok(()) => Err(e),
Err(reopen) => Err(NotReopened(reopen.context(format!("{e:#}"))).into()),
};
}
if let Err(e) = reopen_store(&mut writer, exclusive, &db, key) {
return Err(NotReopened(e).into());
}
let pause = started.elapsed();
Ok(Rewrite {
bytes_before,
bytes_after: file_len(&db)?,
pause,
})
}
pub fn rewrite_space(&self) -> Result<VacuumSpace> {
let db = self
.file_path()?
.ok_or_else(|| anyhow!("the store has no file to rewrite"))?;
let dir = db
.parent()
.map_or_else(|| PathBuf::from("."), Path::to_path_buf);
let live = self.live_bytes()?;
Ok(VacuumSpace {
free_bytes: fs4::available_space(&dir)
.with_context(|| format!("reading free space at {}", dir.display()))?,
path: dir,
needed_bytes: live + live / 4 + SPACE_MARGIN,
})
}
pub fn write_plaintext_copy(&self, dir: &Path) -> Result<PathBuf> {
let path = dir.join(format!(
"{PLAINTEXT_COPY_PREFIX}{}",
uuid::Uuid::new_v4().simple()
));
create_private_empty(&path)?;
let copied = (|| -> Result<()> {
let conn = self.reader();
pool::require_sqlcipher(&conn)?;
conn.execute(
"ATTACH DATABASE ?1 AS plaintext KEY ''",
params![path_text(&path)?],
)?;
let sort_on_disk = SortOnDisk::begin(&conn)?;
let exported = (|| -> Result<()> {
conn.execute_batch("BEGIN;")?;
let inner = (|| -> Result<()> {
conn.query_row("SELECT sqlcipher_export('plaintext')", [], |_| Ok(()))?;
let version: i64 =
conn.query_row("PRAGMA main.user_version", [], |r| r.get(0))?;
conn.execute_batch(&format!("PRAGMA plaintext.user_version = {version};"))?;
conn.execute(
"DELETE FROM plaintext.index_meta WHERE substr(key, 1, length(?1)) = ?1",
params![super::at_rest_keys::ROW_PREFIX],
)?;
Ok(())
})();
let end = if inner.is_ok() {
"COMMIT;"
} else {
"ROLLBACK;"
};
inner.and(conn.execute_batch(end).map_err(Into::into))
})();
drop(sort_on_disk);
let detached = conn.execute_batch("DETACH DATABASE plaintext;");
exported.and(detached.map_err(Into::into))
})();
match copied {
Ok(()) => Ok(path),
Err(e) => match fs::remove_file(&path) {
Ok(()) => Err(e),
Err(cleanup) => Err(e.context(format!(
"the partial copy {} is left: {cleanup}",
path.display()
))),
},
}
}
pub(crate) fn file_path(&self) -> Result<Option<PathBuf>> {
Ok(match &self.readers {
super::ReadBackend::Pooled(readers) => Some(readers.path().to_path_buf()),
super::ReadBackend::SameAsWriter => None,
})
}
}
pub fn key_opens_file(db: &Path, key: &StoreKey) -> bool {
Connection::open(db).is_ok_and(|conn| pool::apply_key(&conn, key, db).is_ok())
}
pub fn remove_plaintext_copies(dir: &Path, max_age: Option<Duration>) -> Result<usize> {
let entries = match fs::read_dir(dir) {
Ok(entries) => entries,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(0),
Err(e) => return Err(e).with_context(|| format!("reading {}", dir.display())),
};
let mut removed = 0;
for entry in entries {
let entry = entry?;
let name = entry.file_name();
if !name
.to_str()
.is_some_and(|n| n.starts_with(PLAINTEXT_COPY_PREFIX))
{
continue;
}
let old_enough = match max_age {
None => true,
Some(age) => entry
.metadata()?
.modified()?
.elapsed()
.is_ok_and(|elapsed| elapsed >= age),
};
if old_enough {
fs::remove_file(entry.path())
.with_context(|| format!("removing {}", entry.path().display()))?;
removed += 1;
}
}
Ok(removed)
}
fn create_private_empty(path: &Path) -> Result<()> {
let mut options = fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, crate::private_files::PRIVATE_FILE_MODE);
options
.open(path)
.map(drop)
.with_context(|| format!("creating {}", path.display()))
}
fn export(
writer: &Connection,
copy: &Path,
key: Option<&StoreKey>,
source_plaintext: bool,
) -> Result<()> {
pool::require_sqlcipher(writer)?;
let literal = key.map(StoreKey::sql_literal);
let key_text = literal.as_ref().map_or("", |l| l.as_str());
writer
.execute(
"ATTACH DATABASE ?1 AS rewrite KEY ?2",
params![path_text(copy)?, key_text],
)
.context("attaching the rewrite copy")?;
let exported = (|| -> Result<()> {
let sort_on_disk = source_plaintext
.then(|| SortOnDisk::begin(writer))
.transpose()?;
let copied = writer
.query_row("SELECT sqlcipher_export('rewrite')", [], |_| Ok(()))
.context("exporting the store into the copy");
drop(sort_on_disk);
copied?;
let version: i64 = writer.query_row("PRAGMA main.user_version", [], |r| r.get(0))?;
writer.execute_batch(&format!("PRAGMA rewrite.user_version = {version};"))?;
Ok(())
})();
let detached = writer
.execute_batch("DETACH DATABASE rewrite;")
.context("detaching the rewrite copy");
exported.and(detached)
}
struct SortOnDisk<'a>(&'a Connection);
impl<'a> SortOnDisk<'a> {
fn begin(conn: &'a Connection) -> Result<Self> {
conn.execute_batch("PRAGMA temp_store = FILE;")?;
Ok(Self(conn))
}
}
impl Drop for SortOnDisk<'_> {
fn drop(&mut self) {
if let Err(e) = self.0.execute_batch("PRAGMA temp_store = DEFAULT;") {
tracing::warn!("cannot reset temp_store after a rewrite: {e}");
}
}
}
fn check_copy(writer: &Connection, copy: &Path, key: Option<&StoreKey>) -> Result<()> {
let conn = Connection::open(copy)
.with_context(|| format!("opening the rewrite copy {}", copy.display()))?;
if let Some(key) = key {
pool::apply_key(&conn, key, copy)?;
let mut check = conn.prepare("PRAGMA cipher_integrity_check")?;
let failures: Vec<String> = check
.query_map([], |r| r.get(0))?
.collect::<rusqlite::Result<_>>()?;
ensure!(
failures.is_empty(),
"the rewrite copy failed its integrity check: {}",
failures.join("; ")
);
}
let quick: String = conn.query_row("PRAGMA quick_check", [], |r| r.get(0))?;
ensure!(
quick == "ok",
"the rewrite copy failed its quick check: {quick}"
);
let mut tables = writer.prepare(
"SELECT name FROM main.sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' \
ORDER BY name",
)?;
let names: Vec<String> = tables
.query_map([], |r| r.get(0))?
.collect::<rusqlite::Result<_>>()?;
for name in names {
let count = |c: &Connection| -> Result<i64> {
let sql = format!("SELECT count(*) FROM \"{}\"", name.replace('"', "\"\""));
c.query_row(&sql, [], |r| r.get(0))
.with_context(|| format!("counting {name}"))
};
let (live, copied) = (count(writer)?, count(&conn)?);
ensure!(
live == copied,
"the rewrite copy holds {copied} rows of {name}, the store {live}"
);
}
let versions = |c: &Connection| -> Result<i64> {
Ok(c.query_row("PRAGMA user_version", [], |r| r.get(0))?)
};
ensure!(
versions(writer)? == versions(&conn)?,
"the rewrite copy lost the schema version"
);
Ok(())
}
fn swap_into_place(db: &Path, copy: &Path) -> Result<()> {
for suffix in ["-wal", "-shm"] {
let path = sidecar(db, suffix);
match fs::remove_file(&path) {
Ok(()) => {}
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => return Err(e).with_context(|| format!("removing {}", path.display())),
}
}
fs::File::open(copy)
.and_then(|f| f.sync_all())
.with_context(|| format!("syncing {}", copy.display()))?;
fs::rename(copy, db)
.with_context(|| format!("renaming {} over {}", copy.display(), db.display()))?;
let dir = db.parent().unwrap_or(Path::new("."));
if let Err(e) = fs::File::open(dir).and_then(|d| d.sync_all()) {
tracing::warn!(
"the store was swapped, but syncing {} failed: {e}",
dir.display()
);
}
Ok(())
}
fn prove_alone(db: &Path, key: Option<&StoreKey>) -> Result<()> {
let conn = Connection::open(db).with_context(|| format!("opening {}", db.display()))?;
conn.busy_timeout(PROBE_BUSY)?;
if let Some(key) = key {
pool::apply_key(&conn, key, db)?;
}
let mode: String = conn
.query_row("PRAGMA journal_mode = DELETE", [], |r| r.get(0))
.map_err(|e| anyhow!("another connection holds the store open: {e}"))?;
ensure!(
mode.eq_ignore_ascii_case("delete"),
"another connection holds the store open, so its WAL cannot be cleared"
);
Ok(())
}
#[cfg(test)]
thread_local! {
pub(crate) static FAIL_REOPEN: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
fn reopen_store(
writer: &mut Connection,
exclusive: pool::Exclusive<'_>,
db: &Path,
key: Option<Arc<StoreKey>>,
) -> Result<()> {
#[cfg(test)]
if FAIL_REOPEN.get() {
return exclusive.fail_reads(anyhow!("the test failed the reopen"));
}
match open_connection(db, key.as_deref()) {
Ok(conn) => {
*writer = conn;
exclusive.refill(key)
}
Err(e) => exclusive.fail_reads(e),
}
}
fn sidecar(path: &Path, suffix: &str) -> PathBuf {
let mut name = path.as_os_str().to_os_string();
name.push(suffix);
PathBuf::from(name)
}
fn file_len(path: &Path) -> Result<u64> {
Ok(fs::metadata(path)
.with_context(|| format!("reading {}", path.display()))?
.len())
}
fn path_text(path: &Path) -> Result<&str> {
path.to_str()
.ok_or_else(|| anyhow!("{} is not valid UTF-8", path.display()))
}
#[cfg(all(test, feature = "encryption"))]
mod tests {
use std::sync::mpsc;
use std::thread;
use super::*;
use crate::keys::KeyGuard;
use crate::schema::{Memory, MemoryType};
use crate::sqlite_store::ReadDeadline;
use crate::store::Store;
fn key(byte: u8) -> StoreKey {
StoreKey::from_bytes(KeyGuard::new([byte; 32]))
}
fn remember(store: &SqliteStore, n: usize) {
for i in 0..n {
store
.store_memory(&Memory::new(
format!("row {i}"),
MemoryType::Semantic,
"test".to_string(),
String::new(),
))
.unwrap();
}
}
fn hold_a_spare<'scope>(
store: &'scope SqliteStore,
scope: &'scope thread::Scope<'scope, '_>,
held: mpsc::Sender<()>,
release: mpsc::Receiver<()>,
) {
scope.spawn(move || {
let pooled = store.reader();
let _bound = ReadDeadline::set(Instant::now() + Duration::from_millis(10));
let spare = store.reader();
held.send(()).unwrap();
release
.recv_timeout(Duration::from_secs(20))
.expect("the test releases the reads");
drop(spare);
drop(pooled);
});
}
fn pool_of_one(dir: &Path) -> SqliteStore {
SqliteStore::open_with_pool_size(&dir.join("brain.db"), "m".into(), 1).unwrap()
}
#[test]
fn the_swap_waits_for_a_spare_read_and_the_new_wal_survives_it() {
let dir = tempfile::tempdir().unwrap();
let store = pool_of_one(dir.path());
remember(&store, 3);
thread::scope(|scope| {
let (held, ready) = mpsc::channel();
let (release, released) = mpsc::channel();
hold_a_spare(&store, scope, held, released);
ready.recv_timeout(Duration::from_secs(10)).unwrap();
let releaser = scope.spawn(move || {
thread::sleep(Duration::from_millis(300));
release.send(()).unwrap();
});
store
.rewrite_waiting(RewriteTo::Key(&key(1)), Duration::from_secs(10))
.unwrap();
releaser.join().unwrap();
});
remember(&store, 2);
assert!(dir.path().join("brain.db-wal").exists());
drop(store);
let reopened =
SqliteStore::open_with_key(&dir.path().join("brain.db"), "m".into(), &key(1)).unwrap();
assert_eq!(reopened.memory_count().unwrap(), 5);
}
#[test]
fn a_spare_read_held_past_the_wait_stops_the_rewrite_and_leaves_the_store_live() {
let dir = tempfile::tempdir().unwrap();
let store = pool_of_one(dir.path());
remember(&store, 3);
let result = thread::scope(|scope| {
let (held, ready) = mpsc::channel();
let (release, released) = mpsc::channel();
hold_a_spare(&store, scope, held, released);
ready.recv_timeout(Duration::from_secs(10)).unwrap();
let result = store.rewrite_waiting(RewriteTo::Key(&key(2)), Duration::from_millis(200));
release.send(()).unwrap();
result
});
let err = format!("{:#}", result.unwrap_err());
assert!(err.contains("held a store connection"), "{err}");
assert!(!store.is_encrypted());
assert!(!rewrite_path(&dir.path().join("brain.db")).exists());
remember(&store, 1);
assert_eq!(store.memory_count().unwrap(), 4);
}
#[test]
fn reads_and_writes_fail_instead_of_waiting_when_the_store_cannot_reopen() {
let dir = tempfile::tempdir().unwrap();
let store = std::sync::Arc::new(pool_of_one(dir.path()));
remember(&store, 2);
FAIL_REOPEN.set(true);
let err = store
.rewrite_waiting(RewriteTo::Key(&key(3)), Duration::from_secs(5))
.unwrap_err();
FAIL_REOPEN.set(false);
assert!(err.downcast_ref::<NotReopened>().is_some(), "{err:#}");
let (done, read) = mpsc::channel();
let reader = store.clone();
thread::spawn(move || done.send(reader.memory_count().is_err()).unwrap());
assert!(
read.recv_timeout(Duration::from_secs(10))
.expect("the read returns instead of waiting on an empty pool"),
"a read on an unopened store fails"
);
assert!(
store
.store_memory(&Memory::new(
"after".to_string(),
MemoryType::Semantic,
"test".to_string(),
String::new(),
))
.is_err()
);
}
}