# =============================================================================
# DRIVE CAP, DAMAGED DISC — the damaged-disc part of the Drive cap test
# =============================================================================
#
#   Run it:   Tools -> Advanced -> Run tests..., then "Drive cap, damaged disc"
#             on the pre-built tab, with the DAMAGED disc in (the 18-track
#             disc of 2026-10-21, 2026-11-29 and 2026-11-30). Run it AFTER
#             Drive cap (drivecap.txt), which needs the reference disc.
#   Costs:    30 minutes to 2 hours: one rip of track 17 with paranoia off
#             (how long depends on the damage), one rip of track 18 stopped
#             60 s in and the wait for it to stop, and three probes that rip
#             nothing.
#
# -----------------------------------------------------------------------------
# WHY THIS EXISTS (round 44: our lap 2 S24, accepted in the fork's lap 3 S42;
# the fork's HW-8, HW-22 and the last case of HW-3; TASKS R255)
# -----------------------------------------------------------------------------
# Three things only this disc in this drive can show:
#
#   D  HW-8: cyanrip's sector-by-sector salvage with paranoia off (`-P 0`) on a
#      damaged track, track 17. Nothing has read a damaged track at -P 0 yet.
#   E  HW-22: how long ONE SIGTERM takes to stop a read on a damaged disc, with
#      the time it was sent in the app log, and the app log in the bundle (the
#      2026-11-30 run's bundle had none, so the signal's time was not known).
#   E  HW-3's last case: a track's `File(s):` line naming a file that was not
#      written whole. Track 18 stopped mid-read leaves exactly that, in the
#      rip's own log, which the bundle carries.
#
# And the cap (R255): on 2026-12-01 the drive's read cap fell from 40x to 14x
# inside one boot, while this disc's track 18 was re-read for 3.5 hours. So the
# cap is probed (`cyanrip -I`, nothing ripped) before D, between D and E, and
# after E, and `compare-drive-speed` says where it changed.
#
# A SEPARATE PRE-BUILT TEST, NOT A SECOND PART OF drivecap.txt, because a script
# cannot swap the disc: drivecap.txt needs the reference disc from its first
# step to its last (its tray cycles reload it), this test needs the damaged disc,
# and each run packs its own bundle, so each disc's evidence arrives separately.
#
# WHY E IS AN APP RIP CANCELLED, AND NOT A `cyanrip` STEP SIGNALLED. drivecap.txt's
# section F stops a `cyanrip` step with the step's own time limit, which kills its
# process group with SIGKILL. A SIGTERM sent the same way would reach only the
# host-side Distrobox wrapper: podman does not forward it into the container
# (the "one signal" world `sigterm-world` measures), so the reader would keep
# reading, unsignalled. The app's own Cancel is the path that delivers one
# SIGTERM to the reader: the wrapper's group first, then, 5 s later, the
# device-scoped rescue (`drive_control.term_unsignalled_holders`), which sends
# no second signal to a reader the first one already reached. Both are logged
# with their times in the app log, and it is the path a user's Cancel takes.
#
# NO SPEED IS SET HERE (`-S` is drivespeed.txt's question), and no `--read-c2`.
#
# A MEASUREMENT, NOT A VERDICT, except where a stop must leave a record: the
# cancelled rip's log must carry its footer and signature (a SIGTERM lets cyanrip
# write them), and the drive must be free before the next probe.
# -----------------------------------------------------------------------------

log =============================================================
log DRIVE CAP, DAMAGED DISC - track 17 at -P 0, one SIGTERM on 18
log 30 minutes to 2 hours
log =============================================================

set debug_logging on
expect debug_logging on
snapshot atstart

# --- A. IDENTITY -----------------------------------------------------------

log --- A. identity ---
cyanrip --version
expect-exit 0
expect-cyanrip platterpus-fork

# --- B. THE DRIVE AND THE DISC ---------------------------------------------

