From 42e78408a713123a32765d661ac8096e437f1ddd Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 25 Feb 2025 17:11:34 +0100 Subject: [PATCH] Fix add-on store reset (#5669) Make sure that add-on store resets do not delete the root folder. This is important so that successive reset attempts do not fail (the directory passed to `remove_folder` must exist, otherwise find fails with an non-zero exit code). While at it, handle find errors properly and report errors as critical. --- supervisor/resolution/fixups/store_execute_reset.py | 5 ++++- supervisor/utils/__init__.py | 3 +-- tests/resolution/fixup/test_store_execute_reset.py | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/supervisor/resolution/fixups/store_execute_reset.py b/supervisor/resolution/fixups/store_execute_reset.py index 45bf15c7f..d5736b40b 100644 --- a/supervisor/resolution/fixups/store_execute_reset.py +++ b/supervisor/resolution/fixups/store_execute_reset.py @@ -1,5 +1,6 @@ """Helpers to check and fix issues with free space.""" +from functools import partial import logging from ...coresys import CoreSys @@ -40,7 +41,9 @@ class FixupStoreExecuteReset(FixupBase): _LOGGER.warning("Can't find store %s for fixup", reference) return - await self.sys_run_in_executor(remove_folder, repository.git.path) + await self.sys_run_in_executor( + partial(remove_folder, folder=repository.git.path, content_only=True) + ) # Load data again try: diff --git a/supervisor/utils/__init__.py b/supervisor/utils/__init__.py index 245d7b423..e402850da 100644 --- a/supervisor/utils/__init__.py +++ b/supervisor/utils/__init__.py @@ -106,8 +106,7 @@ def remove_folder( except OSError as err: _LOGGER.exception("Can't remove folder %s: %s", folder, err) except subprocess.CalledProcessError as procerr: - _LOGGER.error("Can't remove folder %s: %s", folder, procerr.stderr.strip()) - raise procerr + _LOGGER.critical("Can't remove folder %s: %s", folder, procerr.stderr.strip()) def remove_folder_with_excludes( diff --git a/tests/resolution/fixup/test_store_execute_reset.py b/tests/resolution/fixup/test_store_execute_reset.py index 8a7a03603..04015435e 100644 --- a/tests/resolution/fixup/test_store_execute_reset.py +++ b/tests/resolution/fixup/test_store_execute_reset.py @@ -1,6 +1,7 @@ """Test evaluation base.""" # pylint: disable=import-error,protected-access +from os import listdir from pathlib import Path from unittest.mock import AsyncMock, patch @@ -25,16 +26,18 @@ async def test_fixup(coresys: CoreSys, tmp_path): ) test_repo.mkdir() + (test_repo / ".git").mkdir() assert test_repo.exists() mock_repositorie = AsyncMock() mock_repositorie.git.path = test_repo coresys.store.repositories["test"] = mock_repositorie + assert len(listdir(test_repo)) > 0 with patch("shutil.disk_usage", return_value=(42, 42, 2 * (1024.0**3))): await store_execute_reset() - assert not test_repo.exists() + assert len(listdir(test_repo)) == 0 assert mock_repositorie.load.called assert len(coresys.resolution.suggestions) == 0 assert len(coresys.resolution.issues) == 0