Merge pull request #3496 from ThomasWaldmann/borg-init-existing-repo-1.1

Correct some confusing error messages from `borg init`, fixes #3465
This commit is contained in:
TW 2017-12-29 10:06:50 +01:00 committed by GitHub
commit b2d4ba6ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 15 deletions

View File

@ -116,6 +116,9 @@ class Repository:
class AlreadyExists(Error):
"""A repository already exists at {}."""
class PathAlreadyExists(Error):
"""There is already something at {}."""
class InvalidRepository(Error):
"""{} is not a valid repository. Check repo config."""
@ -195,9 +198,24 @@ class Repository:
def id_str(self):
return bin_to_hex(self.id)
@staticmethod
def is_repository(path):
"""Check whether there is already a Borg repository at *path*."""
try:
# Use binary mode to avoid troubles if a README contains some stuff not in our locale
with open(os.path.join(path, 'README'), 'rb') as fd:
# Read only the first ~100 bytes (if any), in case some README file we stumble upon is large.
readme_head = fd.read(100)
# The first comparison captures our current variant (REPOSITORY_README), the second comparison
# is an older variant of the README file (used by 1.0.x).
return b'Borg Backup repository' in readme_head or b'Borg repository' in readme_head
except OSError:
# Ignore FileNotFound, PermissionError, ...
return False
def check_can_create_repository(self, path):
"""
Raise self.AlreadyExists if a repository already exists at *path* or any parent directory.
Raise an exception if a repository already exists at *path* or any parent directory.
Checking parent directories is done for two reasons:
(1) It's just a weird thing to do, and usually not intended. A Borg using the "parent" repository
@ -207,8 +225,11 @@ class Repository:
repository, user's can only use the quota'd repository, when their --restrict-to-path points
at the user's repository.
"""
if os.path.exists(path) and (not os.path.isdir(path) or os.listdir(path)):
if os.path.exists(path):
if self.is_repository(path):
raise self.AlreadyExists(path)
if not os.path.isdir(path) or os.listdir(path):
raise self.PathAlreadyExists(path)
while True:
# Check all parent directories for Borg's repository README
@ -218,18 +239,8 @@ class Repository:
if path == previous_path:
# We reached the root of the directory hierarchy (/.. = / and C:\.. = C:\).
break
try:
# Use binary mode to avoid troubles if a README contains some stuff not in our locale
with open(os.path.join(path, 'README'), 'rb') as fd:
# Read only the first ~100 bytes (if any), in case some README file we stumble upon is large.
readme_head = fd.read(100)
# The first comparison captures our current variant (REPOSITORY_README), the second comparison
# is an older variant of the README file (used by 1.0.x).
if b'Borg Backup repository' in readme_head or b'Borg repository' in readme_head:
if self.is_repository(path):
raise self.AlreadyExists(path)
except OSError:
# Ignore FileNotFound, PermissionError, ...
pass
def create(self, path):
"""Create a new empty repository at `path`