From bf5bc14c8722a4e712cec0dfa2531a5005ed8650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 13 Dec 2018 16:29:22 +0100 Subject: [PATCH] backup: fix needed space calculation It wasn't taking excludes into account. Now its calculation mirrors the zip file creation. --- src/octoprint/plugins/backup/__init__.py | 34 +++++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/octoprint/plugins/backup/__init__.py b/src/octoprint/plugins/backup/__init__.py index b00f5faf2..1d36918d1 100644 --- a/src/octoprint/plugins/backup/__init__.py +++ b/src/octoprint/plugins/backup/__init__.py @@ -514,11 +514,17 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, thread.start() @classmethod - def _get_disk_size(cls, path): + def _get_disk_size(cls, path, ignored=None): + if ignored is None: + ignored = [] + + if path in ignored: + return 0 + total = 0 for entry in scandir(path): if entry.is_dir(): - total += cls._get_disk_size(entry.path) + total += cls._get_disk_size(entry.path, ignored=ignored) elif entry.is_file(): total += entry.stat().st_size return total @@ -595,6 +601,8 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, on_backup_start=None, on_backup_done=None, on_backup_error=None): + exclude_by_default = ("generated", "logs", "watched",) + try: if exclude is None: exclude = [] @@ -605,14 +613,23 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, temporary_path = os.path.join(datafolder, ".{}".format(name)) final_path = os.path.join(datafolder, name) - size = cls._get_disk_size(basedir) - if not cls._free_space(os.path.dirname(temporary_path), size): - raise InsufficientSpace() - own_folder = datafolder defaults = [os.path.join(basedir, "config.yaml"),] + \ [os.path.join(basedir, folder) for folder in default_settings["folder"].keys()] + # check how much many bytes we are about to backup + size = os.stat(configfile).st_size + for folder in default_settings["folder"].keys(): + if folder in exclude or folder in exclude_by_default: + continue + size += cls._get_disk_size(settings.global_get_basefolder(folder), + ignored=[own_folder,]) + size += cls._get_disk_size(basedir, ignored=defaults + [own_folder,]) + + # since we can't know the compression ratio beforehand, we assume we need the same amount of space + if not cls._free_space(os.path.dirname(temporary_path), size): + raise InsufficientSpace() + compression = zipfile.ZIP_DEFLATED if zlib else zipfile.ZIP_STORED if callable(on_backup_start): @@ -642,10 +659,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, # backup configured folder paths for folder in default_settings["folder"].keys(): - if folder in exclude: - continue - - if folder in ("generated", "logs", "watched",): + if folder in exclude or folder in exclude_by_default: continue add_to_zip(settings.global_get_basefolder(folder),