Sweep some more panics (#2848)

* purge unwraps from ethcrypto, ethstore

* sweep panics from util
This commit is contained in:
Robert Habermeier
2016-10-25 22:34:52 +02:00
committed by Gav Wood
parent d1d82e787b
commit 33748c2046
13 changed files with 48 additions and 28 deletions

View File

@@ -18,6 +18,7 @@ docopt = { version = "0.6", optional = true }
time = "0.1.34"
lazy_static = "0.2"
itertools = "0.4"
parking_lot = "0.3"
ethcrypto = { path = "../ethcrypto" }
[build-dependencies]

View File

@@ -28,7 +28,9 @@ const IGNORED_FILES: &'static [&'static str] = &["thumbs.db", "address_book.json
fn restrict_permissions_to_owner(file_path: &Path) -> Result<(), i32> {
use std::ffi;
use libc;
let cstr = ffi::CString::new(file_path.to_str().unwrap()).unwrap();
let cstr = try!(ffi::CString::new(&*file_path.to_string_lossy())
.map_err(|_| -1));
match unsafe { libc::chmod(cstr.as_ptr(), libc::S_IWUSR | libc::S_IRUSR) } {
0 => Ok(()),
x => Err(x),
@@ -63,15 +65,15 @@ impl DiskDirectory {
let paths = try!(fs::read_dir(&self.path))
.flat_map(Result::ok)
.filter(|entry| {
let metadata = entry.metadata();
let metadata = entry.metadata().ok();
let file_name = entry.file_name();
let name = file_name.to_str().unwrap();
let name = file_name.to_string_lossy();
// filter directories
metadata.is_ok() && !metadata.unwrap().is_dir() &&
metadata.map_or(false, |m| !m.is_dir()) &&
// hidden files
!name.starts_with(".") &&
// other ignored files
!IGNORED_FILES.contains(&name)
!IGNORED_FILES.contains(&&*name)
})
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>();

View File

@@ -15,7 +15,6 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::collections::BTreeMap;
use std::sync::RwLock;
use std::mem;
use ethkey::KeyPair;
use crypto::KEY_ITERATIONS;
@@ -26,6 +25,7 @@ use account::SafeAccount;
use {Error, SecretStore};
use json;
use json::UUID;
use parking_lot::RwLock;
use presale::PresaleWallet;
use import;
@@ -56,13 +56,13 @@ impl EthStore {
let account = try!(self.dir.insert(account.clone()));
// update cache
let mut cache = self.cache.write().unwrap();
let mut cache = self.cache.write();
cache.insert(account.address.clone(), account);
Ok(())
}
fn reload_accounts(&self) -> Result<(), Error> {
let mut cache = self.cache.write().unwrap();
let mut cache = self.cache.write();
let accounts = try!(self.dir.load());
let new_accounts: BTreeMap<_, _> = accounts.into_iter().map(|account| (account.address.clone(), account)).collect();
mem::replace(&mut *cache, new_accounts);
@@ -71,13 +71,13 @@ impl EthStore {
fn get(&self, address: &Address) -> Result<SafeAccount, Error> {
{
let cache = self.cache.read().unwrap();
let cache = self.cache.read();
if let Some(account) = cache.get(address) {
return Ok(account.clone())
}
}
try!(self.reload_accounts());
let cache = self.cache.read().unwrap();
let cache = self.cache.read();
cache.get(address).cloned().ok_or(Error::InvalidAccount)
}
}
@@ -111,7 +111,7 @@ impl SecretStore for EthStore {
fn accounts(&self) -> Result<Vec<Address>, Error> {
try!(self.reload_accounts());
Ok(self.cache.read().unwrap().keys().cloned().collect())
Ok(self.cache.read().keys().cloned().collect())
}
fn change_password(&self, address: &Address, old_password: &str, new_password: &str) -> Result<(), Error> {
@@ -131,7 +131,7 @@ impl SecretStore for EthStore {
if can_remove {
try!(self.dir.remove(address));
let mut cache = self.cache.write().unwrap();
let mut cache = self.cache.write();
cache.remove(address);
Ok(())
} else {

View File

@@ -26,12 +26,15 @@ extern crate serde_json;
extern crate rustc_serialize;
extern crate crypto as rcrypto;
extern crate tiny_keccak;
#[macro_use]
extern crate lazy_static;
extern crate parking_lot;
// reexport it nicely
extern crate ethkey as _ethkey;
extern crate ethcrypto as crypto;
#[macro_use]
extern crate lazy_static;
pub mod dir;
pub mod ethkey;

View File

@@ -33,7 +33,8 @@ impl From<json::PresaleWallet> for PresaleWallet {
impl PresaleWallet {
pub fn open<P>(path: P) -> Result<Self, Error> where P: AsRef<Path> {
let file = try!(fs::File::open(path));
let presale = json::PresaleWallet::load(file).unwrap();
let presale = try!(json::PresaleWallet::load(file)
.map_err(|e| Error::InvalidKeyFile(format!("{}", e))));
Ok(PresaleWallet::from(presale))
}