Reputation: 4877
I am writing a program to change a file in place. The semantics I want are that the old version should remain on the file system until the new version has been written. That is, the transition between from the old to the new version should happen atomically. What is the right way to do this on Linux or, preferably, any POSIX system?
Upvotes: 0
Views: 187
Reputation: 5909
I am writing a program to change a file in place
The already existing program is patch
.
Run diff -[option] old-file new-file >> name.patch
e.g. diff -Naur old-file new-file >> name.patch
... and use the patch command to edit the file, like patch -p0 < name.patch
Upvotes: 0
Reputation: 3543
The canonical way to do it atomicaly is to create a temporary file, and when you're done, you move it to overwrite the original file. Then you enter a whole other domain of problems. Have a look at this Is rename() without fsync() safe?
Upvotes: 3
Reputation: 120051
Write the new file under a different name, then call rename
.
Upvotes: 0