From 960061e6150556ed48a7af848fc156421a5876f3 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 18:01:38 -0500 Subject: [PATCH] musikr: strip leading segments when probing absolute playlist entries --- .../musikr/playlist/PlaylistPathResolver.kt | 25 +++++++++---- .../playlist/PlaylistPathResolverTest.kt | 37 +++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt index 4ec55c488..2e3eaa730 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt @@ -24,7 +24,9 @@ import org.oxycblt.musikr.fs.Path /** * Resolves playlist file entries against the indexed library by probe-and-lock: probe entries * against the candidate roots in order until one resolves, lock that root, then resolve every - * entry against it. All entries in a playlist are assumed to share one root. + * entry against it. All entries in a playlist are assumed to share one root. Absolute entries + * probe with leading components stripped one at a time, which rescues paths written under + * another machine's layout; the locked strip depth then applies to every absolute entry. * * Unresolvable entries drop out of the result. A null result means no entry resolved anywhere — * the only case a playlist fails to resolve. @@ -48,11 +50,19 @@ internal class PlaylistPathResolver { */ fun resolve(entries: List, roots: List, exists: (Path) -> Boolean): List? { var lockedRoot: Path? = null + var lockedStrip = 0 probe@ for (entry in entries) { - for (root in roots) { - if (exists(Path(root.volume, entry.components.joinedTo(root.components)))) { - lockedRoot = root - break@probe + // Never strip the file name itself. + val maxStrip = if (entry.absolute) entry.components.components.size - 1 else 0 + for (strip in 0..maxStrip) { + for (root in roots) { + val candidate = + Path(root.volume, entry.components.depth(strip).joinedTo(root.components)) + if (exists(candidate)) { + lockedRoot = root + lockedStrip = strip + break@probe + } } } } @@ -60,8 +70,9 @@ internal class PlaylistPathResolver { return null } return entries.mapNotNull { entry -> - Path(lockedRoot.volume, entry.components.joinedTo(lockedRoot.components)) - .takeIf(exists) + val components = + if (entry.absolute) entry.components.depth(lockedStrip) else entry.components + Path(lockedRoot.volume, components.joinedTo(lockedRoot.components)).takeIf(exists) } } diff --git a/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt b/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt index be4d6dd7d..ee72b4965 100644 --- a/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt +++ b/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt @@ -156,6 +156,43 @@ class PlaylistPathResolverTest { ) } + @Test + fun resolve_foreignAbsoluteEntries_stripLeadingSegmentsToLock() { + val volume = FakeVolume() + val playlistDir = Path(volume, Components.parseUnix("Music/Playlists")) + val musicRoot = Path(volume, Components.parseUnix("Music")) + val library = + setOf( + Path(volume, Components.parseUnix("Music/Library/Artist/a.mp3")), + Path(volume, Components.parseUnix("Music/Library/Artist/b.mp3")), + ) + // Written on another machine: C:\Users\me\Music\... with the volume prefix consumed. + val entries = + listOf( + PlaylistPathResolver.Entry( + Components.parseWindows("Users\\me\\Music\\Library\\Artist\\a.mp3"), + absolute = true, + ), + PlaylistPathResolver.Entry( + Components.parseWindows("Users\\me\\Music\\Library\\Artist\\b.mp3"), + absolute = true, + ), + ) + + val resolved = + PlaylistPathResolver().resolve(entries, listOf(playlistDir, musicRoot)) { + it in library + } + + assertEquals( + listOf( + Path(volume, Components.parseUnix("Music/Library/Artist/a.mp3")), + Path(volume, Components.parseUnix("Music/Library/Artist/b.mp3")), + ), + resolved, + ) + } + private class FakeVolume : Volume.External { override val mediaStoreName = "fake" override val id = "fake"