From cf5ae81ced744b6c2d0e780e724983952ac4e305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Wed, 20 Jun 2018 10:30:41 +0100 Subject: [PATCH] ethstore: retry deduplication of wallet file names until success (#8910) --- ethstore/src/accounts_dir/disk.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ethstore/src/accounts_dir/disk.rs b/ethstore/src/accounts_dir/disk.rs index a9253cff0..6917a6278 100644 --- a/ethstore/src/accounts_dir/disk.rs +++ b/ethstore/src/accounts_dir/disk.rs @@ -182,9 +182,22 @@ impl DiskDirectory where T: KeyFileManager { // check for duplicate filename and append random suffix if dedup && keyfile_path.exists() { - let suffix = ::random::random_string(4); - filename.push_str(&format!("-{}", suffix)); - keyfile_path.set_file_name(&filename); + const MAX_RETRIES: usize = 500; + let mut retries = 0; + let mut deduped_filename = filename.clone(); + + while keyfile_path.exists() { + if retries >= MAX_RETRIES { + return Err(Error::Custom(format!("Exceeded maximum retries when deduplicating account filename."))); + } + + let suffix = ::random::random_string(4); + deduped_filename = format!("{}-{}", filename, suffix); + keyfile_path.set_file_name(&deduped_filename); + retries += 1; + } + + filename = deduped_filename; } // update account filename