File::AtomicWrite Perl Module

The new Perl module File::AtomicWrite satisfies my paranoia regarding file writes,1 paranoia unfortunately beyond the atomic writes offered by the excellent IO::Atomic module, and at a smaller scale than the file handling in File::Transaction::Atomic. The module already simplifies the code in several of my scripts, and should be even more useful after I split the “create and return a filehandle to use” and “confirm and write out the real file” tasks into two distinct methods.

The unit tests were challenging and time consuming to write. They either need to write somewhere under the module build directory—while making perhaps rash assumptions about the specifics of the build environment in question—or into a system-wide /tmp directory, in which case the usual security problems with /tmp apply. Hopefully stashing the test results under a File::Temp::tempdir( CLEANUP => 1 ) directory will suffice. Writing the unit tests (in conjunction with fixing the revealed bugs) took me about as long as writing the module itself.

1 You do check that your workflow for critical system files checks that the said critical system files are not zero bytes or otherwise corrupt before making them available to production systems, right?

Technorati Tags: ,


Updates: due to excellent feedback, added flush and sync calls to help ensure the bits reach the disk, as some file systems, for performance, otherwise neglect to write data for some number of minutes. Also, due to some not-so-excellent feedback, updated the documentation to point out how rename used under the same parent directory will in most cases probably not span two different partitions. Given that common utilities such as mv(1) and rsync(1) use rename(2) heavily, I am not sure where the sky-is-falling fear of the rename call originates.