JustSid
JustSid

Reputation: 25328

Write multiple files atomically

Suppose I have a folder with a few files, images, texts, whatever, it only matters that there are multiple files and the folder is rather large (> 100 mb). Now I want to update five files in this folder, but I want to do this atomically, normally I would just create a temporary folder and write everything into it and if it succeeds, just replace the existing folder. But because I/O is expensive, I don't really want to go this way (resaving hundreds of files just to update five seems like a huge overhead). But how am I supposed to write these five files atomically? Note, I want the writing of all files to be atomic, not each file separately.

Upvotes: 2

Views: 936

Answers (1)

paulmelnikow
paulmelnikow

Reputation: 17218

You could adapt your original solution:

  1. Create a temporary folder full of hard links to the original files.
  2. Save the five new files into the temporary folder.
  3. Delete the original folder and move the folder of hard links in its place.

Creating a few links should be speedy, and it avoids rewriting all the files.

Upvotes: 4

Related Questions