szabodanika / microbin

A secure, configurable file-sharing and URL shortening web app written in Rust.
https://microbin.eu
BSD 3-Clause "New" or "Revised" License
2.71k stars 172 forks source link

Json db can lose data in the event of power failure #278

Closed dvdsk closed 2 weeks ago

dvdsk commented 4 weeks ago

Given the save_to_file method: https://github.com/szabodanika/microbin/blob/b8a0c5490d681550d982ad02d67a1aaa0897f503/src/util/db_json.rs#L17

Here File::create truncates the file before serde_json writes the pastas into it. If power fails or microbin crashes in between those calls all pasta's are lost.

Solution: open a new file for writing next to the current. Write the pasta's to it then rename the new file to become the old. The rename is atomic.