Skip to main content

khive_db/
database_owner_identity.rs

1//! Durable ownership evidence captured from the database SQLite opened.
2
3#[cfg(any(unix, windows))]
4use crate::file_identity::DatabaseFileIdentity;
5
6/// The stored database UUID paired with its opened physical file identity.
7/// A copied database preserves the UUID but cannot preserve this owner pair.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct DatabaseOwnerIdentity {
10    pub(crate) durable_id: uuid::Uuid,
11    #[cfg(any(unix, windows))]
12    pub(crate) file_identity: DatabaseFileIdentity,
13}
14
15impl DatabaseOwnerIdentity {
16    /// UUID read from the pool-installed `_khive_database_identity` singleton.
17    pub fn durable_id(&self) -> uuid::Uuid {
18        self.durable_id
19    }
20
21    /// The existing physical identity validated against SQLite's opened file.
22    #[cfg(any(unix, windows))]
23    pub fn file_identity(&self) -> DatabaseFileIdentity {
24        self.file_identity
25    }
26
27    pub(crate) fn verify_owner(&self, expected: &Self) -> Result<(), DatabaseOwnerIdentityError> {
28        if self != expected {
29            return Err(DatabaseOwnerIdentityError::OwnerMismatch);
30        }
31        Ok(())
32    }
33}
34
35/// A database cannot supply the required durable ownership evidence.
36#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
37pub enum DatabaseOwnerIdentityError {
38    #[error("an in-memory database cannot own a durable filesystem root")]
39    InMemory,
40    #[error("the opened database has no installed durable identity; reopen after installation")]
41    DurableIdentityUnavailable,
42    #[error("the opened database has no validated physical file identity")]
43    PhysicalIdentityUnavailable,
44    #[error("physical database ownership identity is unsupported on this platform")]
45    UnsupportedPlatform,
46    #[error("the stored database identity or opened file identity does not match the owner")]
47    OwnerMismatch,
48}