From e91156b5cfabf1fe599f9a5d6d9550d67cf9a11a Mon Sep 17 00:00:00 2001 From: thehilll Date: Thu, 15 Jul 2021 16:17:54 -0400 Subject: [PATCH 1/2] Cache friendly name by UDID rather than globally Create a directory for caching machine friendly name, within that store name in .txt file. Cleans out any file not for the current UDID. This addresses the situation where the global cache file is migrated via Migration Assistant and Sal reports the old machine friendly name. --- .../sal/checkin_modules/machine_checkin.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/payload/usr/local/sal/checkin_modules/machine_checkin.py b/payload/usr/local/sal/checkin_modules/machine_checkin.py index 355310e..a360126 100755 --- a/payload/usr/local/sal/checkin_modules/machine_checkin.py +++ b/payload/usr/local/sal/checkin_modules/machine_checkin.py @@ -18,7 +18,7 @@ import sal -MODEL_PATH = pathlib.Path("/usr/local/sal/mac_model.txt") +MODEL_PATH = pathlib.Path("/usr/local/sal/model_cache") MEMORY_EXPONENTS = {"KB": 0, "MB": 1, "GB": 2, "TB": 3} __version__ = "1.1.0" @@ -50,7 +50,9 @@ def process_system_profile(): machine_results["machine_model"] = system_profile["SPHardwareDataType"][0][ "machine_model" ] - friendly_model = get_friendly_model(machine_results["serial"]) + + udid = system_profile["SPHardwareDataType"][0]['provisioning_UDID'] + friendly_model = get_friendly_model(serial=machine_results["serial"], udid=udid) if friendly_model: machine_results["machine_model_friendly"] = friendly_model machine_results["cpu_type"] = system_profile["SPHardwareDataType"][0].get( @@ -98,17 +100,29 @@ def get_machine_name(net_config, nametype): ).strip() -def get_friendly_model(serial): +def get_friendly_model(serial, udid): """Return friendly model name""" - if not MODEL_PATH.exists(): + + # set up cache file for this udid...create dir, + MODEL_PATH.mkdir(mode=0o755, parents=True, exist_ok=True) + + # name cache for this udid + UDID_CACHE_PATH = pathlib.Path(MODEL_PATH, '%s.txt' % (udid)) + for cache_file in MODEL_PATH.iterdir(): + # clean up any other files in dir + if cache_file != UDID_CACHE_PATH: + cache_file.unlink() + + if not UDID_CACHE_PATH.exists(): model = cleanup_model(query_apple_support(serial)) if model: - MODEL_PATH.write_text(model) + UDID_CACHE_PATH.write_text(model) else: try: - model = MODEL_PATH.read_text().strip() + model = UDID_CACHE_PATH.read_text().strip() except: model = None + return model From 4855cd44d962f4a407594f871cea621c10773e90 Mon Sep 17 00:00:00 2001 From: thehilll Date: Thu, 15 Jul 2021 17:19:11 -0400 Subject: [PATCH 2/2] Handle failing to unlink old cache files Use a try/except to avoid killing the whole process if an unused cache file can't be removed --- payload/usr/local/sal/checkin_modules/machine_checkin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/payload/usr/local/sal/checkin_modules/machine_checkin.py b/payload/usr/local/sal/checkin_modules/machine_checkin.py index a360126..cbe56c5 100755 --- a/payload/usr/local/sal/checkin_modules/machine_checkin.py +++ b/payload/usr/local/sal/checkin_modules/machine_checkin.py @@ -111,7 +111,10 @@ def get_friendly_model(serial, udid): for cache_file in MODEL_PATH.iterdir(): # clean up any other files in dir if cache_file != UDID_CACHE_PATH: - cache_file.unlink() + try: + cache_file.unlink() + except: + pass if not UDID_CACHE_PATH.exists(): model = cleanup_model(query_apple_support(serial))