Key files include timestamp in name. (#1700)

* Key files include timestamp in name.

Introduce timestamp into new key files; keep filename around, so
that we don't accidentally duplicate keys.

* Remove unnecessary clone

* Fix test code.

* Remove log module from ethstore
This commit is contained in:
Gav Wood
2016-07-25 10:45:45 +02:00
committed by GitHub
parent f048839a4b
commit 435ba186f8
11 changed files with 55 additions and 26 deletions

View File

@@ -17,6 +17,7 @@
use std::{fs, ffi, io};
use std::path::{PathBuf, Path};
use std::collections::HashMap;
use time;
use ethkey::Address;
use {libc, json, SafeAccount, Error};
use super::KeyDirectory;
@@ -73,7 +74,7 @@ impl DiskDirectory {
let accounts = files.into_iter()
.map(json::KeyFile::load)
.zip(paths.into_iter())
.filter_map(|(file, path)| file.ok().map(|file| (path, SafeAccount::from(file))))
.filter_map(|(file, path)| file.ok().map(|file| (path.clone(), SafeAccount::from_file(file, path))))
.collect();
Ok(accounts)
@@ -89,24 +90,32 @@ impl KeyDirectory for DiskDirectory {
Ok(accounts)
}
fn insert(&self, account: SafeAccount) -> Result<(), Error> {
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
// transform account into key file
let keyfile: json::KeyFile = account.into();
let keyfile: json::KeyFile = account.clone().into();
// build file path
let mut keyfile_path = self.path.clone();
keyfile_path.push(format!("{}", keyfile.id));
let mut account = account;
account.path = account.path.or_else(|| {
let mut keyfile_path = self.path.clone();
let timestamp = time::strftime("%Y-%m-%d_%H:%M:%S_%Z", &time::now()).unwrap_or("???".to_owned());
keyfile_path.push(format!("{}-{}.json", keyfile.id, timestamp));
Some(keyfile_path)
});
// save the file
let mut file = try!(fs::File::create(&keyfile_path));
try!(keyfile.write(&mut file).map_err(|e| Error::Custom(format!("{:?}", e))));
{
// save the file
let path = account.path.as_ref().expect("build-file-path ensures is not None; qed");
let mut file = try!(fs::File::create(path));
try!(keyfile.write(&mut file).map_err(|e| Error::Custom(format!("{:?}", e))));
if let Err(_) = restrict_permissions_to_owner(&keyfile_path) {
fs::remove_file(&keyfile_path).expect("Expected to remove recently created file");
return Err(Error::Io(io::Error::last_os_error()));
if let Err(_) = restrict_permissions_to_owner(path) {
fs::remove_file(path).expect("Expected to remove recently created file");
return Err(Error::Io(io::Error::last_os_error()));
}
}
Ok(())
Ok(account)
}
fn remove(&self, address: &Address) -> Result<(), Error> {