write_main() should do a better job erroring out on write errors.
write_main() and write_ext(): even on error do not leave the temporary file behind.