From 6c15e6c8bc51349b7f3eed2fd3b3b50c1e86c201 Mon Sep 17 00:00:00 2001 From: Thomas Windt Date: Mon, 2 Nov 2026 16:01:39 +0000 Subject: [PATCH 1/2] pdb: keep USB file names apart when the caps make them collide PrepareUSBLayout caps a file name at 48 characters, or less to keep the whole path within 126, but never checked whether the capped path was already taken in the export. Two tracks whose names share their first 44 characters landed on one path, as did names that differ only in case, which FAT32 folds together. The second copy overwrote the first, and both PDB rows played it. Exporting an S3RL pack, "S3RL & Synthwulf - Everybody Wants to be a DJ [Wav File].wav" and its "(S3RL hard mix) [Wav File].wav" both became ".../S3RL & Synthwulf - Everybody Wants to be a D.wav", and picking the plain version on the deck played the hard mix. The export now remembers the paths it has handed out, compared in lower case as FAT does, and gives a later colliding name " (2)", " (3)" and so on before its extension, shortening the base to stay within the same cap: the hard mix becomes ".../S3RL & Synthwulf - Everybody Wants to be (2).wav". Names that don't collide are unchanged. TestPrepareUSBLayoutKeepsCollidingNamesApart lays out both cases and checks that every row gets its own path and plays its own file. On main both pairs share a path, and the plain mix's row plays the hard mix. Co-Authored-By: Claude Opus 5.5 --- pdb/usblayout_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++ pdb/writer.go | 29 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 pdb/usblayout_test.go diff --git a/pdb/usblayout_test.go b/pdb/usblayout_test.go new file mode 100644 index 0000000..5bc4022 --- /dev/null +++ b/pdb/usblayout_test.go @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package pdb + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// TestPrepareUSBLayoutKeepsCollidingNamesApart lays out tracks whose USB file +// names collide once capped: two mixes sharing their first 44 characters, and +// two names that differ only in case (one file on FAT). Before the fix each +// pair got one path, the second copy overwrote the first, and both PDB rows +// played the same audio. +func TestPrepareUSBLayoutKeepsCollidingNamesApart(t *testing.T) { + src, out := t.TempDir(), t.TempDir() + names := []string{ + "S3RL & Synthwulf - Everybody Wants to be a DJ [Wav File].mp3", + "S3RL & Synthwulf - Everybody Wants to be a DJ (S3RL hard mix) [Wav File].mp3", + "Intro.mp3", + "INTRO.mp3", + } + var tracks []*Track + for i, n := range names { + p := filepath.Join(src, n) + if err := os.WriteFile(p, []byte(n), 0o644); err != nil { + t.Fatal(err) + } + tracks = append(tracks, &Track{ID: uint32(i + 1), Artist: "S3RL", Album: "Pack", FilePath: p}) + } + + if err := PrepareUSBLayout(tracks, src, out, true); err != nil { + t.Fatal(err) + } + + seen := map[string]string{} + for i, tr := range tracks { + key := strings.ToLower(tr.FilePath) + if other, dup := seen[key]; dup { + t.Errorf("%q and %q share the USB path %s", other, names[i], tr.FilePath) + } + seen[key] = names[i] + if len(tr.FilePath) > 126 { + t.Errorf("USB path %q is %d chars, over the 126 cap", tr.FilePath, len(tr.FilePath)) + } + got, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(tr.FilePath))) + if err != nil { + t.Fatal(err) + } + if string(got) != names[i] { + t.Errorf("%s plays %q, want %q", tr.FilePath, got, names[i]) + } + } +} diff --git a/pdb/writer.go b/pdb/writer.go index ecb2109..38d78c7 100644 --- a/pdb/writer.go +++ b/pdb/writer.go @@ -1286,6 +1286,26 @@ func TruncateFilename(name string, maxLen int) string { return base[:keep] + ext } +// uniqueUSBName returns name, or name with " (2)", " (3)", … before its +// extension (the base shortened to stay within maxLen), for the first variant +// whose path under dir isn't taken yet, and marks that path taken. Paths +// compare case-insensitively, as on FAT. +func uniqueUSBName(taken map[string]bool, dir, name string, maxLen int) string { + ext := filepath.Ext(name) + base := strings.TrimSuffix(name, ext) + try := name + for n := 2; taken[strings.ToLower(dir+try)]; n++ { + sfx := fmt.Sprintf(" (%d)", n) + b := base + if keep := max(maxLen-len(sfx)-len(ext), 1); len(b) > keep { + b = b[:keep] + } + try = b + sfx + ext + } + taken[strings.ToLower(dir+try)] = true + return try +} + // reencodeAudio runs ffmpeg to re-encode `src` into `dst`, choosing the // codec based on the destination extension. Used by PrepareUSBLayout to // salvage tracks whose source has decode errors the CDJ can't tolerate @@ -1325,6 +1345,7 @@ func reencodeAudio(src, dst string) error { func PrepareUSBLayout(tracks []*Track, srcDir, outDir string, copyFiles bool) error { contentsDir := filepath.Join(outDir, "Contents") now := time.Now().Format("2006-01-02") + taken := make(map[string]bool, len(tracks)) // USB paths handed out so far, lower-cased for _, t := range tracks { artist := t.Artist @@ -1385,6 +1406,14 @@ func PrepareUSBLayout(tracks []*Track, srcDir, outDir string, copyFiles bool) er } } + // The caps above can give two tracks the same path (two mixes whose + // names share their first 40-odd characters, or names that differ + // only in case, which FAT folds together). The second copy would + // overwrite the first and both rows would play it, so a later + // track gets " (2)", " (3)", … instead. + nameCap := max(min(48, maxUSBPath-fixed), len(ext)+5) + safeName = uniqueUSBName(taken, "/Contents/"+safeArtist+"/"+safeAlbum+"/", safeName, nameCap) + destDir := filepath.Join(contentsDir, safeArtist, safeAlbum) if err := os.MkdirAll(destDir, 0o755); err != nil { return fmt.Errorf("mkdir %s: %w", destDir, err) From 6ade1bbac6ff2ef93ab26225a8d2a5a570a2e4fb Mon Sep 17 00:00:00 2001 From: Vynull App Date: Sat, 21 Nov 2026 21:44:46 +0000 Subject: [PATCH 2/2] pdb: name colliding files as rekordbox does, and cut names whole MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to the review. rekordbox 7 names a colliding file "-1", "-2" and so on before its extension ("Same Name.mp3", "Same Name-1.mp3"), the base shortened so the whole name stays within the cap, so the export now does the same instead of " (2)". The track exported first keeps the plain name, as in rekordbox. Cutting a name to its cap now stops at the start of a character, so a cut never splits a multi-byte one into invalid UTF-8. TruncateFilename cut by bytes too and shares the fix. The test uses made-up names, and adds the same name from two folders, a third long name ("-2") and cuts through an "é". --- pdb/usblayout_test.go | 90 ++++++++++++++++++++++++++++++++----------- pdb/writer.go | 54 +++++++++++++++----------- 2 files changed, 99 insertions(+), 45 deletions(-) diff --git a/pdb/usblayout_test.go b/pdb/usblayout_test.go index 5bc4022..7ea1010 100644 --- a/pdb/usblayout_test.go +++ b/pdb/usblayout_test.go @@ -7,50 +7,94 @@ import ( "path/filepath" "strings" "testing" + "unicode/utf8" ) // TestPrepareUSBLayoutKeepsCollidingNamesApart lays out tracks whose USB file -// names collide once capped: two mixes sharing their first 44 characters, and -// two names that differ only in case (one file on FAT). Before the fix each -// pair got one path, the second copy overwrote the first, and both PDB rows -// played the same audio. +// names collide: two mixes sharing their first 44 characters, two names that +// differ only in case (one file on FAT), and the same name from two folders. +// Before the fix each pair got one path, the second copy overwrote the first, +// and both PDB rows played the same audio. The later track of a pair gets +// rekordbox's "-1" suffix (rekordbox 7 export: "Same Name.mp3" and +// "Same Name-1.mp3", "...Shared Title .mp3" and "...Shared Titl-1.mp3"). func TestPrepareUSBLayoutKeepsCollidingNamesApart(t *testing.T) { src, out := t.TempDir(), t.TempDir() - names := []string{ - "S3RL & Synthwulf - Everybody Wants to be a DJ [Wav File].mp3", - "S3RL & Synthwulf - Everybody Wants to be a DJ (S3RL hard mix) [Wav File].mp3", - "Intro.mp3", - "INTRO.mp3", + const long = "Fixture Artist - A Very Long Shared Title For Two Mixes" + files := []string{ + long + " (Mix A).mp3", + long + " (Mix B).mp3", + long + " (Mix C).mp3", + "Case Test.mp3", + "CASE TEST.mp3", + "one/Same Name.mp3", + "two/Same Name.mp3", + } + want := []string{ + "Fixture Artist - A Very Long Shared Title Fo.mp3", + "Fixture Artist - A Very Long Shared Title -1.mp3", + "Fixture Artist - A Very Long Shared Title -2.mp3", + "Case Test.mp3", + "CASE TEST-1.mp3", + "Same Name.mp3", + "Same Name-1.mp3", } var tracks []*Track - for i, n := range names { - p := filepath.Join(src, n) - if err := os.WriteFile(p, []byte(n), 0o644); err != nil { + for i, f := range files { + p := filepath.Join(src, f) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(f), 0o644); err != nil { t.Fatal(err) } - tracks = append(tracks, &Track{ID: uint32(i + 1), Artist: "S3RL", Album: "Pack", FilePath: p}) + tracks = append(tracks, &Track{ID: uint32(i + 1), Artist: "Fixture Artist", Album: "Collisions", FilePath: p}) } if err := PrepareUSBLayout(tracks, src, out, true); err != nil { t.Fatal(err) } - seen := map[string]string{} for i, tr := range tracks { - key := strings.ToLower(tr.FilePath) - if other, dup := seen[key]; dup { - t.Errorf("%q and %q share the USB path %s", other, names[i], tr.FilePath) - } - seen[key] = names[i] - if len(tr.FilePath) > 126 { - t.Errorf("USB path %q is %d chars, over the 126 cap", tr.FilePath, len(tr.FilePath)) + if got := "/Contents/Fixture Artist/Collisions/" + want[i]; tr.FilePath != got { + t.Errorf("%s: USB path %q, want %q", files[i], tr.FilePath, got) } got, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(tr.FilePath))) if err != nil { t.Fatal(err) } - if string(got) != names[i] { - t.Errorf("%s plays %q, want %q", tr.FilePath, got, names[i]) + if string(got) != files[i] { + t.Errorf("%s plays %q, want %q", tr.FilePath, got, files[i]) + } + } +} + +// A cut never splits a character: a two-byte "é" that the 48-byte cap or a +// suffix would cut in half is left out whole. +func TestPrepareUSBLayoutCutsWholeCharacters(t *testing.T) { + src, out := t.TempDir(), t.TempDir() + // In the first two names "é" takes bytes 42 and 43: the 44-byte base of + // the plain cap keeps it, the 42-byte base before "-1" would split it. + // In the third it takes bytes 44 and 45, split by the plain cap. + prefix := strings.Repeat("x", 41) + var tracks []*Track + for i, f := range []string{prefix + "é one.mp3", prefix + "é two.mp3", strings.Repeat("y", 43) + "é.mp3"} { + p := filepath.Join(src, f) + if err := os.WriteFile(p, []byte(f), 0o644); err != nil { + t.Fatal(err) + } + tracks = append(tracks, &Track{ID: uint32(i + 1), Artist: "Fixture Artist", Album: "Cuts", FilePath: p}) + } + + if err := PrepareUSBLayout(tracks, src, out, true); err != nil { + t.Fatal(err) + } + + for i, want := range []string{prefix + "é .mp3", prefix + "-1.mp3", strings.Repeat("y", 43) + ".mp3"} { + name := filepath.Base(tracks[i].FilePath) + if !utf8.ValidString(name) { + t.Errorf("track %d: %q is not valid UTF-8", i+1, name) + } else if name != want { + t.Errorf("track %d: %q, want %q", i+1, name, want) } } } diff --git a/pdb/writer.go b/pdb/writer.go index 38d78c7..9ee62b9 100644 --- a/pdb/writer.go +++ b/pdb/writer.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" "time" "unicode/utf16" @@ -1272,35 +1273,44 @@ func truncateComponent(s string, maxLen int) string { return out } -// TruncateFilename shortens a filename to maxLen chars, preserving extension. +// TruncateFilename shortens a filename to maxLen bytes, preserving extension. func TruncateFilename(name string, maxLen int) string { if len(name) <= maxLen { return name } ext := filepath.Ext(name) - base := strings.TrimSuffix(name, ext) - keep := maxLen - len(ext) - if keep < 1 { - keep = 1 + return cutName(strings.TrimSuffix(name, ext), maxLen-len(ext)) + ext +} + +// cutName cuts a file name's base to at most n bytes on a UTF-8 rune +// boundary, so the cut never splits a character. It keeps at least the first +// character. +func cutName(base string, n int) string { + if len(base) <= n { + return base } - return base[:keep] + ext + cut := max(n, 1) + for cut > 0 && !utf8.RuneStart(base[cut]) { + cut-- + } + if cut == 0 { + _, cut = utf8.DecodeRuneInString(base) + } + return base[:cut] } -// uniqueUSBName returns name, or name with " (2)", " (3)", … before its -// extension (the base shortened to stay within maxLen), for the first variant -// whose path under dir isn't taken yet, and marks that path taken. Paths -// compare case-insensitively, as on FAT. +// uniqueUSBName returns name, or for a name whose path under dir is already +// taken, the first of "name-1", "name-2", … that isn't (the base shortened so +// the whole name stays within maxLen), and marks the path it returns taken. +// Paths compare ignoring case, as on FAT. rekordbox names colliding files the +// same way: "Same Name.mp3" and "Same Name-1.mp3". func uniqueUSBName(taken map[string]bool, dir, name string, maxLen int) string { ext := filepath.Ext(name) base := strings.TrimSuffix(name, ext) try := name - for n := 2; taken[strings.ToLower(dir+try)]; n++ { - sfx := fmt.Sprintf(" (%d)", n) - b := base - if keep := max(maxLen-len(sfx)-len(ext), 1); len(b) > keep { - b = b[:keep] - } - try = b + sfx + ext + for n := 1; taken[strings.ToLower(dir+try)]; n++ { + sfx := "-" + strconv.Itoa(n) + try = cutName(base, maxLen-len(sfx)-len(ext)) + sfx + ext } taken[strings.ToLower(dir+try)] = true return try @@ -1406,11 +1416,11 @@ func PrepareUSBLayout(tracks []*Track, srcDir, outDir string, copyFiles bool) er } } - // The caps above can give two tracks the same path (two mixes whose - // names share their first 40-odd characters, or names that differ - // only in case, which FAT folds together). The second copy would - // overwrite the first and both rows would play it, so a later - // track gets " (2)", " (3)", … instead. + // Two tracks can land on one path: the same file name in two + // folders, two names that share their first 44 characters once + // capped, or names that differ only in case, which FAT folds + // together. The second copy would overwrite the first and both + // rows would play it, so a later track gets "-1", "-2", … instead. nameCap := max(min(48, maxUSBPath-fixed), len(ext)+5) safeName = uniqueUSBName(taken, "/Contents/"+safeArtist+"/"+safeAlbum+"/", safeName, nameCap)