From 6e62fcbe690a52a8a4b19bad12d3a671951254bf Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Tue, 10 Aug 2021 18:09:13 +0200 Subject: [PATCH] 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 --- roles/web-data-analysis/files/sync-http-logs.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/roles/web-data-analysis/files/sync-http-logs.py b/roles/web-data-analysis/files/sync-http-logs.py index affebb1337..f72e5b3f6e 100644 --- a/roles/web-data-analysis/files/sync-http-logs.py +++ b/roles/web-data-analysis/files/sync-http-logs.py @@ -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