From b9f87fbf36cbc55c1bbe88d2f890b8db1ff35de1 Mon Sep 17 00:00:00 2001 From: Janik Besendorf Date: Tue, 17 Nov 2026 22:33:26 +0100 Subject: [PATCH] Handle empty sysdiagnose recovery and partial archive filenames --- src/mvt/ios/cmd_check_sysdiagnose.py | 14 ++++----- src/mvt/ios/modules/sysdiagnose/base.py | 41 +++++++++++++------------ tests/test_check_ios_sysdiagnose.py | 14 +++++++++ tests/test_cmd_check_sysdiagnose.py | 19 +++++++----- 4 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/mvt/ios/cmd_check_sysdiagnose.py b/src/mvt/ios/cmd_check_sysdiagnose.py index d7c33820..f37a6fd4 100644 --- a/src/mvt/ios/cmd_check_sysdiagnose.py +++ b/src/mvt/ios/cmd_check_sysdiagnose.py @@ -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() diff --git a/src/mvt/ios/modules/sysdiagnose/base.py b/src/mvt/ios/modules/sysdiagnose/base.py index 345a6723..ed7b72ac 100644 --- a/src/mvt/ios/modules/sysdiagnose/base.py +++ b/src/mvt/ios/modules/sysdiagnose/base.py @@ -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 diff --git a/tests/test_check_ios_sysdiagnose.py b/tests/test_check_ios_sysdiagnose.py index 4e4c09dc..58b18b72 100644 --- a/tests/test_check_ios_sysdiagnose.py +++ b/tests/test_check_ios_sysdiagnose.py @@ -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" diff --git a/tests/test_cmd_check_sysdiagnose.py b/tests/test_cmd_check_sysdiagnose.py index 383be9d5..613dc7fe 100644 --- a/tests/test_cmd_check_sysdiagnose.py +++ b/tests/test_cmd_check_sysdiagnose.py @@ -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()