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
43 changes: 43 additions & 0 deletions tests/test_meshlet_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,49 @@ def test_json_cannot_create_a_report_in_place_of_the_carve_manifest(self):
self.assertNotIn("Traceback", r.stderr)
self.assertFalse(os.path.exists(manifest))

def test_json_cannot_create_the_manifest_under_another_case(self):
# normcase keeps case on macOS, so carve/index.json and CARVE/index.json used to pass as two paths.
with tempfile.TemporaryDirectory() as tmp:
carve = os.path.join(tmp, "carve")
os.mkdir(carve)
with open(os.path.join(carve, "0000.bin"), "wb") as fh:
fh.write(page(block(4)))
probe = os.path.join(carve, "Probe.tmp")
with open(probe, "wb"):
pass
insensitive = os.path.exists(os.path.join(carve, "probe.tmp"))
os.remove(probe)
if not insensitive:
self.skipTest("volume keeps case")
other = os.path.join(tmp, "CARVE", "index.json")
r = self.run_tool(carve, "--json", other, "--quiet")
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("would overwrite the input", r.stderr)
self.assertFalse(os.path.exists(os.path.join(carve, "index.json")))

def test_json_cannot_replace_a_page_under_another_case_of_the_folder(self):
# normcase(abspath()) keeps case on macOS, so CARVE/0000.bin was not inside carve.
with tempfile.TemporaryDirectory() as tmp:
carve = os.path.join(tmp, "carve")
os.mkdir(carve)
bin_path = os.path.join(carve, "0000.bin")
data = page(block(4))
with open(bin_path, "wb") as fh:
fh.write(data)
probe = os.path.join(carve, "Probe.tmp")
with open(probe, "wb"):
pass
insensitive = os.path.exists(os.path.join(carve, "probe.tmp"))
os.remove(probe)
if not insensitive:
self.skipTest("volume keeps case")
other = os.path.join(tmp, "CARVE", "0000.bin")
r = self.run_tool(carve, "--json", other, "--quiet")
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("would overwrite the input", r.stderr)
with open(bin_path, "rb") as fh:
self.assertEqual(fh.read(), data)

def test_json_over_carve_dir_manifest_or_payload_is_refused(self):
with tempfile.TemporaryDirectory() as tmp:
carve = os.path.join(tmp, "carve")
Expand Down
21 changes: 9 additions & 12 deletions tools/meshlet_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
import sys
import zlib

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # -I keeps the script directory off sys.path
from report_paths import path_key, same_report_path as same_output_path # noqa: E402

BLOCK_MAGIC = 0x3063
OTHER_BLOCK_MAGIC = 0x4042
HEADER_SIZE = 0x60
Expand Down Expand Up @@ -915,15 +918,6 @@ def buffer(at):
return {"vertex_block": vertex, "meshlet_group": index, "vertices": count}


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 is_scanned_input(path, input_path):
"""True when a report path is one of the files the scan reads: writing it would destroy the input."""
if same_output_path(path, input_path):
Expand All @@ -935,9 +929,12 @@ def is_scanned_input(path, input_path):
return True
if not os.path.isfile(path):
return False
root = os.path.normcase(os.path.abspath(input_path))
target = os.path.normcase(os.path.abspath(path))
if os.path.dirname(target) != root and not target.startswith(root + os.sep):
# path_key folds case on a volume that ignores it. normcase(abspath()) keeps the typed case on macOS, so a
# page.bin inside CARVE was not seen as inside carve.
root = path_key(input_path)
target = path_key(path)
prefix = root if root.endswith(os.sep) else root + os.sep
if target != root and not target.startswith(prefix):
return False
base = os.path.basename(target)
return base.endswith(".bin") or base.endswith(".rpf")
Expand Down