Write archive members as regular files again (mode 0o100644, as main did) and check a stored archive with testzip before restoring it, building it again when it is damaged

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 17:53:38 -07:00
1 parent 622ffc74be
commit d0782b0d58
2 files changed
+32 -4

No files matched your search

+5 -4
View File
@@ -17,6 +17,7 @@ import os
import shutil
import time
import zipfile
import zlib
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
@@ -599,8 +600,8 @@ def build_archive(name, recs, out_dir, stats):
try:
shutil.copyfile(kept, tmp)
with zipfile.ZipFile(tmp) as zf:
restored = zf.namelist() == expected
except (OSError, zipfile.BadZipFile):
restored = zf.namelist() == expected and zf.testzip() is None # whole, not just named right
except (OSError, zipfile.BadZipFile, zlib.error, EOFError, NotImplementedError):
restored = False
if restored:
os.replace(tmp, path)
@@ -612,12 +613,12 @@ def build_archive(name, recs, out_dir, stats):
# one made elsewhere to a build here).
info = zipfile.ZipInfo("README.txt", date_time=ARCHIVE_TIME)
info.compress_type = zipfile.ZIP_DEFLATED
info.external_attr = 0o644 << 16
info.external_attr = 0o100644 << 16
zf.writestr(info, archive_readme(name))
for arc, src in members:
info = zipfile.ZipInfo(arc, date_time=ARCHIVE_TIME)
info.compress_type = zipfile.ZIP_DEFLATED
info.external_attr = 0o644 << 16
info.external_attr = 0o100644 << 16
with open(src, "rb") as source, zf.open(info, "w") as member:
shutil.copyfileobj(source, member, 1 << 20)
try:
+27
View File
@@ -260,6 +260,33 @@ class BuildTreeTest(unittest.TestCase):
self.assertNotEqual(os.path.getmtime(gpx), stamps[0])
self.assertNotEqual(os.path.getmtime(geojson), stamps[1])
def test_archive_members_are_regular_files_with_the_usual_mode(self):
self.build()
for name in ("all-gpx.zip", "all-geojson.zip"):
with zipfile.ZipFile(self.path("data", name)) as zf:
for info in zf.infolist():
self.assertEqual(info.external_attr >> 16, 0o100644, f"{name}: {info.filename}")
def test_a_damaged_stored_archive_is_not_restored(self):
self.build()
gpx = self.path("data", "all-gpx.zip")
with open(gpx, "rb") as f:
good = f.read()
kept = os.path.join(build.CACHE_DIR, "store", "archives")
stored = [os.path.join(kept, n) for n in os.listdir(kept) if n.startswith("all-gpx.zip.")]
self.assertEqual(len(stored), 1)
with open(stored[0], "rb") as f:
damaged = bytearray(f.read())
damaged[len("README.txt") + 30 + 4] ^= 0xFF # inside the first member's data; the names stay
with open(stored[0], "wb") as f:
f.write(damaged)
os.remove(gpx) # so the store is where the archive comes from
self.build()
with zipfile.ZipFile(gpx) as zf:
self.assertIsNone(zf.testzip())
with open(gpx, "rb") as f:
self.assertEqual(f.read(), good)
def test_missing_declared_line_warns(self):
os.remove(os.path.join(self.root, "avon", "line.geojson"))
warnings = self.build()