Handle empty sysdiagnose recovery and partial archive filenames

This commit is contained in:
Janik Besendorf committed 2026-11-17 22:33:26 +01:00
1 parent 34f808c761
commit b9f87fbf36
4 files changed
+54 -34

No files matched your search

+7 -7
View File
@@ -147,7 +147,7 @@ class CmdIOSCheckSysdiagnose(Command):
while compressed.read1(1024 * 1024):
pass
except (EOFError, gzip.BadGzipFile, zlib.error) as exc:
self._warn_incomplete_archive(exc)
self._handle_incomplete_archive(exc)
except (tarfile.ReadError, EOFError, zlib.error, OSError) as exc:
self.log.critical(
"Unable to read the sysdiagnose archive %s: %s. "
@@ -158,8 +158,12 @@ class CmdIOSCheckSysdiagnose(Command):
self.finish()
sys.exit(1)
def _warn_incomplete_archive(self, exc: Exception) -> None:
def _handle_incomplete_archive(self, exc: Exception) -> None:
self.archive_incomplete = True
if not self.sysdiagnose_files:
raise tarfile.ReadError(
"No complete sysdiagnose files could be recovered"
) from exc
self.log.warning(
"The sysdiagnose archive is truncated or damaged: %s. Recovered "
"%d complete files; incomplete files are excluded. Analysis will "
@@ -182,11 +186,7 @@ class CmdIOSCheckSysdiagnose(Command):
for member in archive:
self._extract_member(archive, member, extraction_root, archive_roots)
except (tarfile.ReadError, EOFError, gzip.BadGzipFile, zlib.error) as exc:
self._warn_incomplete_archive(exc)
if not self.sysdiagnose_files:
raise tarfile.ReadError(
"No complete sysdiagnose files could be recovered"
) from exc
self._handle_incomplete_archive(exc)
if len(archive_roots) != 1:
self.finish()
+22 -19
View File
@@ -66,32 +66,35 @@ class SysdiagnoseExtraction(MVTModule):
"utf-8", errors="replace"
)
filenames = re.findall(r"sysdiagnose_\S+?\.tar\.gz", content)
if not filenames and self.target_path:
if self.target_path:
# A recovered archive can be missing sysdiagnose.log. Its original
# filename still records the collection offset, when not renamed.
filename = Path(self.target_path).name
if re.match(
r"sysdiagnose_\d{4}\.\d{2}\.\d{2}_\d{2}-\d{2}-\d{2}[+-]\d{4}(?:_|\.)",
filenames.append(Path(self.target_path).name)
for filename in filenames:
match = re.match(
r"sysdiagnose_(\d{4}\.\d{2}\.\d{2}_[^_\s]+?)(?:_|\.tar\.gz|$)",
filename,
):
filenames = [filename]
if not filenames:
self.log.info(
"Unable to determine the timezone in which the sysdiagnose was "
"generated. Assuming UTC for logs without a timezone."
)
return timezone.utc
if not match:
continue
try:
sysdiagnose_timezone = datetime.strptime(
match[1], "%Y.%m.%d_%H-%M-%S%z"
).tzinfo
except ValueError:
continue
self.log.info(
"Based on the sysdiagnose filename, assuming timezone %s for logs "
"without a timezone.",
sysdiagnose_timezone,
)
return sysdiagnose_timezone
timestamp = "_".join(filenames[0].removesuffix(".tar.gz").split("_")[1:3])
sysdiagnose_timezone = datetime.strptime(
timestamp, "%Y.%m.%d_%H-%M-%S%z"
).tzinfo
self.log.info(
"Based on the sysdiagnose filename, assuming timezone %s for logs "
"without a timezone.",
sysdiagnose_timezone,
"Unable to determine the timezone in which the sysdiagnose was "
"generated. Assuming UTC for logs without a timezone."
)
return sysdiagnose_timezone
return timezone.utc
def _get_files_by_pattern(self, pattern: str) -> list[str]:
file_names = self.tar_files if self.tar else self.files
+14
View File
@@ -95,6 +95,20 @@ def test_check_sysdiagnose_reports_a_truncated_archive(tmp_path, caplog):
assert "Aborted!" not in result.output
def test_damaged_gzip_trailer_without_recoverable_files_fails(tmp_path, caplog):
archive = tmp_path / "sysdiagnose.tar.gz"
with tarfile.open(archive, "w:gz") as tar:
directory = tarfile.TarInfo("sysdiagnose")
directory.type = tarfile.DIRTYPE
tar.addfile(directory)
archive.write_bytes(archive.read_bytes()[:-8])
result = CliRunner().invoke(check_sysdiagnose, [str(archive)])
assert result.exit_code == 1
assert "No complete sysdiagnose files could be recovered" in caplog.text
def test_check_sysdiagnose_recovers_and_saves_warning(tmp_path, caplog):
archive = _recovery_archive(tmp_path, "gzip-body")
module_path = tmp_path / "custom_sysdiagnose.py"
+11 -8
View File
@@ -236,12 +236,18 @@ def test_output_write_error_is_fatal_not_recovered(tmp_path, monkeypatch, caplog
assert command.sysdiagnose_archive is None
@pytest.mark.parametrize("offset", ["+0200", "-0530"])
def test_recovered_timezone_falls_back_to_original_filename(tmp_path, offset):
@pytest.mark.parametrize(
("suffix", "expected"),
[
("+0200_iPhone-OS_iPhone_24A000.tar.gz", timedelta(hours=2)),
("-0530_iPhone-OS_iPhone_24A000.tar.gz", -timedelta(hours=5, minutes=30)),
("+0100.tar.gz.partial", timedelta(hours=1)),
("+2500.tar.gz.partial", timedelta(0)),
],
)
def test_recovered_timezone_falls_back_to_original_filename(tmp_path, suffix, expected):
path = _recovery_archive(tmp_path, "gzip-body")
renamed = path.with_name(
f"sysdiagnose_2026.09.14_20-04-52{offset}_iPhone-OS_iPhone_24A000.tar.gz"
)
renamed = path.with_name(f"sysdiagnose_2026.09.14_20-04-52{suffix}")
path.rename(renamed)
command = CmdIOSCheckSysdiagnose(target_path=str(renamed))
try:
@@ -250,9 +256,6 @@ def test_recovered_timezone_falls_back_to_original_filename(tmp_path, offset):
command.module_init(module)
# Simulate the diagnostic log being in the missing part of an archive.
module.files = [f for f in module.files if not f.endswith("/sysdiagnose.log")]
expected = (
timedelta(hours=2) if offset == "+0200" else -timedelta(hours=5, minutes=30)
)
assert module._extract_timezone().utcoffset(None) == expected
finally:
command.finish()