diff --git a/monkey/monkey_island/cc/services/directory_file_storage_service.py b/monkey/monkey_island/cc/services/directory_file_storage_service.py index 30aa2a4d0..48fb95bfc 100644 --- a/monkey/monkey_island/cc/services/directory_file_storage_service.py +++ b/monkey/monkey_island/cc/services/directory_file_storage_service.py @@ -50,5 +50,5 @@ class DirectoryFileStorageService(IFileStorageService): return self._storage_directory / safe_file_name def delete_all_files(self): - for file in self._storage_directory.iterdir(): + for file in filter(lambda f: f.is_file(), self._storage_directory.iterdir()): file.unlink() diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py index 7d65489d1..09ed308c5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_directory_file_storage_service.py @@ -106,3 +106,16 @@ def test_remove_all_files(tmp_path): for file in tmp_path.iterdir(): assert False, f"{tmp_path} was expected to be empty, but contained files" + + +def test_remove_all_files__skip_directories(tmp_path): + test_dir = tmp_path / "test_dir" + test_dir.mkdir() + for filename in ["1.txt", "2.txt", "3.txt"]: + (tmp_path / filename).touch() + + fss = DirectoryFileStorageService(tmp_path) + fss.delete_all_files() + + for file in tmp_path.iterdir(): + assert file.name == test_dir.name