wallet: the file-name picker consults the disk, not only the catalogue
next_wallet_filename returned <default>-<n> for the first n the SQLCipher catalogue did not hold, and never looked at the wallet directory. monero writes two files per wallet (<name> and <name>.keys) and refuses a create that would collide with either, so a file whose catalogue row is gone - Store::delete_wallet keeps the file, and a create whose record_wallet failed leaves one - meant every attempt proposed the same taken name and the daemon answered -21 Cannot create wallet. Already exists. for ever. Add wallet_name_taken (src/wallet_child.rs): it lists the wallet dir and answers whether <name> or <name>.keys is already there. next_wallet_filename now needs the candidate free in both places, and Node::wallet_dir() is the single source of $ONIONWIRE_HOME/wallet, used by the child spawn too. Fail closed: an unreadable (or missing) wallet dir is an error, not a guess. Reads only - nothing here deletes a file the catalogue does not hold, which may be the only copy of a funded wallet.
This commit is contained in:
parent
ed29131a67
commit
59175f6931
3 changed files with 196 additions and 9 deletions
|
|
@ -1161,6 +1161,33 @@ fn create_dir_700(path: &Path) -> std::io::Result<()> {
|
|||
fs::set_permissions(path, perms)
|
||||
}
|
||||
|
||||
/// Is `name` already a wallet file in `wallet_dir`?
|
||||
///
|
||||
/// monero writes two files per wallet — `<name>` and `<name>.keys` — and either
|
||||
/// one makes `monero-wallet-rpc` refuse a create with `-21 Cannot create wallet.
|
||||
/// Already exists.`, so both count as taken. The catalogue alone is not enough:
|
||||
/// `Store::delete_wallet` drops the row and keeps the file, and a create whose
|
||||
/// row was never written leaves a file too, so a picker that only consults the
|
||||
/// catalogue proposes a colliding name for ever.
|
||||
///
|
||||
/// Fail closed: a directory that cannot be listed is an error, never "every
|
||||
/// name is free". Guessing is how the collision comes back.
|
||||
///
|
||||
/// Reads only: nothing here deletes anything — a file the catalogue does not
|
||||
/// know about may be the only copy of a funded wallet.
|
||||
pub fn wallet_name_taken(wallet_dir: &Path, name: &str) -> Result<bool> {
|
||||
let keys = format!("{name}.keys");
|
||||
let unreadable =
|
||||
|e: std::io::Error| ChildError(format!("wallet dir {}: {e}", wallet_dir.display()));
|
||||
for entry in fs::read_dir(wallet_dir).map_err(unreadable)? {
|
||||
let found = entry.map_err(unreadable)?.file_name();
|
||||
if found == std::ffi::OsStr::new(name) || found == std::ffi::OsStr::new(keys.as_str()) {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn ensure_parent(path: &Path) -> Result<()> {
|
||||
if let Some(parent) = path.parent()
|
||||
&& !parent.as_os_str().is_empty()
|
||||
|
|
|
|||
Loading…
Reference in a new issue