Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is appending to a file atomic with Windows/NTFS?

If I'm writing a simple text log file from multiple processes, can they overwrite/corrupt each other's entries?

(Basically, this question Is file append atomic in UNIX? but for Windows/NTFS.)

like image 789
Gkakk McJkakk Avatar asked Dec 08 '22 02:12

Gkakk McJkakk


1 Answers

You can get atomic append on local files. Open the file with FILE_APPEND_DATA access (Documented in WDK). When you omit FILE_WRITE_DATA access then all writes will ignore the the current file pointer and be done at the end-of file. Or you may use FILE_WRITE_DATA access and for append writes specify it in overlapped structure (Offset = FILE_WRITE_TO_END_OF_FILE and OffsetHigh = -1 Documented in WDK).

The append behavior is properly synchronized between writes via different handles. I use that regularly for logging by multiple processes. I do write BOM at every open to offset 0 and all other writes are appended. The timestamps are not a problem, they can be sorted when needed.

like image 56
Yakeen Avatar answered Dec 27 '22 23:12

Yakeen