Write to disk more atomically
We write several state files to disk, e.g., "index.end" and then "root.end". If the first write succeeds while the second one fails, our state would be wrong.
We should take a pass over how we write things to disk, to avoid inconsistencies.