fix(droiddeckctl): address review on logs latest

Stream each archive entry to disk instead of reading it into memory,
refuse an archive from a session that started after the state call, and
delete the exported zip from shared storage once it has been pulled.
This commit is contained in:
angelrenefm authored and Kurt Himebauch committed 2026-11-05 13:52:43 -05:00
1 parent 2fb066507a
commit 8cc652e812
2 files changed
+24 -6

No files matched your search

+14 -6
View File
@@ -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")