diff --git a/tools/droiddeckctl b/tools/droiddeckctl index 72f996ae6..559a437a8 100755 --- a/tools/droiddeckctl +++ b/tools/droiddeckctl @@ -434,7 +434,8 @@ def unpack_logs(archive: Path, destination: Path, session_id: str) -> None: if entry.is_dir() or not name or not target.is_relative_to(destination): continue target.parent.mkdir(parents=True, exist_ok=True) - target.write_bytes(z.read(entry)) + with z.open(entry) as source, target.open("wb") as copy: + shutil.copyfileobj(source, copy) except (OSError, zipfile.BadZipFile) as exc: raise ControlError(f"Could not unpack the session logs: {exc}", "LOG_PULL_FAILED") from exc @@ -765,11 +766,18 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict: result = run_adb(adb, serial, "pull", remote_dir, str(destination), timeout=120) else: zip_path = checked(exported, "LOG_PULL_FAILED")["zip"] - with tempfile.TemporaryDirectory() as temp: - local_zip = Path(temp) / "logs.zip" - result = run_adb(adb, serial, "pull", zip_path, str(local_zip), timeout=120) - if result.returncode == 0: - unpack_logs(local_zip, destination, exported.get("sessionId") or session["id"]) + try: + if exported.get("sessionId", session["id"]) != session["id"]: + raise ControlError("A new session started while the logs were exported; run logs again", + "LOG_PULL_FAILED") + with tempfile.TemporaryDirectory() as temp: + local_zip = Path(temp) / "logs.zip" + result = run_adb(adb, serial, "pull", zip_path, str(local_zip), timeout=120) + if result.returncode == 0: + unpack_logs(local_zip, destination, session["id"]) + finally: + # The copy sits in shared storage, so it does not outlive the pull. + run_adb(adb, serial, "shell", "rm", "-f", zip_path, timeout=30) if result.returncode != 0: detail = (result.stderr or result.stdout).decode(errors="replace").strip() raise ControlError(detail or "adb pull failed", "LOG_PULL_FAILED") diff --git a/tools/tests/test_droiddeckctl.py b/tools/tests/test_droiddeckctl.py index 8d920802d..565e918c8 100644 --- a/tools/tests/test_droiddeckctl.py +++ b/tools/tests/test_droiddeckctl.py @@ -542,6 +542,16 @@ class DroiddeckctlTest(FakeAdbTest): self.assertEqual("e", (destination / "stores" / "epic.log").read_text()) self.assertFalse((self.dir / "outside").exists()) self.assertEqual([["pull", EXPORTED_ZIP]], [c["args"][:2] for c in self.calls() if c["args"][:1] == ["pull"]]) + self.assertIn(["shell", "rm", "-f", EXPORTED_ZIP], [c["args"] for c in self.calls()]) + + def test_logs_of_a_session_that_started_meanwhile_are_refused(self): + self.configure({"state": ended_state(), "logs": {"ok": True, "sessionId": "s2", "zip": EXPORTED_ZIP}}, + files={EXPORTED_ZIP: share_zip({"s2/session.log": "other"})}) + code, out = self.run_ctl("logs", "latest", str(self.dir / "artifacts")) + self.assertEqual(6, code) + self.assertEqual("LOG_PULL_FAILED", out["error"]["code"]) + self.assertFalse((self.dir / "artifacts").exists()) + self.assertIn(["shell", "rm", "-f", EXPORTED_ZIP], [c["args"] for c in self.calls()]) def test_logs_from_an_older_build_fall_back_to_the_private_folder(self): self.configure({"state": ended_state()})