fix: copy albums whole when a re-level changes only their tags
Build and publish container / build (pull_request) Successful in 1m43s

A ReplayGain album gain belongs to the whole record, so a track arriving or
leaving rewrites the tags on every one of its siblings. rsgain fits the new
values into the padding its previous write left behind, which changes neither
the file's size nor its mtime:

  before: 277757 bytes, mtime 1577880000, album gain 3.75 dB
  after:  277757 bytes, mtime 1577880000, album gain 6.25 dB

Those are the two things rsync's quick check compares, so the siblings are
invisible to it and the device keeps the old gains indefinitely.

The track that arrived or left is always visible. So sync-to-ipod.sh now runs a
second pass over the albums the first one touched, with --ignore-times to
defeat the same quick check. touched_albums.py derives the list from rsync's
own report of what it moved, which costs no extra traversal of either tree, and
leaves out the tracks the first pass has already copied so a whole-album
quality upgrade is not sent twice. Albums whose file set has not changed are
left alone.

A dry run reports how many further tracks are involved.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Emma Thorpe
2026-08-28 17:05:08 +01:00
co-authored by Claude Opus 5
parent c63115f246
commit 9a3b4f9955
5 changed files with 393 additions and 6 deletions
+43 -1
View File
@@ -56,6 +56,10 @@ The destination must be on a mounted FAT filesystem. That check is also what
catches an unmounted device: /media/IPOD/Music then resolves to the host's own
root filesystem, and this refuses to empty that.
An album that gained or lost a track is copied again in full afterwards. Its
surviving tracks have had their ReplayGain tags rewritten in place, which
changes neither their size nor their mtime, so the main pass cannot see them.
Progress is one line that rewrites itself, showing the album currently going
across, how far through the transfer is, the rate, and an estimate of what is
left. Working the totals out first means a second pass over the tree, which is
@@ -193,8 +197,23 @@ done
printf 'sync-to-ipod: %s -> %s\n' "$mirror" "$destination" >&2
# Both passes below feed their file list through touched_albums.py, which reads
# rsync's own report of what it moved. --files-from separates paths by newline,
# so a filename containing one would be read as two -- which is a path FAT32
# will not take either, and check_fat32.py above has already refused the run
# unless -f was given to skip it.
changed=$(mktemp)
relevelled=$(mktemp)
trap 'rm -f "$changed" "$relevelled"' EXIT
if $dry_run; then
rsync "${options[@]}" --dry-run --verbose "$mirror/" "$destination/"
rsync "${options[@]}" --dry-run --verbose --out-format='%l %n' \
"$mirror/" "$destination/" | tee "$changed"
also=$(python3 "$here/touched_albums.py" --mirror "$mirror" <"$changed" | wc -l)
if [ "$also" -gt 0 ]; then
printf 'sync-to-ipod: and %s more in those albums, whose ReplayGain tags\n' "$also" >&2
printf 'sync-to-ipod: change without changing their size or their mtime\n' >&2
fi
printf 'sync-to-ipod: dry run, nothing was written\n' >&2
exit 0
fi
@@ -256,10 +275,33 @@ interrupted() {
trap interrupted INT TERM
rsync "${options[@]}" --out-format='%l %n' "$mirror/" "$destination/" |
tee "$changed" |
python3 "$here/rsync_progress.py" --total "$total" --bytes "$total_bytes"
status=${PIPESTATUS[0]}
[ "$status" -eq 0 ] || die "rsync exited $status"
# A ReplayGain album gain belongs to the whole record, so a track arriving or
# leaving rewrites the tags on all of its siblings. rsgain fits the new values
# into the padding its previous write left behind, which changes neither the
# size nor the mtime -- the only two things the pass above compares. Those
# tracks are invisible to it, and the device would keep the old gains.
#
# The track that arrived or left is visible, though. So every album the pass
# touched has the rest of its tracks copied again, with --ignore-times to
# defeat the same quick check. No --delete: the pass above has already settled
# what should be on the device, and --delete aimed at an explicit file list
# does not mean what it looks like it means.
python3 "$here/touched_albums.py" --mirror "$mirror" <"$changed" >"$relevelled"
if [ -s "$relevelled" ]; then
also=$(wc -l <"$relevelled")
printf 'sync-to-ipod: re-copying %s tracks whose album was re-levelled\n' "$also" >&2
rsync --times --modify-window=2 --whole-file --omit-dir-times --ignore-times \
--files-from="$relevelled" --out-format='%l %n' "$mirror/" "$destination/" |
python3 "$here/rsync_progress.py" --total "$also"
status=${PIPESTATUS[0]}
[ "$status" -eq 0 ] || die "the re-levelled tracks failed to copy: rsync exited $status"
fi
# Rockbox reads its database from .tcd files in .rockbox. Building them here
# rather than on the device is not just faster: the on-device commit sorts the
# whole index in whatever memory it can scrape together, and on a large library