db: add read_log_epoch method

This commit is contained in:
Vincent Breitmoser 2021-07-20 17:12:33 +02:00
parent b70b042194
commit 17e28cac5a
2 changed files with 34 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, io::{BufRead, BufReader}};
use std::convert::TryFrom;
use std::fs::{OpenOptions, File, create_dir_all, read_link, remove_file, rename, set_permissions, Permissions};
use std::io::Write;
@ -22,6 +22,8 @@ use tempfile::NamedTempFile;
use openpgp::Cert;
use openpgp_utils::POLICY;
use crate::updates::Epoch;
pub struct Filesystem {
tmp_dir: PathBuf,
@ -401,6 +403,35 @@ impl Database for Filesystem {
Ok(())
}
fn read_log_epoch(&self, epoch: Epoch) -> Result<Option<Vec<u32>>> {
if epoch >= Epoch::current().expect("before end of time") {
Err(anyhow!("Epoch must be in the past to read"))?;
}
let path = self.keys_dir_log.join(epoch.to_string());
let file = match std::fs::File::open(&path) {
Ok(file) => file,
Err(_) => return Ok(None),
};
let mut result: Vec<u32> = Vec::new();
for (i, line) in BufReader::new(file).lines().enumerate() {
let line = line?;
let mut fields = line.split_whitespace();
// timestamp field - ignore
fields.next()
.ok_or_else(|| anyhow!("Malformed line {:?}:{}: {:?}", path, i + 1, line))?;
// fingerprint field
let field = fields.next()
.ok_or_else(|| anyhow!("Malformed line {:?}:{}: {:?}", path, i + 1, line))?;
// parse only the prefix
let prefix = u32::from_str_radix(&field[0..8], 16)
.map_err(|_| anyhow!("Malformed fingerprint in line {:?}:{}", path, i + 1))?;
result.push(prefix);
}
result.sort();
Ok(Some(result))
}
fn move_tmp_to_full(&self, file: NamedTempFile, fpr: &Fingerprint) -> Result<()> {
if self.dry_run {
return Ok(());

View File

@ -178,6 +178,8 @@ pub trait Database: Sync + Send {
fn by_email(&self, email: &Email) -> Option<String>;
fn by_email_wkd(&self, email: &Email) -> Option<Vec<u8>>;
fn read_log_epoch(&self, epoch: Epoch) -> Result<Option<Vec<u32>>>;
/// Complex operation that updates a Cert in the database.
///
/// 1. Merge new Cert with old, full Cert