Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic writing to file on linux

Is there a way to dump a buffer to file atomically?

By "atomically" I mean: if for example someone terminates my application during writing, I'd like to have file in either before- or after-writing state, but not in a corrupted intermediate state.

If the answer is "no", then probably it could be done with a really small buffers? For example, can I dump 2 consequent int32_t variables with a single 8 bytes fwrite (on x64 platform), and be sure that both of those int32s are dumped, or neither of them, but not only just one of them?

like image 622
mambo_sun Avatar asked Mar 25 '15 16:03

mambo_sun


People also ask

Are file operations atomic?

Several Files methods, such as move , can perform certain operations atomically in some file systems. An atomic file operation is an operation that cannot be interrupted or "partially" performed. Either the entire operation is performed or the operation fails.

Is write to a file atomic?

write(toFile:atomically:)Writes the data object's bytes to the file specified by a given path.

Is Linux mv command Atomic?

mv is most definitely not atomic when the move that it performs is from one filesystem to another, or when a remote filesystem cannot implement the mv operation locally. In these instances mv could be said to be implemented by the equivalent of a cp followed by rm .

What is an atomic write?

For example, an atomic read / write operation. Or atomic access to a property. But what does this mean? Generally, you can summarize atomic as "one at a time". For example, when accessing or mutating a property is atomic, it means that only one read or write operation can be performed at a time.


1 Answers

I recommend writing to a temporary file and then doing a rename(2) on it.

ofstream o("file.tmp"); //Write to a temporary file
o << "my data";
o.close();

//Perform an atomic move operation... needed so readers can't open a partially written file
rename("file.tmp", "file.real");
like image 169
Gillespie Avatar answered Oct 14 '22 07:10

Gillespie