diff --git a/tests/test_text_tables.py b/tests/test_text_tables.py index 3b601ff579..909f5e2355 100644 --- a/tests/test_text_tables.py +++ b/tests/test_text_tables.py @@ -365,6 +365,46 @@ def test_report_cannot_replace_the_carve_index(self): with open(index, "rb") as f: self.assertEqual(f.read(), b'{"files": []}') + def test_report_cannot_replace_a_file_read_from_a_source_directory(self): + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, "src") + os.makedirs(os.path.join(src, "sub")) + resource = write(os.path.join(src, "a.bin"), make_resource(EN)) + nested = write(os.path.join(src, "sub", "notes.txt"), b"keep") + for flag, target, old in (("--tables", nested, b"keep"), ("--strings", resource, make_resource(EN))): + with contextlib.redirect_stderr(io.StringIO()) as err, self.assertRaises(SystemExit) as code: + T.main([src, flag, target]) + self.assertEqual(code.exception.code, 2) + self.assertIn(f"{flag} would overwrite the input", err.getvalue()) + with open(target, "rb") as f: + self.assertEqual(f.read(), old) + + def test_new_report_inside_a_source_directory_is_still_written(self): + with tempfile.TemporaryDirectory() as tmp: + src = os.path.join(tmp, "src") + os.mkdir(src) + write(os.path.join(src, "a.bin"), make_resource(EN)) + out = os.path.join(src, "tables.csv") + with contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(T.main([src, "--tables", out]), 0) + self.assertTrue(os.path.isfile(out)) + + def test_report_cannot_replace_a_carved_stream(self): + with tempfile.TemporaryDirectory() as tmp: + carve = os.path.join(tmp, "carve") + os.mkdir(carve) + data = make_resource(EN) + stream = write(os.path.join(carve, f"{0x1100:012x}.bin"), data) + with open(os.path.join(carve, "index.json"), "w", encoding="utf-8") as fh: + json.dump({"files": [{"off": 0x1100, "stored": len(data), "size": len(data), "pre": "", + "head": data[:16].hex()}]}, fh) + with contextlib.redirect_stderr(io.StringIO()) as err, self.assertRaises(SystemExit) as code: + T.main([carve, "--dialogue", stream]) + self.assertEqual(code.exception.code, 2) + self.assertIn("--dialogue would overwrite the input", err.getvalue()) + with open(stream, "rb") as f: + self.assertEqual(f.read(), data) + def test_two_outputs_naming_one_file_are_refused(self): with tempfile.TemporaryDirectory() as tmp: src = os.path.join(tmp, "src") diff --git a/tools/text_tables.py b/tools/text_tables.py index 37638c7d06..dacd751a3f 100644 --- a/tools/text_tables.py +++ b/tools/text_tables.py @@ -267,6 +267,25 @@ def scan_failed(error: OSError) -> None: yield path, 0, fh.read() +def input_under(source: str, address: str): + """The input file below directory `source` that a report at `address` would overwrite, or None. + + blobs() reads every file below a plain directory and every carved stream beside an rpf9_carve.py index.json, + so checking a report only against the named sources left it free to replace one of those files. + """ + if os.path.isfile(os.path.join(source, "index.json")): + import gamedata_index + return gamedata_index.carved_stream(address, source) + if not os.path.isfile(address): # a new file is written after the scan, so it was never read + return None + from report_paths import same_report_path + for root, _dirs, names in os.walk(source): # an unlistable folder fails the scan before anything is written + for name in names: + if same_report_path(address, os.path.join(root, name)): + return os.path.join(root, name) + return None + + def nested_archives(path: str) -> list: """Offsets of the RPF9 archives nested in an archive file, in order. @@ -464,6 +483,11 @@ def main(argv=None) -> int: index = os.path.join(source, "index.json") if os.path.isdir(source) and os.path.isfile(index) and any(_same_output_path(index, o) for o in outs): ap.error(f"an output must not overwrite an input: {index}") + if os.path.isdir(source): + for flag, path in out_flags: + clash = input_under(source, path) + if clash: + ap.error(f"{flag} would overwrite the input {clash}") scan = Scan() try: