simplify fs db code

This commit is contained in:
Vincent Breitmoser 2019-04-16 15:51:00 +02:00
parent 47d9bf3820
commit 3b77eb6209
No known key found for this signature in database
GPG Key ID: 7BD18320DEADFA11
1 changed files with 15 additions and 24 deletions

View File

@ -114,6 +114,17 @@ impl Filesystem {
}
}
fn read_from_path(&self, path: &Path) -> Option<String> {
use std::fs;
// TODO check that we're within our bounds! never read from outside
if path.exists() {
fs::read_to_string(path).ok()
} else {
None
}
}
/// Returns the KeyID the given path is pointing to.
fn path_to_keyid(&self, path: &Path) -> Option<KeyID> {
use std::str::FromStr;
@ -611,40 +622,20 @@ impl Database for Filesystem {
// XXX: slow
fn by_fpr(&self, fpr: &Fingerprint) -> Option<String> {
let target = self.fingerprint_to_path(fpr);
File::open(target).ok().and_then(|mut fd| {
let mut buf = String::new();
if fd.read_to_string(&mut buf).is_ok() {
Some(buf)
} else {
None
}
})
let path = self.fingerprint_to_path(fpr);
self.read_from_path(&path)
}
// XXX: slow
fn by_email(&self, email: &Email) -> Option<String> {
use std::fs;
let path = self.email_to_path(&email);
if path.exists() {
fs::read_to_string(path).ok()
} else {
None
}
self.read_from_path(&path)
}
// XXX: slow
fn by_kid(&self, kid: &KeyID) -> Option<String> {
use std::fs;
let path = self.keyid_to_path(kid);
if path.exists() {
fs::read_to_string(path).ok()
} else {
None
}
self.read_from_path(&path)
}
}