musikr: strip leading segments when probing absolute playlist entries

This commit is contained in:
jay
2026-11-12 18:01:38 -05:00
parent 3387ccedaf
commit 960061e615
2 changed files with 55 additions and 7 deletions
@@ -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<Entry>, roots: List<Path>, exists: (Path) -> Boolean): List<Path>? {
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)
}
}
@@ -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"