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_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,49 @@ def test_walk_normalizes_to_forward_slashes_and_yields_all_files(self):
self.assertTrue(os.path.isabs(abs_path))
self.assertTrue(os.path.exists(abs_path))

def _case_insensitive(self, folder):
probe = os.path.join(folder, "Probe.tmp")
with open(probe, "wb"):
pass
folded = os.path.exists(os.path.join(folder, "probe.tmp"))
os.remove(probe)
return folded

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):
self.skipTest("volume keeps case")
with open(os.path.join(self.pkg, "Manifest.json"), "w", encoding="utf-8", newline="\n") as f:
f.write("{}\n")
other = os.path.join(self.tmp.name, "PKG", "Manifest.json")
rels = [rel for rel, _ in manifest.walk(self.pkg, exclude=other)]
self.assertNotIn("Manifest.json", rels)
self.assertIn("root.dat", rels)

def test_create_replaces_a_manifest_under_another_case_and_verify_ignores_it(self):
if not self._case_insensitive(self.pkg):
self.skipTest("volume keeps case")
other = os.path.join(self.tmp.name, "PKG", "Manifest.json")
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(manifest.create(self.pkg, other, jobs=1), 0)
self.assertEqual(manifest.create(self.pkg, other, jobs=1), 0)
out = io.StringIO()
with contextlib.redirect_stdout(out):
self.assertEqual(manifest.verify(self.pkg, other, jobs=1), 0)
self.assertEqual(out.getvalue(), "OK\n")
self.assertNotIn("Manifest.json", manifest.load_manifest(other))

def test_create_refuses_another_case_of_a_hashed_file(self):
if not self._case_insensitive(self.pkg):
self.skipTest("volume keeps case")
target = os.path.join(self.tmp.name, "PKG", "ROOT.DAT")
with open(os.path.join(self.pkg, "root.dat"), "rb") as f:
before = f.read()
with contextlib.redirect_stdout(io.StringIO()), self.assertRaises(manifest.ManifestError):
manifest.create(self.pkg, target, jobs=1)
with open(os.path.join(self.pkg, "root.dat"), "rb") as f:
self.assertEqual(f.read(), before)

def test_walk_reports_an_unreadable_subdirectory(self):
blocked = os.path.join(self.pkg, "sub", "deep")
scandir = os.scandir
Expand Down
35 changes: 20 additions & 15 deletions tools/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# -I keeps the script directory off sys.path, so add it explicitly before importing the sibling module.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from report_paths import same_report_path # noqa: E402
from report_paths import path_key, same_report_path # noqa: E402

CHUNK = 8 * 1024 * 1024
DEFAULT = os.path.join(os.path.dirname(__file__), "..", "reports", "manifest.json")
Expand Down Expand Up @@ -91,23 +91,33 @@ def sha256(path: str, *, expected_size: int | None = None) -> str:
return _hash_file(path, expected_size=expected_size)[1]


def _inside(parent: str, child: str) -> bool:
"""True when `child` is `parent` or a file inside it, including another case spelling of that folder."""
try:
folder = path_key(parent)
target = path_key(child)
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 walk(game_dir: str, *, exclude=None):
def scan_failed(error: OSError) -> None:
raise error

excluded_dir = excluded_name = None
if exclude is not None:
directory, name = os.path.split(os.path.abspath(exclude))
excluded_dir = os.path.normcase(os.path.realpath(directory))
excluded_name = os.path.normcase(name)
# 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

# os.walk otherwise treats missing or unreadable directories as empty.
for root, _, files in os.walk(game_dir, onerror=scan_failed):
contains_manifest = excluded_dir is not None and os.path.normcase(os.path.realpath(root)) == excluded_dir
for name in sorted(files):
if contains_manifest and os.path.normcase(name) == excluded_name:
continue
p = os.path.join(root, name)
if excluded is not None and path_key(p) == excluded:
continue
rel = os.path.relpath(p, game_dir)
if os.sep != "/":
rel = rel.replace(os.sep, "/")
Expand All @@ -124,12 +134,7 @@ def create(game_dir: str, out: str, jobs: int = 1) -> int:
if os.path.isdir(out):
raise ManifestError(f"--out is a folder, not a manifest file: {out}")
if os.path.isfile(out):
try:
inside = os.path.normcase(os.path.commonpath(
[os.path.realpath(game_dir), os.path.realpath(out)])) == os.path.normcase(os.path.realpath(game_dir))
except ValueError:
inside = False
if inside:
if _inside(game_dir, out):
try:
load_manifest(out)
except (ManifestError, OSError):
Expand Down