From afb684f9536105799f8311c807eb9edc23d61738 Mon Sep 17 00:00:00 2001 From: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:22:08 +0100 Subject: [PATCH 1/4] rbf0_tree: guard out/--csv/--xml with the shared report_paths.same_report_path The private same_output_path copy compared realpath+normcase and samefile only, so a case-only spelling of the report path on a volume that folds case without normpath saying so (a vfat/exFAT/CIFS mount on Linux) passed the guard and the report replaced the source stream. tools/report_paths.py asks the volume itself for that pair. --- tools/rbf0_tree.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tools/rbf0_tree.py b/tools/rbf0_tree.py index 3e7adbb8e7..385645f694 100644 --- a/tools/rbf0_tree.py +++ b/tools/rbf0_tree.py @@ -31,6 +31,7 @@ import rbf0_index # noqa: E402 from file_mode import published_mode # noqa: E402 +from report_paths import same_report_path # noqa: E402 MAGIC = rbf0_index.MAGIC PROLOGUE = rbf0_index.PROLOGUE @@ -677,15 +678,6 @@ def write_json(path, docs, summary): raise -def same_output_path(first, second): - if os.path.normcase(os.path.realpath(first)) == os.path.normcase(os.path.realpath(second)): - return True - try: - return os.path.samefile(first, second) - except OSError: - return False - - def main(argv=None): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("source", help="a carve directory, a complete resource, or a single RBF0 stream") @@ -699,11 +691,11 @@ def main(argv=None): args = ap.parse_args(argv) # The reports are written after the decode: an out or --csv naming the source used to replace it, and the # same file for both left only the CSV. - if args.csv and same_output_path(args.out, args.csv): + if args.csv and same_report_path(args.out, args.csv): ap.error("out and --csv must be different files") if os.path.isfile(args.source): for flag, path in (("out", args.out), ("--csv", args.csv)): - if path and same_output_path(path, args.source): + if path and same_report_path(path, args.source): ap.error(f"{flag} would overwrite the source {args.source}") elif os.path.isdir(args.source): # A carve folder: out or --csv naming one of its streams used to replace that stream, and --xml on the @@ -714,9 +706,9 @@ def main(argv=None): except OSError: streams = [] # decode_source reports the unreadable folder for flag, path in (("out", args.out), ("--csv", args.csv)): - if path and os.path.exists(path) and any(same_output_path(path, s) for s in streams): + if path and os.path.exists(path) and any(same_report_path(path, s) for s in streams): ap.error(f"{flag} would overwrite the stream {path} in the source folder") - if args.xml and same_output_path(args.xml, args.source): + if args.xml and same_report_path(args.xml, args.source): ap.error("--xml must not be the source folder: its XML files would be read as streams") def progress(n, total): From 83460eca1ec551f337a4ad6e2c7e5c22e735bfdd Mon Sep 17 00:00:00 2001 From: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:22:08 +0100 Subject: [PATCH 2/4] test_report_paths: cover rbf0_tree in the shared-helper list Without the import the new entry fails, so a re-added private copy is caught by the suite. --- tests/test_report_paths.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_report_paths.py b/tests/test_report_paths.py index d62bcd78c6..d2ca82a650 100644 --- a/tests/test_report_paths.py +++ b/tests/test_report_paths.py @@ -21,7 +21,7 @@ # Tools that use report_paths and must not define their own copy again. SHARED = ("anim_index", "awc_manifest", "gamedata_index", "gamedata_xml", "glb_inspect", "navmesh_index", - "rpf_probe", "tile_probe", "world_lights_paths", "world_names") + "rbf0_tree", "rpf_probe", "tile_probe", "world_lights_paths", "world_names") # The one tool that still carries its own copy (an open pull request edits its call site). Do not add to this list: # import report_paths instead. LEGACY_COPIES = {"navmesh_decode"} From 3ecbdc31741f55be20ca4e9fdff2805a4d256fbd Mon Sep 17 00:00:00 2001 From: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:52:43 +0100 Subject: [PATCH 3/4] rbf0_tree: name the XML exports once and refuse one that lands on a stream or a report A single stream file named 00000000.xml got the first XML export name, so --xml on its folder replaced the stream with XML and exited 0. The generated names now come from one xml_names() list that both the writer and the pre-publication guard read, and an --xml folder whose export path is the source, the out JSON or the CSV is refused before anything is written. --- tools/rbf0_tree.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/rbf0_tree.py b/tools/rbf0_tree.py index 385645f694..53499c43cb 100644 --- a/tools/rbf0_tree.py +++ b/tools/rbf0_tree.py @@ -569,12 +569,27 @@ def _xml_node(parent, node, tag=None): return el +def xml_names(docs): + """The file name each resource gets under --xml, in the same order as `docs`. + + The directory and the guard that refuses to replace a stream both read this list, so they cannot disagree. + """ + names = [] + used = set() + for i, doc in enumerate(docs): + name = f"{doc['offset']:08x}.xml" + if name in used: + name = f"{doc['offset']:08x}-{i:04d}.xml" + used.add(name) + names.append(name) + return names + + def write_xml(directory, docs): """Write one generic XML file per resource. Element names are fixed, so any descriptor name is safe.""" os.makedirs(directory, exist_ok=True) paths = [] - used = set() - for i, doc in enumerate(docs): + for name, doc in zip(xml_names(docs), docs): root = ET.Element("rbf0", { "root": _xml_safe(doc["root_name"]), "layout": doc["layout"], "offset": f"0x{doc['offset']:x}", @@ -585,10 +600,6 @@ def write_xml(directory, docs): if doc["tree"] is not None: _xml_node(root, doc["tree"]) ET.indent(root, space=" ") - name = f"{doc['offset']:08x}.xml" - if name in used: - name = f"{doc['offset']:08x}-{i:04d}.xml" - used.add(name) path = os.path.join(directory, name) # XML readers normalize literal carriage returns. Escape them after serializing ordinary XML text. xml = ET.tostring(root, encoding="utf-8", xml_declaration=True).replace(b"\r", b" ") @@ -719,6 +730,13 @@ def progress(n, total): try: docs, summary = decode_source(path, roots=args.root, offset=args.offset, limit=args.limit, progress=progress) + if args.xml: + # A stream named 00000000.xml is also the first XML export name, so --xml on its folder replaced it + # with XML and exited 0. Refuse before the reports are published, with the source bytes intact. + for name in xml_names(docs): + target = os.path.join(args.xml, name) + if any(other and same_report_path(target, other) for other in (args.source, args.out, args.csv)): + ap.error(f"--xml would overwrite {target}") path = args.out write_json(path, docs, summary) if args.csv: From 0492a6a9221024e4bd7329f89750a543e406ab12 Mon Sep 17 00:00:00 2001 From: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:52:43 +0100 Subject: [PATCH 4/4] tests: an --xml folder may not replace a stream named 00000000.xml or the JSON report Covers both collisions: the source stream is the first export name, and --out names that same export. Asserts exit 2, the path error, no traceback, the source bytes intact and no XML or JSON published. --- tests/test_rbf0_tree.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_rbf0_tree.py b/tests/test_rbf0_tree.py index ff55acd4c7..184ac1bd69 100644 --- a/tests/test_rbf0_tree.py +++ b/tests/test_rbf0_tree.py @@ -497,6 +497,35 @@ def test_out_csv_or_xml_over_a_stream_of_the_source_folder_is_refused(self): self.assertEqual(f.read(), blob) self.assertEqual(sorted(os.listdir(self.root)), ["00000000.bin"]) + def test_xml_export_over_a_stream_or_the_report_is_refused(self): + # A stream named 00000000.xml carries the first XML export name, so --xml on its folder replaced it. + blob = wide_blob() + source = os.path.join(self.root, "resource.bin") + with open(source, "wb") as f: + f.write(blob) + xml_dir = os.path.join(self.root, "xml") + os.makedirs(xml_dir) + stream = os.path.join(xml_dir, "00000000.xml") + with open(stream, "wb") as f: + f.write(blob) + out = os.path.join(self.root, "tree.json") + empty = os.path.join(self.root, "xml2") + os.makedirs(empty) + cases = (((stream, out, "--xml", xml_dir), stream), + ((source, os.path.join(empty, "00000000.xml"), "--xml", empty), None)) + for argv, kept in cases: + with self.subTest(argv=argv): + result = self._run_cli(*argv) + self.assertEqual(result.returncode, 2, result.stderr) + self.assertIn("--xml would overwrite", result.stderr) + self.assertNotIn("Traceback", result.stderr) + if kept: + with open(kept, "rb") as f: + self.assertEqual(f.read(), blob) + self.assertEqual(os.listdir(xml_dir), ["00000000.xml"]) + self.assertEqual(os.listdir(empty), []) + self.assertFalse(os.path.exists(out)) + def test_reports_beside_the_streams_are_still_allowed(self): with open(os.path.join(self.root, "00000000.bin"), "wb") as f: f.write(wide_blob())