diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index abbf277..23ee2c9 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -92,7 +92,9 @@ class ProfileController: profile_id = profile.id # 2. Permanently delete from disk - profile.delete() + removed = model_manipulator.delete(profile) + if not removed.valid: + return removed.wrap("Unable to delete files, so keeping repo intact.") # 3. update cache with the temp variable to prevent drift ProfileController._repo.invalidate(profile_id=profile_id) diff --git a/core/controllers/profile_state/update_profile.py b/core/controllers/profile_state/update_profile.py index 99da85f..957f0e3 100644 --- a/core/controllers/profile_state/update_profile.py +++ b/core/controllers/profile_state/update_profile.py @@ -1,7 +1,7 @@ # utils from core.errors.logger import logger from core.models.Result import Result, ResultError -from core.services.networking.systemwide.general_tools.manage_sudo_configs import remove_sudo_config +from core.services.config_tools.manage_sudo_configs import remove_sudo_config from core.controllers.ProfileController import ProfileController from core.models.BaseProfile import BaseProfile as Profile diff --git a/core/services/networking/systemwide/general_tools/manage_sudo_configs.py b/core/services/config_tools/manage_sudo_configs.py similarity index 67% rename from core/services/networking/systemwide/general_tools/manage_sudo_configs.py rename to core/services/config_tools/manage_sudo_configs.py index c83c6db..71463af 100644 --- a/core/services/networking/systemwide/general_tools/manage_sudo_configs.py +++ b/core/services/config_tools/manage_sudo_configs.py @@ -1,9 +1,11 @@ +from core.models.enum_models import ProtocolChoice from core.utils.run_commands import run_generic_command from core.models.Result import Result from core.models.system.SystemProfile import SystemProfile +from core.utils.basic_operations.does_file_exist import does_file_exist def get_target_filename(profile: SystemProfile) -> str: - if profile.connection.code == "wireguard": + if profile.protocol == ProtocolChoice.WIREGUARD: return "wg.conf" else: return "proxy.json" @@ -14,8 +16,10 @@ def remove_sudo_config(profile: SystemProfile) -> Result: file_name = get_target_filename(profile) sudo_filepath = f"{base_folder}/{file_name}" + if not does_file_exist(sudo_filepath): + return Result(valid=True, message="Sudo config is already gone, so it doesn't need to be deleted.") + # REMOVE: command = ['pkexec', 'rm', '-f', sudo_filepath] human_readable_goal = "Removing file from sudo folder" return run_generic_command(command, human_readable_goal, timeout=35) - diff --git a/core/services/profiles/model_manipulator.py b/core/services/profiles/model_manipulator.py index 7a2f6ec..17acde8 100644 --- a/core/services/profiles/model_manipulator.py +++ b/core/services/profiles/model_manipulator.py @@ -1,3 +1,4 @@ +from core.services.config_tools.manage_sudo_configs import remove_sudo_config from core.models.session.SessionProfile import SessionProfile from core.models.orm_calls.location_calls import LocationCache from core.models.BaseProfile import BaseProfile as Profile @@ -292,3 +293,17 @@ def save_to_disk(profile: Profile): config_file_path = f'{profile_config_path}/config.json' write_atomically(config_file_path, config_file_contents) + +def delete(profile: Profile) -> Result: + """Delete the files on disk for a given profile.""" + + logger.info(f"Deleting profile {profile.name} id {profile.id}") + + if profile.type == ProfileType.SYSTEM: + removed = remove_sudo_config(profile) + if not removed.valid: + return removed.wrap("Unable to delete the systemwide configs behind sudo protection.") + + shutil.rmtree(get_config_path(profile.id), ignore_errors=True) + shutil.rmtree(get_data_path(profile.id), ignore_errors=True) + return Result(valid=True) \ No newline at end of file diff --git a/core/services/profiles/profile_connections.py b/core/services/profiles/profile_connections.py index ed2b7ce..82fe523 100644 --- a/core/services/profiles/profile_connections.py +++ b/core/services/profiles/profile_connections.py @@ -361,10 +361,10 @@ def destroy_profile( #################################### # DESTROY PROFILE #################################### - logger.info("Disabling profile") - disable_profile(profile) + # logger.info("Disabling profile") + # disable_profile(profile) - logger.info("Wiping profile from disk..") + logger.info("Wiping profile from disk and cache..") ProfileController.delete(profile) if profile_observer is not None: