From d0a3b30fdf2db48fd9079b4507cd6369fc08fe2b Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 7 Dec 2021 21:47:15 +0100 Subject: [PATCH] avoid create the cache tag file on every get_cache_dir call this re-introduces a race between os.path.exists vs. SaveFile creating that file, but due to the way how SaveFile works, it still makes sure that in the end there is a good cache tag file in place. --- src/borg/helpers/fs.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index f448ba654..3bd89e2f2 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -93,14 +93,15 @@ def get_cache_dir(): # Create path if it doesn't exist yet ensure_dir(cache_dir) cache_tag_fn = os.path.join(cache_dir, CACHE_TAG_NAME) - cache_tag_contents = CACHE_TAG_CONTENTS + textwrap.dedent(""" - # This file is a cache directory tag created by Borg. - # For information about cache directory tags, see: - # http://www.bford.info/cachedir/spec.html - """).encode('ascii') - from ..platform import SaveFile - with SaveFile(cache_tag_fn, binary=True) as fd: - fd.write(cache_tag_contents) + if not os.path.exists(cache_tag_fn): + cache_tag_contents = CACHE_TAG_CONTENTS + textwrap.dedent(""" + # This file is a cache directory tag created by Borg. + # For information about cache directory tags, see: + # http://www.bford.info/cachedir/spec.html + """).encode('ascii') + from ..platform import SaveFile + with SaveFile(cache_tag_fn, binary=True) as fd: + fd.write(cache_tag_contents) return cache_dir