db: correctly abstract NamedTempFile as type trait

This commit is contained in:
Vincent Breitmoser 2022-01-04 13:28:26 +01:00
parent 6782c57520
commit 77407e03cc
2 changed files with 11 additions and 11 deletions

View File

@ -348,7 +348,7 @@ impl Filesystem {
// Like `symlink`, but instead of failing if `symlink_name` already
// exists, atomically update `symlink_name` to have `symlink_content`.
fn symlink(symlink_content: &Path, symlink_name: &Path) -> Result<()> {
use std::os::unix::fs::{symlink};
use std::os::unix::fs::symlink;
let symlink_dir = ensure_parent(symlink_name)?.parent().unwrap();
let tmp_dir = tempfile::Builder::new()
@ -374,12 +374,13 @@ fn symlink_unlink_with_check(link: &Path, expected: &Path) -> Result<()> {
impl Database for Filesystem {
type MutexGuard = FlockMutexGuard;
type TempCert = NamedTempFile;
fn lock(&self) -> Result<Self::MutexGuard> {
FlockMutexGuard::lock(&self.keys_internal_dir)
}
fn write_to_temp(&self, content: &[u8]) -> Result<NamedTempFile> {
fn write_to_temp(&self, content: &[u8]) -> Result<Self::TempCert> {
let mut tempfile = tempfile::Builder::new()
.prefix("key")
.rand_bytes(16)
@ -401,7 +402,7 @@ impl Database for Filesystem {
Ok(())
}
fn move_tmp_to_full(&self, file: NamedTempFile, fpr: &Fingerprint) -> Result<()> {
fn move_tmp_to_full(&self, file: Self::TempCert, fpr: &Fingerprint) -> Result<()> {
if self.dry_run {
return Ok(());
}
@ -411,7 +412,7 @@ impl Database for Filesystem {
Ok(())
}
fn move_tmp_to_published(&self, file: NamedTempFile, fpr: &Fingerprint) -> Result<()> {
fn move_tmp_to_published(&self, file: Self::TempCert, fpr: &Fingerprint) -> Result<()> {
if self.dry_run {
return Ok(());
}
@ -421,7 +422,7 @@ impl Database for Filesystem {
Ok(())
}
fn move_tmp_to_published_wkd(&self, file: Option<NamedTempFile>, fpr: &Fingerprint) -> Result<()> {
fn move_tmp_to_published_wkd(&self, file: Option<Self::TempCert>, fpr: &Fingerprint) -> Result<()> {
if self.dry_run {
return Ok(());
}

View File

@ -27,8 +27,6 @@ extern crate walkdir;
extern crate chrono;
extern crate zbase32;
use tempfile::NamedTempFile;
extern crate sequoia_openpgp as openpgp;
use openpgp::{
Cert,
@ -134,6 +132,7 @@ pub enum RegenerateResult {
pub trait Database: Sync + Send {
type MutexGuard;
type TempCert;
/// Lock the DB for a complex update.
///
@ -161,10 +160,10 @@ pub trait Database: Sync + Send {
fn by_fpr_full(&self, fpr: &Fingerprint) -> Option<String>;
fn by_primary_fpr(&self, fpr: &Fingerprint) -> Option<String>;
fn write_to_temp(&self, content: &[u8]) -> Result<NamedTempFile>;
fn move_tmp_to_full(&self, content: NamedTempFile, fpr: &Fingerprint) -> Result<()>;
fn move_tmp_to_published(&self, content: NamedTempFile, fpr: &Fingerprint) -> Result<()>;
fn move_tmp_to_published_wkd(&self, content: Option<NamedTempFile>, fpr: &Fingerprint) -> Result<()>;
fn write_to_temp(&self, content: &[u8]) -> Result<Self::TempCert>;
fn move_tmp_to_full(&self, content: Self::TempCert, fpr: &Fingerprint) -> Result<()>;
fn move_tmp_to_published(&self, content: Self::TempCert, fpr: &Fingerprint) -> Result<()>;
fn move_tmp_to_published_wkd(&self, content: Option<Self::TempCert>, fpr: &Fingerprint) -> Result<()>;
fn write_to_quarantine(&self, fpr: &Fingerprint, content: &[u8]) -> Result<()>;
fn write_log_append(&self, filename: &str, fpr_primary: &Fingerprint) -> Result<()>;