Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_rbf0_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ def test_directory_report_cannot_replace_a_carve_input(self):
with open(path, "rb") as f:
self.assertEqual(f.read(), original)

def test_new_report_with_a_case_variant_of_the_carve_manifest(self):
carve = os.path.join(self.root, "carve")
os.mkdir(carve)
manifest = os.path.join(carve, "index.json")
report = os.path.join(carve, "INDEX.JSON")
with open(manifest, "xb"):
pass
folds_case = os.path.exists(report)
os.remove(manifest) # The guard must work before either path exists.

result = self.run_tool(carve, report)
if folds_case:
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("would overwrite a carve input", result.stderr)
self.assertFalse(os.path.exists(report))
else:
self.assertEqual(result.returncode, 0, result.stderr)
with open(report, encoding="utf-8") as f:
self.assertEqual(json.load(f)["files"], [])
self.assertFalse(os.path.exists(manifest))

def test_directory_report_cannot_replace_a_stream_via_a_hard_link(self):
stream = os.path.join(self.root, "00000001.bin")
out = os.path.join(self.root, "report.csv")
Expand Down
19 changes: 19 additions & 0 deletions tests/test_rbf0_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,25 @@ def test_out_and_csv_on_the_same_file_are_refused(self):
self.assertIn("must be different files", result.stderr)
self.assertFalse(os.path.exists(out))

def test_new_json_and_csv_paths_that_differ_only_in_case(self):
source = os.path.join(self.root, "sample.bin")
with open(source, "wb") as f:
f.write(wide_blob())
out = os.path.join(self.root, "tree.json")
table = os.path.join(self.root, "TREE.JSON")

result = self._run_cli(source, out, "--csv", table)
if os.path.exists(os.path.join(self.root, "SAMPLE.BIN")):
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("must be different files", result.stderr)
self.assertFalse(os.path.exists(out))
else:
self.assertEqual(result.returncode, 0, result.stderr)
with open(out, encoding="utf-8") as f:
self.assertEqual(json.load(f)["summary"]["resources"], 1)
with open(table, encoding="utf-8", newline="") as f:
self.assertEqual(next(csv.DictReader(f))["root"], "TestRoot")

def test_root_filter_and_limit(self):
with open(os.path.join(self.root, "00000000.bin"), "wb") as f:
f.write(wide_blob())
Expand Down
17 changes: 4 additions & 13 deletions tools/rbf0_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
sys.path.insert(0, ROOT)
from file_mode import published_mode # noqa: E402
from replace_retry import replace as replace_retry # noqa: E402
from report_paths import same_report_path # noqa: E402
from zero_span import gap_split # noqa: E402

MAGIC = b"RBF0"
Expand Down Expand Up @@ -539,16 +540,6 @@ def byte_count(text):
return value


def same_output_path(first, second):
"""True when writing ``second`` would truncate ``first`` (same file, path or hard link)."""
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():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("source", help="an .rpf archive or a directory of carved streams")
Expand All @@ -559,7 +550,7 @@ def main():
ap.add_argument("--all-strings", action="store_true", help="write every readable string to JSON (game data)")
ap.add_argument("--quiet", action="store_true")
a = ap.parse_args()
if same_output_path(a.source, a.out):
if same_report_path(a.source, a.out):
ap.error("output must not overwrite the source")
if a.strings < 0:
ap.exit(2, f"{ap.prog}: error: argument --strings: must be 0 or more\n")
Expand All @@ -574,8 +565,8 @@ def main():
try:
if os.path.isdir(a.source):
manifest = os.path.join(a.source, "index.json")
if same_output_path(manifest, a.out) or (os.path.isfile(a.out) and any(
same_output_path(path, a.out) for _, path in carved_streams(a.source))):
if same_report_path(manifest, a.out) or (os.path.isfile(a.out) and any(
same_report_path(path, a.out) for _, path in carved_streams(a.source))):
ap.error(f"output would overwrite a carve input: {a.out}")
rows, records, summary = scan_dir(a.source, string_limit=limit)
else:
Expand Down
18 changes: 5 additions & 13 deletions tools/rbf0_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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):
Expand Down