diff --git a/resources/global_settings.py b/resources/global_settings.py index 47511fb6b..36d56b5e9 100644 --- a/resources/global_settings.py +++ b/resources/global_settings.py @@ -6,6 +6,8 @@ from pathlib import Path import plistlib import logging +import os +import subprocess class global_settings: @@ -13,25 +15,28 @@ class global_settings: self.file_name = ".com.dortania.opencore-legacy-patcher.plist" self.global_settings_folder = "/Users/Shared" self.global_settings_plist = f"{self.global_settings_folder}/{self.file_name}" - self.generate_settings_file() - self.convert_defaults_to_global_settings() + self._generate_settings_file() + self._convert_defaults_to_global_settings() + self._fix_file_permission() - def generate_settings_file(self): - if Path(self.global_settings_plist).exists(): - return - try: - plistlib.dump({"Developed by Dortania": True,}, Path(self.global_settings_plist).open("wb")) - except PermissionError: - logging.info("- Permission error: Unable to write to global settings file") def read_property(self, property_name): + """ + Reads a property from the global settings file + """ + if Path(self.global_settings_plist).exists(): plist = plistlib.load(Path(self.global_settings_plist).open("rb")) if property_name in plist: return plist[property_name] return None + def write_property(self, property_name, property_value): + """ + Writes a property to the global settings file + """ + if Path(self.global_settings_plist).exists(): plist = plistlib.load(Path(self.global_settings_plist).open("rb")) plist[property_name] = property_value @@ -41,7 +46,20 @@ class global_settings: logging.info("- Failed to write to global settings file") - def convert_defaults_to_global_settings(self): + def _generate_settings_file(self): + if Path(self.global_settings_plist).exists(): + return + try: + plistlib.dump({"Developed by Dortania": True,}, Path(self.global_settings_plist).open("wb")) + except PermissionError: + logging.info("- Permission error: Unable to write to global settings file") + + + def _convert_defaults_to_global_settings(self): + """ + Converts legacy defaults to global settings + """ + defaults_path = "~/Library/Preferences/com.dortania.opencore-legacy-patcher.plist" defaults_path = Path(defaults_path).expanduser() @@ -60,4 +78,23 @@ class global_settings: try: Path(defaults_path).unlink() except PermissionError: - logging.info("- Permission error: Unable to delete defaults plist") \ No newline at end of file + logging.info("- Permission error: Unable to delete defaults plist") + + + def _fix_file_permission(self): + """ + Fixes file permission for log file + + If OCLP was invoked as root, file permission will only allow root to write to settings file + This in turn breaks normal OCLP execution to write to settings file + """ + + if os.geteuid() != 0: + return + + # Set file permission to allow any user to write to log file + result = subprocess.run(["chmod", "777", self.global_settings_plist], capture_output=True) + if result.returncode != 0: + logging.warning("- Failed to fix settings file permissions:") + if result.stderr: + logging.warning(result.stderr.decode("utf-8")) \ No newline at end of file diff --git a/resources/logging_handler.py b/resources/logging_handler.py index 89e50f42f..f5a414528 100644 --- a/resources/logging_handler.py +++ b/resources/logging_handler.py @@ -1,6 +1,8 @@ import logging import sys import threading +import os +import subprocess from pathlib import Path @@ -19,6 +21,9 @@ class InitializeLoggingSupport: >>> from resources.logging_handler import InitializeLoggingSupport >>> InitializeLoggingSupport() + FOR DEVELOPERS: + - Do not invoke logging until after '_attempt_initialize_logging_configuration()' has been invoked + """ def __init__(self) -> None: @@ -35,6 +40,7 @@ class InitializeLoggingSupport: self._clean_log_file() self._attempt_initialize_logging_configuration() self._implement_custom_traceback_handler() + self._fix_file_permission() def __del__(self): @@ -81,6 +87,24 @@ class InitializeLoggingSupport: print(f"- Failed to clean log file: {e}") + def _fix_file_permission(self): + """ + Fixes file permission for log file + + If OCLP was invoked as root, file permission will only allow root to write to log file + This in turn breaks normal OCLP execution to write to log file + """ + + if os.geteuid() != 0: + return + + result = subprocess.run(["chmod", "777", self.log_filepath], capture_output=True) + if result.returncode != 0: + print(f"- Failed to fix log file permissions") + if result.stderr: + print(result.stderr.decode("utf-8")) + + def _initialize_logging_configuration(self, log_to_file: bool = True): """ Initialize logging framework configuration