Skip to main content

khive_db/backend/
claimed_file_identity.rs

1//! SQLite construction constrained by an already held physical file claim.
2use super::*;
3use crate::file_identity::DatabaseFileIdentity;
4
5impl StorageBackend {
6    /// Refuse any database other than `expected` before initializing identity or WAL.
7    /// `read_only` retains the explicit topology mode; a writable request still
8    /// honors an existing file's filesystem read-only mode.
9    ///
10    /// On Unix, the host must initialize the claimed-file observer at process
11    /// startup before any SQLite I/O, under `pool::initialize_claimed_file_observer`'s
12    /// unsafe precondition. Unsupported or uninitialized Unix hosts refuse.
13    /// Native open-time I/O is outside the pre-SQL refusal guarantee.
14    pub fn sqlite_with_claimed_file_identity(
15        path: impl AsRef<Path>,
16        max_readers: Option<usize>,
17        wal_ceiling: WalCeilingPolicy,
18        read_only: bool,
19        expected: DatabaseFileIdentity,
20    ) -> Result<Self, SqliteError> {
21        let config = PoolConfig {
22            expected_file_identity: Some(expected),
23            wal_ceiling,
24            ..PoolConfig::default()
25        };
26        if read_only {
27            Self::sqlite_read_only_with_pool_config(path, config, max_readers)
28        } else {
29            Self::sqlite_with_pool_config(path, config, max_readers)
30        }
31    }
32
33    /// Writable [`Self::sqlite_with_claimed_file_identity`] that carries the
34    /// host's captured disk policy and shared volume-lock directory.
35    pub fn sqlite_with_claimed_file_identity_and_policies(
36        path: impl AsRef<Path>,
37        max_readers: Option<usize>,
38        wal_ceiling: WalCeilingPolicy,
39        expected: DatabaseFileIdentity,
40        disk_guard_config: crate::EffectiveDiskGuardConfig,
41        volume_lock_dir: std::path::PathBuf,
42    ) -> Result<Self, SqliteError> {
43        let config = PoolConfig {
44            expected_file_identity: Some(expected),
45            wal_ceiling,
46            disk_guard_config: Some(disk_guard_config),
47            volume_lock_dir: Some(volume_lock_dir),
48            ..PoolConfig::default()
49        };
50        Self::sqlite_with_pool_config(path, config, max_readers)
51    }
52}