1

I am given a binary file (consider it large) and a several binary blobs, which I should insert/replace somewhere in the middle of the file (offsets are known).

The same time user may gain access to the file, thus I must have "all of nothing", either user have an old version of the file if she opens it before I have updated everything, or she has a new version if I succeeded.

I am interesting in solutions for Linux, Windows and OS X. Of course, implementation may be different.

O'Mutt
  • 1,557
  • 1
  • 13
  • 27
galadog
  • 960
  • 2
  • 10
  • 20
  • Your question is a little scarce in details. How are you opening the file? Are you creating an application? When the file is opened, is it always through our application? What language are you using? – Tim Lamballais Oct 01 '12 at 17:39
  • Yes, I am creating an application. I guess specific language does not matter, so let us consider c++. My application knows if somebody opens the file via inotify/WinAPI. – galadog Oct 01 '12 at 17:44
  • See: http://stackoverflow.com/questions/4662115/is-an-atomic-rename-possible-in-mac-vfs-hfs and http://stackoverflow.com/a/660679/371250 – ninjalj Oct 01 '12 at 20:30

1 Answers1

0

For Linux:

  • Do everything on a temporary file.
  • fsync() the temporary file.
  • rename() the temporary file to the real file.

This idiom is known as atomic-rename.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • This is a straight forward solution, but one needs at least twice more space for a temporary file. – galadog Oct 01 '12 at 21:11