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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ reqwest = { version = "=0.13.5", default-features = false, features = [
ring = { version = "=0.17.14", default-features = false }

# Database and storage
redb = "4.2.0"
redb = "4.6.0"
regex = "1.13.1"
rs_merkle = "1.5.0"
# Exact-pinned: the one TLS implementation in the workspace (TLS 1.2 floor, 1.3
Expand Down
81 changes: 81 additions & 0 deletions daemoneye-lib/src/audit/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Read-only open of an audit ledger file written by another process.

use std::io::ErrorKind;
use std::path::Path;

use redb::{DatabaseError, ReadOnlyDatabase, StorageError};
use thiserror::Error;

/// Why an audit ledger cannot be read.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum LedgerReadError {
/// The ledger file does not exist.
#[error("audit ledger file is missing")]
Missing,
/// The OS refused read access to the ledger file.
#[error("permission denied reading the audit ledger")]
PermissionDenied,
/// A crashed writer left the file unclean and no live writer can repair it.
#[error("audit ledger needs recovery by its writer")]
NeedsRecovery,
/// Another handle holds the file in a mode that excludes this reader.
#[error("audit ledger is locked by another handle")]
Locked,
/// This platform cannot share a ledger between processes.
#[error("audit ledger sharing is unsupported on this platform")]
UnsupportedPlatform,
/// Any other storage failure; the redb error stays available as the source.
#[error("audit ledger storage error: {0}")]
Storage(#[source] Box<DatabaseError>),
}

impl From<DatabaseError> for LedgerReadError {
// redb's DatabaseError and io::ErrorKind are #[non_exhaustive], so `if` chains stand in for
// a match: the catch-all is the explicit last line, not a wildcard arm.
fn from(err: DatabaseError) -> Self {
if matches!(err, DatabaseError::DatabaseAlreadyOpen) {
return Self::Locked;
}
if matches!(err, DatabaseError::RepairAborted) {
return Self::NeedsRecovery;
}
if matches!(err, DatabaseError::Storage(StorageError::Unsupported)) {
return Self::UnsupportedPlatform;
}
if let DatabaseError::Storage(StorageError::Io(ref io)) = err {
if io.kind() == ErrorKind::NotFound {
return Self::Missing;
}
if io.kind() == ErrorKind::PermissionDenied {
return Self::PermissionDenied;
}
}
Self::Storage(Box::new(err))
}
}

/// Open the ledger at `path` read-only in redb's single-writer mode, so a writer in another
/// process can keep committing and this handle sees each commit in a new read transaction.
///
/// `cache_bytes` caps the page cache (redb's default is 1 GiB).
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub fn open_ledger_read_only(
path: &Path,
cache_bytes: usize,
) -> Result<ReadOnlyDatabase, LedgerReadError> {
redb::Builder::new()
.set_cache_size(cache_bytes)
.set_concurrency_mode(redb::ConcurrencyMode::SingleWriter)
.open_read_only(path)
.map_err(LedgerReadError::from)
}

/// Other targets have no file locks redb can share across processes; nothing is opened.
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub fn open_ledger_read_only(
_path: &Path,
_cache_bytes: usize,
) -> Result<ReadOnlyDatabase, LedgerReadError> {
Err(LedgerReadError::UnsupportedPlatform)
}
5 changes: 5 additions & 0 deletions daemoneye-lib/src/audit/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Audit ledger read access. The writer lives in another crate; this module only reads.

mod db;

pub use db::{LedgerReadError, open_ledger_read_only};
1 change: 1 addition & 0 deletions daemoneye-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//! - Cross-platform IPC communication

// Core modules (always available)
pub mod audit;
pub mod config;
pub mod crypto;
/// Fixed, non-configurable bounds for the detection rule-load pipeline.
Expand Down
Loading
Loading