mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 08:49:29 +00:00
accounts/keystore: fsync temp keyfile before rename
Without this, a crash or power loss between the write and the rename can leave the final UTC--... keyfile on disk with zero bytes, which permanently loses the newly generated key. Mirrors the fix in core/rawdb for freezer index files (573d94013).
This commit is contained in:
parent
573d94013c
commit
7a3d6cf3f9
1 changed files with 12 additions and 1 deletions
|
|
@ -205,7 +205,18 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
|
|||
os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
// Persist the contents to disk before the caller renames it into place,
|
||||
// so that a crash or power loss between the write and the rename cannot
|
||||
// leave a zero-length keyfile at the final path.
|
||||
if err := f.Sync(); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue