Don't drop temporary files all over the place

When renaming a file over another which is the same hard link, the
rename is a no-op. This left many temporary files in /var/log/hosts
because a file is attempted to be synced (and thus hard-linked between
dated and undated file names) over a couple of days. The solution to
this is how the `ln` command does it: rename, then unlink the temporary
file.

Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2021-08-10 18:09:13 +02:00 committed by asaleh
parent f8e3bdee6b
commit 6e62fcbe69

View file

@ -67,8 +67,17 @@ def link_force_atomic(src, dst):
# ignore if the file exists, just try another file name
pass
else:
# no exception: linking succeeded, rename & break out of the loop
# no exception: linking to temp file succeeded, rename it over dst
os.rename(tmpdst, dst)
# if tmpdst and dst are the same hard link, unlink tmpdst here because os.rename() would
# have been a no-op
try:
os.unlink(tmpdst)
except FileNotFoundError:
# os.rename() above wasn't a no-op, i.e. tmpdst was renamed already
pass
break