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
14 changes: 12 additions & 2 deletions tests/test_restofworld_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,14 @@ class FolderModeStoredBytesTest(unittest.TestCase):
"""A carve folder's stored (compressed) sizes and span come from rpf9_carve.py's index.json, as in archive mode;
without an index the length on disk is all there is."""

def make(self, tmp, with_index):
def make(self, tmp, with_index, encoding="utf-8"):
data = b"raw bytes " * 100 # 1,000 inflated bytes
with open(os.path.join(tmp, "000000001000.bin"), "wb") as f:
f.write(data)
if with_index:
index = {"summary": {"span": 2000, "zero_padding": 500, "unread_bytes": 1200},
"files": [{"off": 0x1000, "stored": 300, "size": 1000}]}
with open(os.path.join(tmp, "index.json"), "w", encoding="utf-8") as f:
with open(os.path.join(tmp, "index.json"), "w", encoding=encoding) as f:
json.dump(index, f)

def test_index_json_supplies_stored_bytes_and_span(self):
Expand All @@ -522,6 +522,16 @@ def test_index_json_supplies_stored_bytes_and_span(self):
self.assertEqual((summary["compressed_bytes"], summary["inflated_bytes"]), (300, 1000))
self.assertAlmostEqual(summary["readable_fraction"], 300 / 2000, places=4)

def test_index_json_saved_with_a_bom_supplies_stored_bytes(self):
"""A carve index.json saved with a UTF-8 BOM (PowerShell 5.1's default) still supplies the stored sizes.
Read as plain UTF-8, json.load failed, _carve_index returned {}, and the inflated length on disk was
reported as the stored size."""
with tempfile.TemporaryDirectory() as tmp:
self.make(tmp, with_index=True, encoding="utf-8-sig")
rows, summary = restofworld_index.scan_dir(tmp)
self.assertEqual(rows[0]["stored"], 300)
self.assertEqual(summary["compressed_bytes"], 300)

def test_without_an_index_the_length_on_disk_is_used(self):
with tempfile.TemporaryDirectory() as tmp:
self.make(tmp, with_index=False)
Expand Down
2 changes: 1 addition & 1 deletion tools/restofworld_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def scan_rpf(path, start=None, limit=None, max_inflate=MAX_INFLATE):
def _carve_index(path):
"""rpf9_carve.py's index.json in `path`: ({file name: stored bytes}, summary), or ({}, {}) without one."""
try:
with open(os.path.join(path, "index.json"), encoding="utf-8") as f:
with open(os.path.join(path, "index.json"), encoding="utf-8-sig") as f:
index = json.load(f)
stored = {f"{int(e['off']):012x}.bin": int(e["stored"]) for e in index.get("files", [])}
summary = index.get("summary", {})
Expand Down