keyfork-shard: ignore duplicate certificate entries

This commit is contained in:
Ryan Heywood 2024-04-22 17:06:13 -04:00
parent 001fc0bccc
commit 94617722a0
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 13 additions and 11 deletions

View File

@ -194,33 +194,35 @@ impl<P: PromptHandler> OpenPGP<P> {
} }
impl<P: PromptHandler> OpenPGP<P> { impl<P: PromptHandler> OpenPGP<P> {
/// Read all OpenPGP certificates in a path and return a [`Vec`] of them. Certificates are read /// Read all OpenPGP certificates in a path and return a [`Vec`] of them.
/// from a file, or from files one level deep in a directory. ///
/// Certificates are read from a file, or from files one level deep in a directory.
/// Certificates with duplicated fingerprints will be discarded.
/// ///
/// # Errors /// # Errors
/// The function may return an error if it is unable to read the directory or if Sequoia is unable /// The function may return an error if it is unable to read the directory or if Sequoia is
/// to load certificates from the file. /// unable to load certificates from the file.
pub fn discover_certs(path: impl AsRef<Path>) -> Result<Vec<Cert>> { pub fn discover_certs(path: impl AsRef<Path>) -> Result<Vec<Cert>> {
let path = path.as_ref(); let path = path.as_ref();
let mut certs = HashMap::new();
if path.is_file() { if path.is_file() {
let mut vec = vec![]; for maybe_cert in CertParser::from_file(path).map_err(Error::Sequoia)? {
for cert in CertParser::from_file(path).map_err(Error::Sequoia)? { let cert = maybe_cert.map_err(Error::Sequoia)?;
vec.push(cert.map_err(Error::Sequoia)?); certs.insert(cert.fingerprint(), cert);
} }
Ok(vec)
} else { } else {
let mut vec = vec![];
for entry in path for entry in path
.read_dir() .read_dir()
.map_err(Error::Io)? .map_err(Error::Io)?
.filter_map(Result::ok) .filter_map(Result::ok)
.filter(|p| p.path().is_file()) .filter(|p| p.path().is_file())
{ {
vec.push(Cert::from_file(entry.path()).map_err(Error::Sequoia)?); let cert = Cert::from_file(entry.path()).map_err(Error::Sequoia)?;
certs.insert(cert.fingerprint(), cert);
} }
Ok(vec)
} }
Ok(certs.into_values().collect())
} }
} }