From a0b4dc1bcbb106ba33279ff4c830d1e94d30898e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 26 Apr 2022 19:36:50 -0400 Subject: [PATCH] Island: Ignore exception in delete_file() if file not found --- .../cc/services/directory_file_storage_service.py | 5 ++++- .../cc/services/test_directory_file_storage_service.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 35e6989f2..d6fd267fe 100644 --- a/monkey/monkey_island/cc/services/directory_file_storage_service.py +++ b/monkey/monkey_island/cc/services/directory_file_storage_service.py @@ -40,7 +40,10 @@ class DirectoryFileStorageService(IFileStorageService): def delete_file(self, unsafe_file_name: str): safe_file_path = self._get_safe_file_path(unsafe_file_name) - safe_file_path.unlink() + try: + safe_file_path.unlink() + except FileNotFoundError: + pass def _get_safe_file_path(self, unsafe_file_name: str): # Remove any path information from the file name. 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 09ed308c5..07f65151d 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 @@ -119,3 +119,10 @@ def test_remove_all_files__skip_directories(tmp_path): for file in tmp_path.iterdir(): assert file.name == test_dir.name + + +def test_remove_nonexistant_file(tmp_path): + fss = DirectoryFileStorageService(tmp_path) + + # This test will fail if this call raises an exception. + fss.delete_file("nonexistant_file.txt")