best effort cleanup on write failure

Hard to write a test for this, since it would only happen in unusual
situations, but at least we can TRY not to leave litter lying around.

There is of course the possibility that a write fails because a file
already exists with the name foo.txt.TMP.cafef00d, and then the
subsequent unlink causes problems.  But in that case, we've already
obliterated the file anyway (or we don't have permission to unlink it)
and it's super unlikely because crypto randoms.

Review plz, @ceejbot