safewrite

Safewrite is a utility meant to safely write a file to disk.  It
implements proper semantics just once on behalf of programs that don't,
and/or lets them be implemented more simply.

Syntax: "safewrite targetfile command [commandargs]"

It runs the specified command as a subprocess, with its standard output
redirected into a tempfile, which will be renamed in place of the target
file if all goes well.

Checks include:
 - verify success of every system call (open, write, close)
 - write data into a temporary file
 - verify that the command exited with status 0
 - sync the file after last write
 - set tempfile permissions to match existing target's
 - use rename() to move the temporary into the official place
 - don't damage an existing target file if any problems occurred
 - try to remove the tempfile if anything bad happens

Options allow setting permissions of the new file and comparing an
existing file to replace only if changed.  See the man page in the
distribution.

There is now also a "take" command, which is shorthand for "safewrite
myfile cat myfile", but which can do multiple files at once.  This is
useful in case myfile is a symlink, hardlink, read-only file, owned by
someone else, or the like, and you want a copy you own and can edit
without causing problems for other links of the file.

NOTE

Up through v1.3, a file newly created by safewrite would have 0600
(rw-------) permissions; now such a file gets normal default permissions.
Existing files continue to have unchanged permissions.

TODO
 - trap signals: unlink tempfile and die (would signal subprocess,
 but it should get SIGPIPE when we go away)

MAYBE
 - lockfile option: other instances of the same calling script would be
 locked out to prevent unnecessary duplicate runs.
 - backup option: save old version of target file.

adb


safewrite-1.1.tar.gz2003-07-11 8974 bytes
safewrite-1.3.tar.gz2004-01-13 9036 bytes
safewrite-1.5.tar.gz2011-02-07 95507 bytes

Project Index