Merge pull request #1452 from Dashhhhhhhh/fix/world-interior-names-case-inside

world_interior_names: refuse a report inside another case of the carve folder (WORLD-INTERIOR-NAMES-CASE-INSIDE)
This commit is contained in:
Aslan2142
2026-12-01 16:16:17 +01:00
committed by GitHub
2 changed files with 31 additions and 4 deletions

View File

@ -370,6 +370,28 @@ class CliTest(unittest.TestCase):
self.assertEqual(f.read(), data, p)
self.assertFalse(os.path.exists(os.path.join(folder, "r.json")))
def test_out_inside_another_case_of_the_carve_folder_is_refused(self):
# normcase(realpath()) keeps the typed case on macOS, so CARVED/r.json used to be outside carved.
folder = os.path.join(self.tmp.name, "carved")
os.makedirs(folder)
probe = os.path.join(folder, "Probe.tmp")
with open(probe, "wb"):
pass
insensitive = os.path.exists(os.path.join(folder, "probe.tmp"))
os.remove(probe)
if not insensitive:
self.skipTest("volume keeps case")
out = os.path.join(self.tmp.name, "CARVED", "r.json")
err = io.StringIO()
sys.stderr, saved = err, sys.stderr
try:
with redirect_stdout(io.StringIO()):
self.assertEqual(T.main([folder, "--names", self.names, "--out", out]), 2)
finally:
sys.stderr = saved
self.assertIn("would overwrite an input", err.getvalue())
self.assertFalse(os.path.exists(os.path.join(folder, "r.json")))
def fails_with_one_line(self, argv):
"""main(argv) returns 2 with exactly one stderr line, prints nothing and writes no report."""
out = os.path.join(self.tmp.name, "fail", "r.json")

View File

@ -45,7 +45,7 @@ if _TOOLS_DIR not in sys.path:
import world_archetypes as WA # noqa: E402
import world_index as WI # noqa: E402
from pool_workers import process_workers # noqa: E402
from report_paths import same_report_path # noqa: E402
from report_paths import path_key, same_report_path # noqa: E402
from world_names import hash64, joaat # noqa: E402
J = WA.joaat_cs
@ -363,11 +363,16 @@ def _inside(folder, path):
"""True when ``folder`` is a folder and ``path`` lies in it (a report there would be read as a carved file)."""
if not os.path.isdir(folder):
return False
root = os.path.normcase(os.path.realpath(folder))
# path_key folds case on a volume that ignores it. normcase(realpath()) keeps the typed case on macOS.
try:
return os.path.commonpath([root, os.path.normcase(os.path.realpath(path))]) == root
except ValueError: # different drives on Windows
root = path_key(folder)
target = path_key(path)
except OSError:
return False
if target == root:
return True
prefix = root if root.endswith(os.sep) else root + os.sep
return target.startswith(prefix)
def main(argv=None):