Skip to content

Commit

Permalink
Cleanup path handling with AsRef<Path> (#3284)
Browse files Browse the repository at this point in the history
* AsRef<Path>

* bump
  • Loading branch information
antiochp authored Mar 30, 2020
1 parent 7385e8c commit e49eeca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
10 changes: 3 additions & 7 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 13 additions & 19 deletions chain/src/txhashset/txhashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,14 @@ pub struct PMMRHandle<T: PMMRable> {
impl<T: PMMRable> PMMRHandle<T> {
/// 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<P: AsRef<Path>>(
path: P,
prunable: bool,
version: ProtocolVersion,
header: Option<&BlockHeader>,
) -> Result<PMMRHandle<T>, 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 })
}
Expand Down Expand Up @@ -130,18 +124,18 @@ impl TxHashSet {
header: Option<&BlockHeader>,
) -> Result<TxHashSet, Error> {
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,
Expand All @@ -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,
Expand Down

0 comments on commit e49eeca

Please sign in to comment.