mirror of
https://github.com/dortania/OpenCore-Legacy-Patcher.git
synced 2026-06-20 22:20:53 +10:00
logging_handler.py: Fix file permissions when root
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import plistlib
|
import plistlib
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
class global_settings:
|
class global_settings:
|
||||||
|
|
||||||
@@ -13,25 +15,28 @@ class global_settings:
|
|||||||
self.file_name = ".com.dortania.opencore-legacy-patcher.plist"
|
self.file_name = ".com.dortania.opencore-legacy-patcher.plist"
|
||||||
self.global_settings_folder = "/Users/Shared"
|
self.global_settings_folder = "/Users/Shared"
|
||||||
self.global_settings_plist = f"{self.global_settings_folder}/{self.file_name}"
|
self.global_settings_plist = f"{self.global_settings_folder}/{self.file_name}"
|
||||||
self.generate_settings_file()
|
self._generate_settings_file()
|
||||||
self.convert_defaults_to_global_settings()
|
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):
|
def read_property(self, property_name):
|
||||||
|
"""
|
||||||
|
Reads a property from the global settings file
|
||||||
|
"""
|
||||||
|
|
||||||
if Path(self.global_settings_plist).exists():
|
if Path(self.global_settings_plist).exists():
|
||||||
plist = plistlib.load(Path(self.global_settings_plist).open("rb"))
|
plist = plistlib.load(Path(self.global_settings_plist).open("rb"))
|
||||||
if property_name in plist:
|
if property_name in plist:
|
||||||
return plist[property_name]
|
return plist[property_name]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def write_property(self, property_name, property_value):
|
def write_property(self, property_name, property_value):
|
||||||
|
"""
|
||||||
|
Writes a property to the global settings file
|
||||||
|
"""
|
||||||
|
|
||||||
if Path(self.global_settings_plist).exists():
|
if Path(self.global_settings_plist).exists():
|
||||||
plist = plistlib.load(Path(self.global_settings_plist).open("rb"))
|
plist = plistlib.load(Path(self.global_settings_plist).open("rb"))
|
||||||
plist[property_name] = property_value
|
plist[property_name] = property_value
|
||||||
@@ -41,7 +46,20 @@ class global_settings:
|
|||||||
logging.info("- Failed to write to global settings file")
|
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 = "~/Library/Preferences/com.dortania.opencore-legacy-patcher.plist"
|
||||||
defaults_path = Path(defaults_path).expanduser()
|
defaults_path = Path(defaults_path).expanduser()
|
||||||
|
|
||||||
@@ -61,3 +79,22 @@ class global_settings:
|
|||||||
Path(defaults_path).unlink()
|
Path(defaults_path).unlink()
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
logging.info("- Permission error: Unable to delete defaults plist")
|
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"))
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
@@ -19,6 +21,9 @@ class InitializeLoggingSupport:
|
|||||||
>>> from resources.logging_handler import InitializeLoggingSupport
|
>>> from resources.logging_handler import InitializeLoggingSupport
|
||||||
>>> InitializeLoggingSupport()
|
>>> InitializeLoggingSupport()
|
||||||
|
|
||||||
|
FOR DEVELOPERS:
|
||||||
|
- Do not invoke logging until after '_attempt_initialize_logging_configuration()' has been invoked
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
@@ -35,6 +40,7 @@ class InitializeLoggingSupport:
|
|||||||
self._clean_log_file()
|
self._clean_log_file()
|
||||||
self._attempt_initialize_logging_configuration()
|
self._attempt_initialize_logging_configuration()
|
||||||
self._implement_custom_traceback_handler()
|
self._implement_custom_traceback_handler()
|
||||||
|
self._fix_file_permission()
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@@ -81,6 +87,24 @@ class InitializeLoggingSupport:
|
|||||||
print(f"- Failed to clean log file: {e}")
|
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):
|
def _initialize_logging_configuration(self, log_to_file: bool = True):
|
||||||
"""
|
"""
|
||||||
Initialize logging framework configuration
|
Initialize logging framework configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user