manifest: resolve the stored entry on Linux casefold directories

This commit is contained in:
merzleckeier
2026-12-02 14:34:41 +01:00
parent 044339e4c0
commit ac2ea358ef
2 changed files with 98 additions and 11 deletions

View File

@ -117,6 +117,63 @@ class ManifestWalkTest(unittest.TestCase):
os.remove(probe)
return folded
def test_create_distinguishes_a_separate_case_variant_hard_link(self):
if self._case_insensitive(self.pkg):
self.skipTest("volume folds case")
target = os.path.join(self.pkg, "Manifest.json")
alias = os.path.join(self.pkg, "manifest.json")
with contextlib.redirect_stdout(io.StringIO()):
manifest.create(self.pkg, target, jobs=1)
try:
os.link(target, alias)
except OSError:
self.skipTest("hard links are not available")
with contextlib.redirect_stdout(io.StringIO()), self.assertRaisesRegex(manifest.ManifestError, "overwrite the input"):
manifest.create(self.pkg, alias, jobs=1)
def test_create_and_verify_with_folded_manifest_basename(self):
if not self._case_insensitive(self.pkg):
self.skipTest("volume keeps case")
cases = [("Manifest.json", "manifest.JSON"), ("Résumé.json", "re\u0301sume\u0301.JSON"),
("Report.json", "Re\u200bport.json")]
for name, spelling in cases:
with self.subTest(spelling=spelling):
original = os.path.join(self.pkg, name)
alternate = os.path.join(self.pkg, spelling)
with contextlib.redirect_stdout(io.StringIO()):
manifest.create(self.pkg, original, jobs=1)
if not os.path.exists(alternate):
os.unlink(original)
self.skipTest("volume distinguishes these spellings")
self.assertTrue(os.path.samefile(original, alternate))
expected = manifest.load_manifest(original)
try:
output = io.StringIO()
with contextlib.redirect_stdout(output):
self.assertEqual(manifest.verify(self.pkg, alternate, jobs=1), 0)
self.assertEqual(output.getvalue(), "OK\n")
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(manifest.create(self.pkg, alternate, jobs=1), 0)
self.assertEqual(manifest.load_manifest(alternate), expected)
finally:
os.unlink(alternate)
def test_walk_keeps_a_manifest_hard_link_in_a_folded_directory(self):
if not (hasattr(os, "O_PATH") and os.path.isdir("/proc/self/fd") and self._case_insensitive(self.pkg)):
self.skipTest("needs Linux casefold and procfs")
original = os.path.join(self.pkg, "Manifest.json")
alternate = os.path.join(self.pkg, "manifest.JSON")
link = os.path.join(self.pkg, "copy.json")
with contextlib.redirect_stdout(io.StringIO()):
manifest.create(self.pkg, original, jobs=1)
os.link(original, link)
self.assertTrue(os.path.samefile(link, alternate))
files = dict(manifest.walk(self.pkg, exclude=alternate))
self.assertNotIn("Manifest.json", files)
self.assertIn("copy.json", files)
with contextlib.redirect_stdout(io.StringIO()), self.assertRaisesRegex(manifest.ManifestError, "overwrite the input"):
manifest.create(self.pkg, alternate, jobs=1)
def test_walk_excludes_another_case_of_the_manifest(self):
# normcase(realpath()) keeps the typed case on macOS, so PKG/Manifest.json stayed in a scan of pkg.
if not self._case_insensitive(self.pkg):

View File

@ -93,31 +93,61 @@ def sha256(path: str, *, expected_size: int | None = None) -> str:
def _inside(parent: str, child: str) -> bool:
"""True when `child` is `parent` or a file inside it, including another case spelling of that folder."""
"""True when the existing child resolves within the existing parent."""
try:
folder = path_key(parent)
target = path_key(child)
target = os.path.realpath(child)
folder = os.stat(parent)
while True:
if os.path.samestat(folder, os.stat(target)):
return True
ancestor = os.path.dirname(target)
if ancestor == target:
return False
target = ancestor
except OSError:
return False
if target == folder:
return True
prefix = folder if folder.endswith(os.sep) else folder + os.sep
return target.startswith(prefix)
def _manifest_entry(root, files, target):
"""Find the existing manifest's directory entry, including spellings the OS treats as aliases."""
directory, name = os.path.split(target)
try:
if not os.path.samefile(root, directory):
return None
if name in files:
return name
inode = os.stat(target).st_ino
with os.scandir(root) as entries:
candidates = [entry.name for entry in entries if entry.inode() == inode]
if len(candidates) == 1:
return candidates[0]
if candidates and hasattr(os, "O_PATH"):
# On Linux the descriptor identifies the opened directory entry even when it has hard links.
fd = os.open(target, os.O_PATH)
try:
name = os.path.basename(os.readlink(f"/proc/self/fd/{fd}"))
finally:
os.close(fd)
if name in candidates:
return name
except OSError:
pass
return None
def walk(game_dir: str, *, exclude=None):
def scan_failed(error: OSError) -> None:
raise error
# path_key folds case on a volume that ignores it. normcase(realpath()) keeps the typed case on macOS, so
# Game/manifest.json and game/manifest.json used to stay in the scan.
excluded = path_key(exclude) if exclude is not None else None
target = os.path.realpath(exclude) if exclude is not None else None
excluded = path_key(target) if target is not None else None
# os.walk otherwise treats missing or unreadable directories as empty.
for root, _, files in os.walk(game_dir, onerror=scan_failed):
entry = _manifest_entry(root, files, target) if target is not None else None
for name in sorted(files):
p = os.path.join(root, name)
if excluded is not None and path_key(p) == excluded:
if name == entry or (excluded is not None and path_key(p) == excluded):
continue
rel = os.path.relpath(p, game_dir)
if os.sep != "/":