Cleanup path handling with AsRef<Path> (#3284)

* AsRef<Path>

* bump
This commit is contained in:
Antioch Peverell 2020-03-30 11:35:21 +01:00 committed by GitHub
parent 7385e8ce7f
commit e49eecae5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 26 deletions

View file

@ -38,7 +38,7 @@ use grin_store::Error::NotFoundErr;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; 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 txhashset = txhashset::TxHashSet::open(db_root.clone(), store.clone(), None)?;
let mut header_pmmr = PMMRHandle::new( let mut header_pmmr = PMMRHandle::new(
&db_root, Path::new(&db_root).join("header").join("header_head"),
"header",
"header_head",
false, false,
ProtocolVersion(1), ProtocolVersion(1),
None, None,
)?; )?;
let mut sync_pmmr = PMMRHandle::new( let mut sync_pmmr = PMMRHandle::new(
&db_root, Path::new(&db_root).join("header").join("sync_head"),
"header",
"sync_head",
false, false,
ProtocolVersion(1), ProtocolVersion(1),
None, None,

View file

@ -55,20 +55,14 @@ pub struct PMMRHandle<T: PMMRable> {
impl<T: PMMRable> PMMRHandle<T> { impl<T: PMMRable> PMMRHandle<T> {
/// Constructor to create a PMMR handle from an existing directory structure on disk. /// 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. /// Creates the backend files as necessary if they do not already exist.
pub fn new( pub fn new<P: AsRef<Path>>(
root_dir: &str, path: P,
sub_dir: &str,
file_name: &str,
prunable: bool, prunable: bool,
version: ProtocolVersion, version: ProtocolVersion,
header: Option<&BlockHeader>, header: Option<&BlockHeader>,
) -> Result<PMMRHandle<T>, Error> { ) -> Result<PMMRHandle<T>, Error> {
let path = Path::new(root_dir).join(sub_dir).join(file_name); fs::create_dir_all(&path)?;
fs::create_dir_all(path.clone())?; let backend = PMMRBackend::new(&path, prunable, version, header)?;
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)?;
let last_pos = backend.unpruned_size(); let last_pos = backend.unpruned_size();
Ok(PMMRHandle { backend, last_pos }) Ok(PMMRHandle { backend, last_pos })
} }
@ -130,18 +124,18 @@ impl TxHashSet {
header: Option<&BlockHeader>, header: Option<&BlockHeader>,
) -> Result<TxHashSet, Error> { ) -> Result<TxHashSet, Error> {
let output_pmmr_h = PMMRHandle::new( let output_pmmr_h = PMMRHandle::new(
&root_dir, Path::new(&root_dir)
TXHASHSET_SUBDIR, .join(TXHASHSET_SUBDIR)
OUTPUT_SUBDIR, .join(OUTPUT_SUBDIR),
true, true,
ProtocolVersion(1), ProtocolVersion(1),
header, header,
)?; )?;
let rproof_pmmr_h = PMMRHandle::new( let rproof_pmmr_h = PMMRHandle::new(
&root_dir, Path::new(&root_dir)
TXHASHSET_SUBDIR, .join(TXHASHSET_SUBDIR)
RANGE_PROOF_SUBDIR, .join(RANGE_PROOF_SUBDIR),
true, true,
ProtocolVersion(1), ProtocolVersion(1),
header, header,
@ -154,9 +148,9 @@ impl TxHashSet {
let versions = vec![ProtocolVersion(2), ProtocolVersion(1)]; let versions = vec![ProtocolVersion(2), ProtocolVersion(1)];
for version in versions { for version in versions {
let handle = PMMRHandle::new( let handle = PMMRHandle::new(
&root_dir, Path::new(&root_dir)
TXHASHSET_SUBDIR, .join(TXHASHSET_SUBDIR)
KERNEL_SUBDIR, .join(KERNEL_SUBDIR),
false, // not prunable false, // not prunable
version, version,
None, None,