log --- B. drive offset and the damaged disc ---
set-drive-offset
expect-drive-offset
abort-if-failed the read offset for this drive is not known — set the drive up once (Tools, Setup and Updates, Set up drive), then run again
pick-release 1 600
expect-tracks 18+
abort-if-failed no disc with eighteen tracks is loaded — put the damaged disc in, then run again
cyanrip-timeout 900
snapshot discready

# --- C. THE CAP AS FOUND ---------------------------------------------------

log --- C. the cap as found, with the damaged disc in ---
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed damagedstart

# --- D. TRACK 17 WITH PARANOIA OFF (HW-8) ----------------------------------
# `-P 0` turns paranoia's re-reads off; the fork's salvage hook then reads a
# failed request sector by sector (cyanrip_main.c, "WITH PARANOIA DISABLED").
# The limit is two hours: past it the step kills the rip (SIGKILL, so its log
# would have no footer), and the step says so. Its log and -j record are in the
# bundle; record-accuracy says what AccurateRip made of it (info).

log --- D. track 17 alone at -P 0, then the cap ---
cyanrip-timeout 7200
cyanrip -N -U -W -d (device) -s (offset) -o flac -r 5 -P 0 -l 17 -D dcdamagedpzero (consumer) (diagnostics)
cyanrip-timeout 900
record-accuracy dcdamagedpzero dcdamagedpzero
expect-drive-free 60
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed afterpzero

# --- E. ONE SIGTERM 60 s INTO TRACK 18 (HW-22, HW-3's last case) ------------
# The app's own rip of track 18 alone, cancelled 60 s in: the read is then deep
# in the damaged area. `expect-drive-free 600` is the measurement: how long the
# reader takes to finish the read in hand and let go, the step's own time in the
# transcript, and the cancel's and the rescue's times in the app log
# (`app-log`, `sigterm-world`). If ten minutes are not enough, that is the
# finding, and the run stops before probing a drive still in use.

log --- E. track 18 alone, cancelled 60 s in: one SIGTERM, timed ---
select-tracks 18
album damaged sigterm (run) (ripper)
rip
log reading track 18 for 60 s, so the stop lands mid-read on the damaged area
wait 60
snapshot beforesigterm
cancel-rip
log cancel issued; timing how long the reader takes to let go
expect-drive-free 600
log HW-22: the cancel's time, then the rescue's SIGTERM and its outcome, from the app log
app-log rip cancel requested by
sigterm-world
snapshot aftersigterm
# A PRECONDITION FOR WHAT FOLLOWS, not a verdict on the stop: a reader still
# holding the drive would share it with the probes below (two ripper processes
# on one drive, mid-read on a damaged disc). The stop's evidence above is
# already recorded, and the bundle is packed either way.
abort-if-failed the reader still holds the drive after ten minutes (or the rip of track 18 did not start) — the probes below would share the drive with it; send the bundle as it is

log --- E2. the log the stop left ---
# cyanrip writes its footer and signature from its SIGTERM handler, and the
# track it was reading names its file in `File(s):`, written only in part
# (HW-3's last case; the fork reads that line from the rip's log in the bundle).
expect-status cancelled
expect-log-well-formed
expect-album-audit ripper_log_integrity argv_agreement

# --- F. THE CAP AFTER, THE KERNEL'S LOG, AND THE COMPARISON ----------------

log --- F. the cap after the stop, the kernel's messages, and where the cap changed ---
cyanrip -N -d (device) -I --read-vendor-settings (consumer) (diagnostics)
record-drive-speed aftersigterm
record-kernel-log drivecapdamaged
compare-drive-speed

# --- G. LEAVE THE RIG AS WE FOUND IT ---------------------------------------

log --- G. restoring ---
set debug_logging off
expect-drive-offset
expect-dialog none
snapshot atend

log =============================================================
log DONE. Send the ONE .tar.gz named on the "SEND THIS ONE FILE"
log line in the app log - it is under
log ~/.local/share/platterpus/bundles/
log Say with it that this was the damaged disc.
log =============================================================
