Files
HyperionOS/tools/nc_mkdisk_meta.py

43 lines
1.6 KiB
Python

#!/usr/bin/env python3
import json
import struct
import sys
from pathlib import Path
def main():
root = Path(sys.argv[1])
src = Path(sys.argv[2]) if len(sys.argv) > 2 else Path(__file__).resolve().parent.parent / "Src"
links = {}
for sj in sorted(src.glob("*/control/symlinks.json")):
try:
data = json.loads(sj.read_text())
except Exception as e:
print(f"warning: cannot parse {sj}: {e}")
continue
for link, target in data.items():
if not link.startswith("/") or not target.startswith("/"):
print(f"warning: skipping non-absolute symlink {link} -> {target}")
continue
parent, _, name = link.rpartition("/")
if not parent:
parent = "/"
links.setdefault(parent, {})[name] = target
PERMS = 32 + 16 + 512 + 8 + 4 + 256 + 2 + 1 + 128 # vfs.PERM.RWXRWXRWX
for parent, entries in sorted(links.items()):
d = root.joinpath(*[p for p in parent.split("/") if p])
d.mkdir(parents=True, exist_ok=True)
blob = struct.pack("B", 0x02)
for name in sorted(entries):
tgt = entries[name].encode()
blob += struct.pack("B", len(name)) + name.encode()
blob += struct.pack("B", 0x01) # etype symlink
blob += struct.pack("<HHH", 0, 0, PERMS)
blob += struct.pack("B", len(tgt)) + tgt
(d / ".meta").write_bytes(blob)
print(f"symlinks in {parent}: " + ", ".join(f"{k} -> {v}" for k, v in sorted(entries.items())))
if __name__ == "__main__":
main()