Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/borg/helpers/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ def get_runtime_dir(*subdirs, create=True):

Any additional path components are appended to the runtime directory.
"""
runtime_dir = os.environ.get(
"BORG_RUNTIME_DIR", join_base_dir(".cache", "borg") or platformdirs.user_runtime_dir("borg", appauthor=False)
)
runtime_dir = os.environ.get("BORG_RUNTIME_DIR")
if runtime_dir is None:
# Only ask platformdirs if needed: it raises PermissionError if it does not find a safe runtime directory.
runtime_dir = join_base_dir(".cache", "borg") or platformdirs.user_runtime_dir("borg", appauthor=False)
if subdirs:
runtime_dir = str(Path(runtime_dir).joinpath(*subdirs))
if create:
Expand Down
40 changes: 29 additions & 11 deletions src/borg/testsuite/helpers/fs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
input_map_check_size,
)
from ...helpers.errors import Error
from ...platform import is_win32, is_darwin
from ...platform import is_win32, is_darwin, is_haiku
from .. import are_fifos_supported, are_hardlinks_supported
from .. import rejected_dotdot_paths

Expand Down Expand Up @@ -183,20 +183,38 @@ def test_get_runtime_dir(monkeypatch):
else:
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
monkeypatch.delenv("BORG_RUNTIME_DIR", raising=False)
uid = str(os.getuid())
assert get_runtime_dir(create=False) in [
os.path.join("/run/user", uid, "borg"), # Linux
os.path.join("/var/run/user", uid, "borg"), # FreeBSD/NetBSD
os.path.join("/tmp/run/user", uid, "borg"), # OpenBSD
# platformdirs falls back to a TMPDIR-based path if the platform default is not writable
os.path.join(tempfile.gettempdir(), f"runtime-{uid}", "borg"),
]
monkeypatch.setenv("XDG_RUNTIME_DIR", "/var/tmp/.cache")
assert get_runtime_dir(create=False) == os.path.join("/var/tmp/.cache", "borg")
# Haiku has no /run/user and its /tmp is writable by all users without having the sticky bit set, so
# platformdirs does not find a safe runtime directory there and raises PermissionError.
if not is_haiku:
uid = str(os.getuid())
assert get_runtime_dir(create=False) in [
os.path.join("/run/user", uid, "borg"), # Linux
os.path.join("/var/run/user", uid, "borg"), # FreeBSD/NetBSD
os.path.join("/tmp/run/user", uid, "borg"), # OpenBSD
# platformdirs falls back to a TMPDIR-based path if the platform default is not writable
os.path.join(tempfile.gettempdir(), f"runtime-{uid}", "borg"),
]
monkeypatch.setenv("XDG_RUNTIME_DIR", "/var/tmp/.cache")
assert get_runtime_dir(create=False) == os.path.join("/var/tmp/.cache", "borg")
monkeypatch.setenv("BORG_RUNTIME_DIR", "/var/tmp")
assert get_runtime_dir(create=False) == "/var/tmp"


def test_get_runtime_dir_without_platformdirs(monkeypatch, tmp_path):
"""BORG_RUNTIME_DIR and BORG_BASE_DIR work even if platformdirs does not find a safe runtime directory"""

def user_runtime_dir(*args, **kwargs):
raise PermissionError("no safe runtime directory")

monkeypatch.setattr("platformdirs.user_runtime_dir", user_runtime_dir)
monkeypatch.delenv("BORG_BASE_DIR", raising=False)
monkeypatch.setenv("BORG_RUNTIME_DIR", str(tmp_path / "runtime"))
assert get_runtime_dir(create=False) == str(tmp_path / "runtime")
monkeypatch.delenv("BORG_RUNTIME_DIR")
monkeypatch.setenv("BORG_BASE_DIR", str(tmp_path))
assert get_runtime_dir(create=False) == str(tmp_path / ".cache" / "borg")


def test_get_cache_dir_subdirs(monkeypatch, tmp_path):
"""extra path components are appended and created; the CACHEDIR.TAG stays in the cache root"""
monkeypatch.delenv("BORG_BASE_DIR", raising=False)
Expand Down
Loading