From e49eecae5c02ca0e72972474d3b9f43dc07e750a Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Mon, 30 Mar 2020 11:35:21 +0100 Subject: [PATCH] Cleanup path handling with AsRef (#3284) * AsRef * bump --- chain/src/chain.rs | 10 +++------- chain/src/txhashset/txhashset.rs | 32 +++++++++++++------------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index a026a6d619..cf2c59fb41 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -38,7 +38,7 @@ use grin_store::Error::NotFoundErr; use std::collections::HashMap; use std::fs::{self, File}; use std::io::Read; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use std::time::{Duration, Instant}; @@ -175,17 +175,13 @@ impl Chain { let mut txhashset = txhashset::TxHashSet::open(db_root.clone(), store.clone(), None)?; let mut header_pmmr = PMMRHandle::new( - &db_root, - "header", - "header_head", + Path::new(&db_root).join("header").join("header_head"), false, ProtocolVersion(1), None, )?; let mut sync_pmmr = PMMRHandle::new( - &db_root, - "header", - "sync_head", + Path::new(&db_root).join("header").join("sync_head"), false, ProtocolVersion(1), None, diff --git a/chain/src/txhashset/txhashset.rs b/chain/src/txhashset/txhashset.rs index 48aa0f80f9..3cf13bb5dc 100644 --- a/chain/src/txhashset/txhashset.rs +++ b/chain/src/txhashset/txhashset.rs @@ -55,20 +55,14 @@ pub struct PMMRHandle { impl PMMRHandle { /// Constructor to create a PMMR handle from an existing directory structure on disk. /// Creates the backend files as necessary if they do not already exist. - pub fn new( - root_dir: &str, - sub_dir: &str, - file_name: &str, + pub fn new>( + path: P, prunable: bool, version: ProtocolVersion, header: Option<&BlockHeader>, ) -> Result, Error> { - let path = Path::new(root_dir).join(sub_dir).join(file_name); - fs::create_dir_all(path.clone())?; - let path_str = path - .to_str() - .ok_or_else(|| ErrorKind::Other("invalid file path".to_owned()))?; - let backend = PMMRBackend::new(path_str.to_string(), prunable, version, header)?; + fs::create_dir_all(&path)?; + let backend = PMMRBackend::new(&path, prunable, version, header)?; let last_pos = backend.unpruned_size(); Ok(PMMRHandle { backend, last_pos }) } @@ -130,18 +124,18 @@ impl TxHashSet { header: Option<&BlockHeader>, ) -> Result { let output_pmmr_h = PMMRHandle::new( - &root_dir, - TXHASHSET_SUBDIR, - OUTPUT_SUBDIR, + Path::new(&root_dir) + .join(TXHASHSET_SUBDIR) + .join(OUTPUT_SUBDIR), true, ProtocolVersion(1), header, )?; let rproof_pmmr_h = PMMRHandle::new( - &root_dir, - TXHASHSET_SUBDIR, - RANGE_PROOF_SUBDIR, + Path::new(&root_dir) + .join(TXHASHSET_SUBDIR) + .join(RANGE_PROOF_SUBDIR), true, ProtocolVersion(1), header, @@ -154,9 +148,9 @@ impl TxHashSet { let versions = vec![ProtocolVersion(2), ProtocolVersion(1)]; for version in versions { let handle = PMMRHandle::new( - &root_dir, - TXHASHSET_SUBDIR, - KERNEL_SUBDIR, + Path::new(&root_dir) + .join(TXHASHSET_SUBDIR) + .join(KERNEL_SUBDIR), false, // not prunable version, None,