NTFS in Vista has transaction support. This is going to be great for me, because I’ll be able to write automated tests for code which operate on the filesystem and clean up after themselves without worrying about leaving behind detritus.

It has been mentioned that this support applies to writing a single file, not just operating on many. This reminds me of a question I haven’t had answered since I started developing for Windows:

How do I write to a file and be sure that if something goes wrong (such as the OS crashing), my changes will either be completely rolled back or completely committed?

In UNIX, I know how to be sure. I write changes to a temporary file on the same filesystem as the file I wish to change (or create). Once I’ve checked that the temporary file was written successfully (checked each write() call and ensured close() didn’t fail), I rename the temporary file over the target.

If anyone knows how to get the same guaranteed reliability in Windows, please drop me a line.

Update: Thanks to zygron on #win32, efnet, I now know about MoveFileEx and its MOVEFILE_REPLACE_EXISTING and MOVEFILE_WRITE_THROUGH flags. I can’t find any guarantees of atomicity, but these might be sufficient. I’ve written more about this here.

No comments