From baee74b761b2d343688ba898f161c52215b47c10 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 26 May 2021 15:22:52 +0530 Subject: [PATCH] Add exception messages during data directory creation --- .../cc/environment/data_dir_generator.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/monkey/monkey_island/cc/environment/data_dir_generator.py b/monkey/monkey_island/cc/environment/data_dir_generator.py index 98907d63948..58e16d4b797 100644 --- a/monkey/monkey_island/cc/environment/data_dir_generator.py +++ b/monkey/monkey_island/cc/environment/data_dir_generator.py @@ -1,14 +1,30 @@ +import logging import os from monkey_island.cc.environment.utils import is_windows_os from monkey_island.cc.environment.windows_permissions import set_full_folder_access +LOG = logging.getLogger(__name__) + def create_data_dir(data_dir: str, create_parent_dirs: bool) -> None: if not os.path.isdir(data_dir): - if create_parent_dirs: - os.makedirs(data_dir, mode=0o700) - else: - os.mkdir(data_dir, mode=0o700) + try: + if create_parent_dirs: + os.makedirs(data_dir, mode=0o700) + else: + os.mkdir(data_dir, mode=0o700) + except Exception as ex: + LOG.error( + f'Could not create data directory at "{data_dir}" (maybe `$HOME` could not be ' + f"resolved?): {str(ex)}" + ) + if is_windows_os(): # `mode=0o700` doesn't work on Windows - set_full_folder_access(folder_path=data_dir) + try: + set_full_folder_access(folder_path=data_dir) + except Exception as ex: + LOG.error( + f'Data directory was created at "{data_dir}" but permissions could not be ' + f"set successfully: {str(ex)}" + )