diff --git a/src/borg/archive.py b/src/borg/archive.py index 9156679a6..88f489ef5 100644 --- a/src/borg/archive.py +++ b/src/borg/archive.py @@ -13,6 +13,7 @@ from io import BytesIO from itertools import groupby, zip_longest from shutil import get_terminal_size +from .platformflags import is_win32, is_linux, is_freebsd, is_darwin from .logger import create_logger logger = create_logger() @@ -679,7 +680,7 @@ Utilization of max. archive size: {csize_max:.0%} uid = item.uid if uid is None else uid gid = item.gid if gid is None else gid # This code is a bit of a mess due to os specific differences - try: + if not is_win32: try: if fd: os.fchown(fd, uid, gid) @@ -719,20 +720,18 @@ Utilization of max. archive size: {csize_max:.0%} except OSError: # some systems don't support calling utime on a symlink pass - except AttributeError: - pass - acl_set(path, item, self.numeric_owner, fd=fd) - # chown removes Linux capabilities, so set the extended attributes at the end, after chown, since they include - # the Linux capabilities in the "security.capability" attribute. - warning = xattr.set_all(fd or path, item.get('xattrs', {}), follow_symlinks=False) - if warning: - set_ec(EXIT_WARNING) - # bsdflags include the immutable flag and need to be set last: - if not self.nobsdflags and 'bsdflags' in item: - try: - set_flags(path, item.bsdflags, fd=fd) - except OSError: - pass + acl_set(path, item, self.numeric_owner, fd=fd) + # chown removes Linux capabilities, so set the extended attributes at the end, after chown, since they include + # the Linux capabilities in the "security.capability" attribute. + warning = xattr.set_all(fd or path, item.get('xattrs', {}), follow_symlinks=False) + if warning: + set_ec(EXIT_WARNING) + # bsdflags include the immutable flag and need to be set last: + if not self.nobsdflags and 'bsdflags' in item: + try: + set_flags(path, item.bsdflags, fd=fd) + except OSError: + pass def set_meta(self, key, value): metadata = self._load_meta(self.id) diff --git a/src/borg/helpers/checks.py b/src/borg/helpers/checks.py index a9fe262ca..3a3bfb721 100644 --- a/src/borg/helpers/checks.py +++ b/src/borg/helpers/checks.py @@ -2,6 +2,7 @@ import os import sys from .errors import Error +from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin class PythonLibcTooOld(Error): @@ -9,7 +10,7 @@ class PythonLibcTooOld(Error): def check_python(): - if sys.platform.startswith(('win32', )): + if is_win32: required_funcs = {os.stat} else: required_funcs = {os.stat, os.utime, os.chown} diff --git a/src/borg/helpers/process.py b/src/borg/helpers/process.py index 2b2026a73..44b49892e 100644 --- a/src/borg/helpers/process.py +++ b/src/borg/helpers/process.py @@ -9,6 +9,7 @@ import sys from .. import __version__ +from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin from ..logger import create_logger logger = create_logger() @@ -117,7 +118,7 @@ def popen_with_error_handling(cmd_line: str, log_prefix='', **kwargs): def is_terminal(fd=sys.stdout): - return hasattr(fd, 'isatty') and fd.isatty() and (sys.platform != 'win32' or 'ANSICON' in os.environ) + return hasattr(fd, 'isatty') and fd.isatty() and (not is_win32 or 'ANSICON' in os.environ) def prepare_subprocess_env(system, env=None): diff --git a/src/borg/platform/__init__.py b/src/borg/platform/__init__.py index 3054c9dcc..80a7d7618 100644 --- a/src/borg/platform/__init__.py +++ b/src/borg/platform/__init__.py @@ -1,11 +1,11 @@ -import sys - """ Platform-specific APIs. Public APIs are documented in platform.base. """ +from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin + from .base import listxattr, getxattr, setxattr, ENOATTR from .base import acl_get, acl_set from .base import set_flags, get_flags @@ -15,24 +15,24 @@ from .base import process_alive, get_process_id, local_pid_alive, fqdn, hostname OS_API_VERSION = API_VERSION -if not sys.platform.startswith(('win32', )): +if not is_win32: from .posix import process_alive, local_pid_alive # posix swidth implementation works for: linux, freebsd, darwin, openindiana, cygwin from .posix import swidth from .posix import get_errno from .posix import uid2user, user2uid, gid2group, group2gid, getosusername -if sys.platform.startswith('linux'): # pragma: linux only +if is_linux: # pragma: linux only from .linux import API_VERSION as OS_API_VERSION from .linux import listxattr, getxattr, setxattr from .linux import acl_get, acl_set from .linux import set_flags, get_flags from .linux import SyncFile -elif sys.platform.startswith('freebsd'): # pragma: freebsd only +elif is_freebsd: # pragma: freebsd only from .freebsd import API_VERSION as OS_API_VERSION from .freebsd import listxattr, getxattr, setxattr from .freebsd import acl_get, acl_set -elif sys.platform == 'darwin': # pragma: darwin only +elif is_darwin: # pragma: darwin only from .darwin import API_VERSION as OS_API_VERSION from .darwin import listxattr, getxattr, setxattr from .darwin import acl_get, acl_set diff --git a/src/borg/platformflags.py b/src/borg/platformflags.py new file mode 100644 index 000000000..8bfea7732 --- /dev/null +++ b/src/borg/platformflags.py @@ -0,0 +1,12 @@ +""" +Flags for Platform-specific APIs. + +Use these Flags instead of sys.platform.startswith('') or try/except. +""" + +import sys + +is_win32 = sys.platform.startswith('win32') +is_linux = sys.platform.startswith('linux') +is_freebsd = sys.platform.startswith('freebsd') +is_darwin = sys.platform.startswith('darwin') diff --git a/src/borg/repository.py b/src/borg/repository.py index 916f66a19..f83e6bedd 100644 --- a/src/borg/repository.py +++ b/src/borg/repository.py @@ -654,7 +654,7 @@ class Repository: return except AttributeError: # TODO move the call to statvfs to platform - logger.warning('Failed to check free space before committing: no statvfs method available' ) + logger.warning('Failed to check free space before committing: no statvfs method available') return # f_bavail: even as root - don't touch the Federal Block Reserve! free_space = st_vfs.f_bavail * st_vfs.f_bsize diff --git a/src/borg/testsuite/platform.py b/src/borg/testsuite/platform.py index c32ac060a..473da3ef0 100644 --- a/src/borg/testsuite/platform.py +++ b/src/borg/testsuite/platform.py @@ -6,6 +6,7 @@ import sys import tempfile import unittest +from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin from ..platform import acl_get, acl_set, swidth from ..platform import get_process_id, process_alive from . import BaseTestCase, unopened_tempfile @@ -42,12 +43,14 @@ def fakeroot_detected(): def user_exists(username): - try: - import pwd # buildin but not on all OS - pwd.getpwnam(username) - return True - except (KeyError, ValueError): - return False + if not is_win32: + import pwd + try: + pwd.getpwnam(username) + return True + except (KeyError, ValueError): + pass + return False @functools.lru_cache()