Skip to content

Improve resilience to database write errors by first writing to a buffer #19

Description

@sseemayer

Following up from keepass-rs#355.

To prevent another issue like the one discussed above causing potential loss of data, consider the following approach.

Instead of directly saving databases to the desired path:

db.save(
    &mut std::fs::File::create(&*path)?,
    DatabaseKey::new().with_password(password.expose_secret()),
)?;

First write into a buffer and then only overwrite the destination path if database serialization was successful:

let mut buf: Vec<u8> = Vec::new();
db.save(
    &mut buf,
    DatabaseKey::new().with_password(password.expose_secret()),
)?;

let mut f = std::fs::File::create(&*path)?;
f.write_all(&buf)?;

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions