ledger path reform: use Path/PathBuf instead of strings (#5344)

This commit is contained in:
Michael Vines
2019-07-30 15:53:41 -07:00
committed by GitHub
parent 294d9288d2
commit c78db6a94b
15 changed files with 76 additions and 54 deletions

View File

@ -166,7 +166,7 @@ impl GenesisBlock {
hash(&serialized.into_bytes())
}
pub fn load(ledger_path: &str) -> Result<Self, std::io::Error> {
pub fn load(ledger_path: &Path) -> Result<Self, std::io::Error> {
let file = OpenOptions::new()
.read(true)
.open(&Path::new(ledger_path).join("genesis.bin"))
@ -179,14 +179,13 @@ impl GenesisBlock {
Ok(genesis_block)
}
pub fn write(&self, ledger_path: &str) -> Result<(), std::io::Error> {
pub fn write(&self, ledger_path: &Path) -> Result<(), std::io::Error> {
let serialized = serialize(&self)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", err)))?;
let dir = Path::new(ledger_path);
std::fs::create_dir_all(&dir)?;
std::fs::create_dir_all(&ledger_path)?;
let mut file = File::create(&dir.join("genesis.bin"))?;
let mut file = File::create(&ledger_path.join("genesis.bin"))?;
file.write_all(&serialized)
}
}
@ -195,12 +194,19 @@ impl GenesisBlock {
mod tests {
use super::*;
use crate::signature::{Keypair, KeypairUtil};
use std::path::PathBuf;
fn make_tmp_path(name: &str) -> String {
fn make_tmp_path(name: &str) -> PathBuf {
let out_dir = std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
let keypair = Keypair::new();
let path = format!("{}/tmp/{}-{}", out_dir, name, keypair.pubkey());
let path = [
out_dir,
"tmp".to_string(),
format!("{}-{}", name, keypair.pubkey()),
]
.iter()
.collect();
// whack any possible collision
let _ignored = std::fs::remove_dir_all(&path);