1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-02-26 07:53:58 +00:00

deal with BORG_BASE_DIR

This commit is contained in:
Thomas Waldmann 2023-02-03 20:36:43 +01:00
parent d3d909ad31
commit 8379ecefaa
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
3 changed files with 39 additions and 13 deletions

View file

@ -11,7 +11,7 @@
from .checks import check_extension_modules, check_python from .checks import check_extension_modules, check_python
from .datastruct import StableDict, Buffer, EfficientCollectionQueue from .datastruct import StableDict, Buffer, EfficientCollectionQueue
from .errors import Error, ErrorWithTraceback, IntegrityError, DecompressionError from .errors import Error, ErrorWithTraceback, IntegrityError, DecompressionError
from .fs import ensure_dir, get_security_dir, get_keys_dir, get_base_dir, get_cache_dir, get_config_dir from .fs import ensure_dir, get_security_dir, get_keys_dir, get_base_dir, join_base_dir, get_cache_dir, get_config_dir
from .fs import dir_is_tagged, dir_is_cachedir, make_path_safe, scandir_inorder from .fs import dir_is_tagged, dir_is_cachedir, make_path_safe, scandir_inorder
from .fs import secure_erase, safe_unlink, dash_open, os_open, os_stat, umount from .fs import secure_erase, safe_unlink, dash_open, os_open, os_stat, umount
from .fs import O_, flags_root, flags_dir, flags_special_follow, flags_special, flags_base, flags_normal, flags_noatime from .fs import O_, flags_root, flags_dir, flags_special_follow, flags_special, flags_base, flags_normal, flags_noatime

View file

@ -42,7 +42,7 @@ def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_dea
raise raise
def get_base_dir(): def get_base_dir(legacy=True):
"""Get home directory / base directory for borg: """Get home directory / base directory for borg:
- BORG_BASE_DIR, if set - BORG_BASE_DIR, if set
@ -50,16 +50,27 @@ def get_base_dir():
- ~$USER, if USER is set - ~$USER, if USER is set
- ~ - ~
""" """
base_dir = os.environ.get("BORG_BASE_DIR") or os.environ.get("HOME") if legacy:
# os.path.expanduser() behaves differently for '~' and '~someuser' as base_dir = os.environ.get("BORG_BASE_DIR") or os.environ.get("HOME")
# parameters: when called with an explicit username, the possibly set # os.path.expanduser() behaves differently for '~' and '~someuser' as
# environment variable HOME is no longer respected. So we have to check if # parameters: when called with an explicit username, the possibly set
# it is set and only expand the user's home directory if HOME is unset. # environment variable HOME is no longer respected. So we have to check if
if not base_dir: # it is set and only expand the user's home directory if HOME is unset.
base_dir = os.path.expanduser("~%s" % os.environ.get("USER", "")) if not base_dir:
base_dir = os.path.expanduser("~%s" % os.environ.get("USER", ""))
else:
# we only care for BORG_BASE_DIR here, as it can be used to override the base dir
# and not use any more or less platform specific way to determine the base dir.
base_dir = os.environ.get("BORG_BASE_DIR")
return base_dir return base_dir
def join_base_dir(*paths, **kw):
legacy = kw.get("legacy", True)
base_dir = get_base_dir(legacy=legacy)
return None if base_dir is None else os.path.join(base_dir, *paths)
def get_keys_dir(legacy=True): def get_keys_dir(legacy=True):
"""Determine where to repository keys and cache""" """Determine where to repository keys and cache"""
keys_dir = os.environ.get("BORG_KEYS_DIR") keys_dir = os.environ.get("BORG_KEYS_DIR")
@ -87,14 +98,16 @@ def get_cache_dir(legacy=True):
if legacy: if legacy:
# Get cache home path # Get cache home path
cache_home = os.path.join(get_base_dir(), ".cache") cache_home = join_base_dir(".cache", legacy=legacy)
# Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set
if not os.environ.get("BORG_BASE_DIR"): if not os.environ.get("BORG_BASE_DIR"):
cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) cache_home = os.environ.get("XDG_CACHE_HOME", cache_home)
# Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path
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"))
else: else:
cache_dir = os.environ.get("BORG_CACHE_DIR", platformdirs.user_cache_dir("borg")) cache_dir = os.environ.get(
"BORG_CACHE_DIR", join_base_dir(".cache", legacy=legacy) or platformdirs.user_cache_dir("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)
@ -122,14 +135,16 @@ def get_config_dir(legacy=True):
# Get config home path # Get config home path
if legacy: if legacy:
config_home = os.path.join(get_base_dir(), ".config") config_home = join_base_dir(".config", legacy=legacy)
# Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set
if not os.environ.get("BORG_BASE_DIR"): if not os.environ.get("BORG_BASE_DIR"):
config_home = os.environ.get("XDG_CONFIG_HOME", config_home) config_home = os.environ.get("XDG_CONFIG_HOME", config_home)
# Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path
config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg"))
else: else:
config_dir = os.environ.get("BORG_CONFIG_DIR", platformdirs.user_config_dir("borg")) config_dir = os.environ.get(
"BORG_CONFIG_DIR", join_base_dir(".config", legacy=legacy) or platformdirs.user_config_dir("borg")
)
# Create path if it doesn't exist yet # Create path if it doesn't exist yet
ensure_dir(config_dir) ensure_dir(config_dir)

View file

@ -593,6 +593,17 @@ def test_get_base_dir(monkeypatch):
assert get_base_dir() == "/var/tmp/base" assert get_base_dir() == "/var/tmp/base"
def test_get_base_dir_compat(monkeypatch):
"""test that it works the same for legacy and for non-legacy implementation"""
monkeypatch.delenv("BORG_BASE_DIR", raising=False)
# old way: if BORG_BASE_DIR is not set, make something up with HOME/USER/~
# new way: if BORG_BASE_DIR is not set, return None and let caller deal with it.
assert get_base_dir(legacy=False) is None
# new and old way: BORG_BASE_DIR overrides all other "base path determination".
monkeypatch.setenv("BORG_BASE_DIR", "/var/tmp/base")
assert get_base_dir(legacy=False) == get_base_dir(legacy=True)
def test_get_config_dir(monkeypatch): def test_get_config_dir(monkeypatch):
"""test that get_config_dir respects environment""" """test that get_config_dir respects environment"""
monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("BORG_CONFIG_DIR", raising=False)