Avoid unwrap() in dirmgr

This commit is contained in:
Nick Mathewson 2021-03-04 13:24:00 -05:00
parent 9b97197762
commit 8bbf093e34
2 changed files with 22 additions and 11 deletions

View File

@ -150,15 +150,17 @@ impl NetDirConfigBuilder {
/// to load directories
pub fn finalize(mut self) -> Result<NetDirConfig> {
if self.legacy_cache_path.is_none() {
// XXXX use dirs crate?
let mut pb: PathBuf = std::env::var_os("HOME").unwrap().into();
pb.push(".tor");
self.legacy_cache_path = Some(pb);
if let Some(home) = std::env::var_os("HOME") {
let mut pb: PathBuf = home.into();
pb.push(".tor");
self.legacy_cache_path = Some(pb);
}
};
if self.cache_path.is_none() {
return Err(Error::BadNetworkConfig("No cache path configured").into());
}
let cache_path = self
.cache_path
.ok_or(Error::BadNetworkConfig("No cache path configured"))?;
if self.network.authority.is_empty() {
return Err(Error::BadNetworkConfig("No authorities configured").into());
}
@ -168,7 +170,7 @@ impl NetDirConfigBuilder {
Ok(NetDirConfig {
legacy_cache_path: self.legacy_cache_path,
cache_path: self.cache_path.unwrap(),
cache_path,
network: self.network,
timing: self.timing,
})
@ -179,7 +181,11 @@ impl NetDirConfig {
#[cfg(feature = "legacy-storage")]
/// Read directory information from the configured storage location.
pub fn load_legacy(&self) -> Result<tor_netdir::PartialNetDir> {
let store = LegacyStore::new(self.legacy_cache_path.as_ref().unwrap().clone());
let path = self
.legacy_cache_path
.as_ref()
.ok_or(Error::BadNetworkConfig("No legacy cache path available"))?;
let store = LegacyStore::new(path.clone());
store.load_legacy(&self.authorities[..])
}

View File

@ -701,7 +701,9 @@ impl UnvalidatedDir {
let mut newcerts = Vec::new();
for cert in AuthCert::parse_multiple(&text) {
if let Ok(parsed) = cert {
let s = parsed.within(&text).unwrap();
let s = parsed
.within(&text)
.expect("Certificate was not in input as expected");
if let Ok(wellsigned) = parsed.check_signature() {
if let Ok(timely) = wellsigned.check_valid_now() {
newcerts.push((timely, s));
@ -946,7 +948,10 @@ async fn download_mds(
{
match annot {
Ok(anno) => {
let txt = anno.within(&text).unwrap().to_string(); //XXXX ugly copy
let txt = anno
.within(&text)
.expect("annotation not from within text as expected")
.to_string(); //XXXX ugly copy
let md = anno.into_microdesc();
if want.contains(md.digest()) {
my_new_mds.push((txt, md))