atomically create the CACHE_TAG file, see #6028

This commit is contained in:
Thomas Waldmann 2021-11-16 16:05:56 +01:00
parent ef6a65214e
commit 9ef0413aeb
1 changed files with 9 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import textwrap
from .errors import Error from .errors import Error
from .process import prepare_subprocess_env from .process import prepare_subprocess_env
from ..platform import SaveFile
from ..platformflags import is_win32 from ..platformflags import is_win32
from ..constants import * # NOQA from ..constants import * # NOQA
@ -92,15 +93,14 @@ def get_cache_dir():
cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(cache_home, 'borg')) cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(cache_home, 'borg'))
# Create path if it doesn't exist yet # Create path if it doesn't exist yet
ensure_dir(cache_dir) ensure_dir(cache_dir)
cache_fn = os.path.join(cache_dir, CACHE_TAG_NAME) cache_tag_fn = os.path.join(cache_dir, CACHE_TAG_NAME)
if not os.path.exists(cache_fn): cache_tag_contents = CACHE_TAG_CONTENTS + textwrap.dedent("""
with open(cache_fn, 'wb') as fd: # This file is a cache directory tag created by Borg.
fd.write(CACHE_TAG_CONTENTS) # For information about cache directory tags, see:
fd.write(textwrap.dedent(""" # http://www.bford.info/cachedir/spec.html
# This file is a cache directory tag created by Borg. """).encode('ascii')
# For information about cache directory tags, see: with SaveFile(cache_tag_fn, binary=True) as fd:
# http://www.bford.info/cachedir/spec.html fd.write(cache_tag_contents)
""").encode('ascii'))
return cache_dir return cache_dir