#!/usr/bin/env python3 """Generate a macOS Finder .DS_Store for a DMG installer window without AppleEvents/TCC.""" import ctypes import plistlib import struct import sys from pathlib import Path def make_carbon_alias(volume_root: Path, target_path: Path) -> bytes: """Create a genuine Carbon AliasRecord ('alis') via macOS CoreServices.""" cs = ctypes.CDLL("/System/Library/Frameworks/CoreServices.framework/CoreServices") cs.FSPathMakeRef.argtypes = [ctypes.c_char_p, ctypes.c_void_p, ctypes.c_void_p] cs.FSPathMakeRef.restype = ctypes.c_int32 cs.FSNewAlias.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] cs.FSNewAlias.restype = ctypes.c_int32 cs.GetHandleSize.argtypes = [ctypes.c_void_p] cs.GetHandleSize.restype = ctypes.c_long cs.DisposeHandle.argtypes = [ctypes.c_void_p] cs.DisposeHandle.restype = None from_ref = (ctypes.c_ubyte * 80)() err = cs.FSPathMakeRef(str(volume_root).encode("utf-8"), from_ref, None) if err != 0: raise RuntimeError(f"FSPathMakeRef({volume_root}) failed with OSStatus {err}") target_ref = (ctypes.c_ubyte * 80)() err = cs.FSPathMakeRef(str(target_path).encode("utf-8"), target_ref, None) if err != 0: raise RuntimeError(f"FSPathMakeRef({target_path}) failed with OSStatus {err}") alias_handle = ctypes.c_void_p() err = cs.FSNewAlias(from_ref, target_ref, ctypes.byref(alias_handle)) if err != 0 or not alias_handle: raise RuntimeError(f"FSNewAlias({target_path}) failed with OSStatus {err}") try: size = cs.GetHandleSize(alias_handle) ptr = ctypes.cast(alias_handle, ctypes.POINTER(ctypes.c_void_p))[0] return ctypes.string_at(ptr, size) finally: cs.DisposeHandle(alias_handle) def encode_record(filename: str, code: bytes, dtype: bytes, value: bytes) -> bytes: utf16 = filename.encode("utf-16-be") return struct.pack(">I", len(filename)) + utf16 + code + dtype + value def build_ds_store(volume_root: Path, bg_path: Path, app_name: str) -> bytes: alias_bytes = make_carbon_alias(volume_root, bg_path) bwsp_plist = plistlib.dumps( { "ContainerShowSidebar": False, "ShowPathbar": False, "ShowSidebar": False, "ShowStatusBar": False, "ShowTabView": False, "ShowToolbar": False, "WindowBounds": "{{200, 300}, {660, 428}}", }, fmt=plistlib.FMT_BINARY, ) icvp_plist = plistlib.dumps( { "arrangeBy": "none", "backgroundColorBlue": 1.0, "backgroundColorGreen": 1.0, "backgroundColorRed": 1.0, "backgroundImageAlias": alias_bytes, "backgroundType": 2, "gridOffsetX": 0.0, "gridOffsetY": 0.0, "gridSpacing": 100.0, "iconSize": 128.0, "labelOnBottom": True, "scrollPositionX": 0.0, "scrollPositionY": 0.0, "showIconPreview": True, "showItemInfo": False, "textSize": 13.0, "viewOptionsVersion": 1, }, fmt=plistlib.FMT_BINARY, ) iloc_app = struct.pack(">IIII", 175, 180, 0xFFFFFFFF, 0xFFFF0000) iloc_apps = struct.pack(">IIII", 485, 180, 0xFFFFFFFF, 0xFFFF0000) records = [ (".", b"bwsp", b"blob", struct.pack(">I", len(bwsp_plist)) + bwsp_plist), (".", b"icvp", b"blob", struct.pack(">I", len(icvp_plist)) + icvp_plist), (".", b"vSrn", b"long", struct.pack(">I", 1)), (".", b"vstl", b"type", b"icnv"), ("Applications", b"Iloc", b"blob", struct.pack(">I", len(iloc_apps)) + iloc_apps), (app_name, b"Iloc", b"blob", struct.pack(">I", len(iloc_app)) + iloc_app), ] records.sort(key=lambda r: (r[0].lower(), r[1].lower())) # B-tree leaf node (4096 bytes) leaf_body = bytearray() leaf_body += struct.pack(">II", 0, len(records)) # next_node=0 (leaf), record_count for fname, code, dtype, val in records: leaf_body += encode_record(fname, code, dtype, val) if len(leaf_body) > 4096: raise RuntimeError(f"Leaf node overflow: {len(leaf_body)} > 4096") leaf_block = bytes(leaf_body).ljust(4096, b"\x00") # DSDB master block (32 bytes) # root_node_block=2, height=0 (single leaf), num_records, num_nodes=1, page_size=4096 dsdb_block = struct.pack(">IIIII", 2, 0, len(records), 1, 4096).ljust(32, b"\x00") # Root bookkeeping block at buddy offset 0x2000 (size 2048 = 2^11) # Block table: 0 -> root (0x200b), 1 -> DSDB (0x0045), 2 -> leaf (0x100c) addrs = [0x2000 | 11, 0x0040 | 5, 0x1000 | 12] root_body = bytearray() root_body += struct.pack(">II", len(addrs), 0) for a in addrs: root_body += struct.pack(">I", a) root_body += b"\x00" * ((256 - len(addrs)) * 4) # Table of Contents: 1 entry ("DSDB" -> block 1) root_body += struct.pack(">IB4sI", 1, 4, b"DSDB", 1) # Free lists for buddy buckets 0..31 covering [0x0000..0x4000) freelists = { 5: [0x0020, 0x0060], 7: [0x0080], 8: [0x0100], 9: [0x0200], 10: [0x0400], 11: [0x0800, 0x2800], 12: [0x3000], } for bucket in range(32): entries = freelists.get(bucket, []) root_body += struct.pack(">I", len(entries)) for off in entries: root_body += struct.pack(">I", off) if len(root_body) > 2048: raise RuntimeError(f"Root block overflow: {len(root_body)} > 2048") root_block = bytes(root_body).ljust(2048, b"\x00") # Assemble buddy heap [0x0000..0x4000) prefixed by 4-byte alignment header (0x00000001) heap = bytearray(0x4000) heap[0x0000:0x0020] = struct.pack(">4sIII16s", b"Bud1", 0x2000, 2048, 0x2000, b"\x00" * 16) heap[0x0040:0x0060] = dsdb_block heap[0x1000:0x2000] = leaf_block heap[0x2000:0x2800] = root_block return struct.pack(">I", 1) + bytes(heap) def main() -> int: if len(sys.argv) != 4: print(f"Usage: {sys.argv[0]} ", file=sys.stderr) return 2 mount_dir = Path(sys.argv[1]).resolve() bg_path = Path(sys.argv[2]).resolve() app_name = sys.argv[3] ds_store_bytes = build_ds_store(mount_dir, bg_path, app_name) out_path = mount_dir / ".DS_Store" out_path.write_bytes(ds_store_bytes) return 0 if __name__ == "__main__": raise SystemExit(main())