Merge pull request #1833 from enkore/issue/1821

borg init: fix free space check crashing if disk is full
This commit is contained in:
enkore 2016-11-12 01:23:34 +01:00 committed by GitHub
commit 80b86709ef
2 changed files with 14 additions and 1 deletions

View File

@ -120,6 +120,7 @@ class Repository:
self.lock_wait = lock_wait
self.do_lock = lock
self.do_create = create
self.created = False
self.exclusive = exclusive
self.append_only = append_only
self.hostname_is_unique = yes(env_var_override='BORG_HOSTNAME_IS_UNIQUE', env_msg=None, prompt=False)
@ -138,6 +139,7 @@ class Repository:
if self.do_create:
self.do_create = False
self.create(self.path)
self.created = True
self.open(self.path, bool(self.exclusive), lock_wait=self.lock_wait, lock=self.do_lock)
return self
@ -437,6 +439,10 @@ class Repository:
free_space = st_vfs.f_bavail * st_vfs.f_bsize
logger.debug('check_free_space: required bytes {}, free bytes {}'.format(required_free_space, free_space))
if free_space < required_free_space:
if self.created:
logger.error('Not enough free space to initialize repository at this location.')
self.destroy()
else:
self._rollback(cleanup=True)
formatted_required = format_file_size(required_free_space)
formatted_free = format_file_size(free_space)

View File

@ -406,6 +406,13 @@ class RepositoryFreeSpaceTestCase(RepositoryTestCaseBase):
self.repository.put(H(0), b'foobar')
with pytest.raises(Repository.InsufficientFreeSpaceError):
self.repository.commit()
assert os.path.exists(self.repository.path)
def test_create_free_space(self):
self.repository.additional_free_space = 1e20
with pytest.raises(Repository.InsufficientFreeSpaceError):
self.add_keys()
assert not os.path.exists(self.repository.path)
class NonceReservation(RepositoryTestCaseBase):