[enh] Store backup size and check free space before restoring (bugfix #189)

This commit is contained in:
Jérôme Lebleu 2016-02-18 16:17:04 +01:00
parent aafa9caeea
commit 4e48aeb388
2 changed files with 21 additions and 1 deletions

View file

@ -6,6 +6,7 @@
"installation_failed" : "Installation failed",
"unexpected_error" : "An unexpected error occured",
"action_invalid" : "Invalid action '{:s}'",
"not_enough_disk_space" : "Not enough free disk space on '{path:s}'",
"license_undefined" : "undefined",
"no_appslist_found" : "No apps list found",

View file

@ -227,6 +227,11 @@ def backup_create(name=None, description=None, output_directory=None,
_clean_tmp_dir(1)
raise MoulinetteError(errno.EINVAL, m18n.n('backup_nothings_done'))
# Calculate total size
size = subprocess.check_output(
['du','-sb', tmp_dir]).split()[0].decode('utf-8')
info['size'] = int(size)
# Create backup info file
with open("%s/info.json" % tmp_dir, 'w') as f:
f.write(json.dumps(info))
@ -311,6 +316,14 @@ def backup_restore(name, hooks=[], apps=[], ignore_apps=False, ignore_hooks=Fals
tmp_dir)
os.system('rm -rf %s' % tmp_dir)
# Check available disk space
statvfs = os.statvfs(backup_path)
free_space = statvfs.f_frsize * statvfs.f_bavail
if free_space < info['size']:
logger.debug("%dB left but %dB is needed", free_space, info['size'])
raise MoulinetteError(
errno.EIO, m18n.n('not_enough_disk_space', path=backup_path))
def _clean_tmp_dir(retcode=0):
ret = hook_callback('post_backup_restore', args=[tmp_dir, retcode])
if not ret['failed']:
@ -538,7 +551,13 @@ def backup_info(name, with_details=False, human_readable=False):
logger.debug("unable to load '%s'", info_file, exc_info=1)
raise MoulinetteError(errno.EIO, m18n.n('backup_invalid_archive'))
size = os.path.getsize(archive_file)
# Retrieve backup size
size = info.get('size', 0)
if not size:
tar = tarfile.open(archive_file, "r:gz")
size = reduce(lambda x,y: getattr(x, 'size', x)+getattr(y, 'size', y),
tar.getmembers())
tar.close()
if human_readable:
size = binary_to_human(size) + 'B'