Expire old unpinned clipboard history
This commit is contained in:
@@ -20,6 +20,9 @@ Each bounded iteration must:
|
||||
refuse file logging when the state directory or existing log permissions
|
||||
cannot be restricted (24 X11-feature tests and Docker GTK/X11/package build,
|
||||
2026-09-12).
|
||||
- [x] Expire unpinned clipboard history older than the 30-day default at
|
||||
startup, while preserving pinned entries (25 X11-feature tests and Docker
|
||||
GTK/X11/package build, 2026-09-12).
|
||||
- [x] Create database, log, config, lock, and runtime directories with private permissions (mode probe and Docker build, 2026-09-10).
|
||||
- [x] Handle pre-existing overly-permissive database/log permissions safely (existing database and date-suffixed log regression tests, 17 tests and Docker build, 2026-09-10).
|
||||
- [x] Add tests/probes for file modes under permissive umasks (database regression test plus runtime probe, 2026-09-10).
|
||||
|
||||
@@ -8,6 +8,7 @@ pub struct Config {
|
||||
pub history_limit: usize,
|
||||
pub pinned_limit: usize,
|
||||
pub max_entry_bytes: usize,
|
||||
pub history_retention_days: u64,
|
||||
pub global_shortcut: String,
|
||||
pub autostart: bool,
|
||||
pub log_level: String,
|
||||
@@ -19,6 +20,7 @@ impl Default for Config {
|
||||
history_limit: 100,
|
||||
pinned_limit: 25,
|
||||
max_entry_bytes: 1_048_576,
|
||||
history_retention_days: 30,
|
||||
global_shortcut: "Super+V".into(),
|
||||
autostart: true,
|
||||
log_level: "info".into(),
|
||||
@@ -77,6 +79,9 @@ impl Config {
|
||||
if self.max_entry_bytes == 0 {
|
||||
anyhow::bail!("max_entry_bytes must be greater than zero")
|
||||
}
|
||||
if self.history_retention_days == 0 {
|
||||
anyhow::bail!("history_retention_days must be greater than zero")
|
||||
}
|
||||
if !matches!(
|
||||
self.log_level.to_ascii_lowercase().as_str(),
|
||||
"trace" | "debug" | "info" | "warn" | "error"
|
||||
@@ -96,6 +101,7 @@ mod tests {
|
||||
assert_eq!(c.history_limit, 100);
|
||||
assert_eq!(c.pinned_limit, 25);
|
||||
assert_eq!(c.max_entry_bytes, 1_048_576);
|
||||
assert_eq!(c.history_retention_days, 30);
|
||||
assert_eq!(c.global_shortcut, "Super+V");
|
||||
assert!(c.validate().is_ok());
|
||||
}
|
||||
|
||||
@@ -49,6 +49,11 @@ fn main() -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
let database = Database::open_default()?;
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs() as i64;
|
||||
database.prune_expired(now, config.history_retention_days)?;
|
||||
|
||||
if args.iter().any(|arg| arg == "--diagnostics") {
|
||||
println!("Version: {}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
@@ -137,6 +137,23 @@ impl Database {
|
||||
self.connection.execute("DELETE FROM entries WHERE pinned = 0 AND id NOT IN (SELECT id FROM entries WHERE pinned = 0 ORDER BY last_used_at DESC, id DESC LIMIT ?1)", params![limit as i64])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn prune_expired(&self, now: i64, retention_days: u64) -> Result<()> {
|
||||
let retention_seconds = retention_days
|
||||
.checked_mul(86_400)
|
||||
.context("history retention period is too large")?;
|
||||
let cutoff = now
|
||||
.checked_sub(
|
||||
i64::try_from(retention_seconds)
|
||||
.context("history retention period is too large")?,
|
||||
)
|
||||
.context("history retention cutoff is out of range")?;
|
||||
self.connection.execute(
|
||||
"DELETE FROM entries WHERE pinned = 0 AND last_used_at < ?1",
|
||||
[cutoff],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
trait OptionalRow<T> {
|
||||
@@ -232,6 +249,20 @@ mod tests {
|
||||
assert!(!entries.iter().any(|entry| entry.content == "old"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expiration_removes_old_unpinned_entries_but_keeps_pinned_entries() {
|
||||
let db = Database::open("file:clipbhoard-expiration?mode=memory&cache=shared").unwrap();
|
||||
let old = db.upsert_content("old", 1).unwrap();
|
||||
let pinned = db.upsert_content("pinned", 1).unwrap();
|
||||
db.toggle_pinned(pinned.id, 25).unwrap();
|
||||
db.upsert_content("recent", 86_400 * 30 + 2).unwrap();
|
||||
db.prune_expired(86_400 * 30 + 2, 30).unwrap();
|
||||
let entries = db.load_entries().unwrap();
|
||||
assert!(!entries.iter().any(|entry| entry.id == old.id));
|
||||
assert!(entries.iter().any(|entry| entry.id == pinned.id));
|
||||
assert!(entries.iter().any(|entry| entry.content == "recent"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_mutations_return_errors() {
|
||||
let db = Database::open("file:clipbhoard-missing?mode=memory&cache=shared").unwrap();
|
||||
|
||||
Reference in New Issue
Block a user