From 7ad4fe5f8cdf49f50d67541c71513e7d1227406d Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 17:56:13 -0500 Subject: [PATCH] musikr: resolve playlist entries by probe-and-lock against candidate roots --- .../musikr/playlist/PlaylistPathResolver.kt | 66 +++++++++++++++++ .../playlist/PlaylistPathResolverTest.kt | 70 +++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt create mode 100644 musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt diff --git a/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt new file mode 100644 index 000000000..1be1774e7 --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistPathResolver.kt @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2026 Auxio Project + * PlaylistPathResolver.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.playlist + +import org.oxycblt.musikr.fs.Components +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. + * + * Unresolvable entries drop out of the result. A null result means no entry resolved anywhere — + * the only case a playlist fails to resolve. + * + * @author OxygenCobalt (Fork) + */ +internal class PlaylistPathResolver { + /** + * A single playlist entry. + * + * @property components The path as written in the playlist, split into components. + * @property absolute Whether the path was written absolute. + */ + data class Entry(val components: Components, val absolute: Boolean) + + /** + * Resolve [entries] against [roots] (in precedence order), using [exists] to test whether a + * resolved path is in the library. + * + * @return The resolved paths in entry order, or null if no entry resolved under any root. + */ + fun resolve(entries: List, roots: List, exists: (Path) -> Boolean): List? { + var lockedRoot: Path? = null + probe@ for (entry in entries) { + for (root in roots) { + if (exists(Path(root.volume, root.components.child(entry.components)))) { + lockedRoot = root + break@probe + } + } + } + if (lockedRoot == null) { + return null + } + return entries.mapNotNull { entry -> + Path(lockedRoot.volume, lockedRoot.components.child(entry.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 new file mode 100644 index 000000000..d7c717cfa --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistPathResolverTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2026 Auxio Project + * PlaylistPathResolverTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.playlist + +import android.content.Context +import org.junit.Assert.assertEquals +import org.junit.Test +import org.oxycblt.musikr.fs.Components +import org.oxycblt.musikr.fs.Path +import org.oxycblt.musikr.fs.Volume + +class PlaylistPathResolverTest { + @Test + fun resolve_relativeEntries_locksMusicLocationRoot() { + 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")), + ) + val entries = + listOf( + PlaylistPathResolver.Entry( + Components.parseUnix("Library/Artist/a.mp3"), absolute = false + ), + PlaylistPathResolver.Entry( + Components.parseUnix("Library/Artist/b.mp3"), absolute = false + ), + ) + + 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" + override val components = Components.parseUnix("storage/emulated/0") + + override fun resolveName(context: Context) = "fake" + } +}