hagrid-keyserver--hagrid/hagridctl/src/import.rs

239 lines
7.1 KiB
Rust
Raw Normal View History

2019-05-20 21:19:37 +00:00
use std::path::{Path,PathBuf};
use std::fs::File;
use std::io::Read;
2019-06-04 18:04:15 +00:00
use std::thread;
use std::cmp::min;
use std::sync::Arc;
2019-05-20 21:19:37 +00:00
extern crate failure;
use failure::Fallible as Result;
extern crate tempfile;
extern crate sequoia_openpgp as openpgp;
use openpgp::Packet;
use openpgp::parse::{PacketParser, PacketParserResult, Parse};
extern crate hagrid_database as database;
use database::{Database, KeyDatabase, ImportResult};
2019-06-04 18:04:15 +00:00
use indicatif::{MultiProgress,ProgressBar,ProgressStyle};
2019-05-20 21:19:37 +00:00
use HagridConfig;
2019-06-04 18:04:15 +00:00
// parsing TPKs takes time, so we benefit from some parallelism. however, the
// database is locked during the entire merge operation, so we get diminishing
// returns after the first few threads.
const NUM_THREADS_MAX: usize = 3;
pub fn do_import(config: &HagridConfig, dry_run: bool, input_files: Vec<PathBuf>) -> Result<()> {
2019-06-04 18:04:15 +00:00
let num_threads = min(NUM_THREADS_MAX, input_files.len());
let input_file_chunks = setup_chunks(input_files, num_threads);
let multi_progress = Arc::new(MultiProgress::new());
let progress_bar = multi_progress.add(ProgressBar::new(0));
2019-06-04 18:04:15 +00:00
let threads: Vec<_> = input_file_chunks
.into_iter()
.map(|input_file_chunk| {
2019-06-04 18:04:15 +00:00
let config = config.clone();
let multi_progress = multi_progress.clone();
2019-06-04 18:04:15 +00:00
thread::spawn(move || {
import_from_files(
&config, dry_run, input_file_chunk, multi_progress).unwrap();
2019-06-04 18:04:15 +00:00
})
})
.collect();
eprintln!("Importing in {} threads", num_threads);
thread::spawn(move || multi_progress.join().unwrap());
2019-06-04 18:04:15 +00:00
threads.into_iter().for_each(|t| t.join().unwrap());
progress_bar.finish();
2019-05-20 21:19:37 +00:00
Ok(())
}
fn setup_chunks(
2019-06-04 18:04:15 +00:00
mut input_files: Vec<PathBuf>,
num_threads: usize,
) -> Vec<Vec<PathBuf>> {
2019-06-04 18:04:15 +00:00
let chunk_size = (input_files.len() + (num_threads - 1)) / num_threads;
(0..num_threads)
2019-06-04 18:04:15 +00:00
.map(|_| {
let len = input_files.len();
input_files.drain(0..min(chunk_size,len)).collect()
})
.collect()
2019-06-04 18:04:15 +00:00
}
struct ImportStats<'a> {
progress: &'a ProgressBar,
filename: String,
2019-05-20 21:19:37 +00:00
count_total: u64,
count_err: u64,
count_new: u64,
count_updated: u64,
count_unchanged: u64,
}
2019-06-04 18:04:15 +00:00
impl <'a> ImportStats<'a> {
fn new(progress: &'a ProgressBar, filename: String) -> Self {
2019-05-20 21:19:37 +00:00
ImportStats {
progress,
2019-06-04 18:04:15 +00:00
filename,
2019-05-20 21:19:37 +00:00
count_total: 0,
count_err: 0,
count_new: 0,
count_updated: 0,
count_unchanged: 0,
}
}
fn update(&mut self, result: Result<ImportResult>) {
// If a new TPK starts, parse and import.
self.count_total += 1;
match result {
Err(_) => self.count_err += 1,
2019-05-20 21:19:37 +00:00
Ok(ImportResult::New(_)) => self.count_new += 1,
Ok(ImportResult::Updated(_)) => self.count_updated += 1,
Ok(ImportResult::Unchanged(_)) => self.count_unchanged += 1,
}
self.progress_update();
}
fn progress_update(&self) {
if (self.count_total % 10) != 0 {
return;
}
self.progress.set_message(&format!(
"{}, imported {:5} keys, {:5} New {:5} Updated {:5} Unchanged {:5} Errors",
2019-06-04 18:04:15 +00:00
&self.filename, self.count_total, self.count_new, self.count_updated, self.count_unchanged, self.count_err));
2019-05-20 21:19:37 +00:00
}
}
fn import_from_files(
config: &HagridConfig,
dry_run: bool,
input_files: Vec<PathBuf>,
multi_progress: Arc<MultiProgress>,
) -> Result<()> {
let db = KeyDatabase::new_internal(
2019-06-04 18:04:15 +00:00
config.keys_internal_dir.as_ref().unwrap(),
config.keys_external_dir.as_ref().unwrap(),
config.tmp_dir.as_ref().unwrap(),
dry_run,
2019-06-04 18:04:15 +00:00
)?;
for input_file in input_files {
import_from_file(&db, &input_file, &multi_progress)?;
}
Ok(())
}
fn import_from_file(db: &KeyDatabase, input: &Path, multi_progress: &MultiProgress) -> Result<()> {
let input_file = File::open(input)?;
let bytes_total = input_file.metadata()?.len();
let progress_bar = multi_progress.add(ProgressBar::new(bytes_total));
2019-06-04 18:04:15 +00:00
progress_bar
.set_style(ProgressStyle::default_bar()
.template("[{elapsed_precise}] {bar:40.cyan/blue} {msg}")
.progress_chars("##-"));
progress_bar.set_message("Starting…");
let input_reader = &mut progress_bar.wrap_read(input_file);
let filename = input.file_name().unwrap().to_string_lossy().to_string();
let mut stats = ImportStats::new(&progress_bar, filename);
2019-05-20 21:19:37 +00:00
read_file_to_tpks(input_reader, &mut |acc| {
let result = import_key(&db, acc);
if let Err(ref e) = result {
progress_bar.println(e.to_string());
}
2019-05-20 21:19:37 +00:00
stats.update(result);
})?;
progress_bar.finish_and_clear();
Ok(())
2019-05-20 21:19:37 +00:00
}
fn read_file_to_tpks(
reader: impl Read,
callback: &mut impl FnMut(Vec<Packet>) -> ()
) -> Result<()> {
let mut ppr = PacketParser::from_reader(reader)?;
let mut acc = Vec::new();
// Iterate over all packets.
while let PacketParserResult::Some(pp) = ppr {
// Get the packet and advance the parser.
let (packet, tmp) = pp.next()?;
ppr = tmp;
if !acc.is_empty() {
if let Packet::PublicKey(_) | Packet::SecretKey(_) = packet {
callback(acc);
acc = vec!();
}
}
acc.push(packet);
}
Ok(())
}
fn import_key(db: &KeyDatabase, packets: Vec<Packet>) -> Result<ImportResult> {
let packet_pile = openpgp::PacketPile::from(packets);
openpgp::TPK::from_packet_pile(packet_pile)
.and_then(|tpk| {
db.merge(tpk)
})
}
/*
#[cfg(test)]
mod import_tests {
use std::fs::File;
use tempfile::tempdir;
use openpgp::serialize::Serialize;
use super::*;
#[test]
fn import() {
let root = tempdir().unwrap();
let db = KeyDatabase::new_from_base(root.path().to_path_buf()).unwrap();
// Generate a key and import it.
let (tpk, _) = openpgp::tpk::TPKBuilder::autocrypt(
None, Some("foo@invalid.example.com".into()))
.generate().unwrap();
let import_me = root.path().join("import-me");
tpk.serialize(&mut File::create(&import_me).unwrap()).unwrap();
do_import(root.path().to_path_buf(), vec![import_me]).unwrap();
let check = |query: &str| {
let tpk_ = db.lookup(&query.parse().unwrap()).unwrap().unwrap();
assert_eq!(tpk.fingerprint(), tpk_.fingerprint());
assert_eq!(tpk.subkeys().map(|skb| skb.subkey().fingerprint())
.collect::<Vec<_>>(),
tpk_.subkeys().map(|skb| skb.subkey().fingerprint())
.collect::<Vec<_>>());
assert_eq!(tpk_.userids().count(), 0);
};
check(&format!("{}", tpk.primary().fingerprint()));
check(&format!("{}", tpk.primary().fingerprint().to_keyid()));
check(&format!("{}", tpk.subkeys().nth(0).unwrap().subkey()
.fingerprint()));
check(&format!("{}", tpk.subkeys().nth(0).unwrap().subkey()
.fingerprint().to_keyid()));
}
}
*/