[PATCH] D27295: Remove existing file in a separate thread asynchronously.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 14:34:09 PST 2016


Rui Ueyama via Phabricator <reviews at reviews.llvm.org> writes:

> ruiu added a comment.
>
> As to writing to an existing file as opposed to atomic renaming, I believe the reason why we are doing that is to not leave a corrupted file in case of crash. If you do atomic renaming, you'll get a temporary file in the worst case instead of a broken, partically-written result.
>
> However, that can be resolved by renaming an existing file different temporary name, write to the temporary file, and then rename it back. Maybe we should do that?

I think the correct way to this is

* Don't remove the existing file.
* Create a temporary file with no name using O_TMPFILE
* Use renameat to give it the correct name at the end of the link.

With this we would get the following nice properties

* Any other process always sees the old file or the new one.
* We don't delete the old file in case of failure.
* A crash or power failure will not leave a broken file around.

I am not sure if renameat actually works for this, but even a
unlink+linkat would be an improvement.

Cheers,
Rafael


More information about the llvm-commits mailing list