From 4f9e471a74ea9f7191fda62ce992b0ecd2f581ce Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:51:36 -0800 Subject: [PATCH 01/13] ADFA-7160: Give each installed plugin build paths of its own Updating a plugin without restarting ran its new code against the old build's assets: the installer wrote every build to files/plugins/.cgp, and Android caches a plugin's resources and assets by path. Native libraries had the same problem twice over: the new build's class loader asked for the path the old one had opened, and an existing plugin_native_libs// folder was reused without checking which build it came from. - Install each build as -.cgp. A .cgp from before keeps loading; the first update replaces it. - Extract native libraries to plugin_native_libs/// through a scratch folder, and delete the plugin's other build folders. Language contributions take the folder from the loaded plugin. - Uninstall deletes every file with the plugin's manifest ID, not just the first, and the plugin's native libraries even when no file is left. - The signature check finds the installed file by manifest ID. The file logic moves out of PluginManager into PluginBuildFiles, which plain JVM tests cover. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../repositories/PluginRepositoryImpl.kt | 5 +- .../plugins/manager/core/PluginBuildFiles.kt | 140 ++++++++++++++++++ .../plugins/manager/core/PluginManager.kt | 64 +++----- .../plugins/manager/loaders/PluginLoader.kt | 42 ------ .../manager/core/PluginBuildFilesTest.kt | 118 +++++++++++++++ 5 files changed, 279 insertions(+), 90 deletions(-) create mode 100644 plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt index 8c3c29860e..f3d8477a5e 100644 --- a/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt +++ b/app/src/main/java/com/itsaky/androidide/repositories/PluginRepositoryImpl.kt @@ -3,6 +3,7 @@ package com.itsaky.androidide.repositories import android.util.Log import com.itsaky.androidide.plugins.PluginInfo import com.itsaky.androidide.plugins.PluginMetadata +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles import com.itsaky.androidide.plugins.manager.core.PluginManager import com.itsaky.androidide.plugins.manager.loaders.toPluginMetadata import kotlinx.coroutines.Dispatchers @@ -144,7 +145,9 @@ class PluginRepositoryImpl( val fileExtension = if (pluginFile.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true)) ".$PLUGIN_ARCHIVE_EXTENSION" else ".apk" - val finalFileName = "${pluginId}$fileExtension" + // A name of its own per build: Android caches a plugin's resources, assets and native + // libraries by path, so the new build would otherwise run against the old one's. + val finalFileName = "${PluginBuildFiles.fileStem(pluginId, pluginFile)}$fileExtension" if (!pluginsDir.exists()) { pluginsDir.mkdirs() diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt new file mode 100644 index 0000000000..3ac57f6413 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -0,0 +1,140 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.security.MessageDigest +import java.util.zip.ZipFile + +/** + * The files of installed plugin builds: the plugin files in [pluginsDir], and the native libraries + * extracted from them under [nativeLibsDir]. + * + * Every build gets paths of its own. Android caches a plugin's resources and assets by file path, + * and refuses to open a native library at a path another class loader already opened, so a build + * installed at the previous build's paths runs against the previous build's files until the app + * restarts. Plugin files are named `-.cgp`, after their content. A file installed + * before that, `.cgp`, keeps loading until its plugin is next updated. + */ +class PluginBuildFiles internal constructor( + private val pluginsDir: File, + private val nativeLibsDir: File, + private val readPluginId: (File) -> String?, +) { + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ + fun filesOf(pluginId: String): List = + pluginsDir + .listFiles { file -> file.isFile && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) } + .orEmpty() + .filter { file -> + runCatching { readPluginId(file) } + .onFailure { log.warn("Could not read the plugin ID of {}", file.name, it) } + .getOrNull() == pluginId + } + + /** + * Deletes every file of [pluginId], not just the first, since two would both load. True when + * one was deleted. The plugin's native libraries go even when it has no file left: an install + * whose load failed deletes its file but not the libraries it extracted. + */ + fun delete(pluginId: String): Boolean { + File(nativeLibsDir, pluginId).deleteRecursively() + val files = filesOf(pluginId) + val undeleted = files.filterNot(File::delete) + undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } + return undeleted.size < files.size + } + + /** + * Extracts [build]'s native libraries for [abi] into a folder of that build's own, and deletes + * the folders of [pluginId]'s other builds. Null when [build] has none for [abi]. + */ + fun extractNativeLibs( + pluginId: String, + build: File, + abi: String, + ): File? { + val pluginDir = File(nativeLibsDir, pluginId) + val buildDir = File(pluginDir, buildKey(pluginId, build)) + if (!buildDir.isDirectory && !extractNativeLibs(build, "lib/$abi/", buildDir)) { + pluginDir.deleteRecursively() + return null + } + pluginDir.listFiles { file -> file != buildDir }?.forEach(File::deleteRecursively) + return buildDir + } + + /** Extracts into a scratch folder renamed when complete, so an interrupted extraction is never reused. */ + private fun extractNativeLibs( + build: File, + libPrefix: String, + buildDir: File, + ): Boolean { + val scratch = File(buildDir.parentFile, ".${buildDir.name}.partial") + scratch.deleteRecursively() + scratch.mkdirs() + val targetPath = scratch.toPath().toAbsolutePath().normalize() + + var found = false + ZipFile(build).use { zip -> + zip + .entries() + .asSequence() + .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } + .forEach { entry -> + val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() + if (outPath.startsWith(targetPath)) { + zip.getInputStream(entry).use { input -> + outPath.toFile().outputStream().use { output -> + input.copyTo(output) + } + } + outPath.toFile().setExecutable(true, false) + found = true + } + } + } + + if (!found) { + scratch.deleteRecursively() + return false + } + if (!scratch.renameTo(buildDir)) { + scratch.deleteRecursively() + throw IOException("Could not move native libraries into ${buildDir.absolutePath}") + } + return true + } + + companion object { + private val log = LoggerFactory.getLogger(PluginBuildFiles::class.java) + + private const val HASH_CHARS = 16 + + /** The name, without extension, to install [build] under: `-`. */ + fun fileStem( + pluginId: String, + build: File, + ): String = "$pluginId-${sha256Hex(build).take(HASH_CHARS)}" + + /** The hash in [installed]'s name, or its whole name for a `.cgp` installed before hashes. */ + internal fun buildKey( + pluginId: String, + installed: File, + ): String = installed.nameWithoutExtension.removePrefix("$pluginId-") + + private fun sha256Hex(file: File): String { + val digest = MessageDigest.getInstance("SHA-256") + file.inputStream().use { input -> + val buffer = ByteArray(DEFAULT_BUFFER_SIZE) + while (true) { + val read = input.read(buffer) + if (read < 0) break + digest.update(buffer, 0, read) + } + } + return digest.digest().joinToString("") { "%02x".format(it) } + } + } +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 03e42340e7..83792e1d60 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -4,6 +4,7 @@ package com.itsaky.androidide.plugins.manager.core import android.app.Activity import android.content.Context +import android.os.Build import com.itsaky.androidide.actions.SidebarSlotManager import com.itsaky.androidide.plugins.IPlugin import com.itsaky.androidide.plugins.PluginContext @@ -331,6 +332,10 @@ class PluginManager private constructor( private val lifecycleDispatcher = PluginLifecycleDispatcher() private val pluginsDir = File(context.filesDir, "plugins") + private val buildFiles = + PluginBuildFiles(pluginsDir, context.getDir("plugin_native_libs", Context.MODE_PRIVATE)) { file -> + PluginLoader(context, file).getPluginMetadata()?.id + } private val documentationManager = PluginDocumentationManager(context) private var templateReloadListener: (() -> Unit)? = null private var snippetRefreshListener: ((String) -> Unit)? = null @@ -588,7 +593,7 @@ class PluginManager private constructor( var nativeLibPath: String? = try { - pluginLoader.extractNativeLibs(manifest.id)?.absolutePath + buildFiles.extractNativeLibs(manifest.id, file, Build.SUPPORTED_ABIS[0])?.absolutePath } catch (e: Exception) { logger.warn("Failed to extract native libs for plugin: ${manifest.id}", e) null @@ -685,6 +690,7 @@ class PluginManager private constructor( isEnabled, iconDayPath = iconDayPath, iconNightPath = iconNightPath, + nativeLibraryDir = nativeLibPath?.let(::File), ) loadedPlugins[manifest.id] = loadedPlugin @@ -842,10 +848,6 @@ class PluginManager private constructor( // Drop lifecycle listeners this plugin registered; its classloader is going away. lifecycleDispatcher.removeAllFrom(pluginId) - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } - notifyLanguageContributionsChanged(loadedPlugin.plugin) logger.info("Unloaded plugin: $pluginId") return true @@ -859,7 +861,13 @@ class PluginManager private constructor( incomingFile: File, existingPluginId: String, ): Boolean { - val existingFile = File(pluginsDir, "$existingPluginId.$PLUGIN_ARCHIVE_EXTENSION") + val existingFile = + loadedPlugins[existingPluginId]?.apkPath?.let(::File)?.takeIf(File::isFile) + ?: buildFiles.filesOf(existingPluginId).firstOrNull() + if (existingFile == null) { + logger.warn("No installed file found for $existingPluginId; treating as mismatch") + return false + } val incomingSig = PluginLoader(context, incomingFile).getSignatureHash() val existingSig = PluginLoader(context, existingFile).getSignatureHash() if (incomingSig == null || existingSig == null) { @@ -886,41 +894,7 @@ class PluginManager private constructor( logger.info("Successfully unloaded plugin from memory: $pluginId") } - // Find and delete the plugin file (CGP) - val pluginFiles = - pluginsDir.listFiles { file -> - file.isFile && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) - } - - if (pluginFiles == null || pluginFiles.isEmpty()) { - logger.error("No plugin files found in plugins directory") - return false - } - - logger.info("Found ${pluginFiles.size} plugin files to check") - var deleted = false - for (pluginFile in pluginFiles) { - try { - // Check if this contains the plugin we want to delete - val loader = PluginLoader(context, pluginFile) - val manifest = loader.getPluginMetadata() - - if (manifest != null) { - if (manifest.id == pluginId) { - if (pluginFile.delete()) { - deleted = true - break // Found and deleted the right file - } else { - logger.error("File exists: ${pluginFile.exists()}, Can write: ${pluginFile.canWrite()}") - } - } - } else { - logger.warn("Could not read manifest from ${pluginFile.name}") - } - } catch (e: Exception) { - logger.error("Error checking plugin file ${pluginFile.name}: ${e.message}", e) - } - } + val deleted = buildFiles.delete(pluginId) // Remove plugin state and cleanup contributions if (deleted) { @@ -1032,8 +1006,7 @@ class PluginManager private constructor( logger.error("No resource context registered for plugin $pluginId; ignoring its languages") return@flatMap emptyList() } - val nativeLibraryDir = - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).takeIf { it.isDirectory } + val nativeLibraryDir = loaded.nativeLibraryDir?.takeIf { it.isDirectory } definitions .filter { definition -> isLanguageAllowed(pluginId, loaded.manifest, definition, nativeLibraryDir) } .map { definition -> PluginLanguageContribution(pluginId, definition, assets, nativeLibraryDir) } @@ -1902,10 +1875,6 @@ class PluginManager private constructor( logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") } - File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() - } - File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> if (dir.exists()) dir.deleteRecursively() } @@ -1985,6 +1954,7 @@ data class LoadedPlugin( var isEnabled: Boolean = true, val iconDayPath: String? = null, val iconNightPath: String? = null, + val nativeLibraryDir: File? = null, ) fun LoadedPlugin.toPluginMetadata(): PluginMetadata = diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt index 6fc6f51e2b..3d23471adc 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt @@ -6,7 +6,6 @@ import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.res.AssetManager import android.content.res.Resources -import android.os.Build import android.os.Bundle import dalvik.system.DexClassLoader import org.slf4j.LoggerFactory @@ -28,7 +27,6 @@ class PluginLoader( private var pluginResources: Resources? = null private var pluginPackageInfo: PackageInfo? = null private var pluginClassLoader: DexClassLoader? = null - private var nativeLibDir: File? = null /** * Load plugin resources from APK @@ -149,46 +147,6 @@ class PluginLoader( ) } - fun extractNativeLibs(pluginId: String): File? { - val pluginNativeDir = File(context.getDir("plugin_native_libs", Context.MODE_PRIVATE), pluginId) - if (pluginNativeDir.exists()) { - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - - val libPrefix = "lib/${Build.SUPPORTED_ABIS[0]}/" - val targetPath = pluginNativeDir.toPath().toAbsolutePath().normalize() - pluginNativeDir.mkdirs() - - var found = false - ZipFile(pluginApk).use { zip -> - zip - .entries() - .asSequence() - .filter { !it.isDirectory && it.name.startsWith(libPrefix) && it.name.endsWith(".so") } - .forEach { entry -> - val outPath = targetPath.resolve(entry.name.substringAfterLast("/")).normalize() - if (outPath.startsWith(targetPath)) { - zip.getInputStream(entry).use { input -> - outPath.toFile().outputStream().use { output -> - input.copyTo(output) - } - } - outPath.toFile().setExecutable(true, false) - found = true - } - } - } - - if (!found) { - pluginNativeDir.deleteRecursively() - return null - } - - nativeLibDir = pluginNativeDir - return pluginNativeDir - } - fun isDebuggable(): Boolean { val packageInfo = pluginPackageInfo diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt new file mode 100644 index 0000000000..0108a2c9cb --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -0,0 +1,118 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** + * Each installed plugin build has paths of its own. Android caches resources, assets and native + * libraries by path, so a build at the previous build's paths ran against the previous build's + * files until the app restarted. + */ +class PluginBuildFilesTest { + @get:Rule + val temp = TemporaryFolder() + + private val pluginsDir by lazy { temp.newFolder("plugins") } + private val nativeLibsDir by lazy { temp.newFolder("plugin_native_libs") } + + // Uninstall tests write the plugin ID as the file's text; a real file has it in its manifest. + private val files by lazy { PluginBuildFiles(pluginsDir, nativeLibsDir) { it.readText() } } + + @Test + fun eachBuildIsNamedAfterItsContent() { + val a = build("a.cgp", "libfoo.so" to "A") + val b = build("b.cgp", "libfoo.so" to "B") + val aAgain = a.copyTo(File(temp.root, "a-again.cgp")) + + val stemA = PluginBuildFiles.fileStem(ID, a) + + assertThat(stemA).matches("${Regex.escape(ID)}-[0-9a-f]{16}") + assertWithMessage("two builds got one name").that(PluginBuildFiles.fileStem(ID, b)).isNotEqualTo(stemA) + assertThat(PluginBuildFiles.fileStem(ID, aAgain)).isEqualTo(stemA) + } + + @Test + fun aNewBuildLoadsItsLibrariesFromANewFolder() { + val dirA = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + val dirB = files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertWithMessage("the new build reuses the old build's library path").that(dirB).isNotEqualTo(dirA) + assertThat(File(dirB, "libfoo.so").readText()).isEqualTo("B") + assertWithMessage("the old build's folder is left behind").that(dirA!!.exists()).isFalse() + } + + @Test + fun librariesExtractedFromAnotherFileAreNotReused() { + // The layout before per-build folders: the libraries straight in the plugin's folder. + File(nativeLibsDir, ID).mkdirs() + File(nativeLibsDir, "$ID/libfoo.so").writeText("old") + val legacy = build("$ID.cgp", "libfoo.so" to "new").copyTo(File(pluginsDir, "$ID.cgp")) + + val dir = files.extractNativeLibs(ID, legacy, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("new") + assertThat(File(nativeLibsDir, "$ID/libfoo.so").exists()).isFalse() + } + + @Test + fun aBuildWithoutLibrariesForTheAbiGetsNoFolder() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + val otherAbi = build("x.cgp", "libfoo.so" to "x", abi = "x86_64") + + assertThat(files.extractNativeLibs(ID, installed(otherAbi), ABI)).isNull() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesTheLibrariesOfAPluginWithNoFileLeft() { + File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() + File(nativeLibsDir, "$ID/0123456789abcdef/libfoo.so").writeText("A") + + assertThat(files.delete(ID)).isFalse() + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun uninstallDeletesEveryFileOfThePluginAndNoOther() { + val legacy = File(pluginsDir, "$ID.cgp").apply { writeText(ID) } + val hashed = File(pluginsDir, "$ID-0123456789abcdef.cgp").apply { writeText(ID) } + // An ID that starts with this one, so only the manifest tells the files apart. + val other = File(pluginsDir, "$ID.extra-0123456789abcdef.cgp").apply { writeText("$ID.extra") } + + assertThat(files.filesOf(ID)).containsExactly(legacy, hashed) + assertThat(files.delete(ID)).isTrue() + assertWithMessage("a second file of the plugin survived").that(pluginsDir.listFiles()!!.toList()).containsExactly(other) + } + + // As PluginRepositoryImpl.installPluginFromFile names and copies it. + private fun installed(build: File): File { + val name = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + return build.copyTo(File(pluginsDir, name), overwrite = true) + } + + private fun build( + name: String, + vararg libs: Pair, + abi: String = ABI, + ): File = + File(temp.newFolder(), name).also { file -> + ZipOutputStream(file.outputStream()).use { zip -> + for ((lib, content) in libs) { + zip.putNextEntry(ZipEntry("lib/$abi/$lib")) + zip.write(content.toByteArray()) + zip.closeEntry() + } + } + } + + private companion object { + const val ID = "org.example.plugin" + const val ABI = "arm64-v8a" + } +} From ebd9ea63c1c538056282b48bd27904bbf989dc4c Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 23:12:43 -0800 Subject: [PATCH 02/13] ADFA-7160: Address review findings on per-build plugin files From an independent review and CodeRabbit: - Lock extraction and deletion per plugin. Two overlapping loads of a build shared its scratch folder, so one could get no native libraries or leave a half-written one in the build's folder. - At load, delete only the folders of builds no longer installed. If two files with one plugin ID both load, neither deletes the other's libraries. - Give a build reinstalled in the same process a new folder, ~, when an uninstall deleted the one it had. The runtime refuses to open a library at a path the old class loader opened. - Uninstall reports success only when every file of the plugin was deleted. A file left behind loads again on the next start. - Test the installer's file name, and that a leftover scratch folder isn't reused. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../PluginRepositoryImplInstallTest.kt | 55 ++++++++++++ .../plugins/manager/core/PluginBuildFiles.kt | 82 ++++++++++++++---- .../manager/core/PluginBuildFilesTest.kt | 86 +++++++++++++++++-- 3 files changed, 200 insertions(+), 23 deletions(-) create mode 100644 app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt diff --git a/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt new file mode 100644 index 0000000000..f7794cf82e --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/repositories/PluginRepositoryImplInstallTest.kt @@ -0,0 +1,55 @@ +package com.itsaky.androidide.repositories + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles +import com.itsaky.androidide.plugins.manager.core.PluginManager +import com.itsaky.androidide.plugins.manager.core.PluginValidation +import com.itsaky.androidide.plugins.manager.loaders.PluginManifest +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +/** + * Each plugin build is installed under a name of its own. Android caches a plugin's resources and + * assets by path, so a build installed at the previous build's path read the previous build's + * assets until the app restarted (ADFA-7160). + */ +@RunWith(RobolectricTestRunner::class) +class PluginRepositoryImplInstallTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun aBuildIsInstalledUnderItsContentHash() = + runTest { + val pluginsDir = temp.newFolder("plugins") + val build = temp.newFile("incoming.cgp").apply { writeText("build A") } + val expected = "${PluginBuildFiles.fileStem(ID, build)}.cgp" + val manager = + mockk(relaxed = true) { + every { getPluginValidation(any()) } returns + Result.success( + PluginValidation( + manifest = PluginManifest(ID, "Example", "1.0", "", "", "Main", "1.0"), + isDebug = false, + iconDayEntryExists = false, + iconNightEntryExists = false, + ), + ) + every { getPlugin(ID) } returns mockk() + } + + PluginRepositoryImpl({ manager }, pluginsDir).installPluginFromFile(build).getOrThrow() + + assertThat(pluginsDir.list()!!.toList()).containsExactly(expected) + } + + private companion object { + const val ID = "org.example.plugin" + } +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 3ac57f6413..a44b444d32 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory import java.io.File import java.io.IOException import java.security.MessageDigest +import java.util.concurrent.ConcurrentHashMap import java.util.zip.ZipFile /** @@ -22,6 +23,15 @@ class PluginBuildFiles internal constructor( private val nativeLibsDir: File, private val readPluginId: (File) -> String?, ) { + // One extraction or deletion per plugin at a time: loads can overlap, and two extractions of a + // build would share its scratch folder. + private val locks = ConcurrentHashMap() + + // Native-library folders handed to a class loader in this process. A folder deleted by an + // uninstall is never handed out again under the same path: the runtime would refuse to open a + // library there for the reinstalled build's class loader while the old one still holds it. + private val handedOut: MutableSet = ConcurrentHashMap.newKeySet() + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ fun filesOf(pluginId: String): List = pluginsDir @@ -35,36 +45,72 @@ class PluginBuildFiles internal constructor( /** * Deletes every file of [pluginId], not just the first, since two would both load. True when - * one was deleted. The plugin's native libraries go even when it has no file left: an install - * whose load failed deletes its file but not the libraries it extracted. + * there was one and all were deleted: a file left behind loads again on the next start. The + * plugin's native libraries go even when it has no file left: an install whose load failed + * deletes its file but not the libraries it extracted. */ - fun delete(pluginId: String): Boolean { - File(nativeLibsDir, pluginId).deleteRecursively() - val files = filesOf(pluginId) - val undeleted = files.filterNot(File::delete) - undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } - return undeleted.size < files.size - } + fun delete(pluginId: String): Boolean = + synchronized(lockFor(pluginId)) { + File(nativeLibsDir, pluginId).deleteRecursively() + val files = filesOf(pluginId) + val undeleted = files.filterNot(File::delete) + undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } + files.isNotEmpty() && undeleted.isEmpty() + } /** * Extracts [build]'s native libraries for [abi] into a folder of that build's own, and deletes - * the folders of [pluginId]'s other builds. Null when [build] has none for [abi]. + * [pluginId]'s folders that belong to no installed build. Null when [build] has none for [abi]. */ fun extractNativeLibs( pluginId: String, build: File, abi: String, - ): File? { - val pluginDir = File(nativeLibsDir, pluginId) - val buildDir = File(pluginDir, buildKey(pluginId, build)) - if (!buildDir.isDirectory && !extractNativeLibs(build, "lib/$abi/", buildDir)) { - pluginDir.deleteRecursively() - return null + ): File? = + synchronized(lockFor(pluginId)) { + val pluginDir = File(nativeLibsDir, pluginId) + val key = buildKey(pluginId, build) + val buildDir = folderFor(pluginDir, key) + val extracted = buildDir.isDirectory || extractNativeLibs(build, "lib/$abi/", buildDir) + // Another installed build of this plugin keeps its folder: it may be loading from it. + val otherBuilds = installedKeys(pluginId) - key + pluginDir + .listFiles { file -> file != buildDir && !(file.isDirectory && file.name.substringBefore('~') in otherBuilds) } + ?.forEach(File::deleteRecursively) + pluginDir.delete() // only when empty + if (extracted) buildDir.also { handedOut += it.absolutePath } else null } - pluginDir.listFiles { file -> file != buildDir }?.forEach(File::deleteRecursively) - return buildDir + + private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } + + /** + * The build's folder: ``, or `~` when an uninstall in this process deleted the + * folders it handed out before. An existing one is reused. + */ + private fun folderFor( + pluginDir: File, + key: String, + ): File { + val existing = pluginDir.listFiles { file -> file.isDirectory && (file.name == key || file.name.startsWith("$key~")) } + existing?.minByOrNull { it.name.length }?.let { return it } + return generateSequence(0) { it + 1 } + .map { n -> File(pluginDir, if (n == 0) key else "$key~$n") } + .first { it.absolutePath !in handedOut } } + /** + * The build keys of [pluginId]'s files, by name, which is cheap enough for every load. A file of + * another plugin whose ID starts with `-` can match too; that only keeps a folder. + */ + private fun installedKeys(pluginId: String): Set = + pluginsDir + .listFiles { file -> + file.isFile && + file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) && + (file.nameWithoutExtension == pluginId || file.nameWithoutExtension.startsWith("$pluginId-")) + }.orEmpty() + .mapTo(HashSet()) { buildKey(pluginId, it) } + /** Extracts into a scratch folder renamed when complete, so an interrupted extraction is never reused. */ private fun extractNativeLibs( build: File, diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index 0108a2c9cb..5af5c81c60 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -6,8 +6,12 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream +import kotlin.random.Random /** * Each installed plugin build has paths of its own. Android caches resources, assets and native @@ -22,7 +26,9 @@ class PluginBuildFilesTest { private val nativeLibsDir by lazy { temp.newFolder("plugin_native_libs") } // Uninstall tests write the plugin ID as the file's text; a real file has it in its manifest. - private val files by lazy { PluginBuildFiles(pluginsDir, nativeLibsDir) { it.readText() } } + private val files by lazy { newFiles() } + + private fun newFiles() = PluginBuildFiles(pluginsDir, nativeLibsDir) { it.readText() } @Test fun eachBuildIsNamedAfterItsContent() { @@ -39,12 +45,24 @@ class PluginBuildFilesTest { @Test fun aNewBuildLoadsItsLibrariesFromANewFolder() { - val dirA = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + val a = installed(build("a.cgp", "libfoo.so" to "A")) + val dirA = files.extractNativeLibs(ID, a, ABI) + a.delete() + val dirB = files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) assertWithMessage("the new build reuses the old build's library path").that(dirB).isNotEqualTo(dirA) assertThat(File(dirB, "libfoo.so").readText()).isEqualTo("B") - assertWithMessage("the old build's folder is left behind").that(dirA!!.exists()).isFalse() + assertWithMessage("the replaced build's folder is left behind").that(dirA!!.exists()).isFalse() + } + + @Test + fun anotherInstalledBuildKeepsItsFolder() { + // Two files with one ID both load, in parallel: neither may delete the other's libraries. + val dirA = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + files.extractNativeLibs(ID, installed(build("b.cgp", "libfoo.so" to "B")), ABI) + + assertThat(File(dirA, "libfoo.so").readText()).isEqualTo("A") } @Test @@ -60,6 +78,62 @@ class PluginBuildFilesTest { assertThat(File(nativeLibsDir, "$ID/libfoo.so").exists()).isFalse() } + @Test + fun aLeftoverScratchFolderIsNotReused() { + val build = installed(build("a.cgp", "libfoo.so" to "A")) + val scratch = File(nativeLibsDir, "$ID/.${PluginBuildFiles.buildKey(ID, build)}.partial") + scratch.mkdirs() + File(scratch, "libstale.so").writeText("from an interrupted extraction") + + val dir = files.extractNativeLibs(ID, build, ABI) + + assertThat(dir!!.list()!!.toList()).containsExactly("libfoo.so") + assertThat(scratch.exists()).isFalse() + } + + @Test + fun aBuildReinstalledInTheSameProcessGetsANewFolder() { + val build = build("a.cgp", "libfoo.so" to "A") + val first = files.extractNativeLibs(ID, installed(build), ABI) + files.delete(ID) + + val second = files.extractNativeLibs(ID, installed(build), ABI) + + // The old class loader may still hold the first path, and the runtime refuses to open it again. + assertWithMessage("the reinstalled build got the path the old class loader opened").that(second).isNotEqualTo(first) + assertThat(File(second, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the next start extracts again instead of reusing the folder") + .that(newFiles().extractNativeLibs(ID, File(pluginsDir, "${PluginBuildFiles.fileStem(ID, build)}.cgp"), ABI)) + .isEqualTo(second) + } + + @Test + fun overlappingExtractionsOfABuildAllGetItsLibraries() { + val content = Random(7).nextBytes(4 shl 20) + repeat(ROUNDS) { round -> + val id = "$ID.r$round" + val build = build("r$round.cgp", "libbig.so" to content) + val installed = build.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, build)}.cgp")) + val start = CountDownLatch(1) + val pool = Executors.newFixedThreadPool(THREADS) + val results = + (1..THREADS).map { + pool.submit { + start.await() + files.extractNativeLibs(id, installed, ABI) + } + } + start.countDown() + val dirs = results.map { it.get(60, TimeUnit.SECONDS) } + pool.shutdown() + + assertWithMessage("round $round: loads got different folders").that(dirs.toSet()).hasSize(1) + assertWithMessage("round $round: a library is incomplete") + .that(File(dirs.first(), "libbig.so").readBytes().contentEquals(content)) + .isTrue() + } + } + @Test fun aBuildWithoutLibrariesForTheAbiGetsNoFolder() { File(nativeLibsDir, "$ID/0123456789abcdef").mkdirs() @@ -98,14 +172,14 @@ class PluginBuildFilesTest { private fun build( name: String, - vararg libs: Pair, + vararg libs: Pair, abi: String = ABI, ): File = File(temp.newFolder(), name).also { file -> ZipOutputStream(file.outputStream()).use { zip -> for ((lib, content) in libs) { zip.putNextEntry(ZipEntry("lib/$abi/$lib")) - zip.write(content.toByteArray()) + zip.write(if (content is ByteArray) content else content.toString().toByteArray()) zip.closeEntry() } } @@ -114,5 +188,7 @@ class PluginBuildFilesTest { private companion object { const val ID = "org.example.plugin" const val ABI = "arm64-v8a" + const val ROUNDS = 10 + const val THREADS = 6 } } From a3aa5a3358fd5b85f0b9c05fcd388b29db509634 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:12:23 -0800 Subject: [PATCH 03/13] ADFA-7160: Reject a plugin ID that is a path From CodeRabbit: nothing checked the format of plugin.id, and CoGo names files and folders after it. Installing a plugin whose ID was ../files deleted the app's files folder: uninstall-before-install deleted app_plugin_native_libs/../files, and loading deleted app_plugin_icons/../files. - The installer's check and loadPlugin refuse an ID that is empty, . or .., or contains /, \ or NUL, with "Invalid plugin ID "": it must be a name, not a path". No allow-list: an ID defaults to the package name, and any other name is safe. - PluginBuildFiles refuses to delete or extract into a plugin folder that isn't a folder of its own directory, an ID that is a path or a link elsewhere. - PLUGIN_AUTHORING.md states the rule. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- docs/PLUGIN_AUTHORING.md | 2 +- .../plugins/manager/core/PluginBuildFiles.kt | 17 ++- .../plugins/manager/core/PluginIds.kt | 13 ++ .../plugins/manager/core/PluginManager.kt | 17 ++- .../manager/core/PluginBuildFilesTest.kt | 27 ++++ .../plugins/manager/core/PluginIdTest.kt | 136 ++++++++++++++++++ 6 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index eff39136fe..85e18dffb1 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -112,7 +112,7 @@ these in `PluginLoader.getPluginMetadata()` | Key | Type | Required | Notes | |---------------------------|-------------------------|----------|-----------------------------------------------------------------------| -| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. | +| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL; CoGo refuses such a plugin. | | `plugin.name` | string | yes\* | Display name. | | `plugin.version` | string | yes\* | Defaults to `"1.0.0"`. Prefer `${pluginVersion}` from `pluginBuilder`. | | `plugin.description` | string | no | Shown in the plugin list. | diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index a44b444d32..19b58f27cb 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -51,7 +51,7 @@ class PluginBuildFiles internal constructor( */ fun delete(pluginId: String): Boolean = synchronized(lockFor(pluginId)) { - File(nativeLibsDir, pluginId).deleteRecursively() + pluginDirOf(pluginId).deleteRecursively() val files = filesOf(pluginId) val undeleted = files.filterNot(File::delete) undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } @@ -68,7 +68,7 @@ class PluginBuildFiles internal constructor( abi: String, ): File? = synchronized(lockFor(pluginId)) { - val pluginDir = File(nativeLibsDir, pluginId) + val pluginDir = pluginDirOf(pluginId) val key = buildKey(pluginId, build) val buildDir = folderFor(pluginDir, key) val extracted = buildDir.isDirectory || extractNativeLibs(build, "lib/$abi/", buildDir) @@ -83,6 +83,19 @@ class PluginBuildFiles internal constructor( private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } + /** + * [pluginId]'s folder of native libraries. Throws unless that is a folder of [nativeLibsDir] + * named [pluginId]: an ID that is a path, or a link, would have it delete a folder elsewhere. + */ + private fun pluginDirOf(pluginId: String): File { + val dir = File(nativeLibsDir, pluginId) + val canonical = dir.canonicalFile + require(canonical.parentFile == nativeLibsDir.canonicalFile && canonical.name == pluginId) { + "Plugin ID \"$pluginId\" names no folder of ${nativeLibsDir.absolutePath}" + } + return dir + } + /** * The build's folder: ``, or `~` when an uninstall in this process deleted the * folders it handed out before. An existing one is reused. diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt new file mode 100644 index 0000000000..6c77fbc323 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt @@ -0,0 +1,13 @@ +package com.itsaky.androidide.plugins.manager.core + +/** + * Fails when [id] can't be a plugin ID. The ID names the plugin's files and folders, so one that + * is a path reaches outside them: installing `../files` would delete the app's whole files folder. + * Nullable because a `plugin.json` manifest without an ID parses to a null one. + */ +internal fun checkPluginId(id: String?): Result = + if (id.isNullOrEmpty() || id == "." || id == ".." || id.any { it == '/' || it == '\\' || it == '\u0000' }) { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": it must be a name, not a path")) + } else { + Result.success(id) + } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 83792e1d60..0fe08e510f 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -484,17 +484,25 @@ class PluginManager private constructor( return Result.success(manifest to loader) } + /** + * The manifest, its ID unchecked: the Add-ons Manager reports this failing as an unreadable file, + * so a bad ID is left for [getPluginValidation] to report. Don't build a path from it. + */ fun getPluginMetadataOnly(pluginFile: File): Result = loadAndValidate(pluginFile).map { it.first } - fun getPluginValidation(pluginFile: File): Result = - loadAndValidate(pluginFile).map { (manifest, loader) -> + /** Fails for a plugin ID that is a path: install deletes and creates folders named after it. */ + fun getPluginValidation(pluginFile: File): Result { + val (manifest, loader) = loadAndValidate(pluginFile).getOrElse { return Result.failure(it) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } + return Result.success( PluginValidation( manifest = manifest, isDebug = loader.isDebuggable(), iconDayEntryExists = manifest.iconDay?.let(loader::hasEntry) ?: false, iconNightEntryExists = manifest.iconNight?.let(loader::hasEntry) ?: false, - ) - } + ), + ) + } /** * Load plugin and return both the plugin instance and its metadata @@ -567,6 +575,7 @@ class PluginManager private constructor( if (manifest == null) { return Result.failure(IllegalArgumentException("Plugin manifest not found in: ${file.name}")) } + checkPluginId(manifest.id).onFailure { return Result.failure(it) } logger.debug("Parsed manifest for plugin: ${manifest.name} (${manifest.id})") diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index 5af5c81c60..8864340004 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -6,6 +6,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import java.nio.file.Files import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit @@ -164,6 +165,32 @@ class PluginBuildFilesTest { assertWithMessage("a second file of the plugin survived").that(pluginsDir.listFiles()!!.toList()).containsExactly(other) } + @Test + fun aPluginIdThatIsAPathDeletesNothingOutsideTheLibrariesFolder() { + val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } + File(victim, "keep.txt").writeText("not a plugin's") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val deleted = runCatching { files.delete("../victim") } + val extracted = runCatching { files.extractNativeLibs("../victim", build, ABI) } + + assertWithMessage("a folder outside the libraries folder lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + assertThat(extracted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun aPluginFolderThatLinksElsewhereIsNotFollowed() { + val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } + File(victim, "keep.txt").writeText("not a plugin's") + Files.createSymbolicLink(File(nativeLibsDir, ID).toPath(), victim.toPath()) + + val deleted = runCatching { files.delete(ID) } + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + } + // As PluginRepositoryImpl.installPluginFromFile names and copies it. private fun installed(build: File): File { val name = "${PluginBuildFiles.fileStem(ID, build)}.cgp" diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt new file mode 100644 index 0000000000..390eda295f --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt @@ -0,0 +1,136 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.PluginLogger +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** + * A plugin ID names the plugin's files and folders, so the installer refuses one that is a path: + * installing a plugin whose ID was `../files` deleted the app's whole files folder. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginIdTest { + @get:Rule + val temp = TemporaryFolder() + + @Test + fun theInstallerRefusesAPluginIdThatIsAPath() { + for (id in listOf("../files", "..", ".", "", "a/b", "/data", "a\\b", "a\u0000b")) { + val result = manager().getPluginValidation(plugin(id)) + + assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": it must be a name, not a path") + } + } + + @Test + fun theInstallerAcceptsAPluginIdThatIsAName() { + for (id in listOf("com.example.myplugin", "org.adfa.shell-hooks", "my_plugin", "a..b", "...")) { + assertThat( + manager() + .getPluginValidation(plugin(id)) + .getOrThrow() + .manifest.id, + ).isEqualTo(id) + } + } + + @Test + fun loadingRefusesAPluginIdThatIsAPath() { + val files = RuntimeEnvironment.getApplication().filesDir + File(files, "keep.txt").writeText("the app's") + + val result = manager().loadPlugin(plugin("../files")) + + assertWithMessage("loading deleted the app's files").that(files.list()?.toList()).contains("keep.txt") + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"../files\": it must be a name, not a path") + } + + @Test + fun aManifestWithoutAnIdIsRefused() { + assertThat(checkPluginId(null).exceptionOrNull()) + .hasMessageThat() + .isEqualTo("Invalid plugin ID \"null\": it must be a name, not a path") + } + + // The manager is a singleton, and each test has an application of its own. + @After + fun forgetManager() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) + } + + private fun manager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), NoLogger) + + // A plugin file whose AndroidManifest states [id]: Robolectric can't read one from an APK. + private fun plugin(id: String): File { + val file = File(temp.newFolder(), "plugin.cgp") + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.plugin" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", id) + putString("plugin.main_class", "org.example.plugin.Main") + // Loading deletes and recreates the plugin's icon folder, named after its ID. + putString("plugin.icon_day", "icon.png") + } + } + } + shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) + return file + } + + private object NoLogger : PluginLogger { + override val pluginId = "test" + + override fun debug(message: String) = Unit + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit + } +} From c828bf0447ebbb4b5837f3584ffd068e2a8ebf51 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:26:44 -0800 Subject: [PATCH 04/13] ADFA-7160: Keep per-build libraries where older builds don't look An older CoGo build installed over this one, as QA phones do when they switch branch builds, loads a plugin's libraries from app_plugin_native_libs/ whenever that folder exists. It held only per-build folders, so the older build found no libraries: on a Note 20, C-d-1116-0707 logged "grammar library not found" for the JS plugin. - Extract into app_plugin_build_libs instead, and delete app_plugin_native_libs once per process. An older build then finds nothing there and extracts its own. - Log a native-library folder that uninstall could not delete. It still doesn't count toward the result: with its files gone the plugin is gone (CodeRabbit). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginBuildFiles.kt | 35 +++++++++++++++++-- .../plugins/manager/core/PluginManager.kt | 2 +- .../manager/core/PluginBuildFilesTest.kt | 29 +++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 19b58f27cb..e68d94c0f3 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -6,6 +6,7 @@ import java.io.File import java.io.IOException import java.security.MessageDigest import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicBoolean import java.util.zip.ZipFile /** @@ -17,12 +18,18 @@ import java.util.zip.ZipFile * installed at the previous build's paths runs against the previous build's files until the app * restarts. Plugin files are named `-.cgp`, after their content. A file installed * before that, `.cgp`, keeps loading until its plugin is next updated. + * + * [legacyNativeLibsDir] is where CoGo builds before per-build folders extracted libraries. It is + * deleted, never read. */ class PluginBuildFiles internal constructor( private val pluginsDir: File, private val nativeLibsDir: File, + private val legacyNativeLibsDir: File? = null, private val readPluginId: (File) -> String?, ) { + private val legacyDeleted = AtomicBoolean(legacyNativeLibsDir == null) + // One extraction or deletion per plugin at a time: loads can overlap, and two extractions of a // build would share its scratch folder. private val locks = ConcurrentHashMap() @@ -47,11 +54,15 @@ class PluginBuildFiles internal constructor( * Deletes every file of [pluginId], not just the first, since two would both load. True when * there was one and all were deleted: a file left behind loads again on the next start. The * plugin's native libraries go even when it has no file left: an install whose load failed - * deletes its file but not the libraries it extracted. + * deletes its file but not the libraries it extracted. They don't count toward the result: with + * its files gone the plugin is gone, and its next install deletes the folders of other builds. */ fun delete(pluginId: String): Boolean = synchronized(lockFor(pluginId)) { - pluginDirOf(pluginId).deleteRecursively() + deleteLegacyNativeLibs() + if (!pluginDirOf(pluginId).deleteRecursively()) { + log.error("Could not delete all native libraries of {}", pluginId) + } val files = filesOf(pluginId) val undeleted = files.filterNot(File::delete) undeleted.forEach { log.error("Could not delete plugin file {}", it.absolutePath) } @@ -68,6 +79,7 @@ class PluginBuildFiles internal constructor( abi: String, ): File? = synchronized(lockFor(pluginId)) { + deleteLegacyNativeLibs() val pluginDir = pluginDirOf(pluginId) val key = buildKey(pluginId, build) val buildDir = folderFor(pluginDir, key) @@ -83,6 +95,12 @@ class PluginBuildFiles internal constructor( private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } + private fun deleteLegacyNativeLibs() { + if (legacyDeleted.compareAndSet(false, true) && legacyNativeLibsDir?.deleteRecursively() == false) { + log.warn("Could not delete {}", legacyNativeLibsDir) + } + } + /** * [pluginId]'s folder of native libraries. Throws unless that is a folder of [nativeLibsDir] * named [pluginId]: an ID that is a path, or a link, would have it delete a folder elsewhere. @@ -171,6 +189,19 @@ class PluginBuildFiles internal constructor( private const val HASH_CHARS = 16 + // Not app_plugin_native_libs: an older CoGo build reuses its plugin's folder there whatever it + // holds, so it would find these per-build folders and no libraries. Folder names as getDir() + // makes them. + private const val NATIVE_LIBS_DIR = "app_plugin_build_libs" + private const val LEGACY_NATIVE_LIBS_DIR = "app_plugin_native_libs" + + /** The plugin build files of the app whose data folder is [dataDir]. */ + internal fun inDataDir( + pluginsDir: File, + dataDir: File, + readPluginId: (File) -> String?, + ) = PluginBuildFiles(pluginsDir, File(dataDir, NATIVE_LIBS_DIR), File(dataDir, LEGACY_NATIVE_LIBS_DIR), readPluginId) + /** The name, without extension, to install [build] under: `-`. */ fun fileStem( pluginId: String, diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 0fe08e510f..fc4be04415 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -333,7 +333,7 @@ class PluginManager private constructor( private val pluginsDir = File(context.filesDir, "plugins") private val buildFiles = - PluginBuildFiles(pluginsDir, context.getDir("plugin_native_libs", Context.MODE_PRIVATE)) { file -> + PluginBuildFiles.inDataDir(pluginsDir, context.dataDir) { file -> PluginLoader(context, file).getPluginMetadata()?.id } private val documentationManager = PluginDocumentationManager(context) diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index 8864340004..b3771050ff 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -67,16 +67,33 @@ class PluginBuildFilesTest { } @Test - fun librariesExtractedFromAnotherFileAreNotReused() { - // The layout before per-build folders: the libraries straight in the plugin's folder. - File(nativeLibsDir, ID).mkdirs() - File(nativeLibsDir, "$ID/libfoo.so").writeText("old") + fun anOlderCoGoBuildFindsNoFolderToReuse() { + val dataDir = temp.newFolder("data") + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, build, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + // Before per-build folders, CoGo loaded a plugin's libraries from getDir("plugin_native_libs")/ + // whenever that folder existed, whatever it held. + assertWithMessage("an older CoGo build would find this build's folder and no libraries in it") + .that(File(dataDir, "app_plugin_native_libs/$ID").exists()) + .isFalse() + } + + @Test + fun librariesAnOlderCoGoBuildExtractedAreDeleted() { + val dataDir = temp.newFolder("data") + File(dataDir, "app_plugin_native_libs/$ID").mkdirs() + File(dataDir, "app_plugin_native_libs/$ID/libfoo.so").writeText("old") val legacy = build("$ID.cgp", "libfoo.so" to "new").copyTo(File(pluginsDir, "$ID.cgp")) - val dir = files.extractNativeLibs(ID, legacy, ABI) + val dir = PluginBuildFiles.inDataDir(pluginsDir, dataDir) { ID }.extractNativeLibs(ID, legacy, ABI) assertThat(File(dir, "libfoo.so").readText()).isEqualTo("new") - assertThat(File(nativeLibsDir, "$ID/libfoo.so").exists()).isFalse() + assertWithMessage("an older CoGo build's libraries are left behind") + .that(File(dataDir, "app_plugin_native_libs").exists()) + .isFalse() } @Test From b07d2f51dfdef27f0e1e751b867d717a264aea18 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:53:20 -0800 Subject: [PATCH 05/13] ADFA-7160: Leave other plugins' files out of the uninstall sweep After an uninstall, and so after every update, cleanupPluginCacheFiles deletes whatever under files/plugins has the plugin's ID in its name. Plugin files are now named -, so a short ID such as "a" or "cafe" matched other plugins' content hashes, and a longer ID that starts with this one matched as before. Uninstalling "a" deleted org.demo.tool-0123456789abcdef.cgp and a.extra-*.cgp. - The sweep skips plugin files. buildFiles.delete() already deleted this plugin's by the ID in their manifests (CodeRabbit). - One definition of a plugin file, PluginBuildFiles.isPluginFile, shared with filesOf. - PluginIdTest's Robolectric helpers move to RobolectricPlugins.kt so the new PluginUninstallTest can use them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginBuildFiles.kt | 6 +- .../plugins/manager/core/PluginManager.kt | 6 +- .../plugins/manager/core/PluginIdTest.kt | 77 ++---------------- .../manager/core/PluginUninstallTest.kt | 41 ++++++++++ .../manager/core/RobolectricPlugins.kt | 79 +++++++++++++++++++ 5 files changed, 134 insertions(+), 75 deletions(-) create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index e68d94c0f3..53a110b0c2 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -39,10 +39,14 @@ class PluginBuildFiles internal constructor( // library there for the reinstalled build's class loader while the old one still holds it. private val handedOut: MutableSet = ConcurrentHashMap.newKeySet() + /** Whether [file] is a plugin file, which only its manifest ties to a plugin. */ + fun isPluginFile(file: File): Boolean = + file.isFile && file.parentFile == pluginsDir && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ fun filesOf(pluginId: String): List = pluginsDir - .listFiles { file -> file.isFile && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) } + .listFiles(::isPluginFile) .orEmpty() .filter { file -> runCatching { readPluginId(file) } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index fc4be04415..c184563781 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -1903,10 +1903,12 @@ class PluginManager private constructor( logger.warn("Failed to cleanup ART cache files for: $pluginId", e) } - // Clean up any other directories or files that contain the plugin ID + // Clean up any other directories or files that contain the plugin ID. Plugin files are + // left to buildFiles.delete(), which reads their IDs: another plugin's file name can + // contain this ID, in a longer ID or in its content hash. try { pluginsDir.walkTopDown().forEach { file -> - if (file != pluginsDir && file.name.contains(pluginId)) { + if (file != pluginsDir && file.name.contains(pluginId) && !buildFiles.isPluginFile(file)) { val deleted = file.deleteRecursively() logger.debug("Deleted plugin-related item: ${file.absolutePath} (success: $deleted)") } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt index 390eda295f..d259cd1950 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt @@ -1,11 +1,7 @@ package com.itsaky.androidide.plugins.manager.core -import android.content.pm.ApplicationInfo -import android.content.pm.PackageInfo -import android.os.Bundle import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage -import com.itsaky.androidide.plugins.PluginLogger import org.junit.After import org.junit.Rule import org.junit.Test @@ -13,11 +9,8 @@ import org.junit.rules.TemporaryFolder import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment -import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import java.io.File -import java.util.zip.ZipEntry -import java.util.zip.ZipOutputStream /** * A plugin ID names the plugin's files and folders, so the installer refuses one that is a path: @@ -32,7 +25,7 @@ class PluginIdTest { @Test fun theInstallerRefusesAPluginIdThatIsAPath() { for (id in listOf("../files", "..", ".", "", "a/b", "/data", "a\\b", "a\u0000b")) { - val result = manager().getPluginValidation(plugin(id)) + val result = pluginManager().getPluginValidation(plugin(id)) assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": it must be a name, not a path") @@ -43,7 +36,7 @@ class PluginIdTest { fun theInstallerAcceptsAPluginIdThatIsAName() { for (id in listOf("com.example.myplugin", "org.adfa.shell-hooks", "my_plugin", "a..b", "...")) { assertThat( - manager() + pluginManager() .getPluginValidation(plugin(id)) .getOrThrow() .manifest.id, @@ -56,7 +49,7 @@ class PluginIdTest { val files = RuntimeEnvironment.getApplication().filesDir File(files, "keep.txt").writeText("the app's") - val result = manager().loadPlugin(plugin("../files")) + val result = pluginManager().loadPlugin(plugin("../files")) assertWithMessage("loading deleted the app's files").that(files.list()?.toList()).contains("keep.txt") assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"../files\": it must be a name, not a path") @@ -69,68 +62,8 @@ class PluginIdTest { .isEqualTo("Invalid plugin ID \"null\": it must be a name, not a path") } - // The manager is a singleton, and each test has an application of its own. @After - fun forgetManager() { - PluginManager::class.java - .getDeclaredField("INSTANCE") - .apply { isAccessible = true } - .set(null, null) - } - - private fun manager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), NoLogger) - - // A plugin file whose AndroidManifest states [id]: Robolectric can't read one from an APK. - private fun plugin(id: String): File { - val file = File(temp.newFolder(), "plugin.cgp") - ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } - val info = - PackageInfo().apply { - packageName = "org.example.plugin" - applicationInfo = - ApplicationInfo().apply { - metaData = - Bundle().apply { - putString("plugin.id", id) - putString("plugin.main_class", "org.example.plugin.Main") - // Loading deletes and recreates the plugin's icon folder, named after its ID. - putString("plugin.icon_day", "icon.png") - } - } - } - shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) - return file - } - - private object NoLogger : PluginLogger { - override val pluginId = "test" - - override fun debug(message: String) = Unit - - override fun debug( - message: String, - error: Throwable, - ) = Unit + fun forgetManager() = forgetPluginManager() - override fun info(message: String) = Unit - - override fun info( - message: String, - error: Throwable, - ) = Unit - - override fun warn(message: String) = Unit - - override fun warn( - message: String, - error: Throwable, - ) = Unit - - override fun error(message: String) = Unit - - override fun error( - message: String, - error: Throwable, - ) = Unit - } + private fun plugin(id: String): File = fakePlugin(File(temp.newFolder(), "plugin.cgp"), id) } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt new file mode 100644 index 0000000000..61b921e8da --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -0,0 +1,41 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for + * every update. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginUninstallTest { + @After + fun forgetManager() = forgetPluginManager() + + @Test + fun anotherPluginsFileWhoseNameContainsTheIdIsKept() { + val pluginsDir = File(RuntimeEnvironment.getApplication().filesDir, "plugins") + val uninstalled = fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + // "a" occurs in the content hash of the first one and in the ID of the second. + val others = + listOf( + fakePlugin(File(pluginsDir, "org.demo.tool-0123456789abcdef.cgp"), "org.demo.tool"), + fakePlugin(File(pluginsDir, "a.extra-1111111111111111.cgp"), "a.extra"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertThat(uninstalled.exists()).isFalse() + assertWithMessage("uninstalling \"a\" deleted another plugin's file") + .that(pluginsDir.listFiles()!!.toList()) + .containsAtLeastElementsIn(others) + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt new file mode 100644 index 0000000000..5188d120b3 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt @@ -0,0 +1,79 @@ +package com.itsaky.androidide.plugins.manager.core + +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.os.Bundle +import com.itsaky.androidide.plugins.PluginLogger +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** The plugin manager of the test's application. */ +internal fun pluginManager() = PluginManager.getInstance(RuntimeEnvironment.getApplication(), Any(), NoLogger) + +/** Forgets the singleton plugin manager: each test has an application of its own. */ +internal fun forgetPluginManager() { + PluginManager::class.java + .getDeclaredField("INSTANCE") + .apply { isAccessible = true } + .set(null, null) +} + +/** Writes [file] as a plugin whose AndroidManifest states [id]: Robolectric can't read one from an APK. */ +internal fun fakePlugin( + file: File, + id: String, +): File { + file.parentFile?.mkdirs() + ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } + val info = + PackageInfo().apply { + packageName = "org.example.plugin" + applicationInfo = + ApplicationInfo().apply { + metaData = + Bundle().apply { + putString("plugin.id", id) + putString("plugin.main_class", "org.example.plugin.Main") + // Loading deletes and recreates the plugin's icon folder, named after its ID. + putString("plugin.icon_day", "icon.png") + } + } + } + shadowOf(RuntimeEnvironment.getApplication().packageManager).setPackageArchiveInfo(file.absolutePath, info) + return file +} + +internal object NoLogger : PluginLogger { + override val pluginId = "test" + + override fun debug(message: String) = Unit + + override fun debug( + message: String, + error: Throwable, + ) = Unit + + override fun info(message: String) = Unit + + override fun info( + message: String, + error: Throwable, + ) = Unit + + override fun warn(message: String) = Unit + + override fun warn( + message: String, + error: Throwable, + ) = Unit + + override fun error(message: String) = Unit + + override fun error( + message: String, + error: Throwable, + ) = Unit +} From 2f5e75b709f35532dff37d2addbc0da6b75363ae Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 11:03:17 -0800 Subject: [PATCH 06/13] ADFA-7160: Name old-style plugin files' libraries by content; uninstall only the plugin's own files From an independent review. - A file installed before hashes, .cgp, had its libraries in a folder named , which an older CoGo build's in-place update of that file didn't change. Back on this build, the new code loaded the old libraries. The folder is now named legacy--. An ID with a "~" no longer makes the cleanup misread the folder name and delete another installed build's libraries. - Uninstall no longer deletes items whose names merely contain the plugin's ID: another plugin's data folder (a.extra/ when uninstalling "a"), a file inside one, or its ART code (a hash can contain the ID). It deletes the plugin's data folder and icons by exact name, its files by manifest ID, and ART code named after its own files. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginBuildFiles.kt | 31 ++++++++-- .../plugins/manager/core/PluginManager.kt | 18 +----- .../manager/core/PluginBuildFilesTest.kt | 30 +++++++++ .../manager/core/PluginUninstallTest.kt | 62 ++++++++++++++++++- 4 files changed, 119 insertions(+), 22 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 53a110b0c2..53fab2b13e 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -17,7 +17,8 @@ import java.util.zip.ZipFile * and refuses to open a native library at a path another class loader already opened, so a build * installed at the previous build's paths runs against the previous build's files until the app * restarts. Plugin files are named `-.cgp`, after their content. A file installed - * before that, `.cgp`, keeps loading until its plugin is next updated. + * before that, `.cgp`, keeps loading until its plugin is next updated. An older CoGo + * build updates that file in place, so its libraries' folder is named after its size and time. * * [legacyNativeLibsDir] is where CoGo builds before per-build folders extracted libraries. It is * deleted, never read. @@ -40,7 +41,7 @@ class PluginBuildFiles internal constructor( private val handedOut: MutableSet = ConcurrentHashMap.newKeySet() /** Whether [file] is a plugin file, which only its manifest ties to a plugin. */ - fun isPluginFile(file: File): Boolean = + private fun isPluginFile(file: File): Boolean = file.isFile && file.parentFile == pluginsDir && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ @@ -212,11 +213,33 @@ class PluginBuildFiles internal constructor( build: File, ): String = "$pluginId-${sha256Hex(build).take(HASH_CHARS)}" - /** The hash in [installed]'s name, or its whole name for a `.cgp` installed before hashes. */ + /** + * The name of [installed]'s library folder: the hash in its name. A `.cgp` installed + * before hashes gets `legacy--`, since an older CoGo build rewrites it in + * place. Its name alone would keep the previous content's libraries. + */ internal fun buildKey( pluginId: String, installed: File, - ): String = installed.nameWithoutExtension.removePrefix("$pluginId-") + ): String = + if (installed.nameWithoutExtension == pluginId) { + "legacy-${installed.length()}-${installed.lastModified()}" + } else { + installed.nameWithoutExtension.removePrefix("$pluginId-") + } + + /** + * Whether [file] is ART's compiled code for a file of [pluginId]: `.odex`, `.vdex` or + * `.art`, or `.cgp.prof` or `.cgp.cur.prof`. Matched exactly, since another plugin's ID + * or content hash can contain this ID. + */ + internal fun isCompiledCodeOf( + pluginId: String, + file: File, + ): Boolean = + file.isFile && + Regex("${Regex.escape(pluginId)}(-[0-9a-f]{$HASH_CHARS})?\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)") + .matches(file.name) private fun sha256Hex(file: File): String { val digest = MessageDigest.getInstance("SHA-256") diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index c184563781..be3d0d1fcd 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -1893,7 +1893,7 @@ class PluginManager private constructor( val oatDir = File(pluginsDir, "oat") if (oatDir.exists() && oatDir.isDirectory) { oatDir.walkTopDown().forEach { file -> - if (file.name.contains(pluginId)) { + if (PluginBuildFiles.isCompiledCodeOf(pluginId, file)) { val deleted = file.deleteRecursively() logger.debug("Deleted ART cache: ${file.absolutePath} (success: $deleted)") } @@ -1903,20 +1903,8 @@ class PluginManager private constructor( logger.warn("Failed to cleanup ART cache files for: $pluginId", e) } - // Clean up any other directories or files that contain the plugin ID. Plugin files are - // left to buildFiles.delete(), which reads their IDs: another plugin's file name can - // contain this ID, in a longer ID or in its content hash. - try { - pluginsDir.walkTopDown().forEach { file -> - if (file != pluginsDir && file.name.contains(pluginId) && !buildFiles.isPluginFile(file)) { - val deleted = file.deleteRecursively() - logger.debug("Deleted plugin-related item: ${file.absolutePath} (success: $deleted)") - } - } - } catch (e: Exception) { - logger.warn("Failed to cleanup plugin-related files for: $pluginId", e) - } - + // Nothing is swept by a name containing the ID: another plugin's ID, content hash or data + // can contain it. The plugin's files went by manifest ID in buildFiles.delete(). logger.debug("Complete plugin cleanup finished for: $pluginId") } } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index b3771050ff..b5baf6080c 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -96,6 +96,36 @@ class PluginBuildFilesTest { .isFalse() } + @Test + fun aFileUnderTheOldNameRewrittenInPlaceGetsItsOwnLibraries() { + val legacy = build("x.cgp", "libfoo.so" to "X").copyTo(File(pluginsDir, "$ID.cgp")) + legacy.setLastModified(1_000_000_000_000) + files.extractNativeLibs(ID, legacy, ABI) + // An older CoGo build updates the plugin under the same name. + build("y.cgp", "libfoo.so" to "Y").copyTo(legacy, overwrite = true) + legacy.setLastModified(1_000_000_001_000) + + val dir = newFiles().extractNativeLibs(ID, legacy, ABI) + + assertWithMessage("the rewritten file ran against the previous content's libraries") + .that(File(dir, "libfoo.so").readText()) + .isEqualTo("Y") + } + + @Test + fun anInstalledBuildUnderTheOldNameOfAnIdWithATildeKeepsItsFolder() { + val id = "org.example~plugin" + val legacy = build("old.cgp", "libfoo.so" to "old").copyTo(File(pluginsDir, "$id.cgp")) + val legacyDir = files.extractNativeLibs(id, legacy, ABI) + val hashed = build("new.cgp", "libfoo.so" to "new") + + files.extractNativeLibs(id, hashed.copyTo(File(pluginsDir, "${PluginBuildFiles.fileStem(id, hashed)}.cgp")), ABI) + + assertWithMessage("loading one build deleted the folder of another installed build") + .that(File(legacyDir, "libfoo.so").exists()) + .isTrue() + } + @Test fun aLeftoverScratchFolderIsNotReused() { val build = installed(build("a.cgp", "libfoo.so" to "A")) diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt index 61b921e8da..5c3b86065a 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -12,19 +12,21 @@ import java.io.File /** * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for - * every update. + * every update. The plugin uninstalled here is "a", which occurs in the other plugins' content + * hashes, IDs and file names. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [34]) class PluginUninstallTest { + private val pluginsDir by lazy { File(RuntimeEnvironment.getApplication().filesDir, "plugins") } + private val oatDir by lazy { File(pluginsDir, "oat") } + @After fun forgetManager() = forgetPluginManager() @Test fun anotherPluginsFileWhoseNameContainsTheIdIsKept() { - val pluginsDir = File(RuntimeEnvironment.getApplication().filesDir, "plugins") val uninstalled = fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") - // "a" occurs in the content hash of the first one and in the ID of the second. val others = listOf( fakePlugin(File(pluginsDir, "org.demo.tool-0123456789abcdef.cgp"), "org.demo.tool"), @@ -38,4 +40,58 @@ class PluginUninstallTest { .that(pluginsDir.listFiles()!!.toList()) .containsAtLeastElementsIn(others) } + + @Test + fun anotherPluginsDataIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = listOf(file(pluginsDir, "a.extra/state.json"), file(pluginsDir, "org.demo.tool/cache.bin")) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's data") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun anotherPluginsCompiledCodeIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val others = + listOf( + file(oatDir, "arm64/org.demo.tool-0123456789abcdef.odex"), + file(oatDir, "arm64/a.extra-1111111111111111.vdex"), + file(oatDir, "org.demo.tool-0123456789abcdef.cgp.cur.prof"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted another plugin's compiled code") + .that(others.filterNot(File::exists)) + .isEmpty() + } + + @Test + fun thePluginsOwnDataAndCompiledCodeAreDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val own = + listOf( + file(pluginsDir, "a/state.json"), + file(oatDir, "arm64/a-0123456789012345.odex"), + file(oatDir, "arm64/a-0123456789012345.vdex"), + file(oatDir, "arm64/a-0123456789012345.art"), + file(oatDir, "a-0123456789012345.cgp.cur.prof"), + file(oatDir, "a-0123456789012345.cgp.prof"), + // From a file installed before hashes. + file(oatDir, "arm64/a.odex"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" left its own files").that(own.filter(File::exists)).isEmpty() + } + + private fun file( + dir: File, + path: String, + ): File = File(dir, path).apply { parentFile!!.mkdirs() }.apply { writeText(path) } } From 4683f8c740bcbf08162a30d696d31487223d57ed Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:18:24 -0800 Subject: [PATCH 07/13] ADFA-7160: Keep compiled code whose plugin file is still installed From CodeRabbit. Uninstall matched ART code to the plugin by name only, so uninstalling "a" deleted the compiled code of a plugin whose ID is "a-0123456789abcdef", installed by an older CoGo build as a-0123456789abcdef.cgp. The cleanup runs after the plugin's own files are deleted, so compiled code whose plugin file is still there belongs to another plugin and is kept. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginBuildFiles.kt | 17 ++++++++++++++++- .../plugins/manager/core/PluginManager.kt | 3 ++- .../manager/core/PluginUninstallTest.kt | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 53fab2b13e..e77ff46b3c 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -44,6 +44,21 @@ class PluginBuildFiles internal constructor( private fun isPluginFile(file: File): Boolean = file.isFile && file.parentFile == pluginsDir && file.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) + /** + * Whether [file] is ART's compiled code for a file of [pluginId] that is gone. Call it after + * [delete], which removed all of [pluginId]'s plugin files. The name can't tell the plugin: an + * older CoGo build's `.cgp` may have an ID that looks like `-`. So a plugin + * file left under the stem belongs to another plugin, and its compiled code is kept. + */ + fun isLeftoverCompiledCodeOf( + pluginId: String, + file: File, + ): Boolean { + if (!isCompiledCodeOf(pluginId, file)) return false + val stem = file.name.replace(Regex("\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)$"), "") + return !File(pluginsDir, "$stem.$PLUGIN_ARCHIVE_EXTENSION").exists() + } + /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ fun filesOf(pluginId: String): List = pluginsDir @@ -231,7 +246,7 @@ class PluginBuildFiles internal constructor( /** * Whether [file] is ART's compiled code for a file of [pluginId]: `.odex`, `.vdex` or * `.art`, or `.cgp.prof` or `.cgp.cur.prof`. Matched exactly, since another plugin's ID - * or content hash can contain this ID. + * or content hash can contain this ID. By name only: see [isLeftoverCompiledCodeOf]. */ internal fun isCompiledCodeOf( pluginId: String, diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index be3d0d1fcd..666395619f 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -1893,7 +1893,8 @@ class PluginManager private constructor( val oatDir = File(pluginsDir, "oat") if (oatDir.exists() && oatDir.isDirectory) { oatDir.walkTopDown().forEach { file -> - if (PluginBuildFiles.isCompiledCodeOf(pluginId, file)) { + // Skips code whose plugin file remains: it is another plugin's. + if (buildFiles.isLeftoverCompiledCodeOf(pluginId, file)) { val deleted = file.deleteRecursively() logger.debug("Deleted ART cache: ${file.absolutePath} (success: $deleted)") } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt index 5c3b86065a..ed71fb590d 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -90,6 +90,24 @@ class PluginUninstallTest { assertWithMessage("uninstalling \"a\" left its own files").that(own.filter(File::exists)).isEmpty() } + @Test + fun compiledCodeOfAnotherPluginWhoseIdLooksLikeThisPluginsFileNameIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + // Installed by an older CoGo build: named by its ID, which looks like "a" plus a hash. + fakePlugin(File(pluginsDir, "a-0123456789abcdef.cgp"), "a-0123456789abcdef") + val others = + listOf( + file(oatDir, "arm64/a-0123456789abcdef.odex"), + file(oatDir, "a-0123456789abcdef.cgp.cur.prof"), + ) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted the compiled code of \"a-0123456789abcdef\"") + .that(others.filterNot(File::exists)) + .isEmpty() + } + private fun file( dir: File, path: String, From 3ef4a28d8c38853aaa41a5a458f59e410bf15abb Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 19:22:23 -0800 Subject: [PATCH 08/13] ADFA-7160: Delete a link as a link, never what it points to From review. Plugin code runs in CoGo's process, so it can put a link in any of the app's folders. File.deleteRecursively() follows a link inside the folder it deletes and empties the link's target. Uninstall, and so every update, did that to: - the plugin's native-library folder, and old builds' folders, which are also cleaned on every load - the plugin's data folder, which is the folder plugins write to, and its icons folder - the compiled-code folder, whose walk went into a linked folder A new helper, deleteWithoutFollowingLinks(), deletes a link itself and leaves its target alone. Every deletion of a plugin's folders uses it. The compiled-code walk doesn't enter links. A link at the plugin's native-library folder no longer blocks uninstall, which had left the plugin with two files after an update. The ID check now compares paths without resolving links, and still refuses an ID that is a path. A link at a build's folder is no longer used as that build's libraries: it is deleted and the libraries are extracted. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/LinkSafeDelete.kt | 59 +++++++++++++++++ .../plugins/manager/core/PluginBuildFiles.kt | 48 ++++++++++---- .../plugins/manager/core/PluginManager.kt | 10 +-- .../plugins/manager/loaders/PluginLoader.kt | 3 +- .../manager/core/PluginBuildFilesTest.kt | 64 +++++++++++++++++-- .../manager/core/PluginUninstallTest.kt | 48 ++++++++++++++ 6 files changed, 209 insertions(+), 23 deletions(-) create mode 100644 plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt new file mode 100644 index 0000000000..aa0c7941c5 --- /dev/null +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/LinkSafeDelete.kt @@ -0,0 +1,59 @@ +package com.itsaky.androidide.plugins.manager.core + +import org.slf4j.LoggerFactory +import java.io.File +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.LinkOption +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes + +private val log = LoggerFactory.getLogger("LinkSafeDelete") + +/** + * Deletes this file or folder tree without ever following a link: a link, the root included, is + * deleted as a link and its target is left alone. File.deleteRecursively() follows a link nested in + * the tree and empties its target, and plugin code can make a link anywhere in the app's storage. + * + * True when nothing is left, as with deleteRecursively(), so true for a path that doesn't exist. Keeps + * going past an entry it can't delete, as deleteRecursively() does. + */ +internal fun File.deleteWithoutFollowingLinks(): Boolean { + val root = toPath() + if (!Files.exists(root, LinkOption.NOFOLLOW_LINKS)) return true + // walkFileTree without FOLLOW_LINKS hands a link to visitFile, never into its target. + val visitor = + object : SimpleFileVisitor() { + override fun visitFile( + file: Path, + attrs: BasicFileAttributes, + ) = tryDelete(file) + + override fun visitFileFailed( + file: Path, + exc: IOException, + ) = tryDelete(file) + + override fun postVisitDirectory( + dir: Path, + exc: IOException?, + ) = tryDelete(dir) + + private fun tryDelete(path: Path): FileVisitResult { + try { + Files.deleteIfExists(path) + } catch (e: IOException) { + log.debug("Could not delete {}", path, e) + } + return FileVisitResult.CONTINUE + } + } + try { + Files.walkFileTree(root, visitor) + } catch (e: IOException) { + log.debug("Could not walk {}", root, e) + } + return !Files.exists(root, LinkOption.NOFOLLOW_LINKS) +} diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index e77ff46b3c..4f68f1b18a 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -4,6 +4,8 @@ import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION import org.slf4j.LoggerFactory import java.io.File import java.io.IOException +import java.nio.file.Files +import java.nio.file.LinkOption import java.security.MessageDigest import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicBoolean @@ -80,7 +82,7 @@ class PluginBuildFiles internal constructor( fun delete(pluginId: String): Boolean = synchronized(lockFor(pluginId)) { deleteLegacyNativeLibs() - if (!pluginDirOf(pluginId).deleteRecursively()) { + if (!pluginDirOf(pluginId).deleteWithoutFollowingLinks()) { log.error("Could not delete all native libraries of {}", pluginId) } val files = filesOf(pluginId) @@ -101,14 +103,20 @@ class PluginBuildFiles internal constructor( synchronized(lockFor(pluginId)) { deleteLegacyNativeLibs() val pluginDir = pluginDirOf(pluginId) + if (Files.isSymbolicLink(pluginDir.toPath())) { + log.warn("{} is a link; deleting the link and extracting into a folder", pluginDir) + pluginDir.deleteWithoutFollowingLinks() + } val key = buildKey(pluginId, build) + // Before folderFor: a link left at the build's path would block the scratch folder's rename. + deleteLinksIn(pluginDir) val buildDir = folderFor(pluginDir, key) - val extracted = buildDir.isDirectory || extractNativeLibs(build, "lib/$abi/", buildDir) + val extracted = isRealDirectory(buildDir) || extractNativeLibs(build, "lib/$abi/", buildDir) // Another installed build of this plugin keeps its folder: it may be loading from it. val otherBuilds = installedKeys(pluginId) - key pluginDir - .listFiles { file -> file != buildDir && !(file.isDirectory && file.name.substringBefore('~') in otherBuilds) } - ?.forEach(File::deleteRecursively) + .listFiles { file -> file != buildDir && !(isRealDirectory(file) && file.name.substringBefore('~') in otherBuilds) } + ?.forEach(File::deleteWithoutFollowingLinks) pluginDir.delete() // only when empty if (extracted) buildDir.also { handedOut += it.absolutePath } else null } @@ -116,22 +124,34 @@ class PluginBuildFiles internal constructor( private fun lockFor(pluginId: String): Any = locks.computeIfAbsent(pluginId) { Any() } private fun deleteLegacyNativeLibs() { - if (legacyDeleted.compareAndSet(false, true) && legacyNativeLibsDir?.deleteRecursively() == false) { + if (legacyDeleted.compareAndSet(false, true) && legacyNativeLibsDir?.deleteWithoutFollowingLinks() == false) { log.warn("Could not delete {}", legacyNativeLibsDir) } } /** * [pluginId]'s folder of native libraries. Throws unless that is a folder of [nativeLibsDir] - * named [pluginId]: an ID that is a path, or a link, would have it delete a folder elsewhere. + * named [pluginId]: an ID that is a path would have it delete a folder elsewhere. Compares + * normalized paths and resolves no link, so a link at that path is returned as it is, for the + * caller to delete as a link. */ private fun pluginDirOf(pluginId: String): File { - val dir = File(nativeLibsDir, pluginId) - val canonical = dir.canonicalFile - require(canonical.parentFile == nativeLibsDir.canonicalFile && canonical.name == pluginId) { + val root = nativeLibsDir.toPath().toAbsolutePath().normalize() + val dir = root.resolve(pluginId).normalize() + require("\\" !in pluginId && dir.parent == root && dir.fileName?.toString() == pluginId) { "Plugin ID \"$pluginId\" names no folder of ${nativeLibsDir.absolutePath}" } - return dir + return dir.toFile() + } + + // A link is never a folder of ours: plugin code can plant one, and it would be followed. + private fun isRealDirectory(file: File): Boolean = Files.isDirectory(file.toPath(), LinkOption.NOFOLLOW_LINKS) + + private fun deleteLinksIn(pluginDir: File) { + pluginDir.listFiles { file -> Files.isSymbolicLink(file.toPath()) }?.forEach { + log.warn("{} is a link; deleting the link", it) + it.deleteWithoutFollowingLinks() + } } /** @@ -142,7 +162,7 @@ class PluginBuildFiles internal constructor( pluginDir: File, key: String, ): File { - val existing = pluginDir.listFiles { file -> file.isDirectory && (file.name == key || file.name.startsWith("$key~")) } + val existing = pluginDir.listFiles { file -> isRealDirectory(file) && (file.name == key || file.name.startsWith("$key~")) } existing?.minByOrNull { it.name.length }?.let { return it } return generateSequence(0) { it + 1 } .map { n -> File(pluginDir, if (n == 0) key else "$key~$n") } @@ -169,7 +189,7 @@ class PluginBuildFiles internal constructor( buildDir: File, ): Boolean { val scratch = File(buildDir.parentFile, ".${buildDir.name}.partial") - scratch.deleteRecursively() + scratch.deleteWithoutFollowingLinks() scratch.mkdirs() val targetPath = scratch.toPath().toAbsolutePath().normalize() @@ -194,11 +214,11 @@ class PluginBuildFiles internal constructor( } if (!found) { - scratch.deleteRecursively() + scratch.deleteWithoutFollowingLinks() return false } if (!scratch.renameTo(buildDir)) { - scratch.deleteRecursively() + scratch.deleteWithoutFollowingLinks() throw IOException("Could not move native libraries into ${buildDir.absolutePath}") } return true diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 666395619f..1a58f57d02 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -93,6 +93,7 @@ import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION import java.io.File +import java.nio.file.Files import java.util.concurrent.ConcurrentHashMap class PluginManager private constructor( @@ -617,7 +618,7 @@ class PluginManager private constructor( } if (nativeLibPath != null && !permissions.contains(PluginPermission.NATIVE_CODE)) { - File(nativeLibPath).deleteRecursively() + File(nativeLibPath).deleteWithoutFollowingLinks() if (manifest.sidebarItems > 0) { SidebarSlotManager.releasePluginSlots(manifest.id) } @@ -1880,19 +1881,20 @@ class PluginManager private constructor( val pluginDir = File(pluginsDir, pluginId) if (pluginDir.exists()) { - val deleted = pluginDir.deleteRecursively() + val deleted = pluginDir.deleteWithoutFollowingLinks() logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") } File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> - if (dir.exists()) dir.deleteRecursively() + dir.deleteWithoutFollowingLinks() } // Clean up ART cache files in oat directory try { val oatDir = File(pluginsDir, "oat") if (oatDir.exists() && oatDir.isDirectory) { - oatDir.walkTopDown().forEach { file -> + // Not into a link: a file named like this plugin's code could be anywhere it points. + oatDir.walkTopDown().onEnter { !Files.isSymbolicLink(it.toPath()) }.forEach { file -> // Skips code whose plugin file remains: it is another plugin's. if (buildFiles.isLeftoverCompiledCodeOf(pluginId, file)) { val deleted = file.deleteRecursively() diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt index 3d23471adc..f106fae698 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginLoader.kt @@ -7,6 +7,7 @@ import android.content.pm.PackageManager import android.content.res.AssetManager import android.content.res.Resources import android.os.Bundle +import com.itsaky.androidide.plugins.manager.core.deleteWithoutFollowingLinks import dalvik.system.DexClassLoader import org.slf4j.LoggerFactory import java.io.File @@ -172,7 +173,7 @@ class PluginLoader( ): Pair { if (manifest.iconDay == null && manifest.iconNight == null) return null to null val iconDir = File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId) - iconDir.deleteRecursively() + iconDir.deleteWithoutFollowingLinks() iconDir.mkdirs() val targetPath = iconDir.toPath().toAbsolutePath().normalize() return try { diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index b5baf6080c..87664ce7a3 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -7,6 +7,7 @@ import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File import java.nio.file.Files +import java.nio.file.LinkOption import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit @@ -228,16 +229,71 @@ class PluginBuildFilesTest { @Test fun aPluginFolderThatLinksElsewhereIsNotFollowed() { - val victim = File(nativeLibsDir.parentFile, "victim").apply { mkdirs() } - File(victim, "keep.txt").writeText("not a plugin's") - Files.createSymbolicLink(File(nativeLibsDir, ID).toPath(), victim.toPath()) + val victim = victim() + val link = File(nativeLibsDir, ID).also { Files.createSymbolicLink(it.toPath(), victim.toPath()) } val deleted = runCatching { files.delete(ID) } assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") - assertThat(deleted.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java) + assertThat(deleted.exceptionOrNull()).isNull() + assertWithMessage("the link is still there").that(Files.exists(link.toPath(), LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aPluginFolderThatLinksElsewhereIsReplacedByAFolderOfLibraries() { + val victim = victim() + Files.createSymbolicLink(File(nativeLibsDir, ID).toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the libraries went through a link").that(Files.isSymbolicLink(File(nativeLibsDir, ID).toPath())).isFalse() + } + + @Test + fun aLinkInThePluginFolderIsNotFollowedByUninstall() { + val victim = victim() + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/some-build").toPath(), victim.toPath()) + + files.delete(ID) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(nativeLibsDir, ID).exists()).isFalse() + } + + @Test + fun aLinkInThePluginFolderIsNotFollowedByTheCleanupOfOldBuilds() { + val victim = victim() + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/stale").toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, installed(build("a.cgp", "libfoo.so" to "A")), ABI) + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertThat(File(nativeLibsDir, "$ID/stale").exists()).isFalse() } + @Test + fun aLinkAtTheBuildsFolderIsNotReused() { + val victim = victim() + val build = installed(build("a.cgp", "libfoo.so" to "A")) + File(nativeLibsDir, ID).mkdirs() + Files.createSymbolicLink(File(nativeLibsDir, "$ID/${PluginBuildFiles.buildKey(ID, build)}").toPath(), victim.toPath()) + + val dir = files.extractNativeLibs(ID, build, ABI) + + assertThat(File(dir, "libfoo.so").readText()).isEqualTo("A") + assertWithMessage("the build's libraries load through a link").that(Files.isSymbolicLink(dir!!.toPath())).isFalse() + assertWithMessage("the link's target changed").that(victim.list()?.toList()).containsExactly("keep.txt") + } + + // Outside the folders the code under test deletes, with a file that must survive. + private fun victim(): File = + File(nativeLibsDir.parentFile, "victim").apply { mkdirs() }.also { File(it, "keep.txt").writeText("not a plugin's") } + // As PluginRepositoryImpl.installPluginFromFile names and copies it. private fun installed(build: File): File { val name = "${PluginBuildFiles.fileStem(ID, build)}.cgp" diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt index ed71fb590d..d7b03e66dc 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -1,5 +1,6 @@ package com.itsaky.androidide.plugins.manager.core +import android.content.Context import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage import org.junit.After @@ -9,6 +10,7 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config import java.io.File +import java.nio.file.Files /** * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for @@ -108,6 +110,52 @@ class PluginUninstallTest { .isEmpty() } + @Test + fun aLinkInThePluginsDataIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + File(pluginsDir, "a").mkdirs() + Files.createSymbolicLink(File(pluginsDir, "a/link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" emptied a link's target").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(File(pluginsDir, "a").exists()).isFalse() + } + + @Test + fun aLinkInThePluginsIconsIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val icons = File(RuntimeEnvironment.getApplication().getDir("plugin_icons", Context.MODE_PRIVATE), "a").apply { mkdirs() } + Files.createSymbolicLink(File(icons, "link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" emptied a link's target").that(victim.list()?.toList()).containsExactly("keep.txt") + assertThat(icons.exists()).isFalse() + } + + @Test + fun aLinkInTheCompiledCodeFolderIsNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim().also { File(it, "a-0123456789012345.odex").writeText("not a plugin's") } + oatDir.mkdirs() + Files.createSymbolicLink(File(oatDir, "link").toPath(), victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted a file through a link") + .that(victim.list()?.toList()) + .containsExactly("keep.txt", "a-0123456789012345.odex") + } + + // Outside the folders an uninstall deletes, with a file that must survive. + private fun victim(): File = + File(RuntimeEnvironment.getApplication().filesDir.parentFile, "victim").apply { mkdirs() }.also { + File(it, "keep.txt").writeText("not a plugin's") + } + private fun file( dir: File, path: String, From 13ecc98b903fbe8dab6916de2a0084fea869ccf7 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 21:05:27 -0800 Subject: [PATCH 09/13] style: spotless reformat, no functional change PluginProjectManager.kt and IdeTemplateServiceImpl.kt were indented with spaces. The next commit edits them, which brings them under the Spotless ratchet, so their reformat comes first and on its own. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../manager/project/PluginProjectManager.kt | 159 +++++++++--------- .../services/IdeTemplateServiceImpl.kt | 141 ++++++++-------- 2 files changed, 150 insertions(+), 150 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt index 3176ac508c..1ea1f1c9df 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt @@ -5,81 +5,86 @@ import org.slf4j.LoggerFactory import java.io.File class PluginProjectManager private constructor() { - - companion object { - private val log = LoggerFactory.getLogger(PluginProjectManager::class.java) - - @Volatile - private var instance: PluginProjectManager? = null - - fun getInstance(): PluginProjectManager { - return instance ?: synchronized(this) { - instance ?: PluginProjectManager().also { instance = it } - } - } - - private const val PLUGIN_CGT_PREFIX = "plugin_" - private const val CGT_EXTENSION = "cgt" - } - - private var templateReloadListener: (() -> Unit)? = null - - fun setTemplateReloadListener(listener: (() -> Unit)?) { - this.templateReloadListener = listener - } - - fun extractBundledCgtTemplates(pluginId: String, classLoader: ClassLoader) { - val searchPaths = listOf("templates/", "assets/templates/") - - val extracted = searchPaths.flatMap { basePath -> - extractCgtFromPath(pluginId, classLoader, basePath) - } - - if (extracted.isNotEmpty()) { - templateReloadListener?.invoke() - } - } - - private fun extractCgtFromPath( - pluginId: String, - classLoader: ClassLoader, - basePath: String - ): List { - val entries = classLoader.getResourceAsStream(basePath) - ?.bufferedReader() - ?.use { it.readLines() } - ?: return emptyList() - - return entries - .filter { it.endsWith(".$CGT_EXTENSION") } - .mapNotNull { entry -> - val inputStream = classLoader.getResourceAsStream("$basePath$entry") ?: return@mapNotNull null - val destFile = File(Environment.TEMPLATES_DIR, "${PLUGIN_CGT_PREFIX}${pluginId}_$entry") - - runCatching { - inputStream.use { input -> - destFile.outputStream().use { output -> input.copyTo(output) } - } - log.info("Extracted bundled template: {} from plugin {}", entry, pluginId) - destFile - }.onFailure { - log.error("Failed to extract bundled template $entry from plugin $pluginId", it) - }.getOrNull() - } - } - - fun cleanupPluginTemplates(pluginId: String) { - val prefix = "${PLUGIN_CGT_PREFIX}${pluginId}_" - val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.count { file -> - file.delete().also { if (it) log.debug("Cleaned up plugin template: {}", file.name) } - } - ?: 0 - - if (deleted > 0) { - log.info("Cleaned up {} template files for plugin {}", deleted, pluginId) - templateReloadListener?.invoke() - } - } + companion object { + private val log = LoggerFactory.getLogger(PluginProjectManager::class.java) + + @Volatile + private var instance: PluginProjectManager? = null + + fun getInstance(): PluginProjectManager = + instance ?: synchronized(this) { + instance ?: PluginProjectManager().also { instance = it } + } + + private const val PLUGIN_CGT_PREFIX = "plugin_" + private const val CGT_EXTENSION = "cgt" + } + + private var templateReloadListener: (() -> Unit)? = null + + fun setTemplateReloadListener(listener: (() -> Unit)?) { + this.templateReloadListener = listener + } + + fun extractBundledCgtTemplates( + pluginId: String, + classLoader: ClassLoader, + ) { + val searchPaths = listOf("templates/", "assets/templates/") + + val extracted = + searchPaths.flatMap { basePath -> + extractCgtFromPath(pluginId, classLoader, basePath) + } + + if (extracted.isNotEmpty()) { + templateReloadListener?.invoke() + } + } + + private fun extractCgtFromPath( + pluginId: String, + classLoader: ClassLoader, + basePath: String, + ): List { + val entries = + classLoader + .getResourceAsStream(basePath) + ?.bufferedReader() + ?.use { it.readLines() } + ?: return emptyList() + + return entries + .filter { it.endsWith(".$CGT_EXTENSION") } + .mapNotNull { entry -> + val inputStream = classLoader.getResourceAsStream("$basePath$entry") ?: return@mapNotNull null + val destFile = File(Environment.TEMPLATES_DIR, "${PLUGIN_CGT_PREFIX}${pluginId}_$entry") + + runCatching { + inputStream.use { input -> + destFile.outputStream().use { output -> input.copyTo(output) } + } + log.info("Extracted bundled template: {} from plugin {}", entry, pluginId) + destFile + }.onFailure { + log.error("Failed to extract bundled template $entry from plugin $pluginId", it) + }.getOrNull() + } + } + + fun cleanupPluginTemplates(pluginId: String) { + val prefix = "${PLUGIN_CGT_PREFIX}${pluginId}_" + val deleted = + Environment.TEMPLATES_DIR + .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + ?.count { file -> + file.delete().also { if (it) log.debug("Cleaned up plugin template: {}", file.name) } + } + ?: 0 + + if (deleted > 0) { + log.info("Cleaned up {} template files for plugin {}", deleted, pluginId) + templateReloadListener?.invoke() + } + } } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt index 202f8a6cea..e255b5f0b4 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt @@ -8,90 +8,85 @@ import org.slf4j.LoggerFactory import java.io.File class IdeTemplateServiceImpl( - private val pluginId: String, - private val permissions: Set, - private val onTemplatesChanged: () -> Unit + private val pluginId: String, + private val permissions: Set, + private val onTemplatesChanged: () -> Unit, ) : IdeTemplateService { + companion object { + private val log = LoggerFactory.getLogger(IdeTemplateServiceImpl::class.java) + private const val PLUGIN_PREFIX = "plugin_" + private const val CGT_EXTENSION = "cgt" + } - companion object { - private val log = LoggerFactory.getLogger(IdeTemplateServiceImpl::class.java) - private const val PLUGIN_PREFIX = "plugin_" - private const val CGT_EXTENSION = "cgt" - } + private fun prefixedName(fileName: String): String = "${PLUGIN_PREFIX}${pluginId}_$fileName" - private fun prefixedName(fileName: String): String { - return "${PLUGIN_PREFIX}${pluginId}_$fileName" - } + override fun createTemplateBuilder(name: String): CgtTemplateBuilder = CgtTemplateBuilder(name) - override fun createTemplateBuilder(name: String): CgtTemplateBuilder { - return CgtTemplateBuilder(name) - } + override fun registerTemplate(cgtFile: File): Boolean { + if (!permissions.contains(PluginPermission.FILESYSTEM_WRITE)) { + log.warn("Plugin $pluginId lacks FILESYSTEM_WRITE permission for template registration") + return false + } - override fun registerTemplate(cgtFile: File): Boolean { - if (!permissions.contains(PluginPermission.FILESYSTEM_WRITE)) { - log.warn("Plugin $pluginId lacks FILESYSTEM_WRITE permission for template registration") - return false - } + if (!cgtFile.exists() || cgtFile.extension != CGT_EXTENSION) { + log.warn("Invalid template file for plugin $pluginId: ${cgtFile.absolutePath}") + return false + } - if (!cgtFile.exists() || cgtFile.extension != CGT_EXTENSION) { - log.warn("Invalid template file for plugin $pluginId: ${cgtFile.absolutePath}") - return false - } + val destFile = File(Environment.TEMPLATES_DIR, prefixedName(cgtFile.name)) + return try { + cgtFile.copyTo(destFile, overwrite = true) + log.info("Plugin $pluginId registered template: ${destFile.name}") + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + true + } catch (e: Exception) { + log.error("Failed to register template for plugin $pluginId", e) + false + } + } - val destFile = File(Environment.TEMPLATES_DIR, prefixedName(cgtFile.name)) - return try { - cgtFile.copyTo(destFile, overwrite = true) - log.info("Plugin $pluginId registered template: ${destFile.name}") - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - true - } catch (e: Exception) { - log.error("Failed to register template for plugin $pluginId", e) - false - } - } + override fun unregisterTemplate(templateFileName: String): Boolean { + val destFile = File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)) + if (!destFile.exists()) return false - override fun unregisterTemplate(templateFileName: String): Boolean { - val destFile = File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)) - if (!destFile.exists()) return false + return if (destFile.delete()) { + log.info("Plugin $pluginId unregistered template: ${destFile.name}") + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + true + } else { + log.warn("Failed to delete template file: ${destFile.name}") + false + } + } - return if (destFile.delete()) { - log.info("Plugin $pluginId unregistered template: ${destFile.name}") - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - true - } else { - log.warn("Failed to delete template file: ${destFile.name}") - false - } - } + override fun isTemplateRegistered(templateFileName: String): Boolean = + File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)).exists() - override fun isTemplateRegistered(templateFileName: String): Boolean { - return File(Environment.TEMPLATES_DIR, prefixedName(templateFileName)).exists() - } + override fun getRegisteredTemplates(): List { + val prefix = prefixedName("") + return Environment.TEMPLATES_DIR + .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + ?.map { it.name.removePrefix(prefix) } + ?: emptyList() + } - override fun getRegisteredTemplates(): List { - val prefix = prefixedName("") - return Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.map { it.name.removePrefix(prefix) } - ?: emptyList() - } + override fun reloadTemplates() { + onTemplatesChanged() + } - override fun reloadTemplates() { - onTemplatesChanged() - } + fun cleanupAllTemplates() { + val prefix = prefixedName("") + val deleted = + Environment.TEMPLATES_DIR + .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + ?.count { it.delete() } + ?: 0 - fun cleanupAllTemplates() { - val prefix = prefixedName("") - val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } - ?.count { it.delete() } - ?: 0 - - if (deleted > 0) { - runCatching { onTemplatesChanged() } - .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } - } - } + if (deleted > 0) { + runCatching { onTemplatesChanged() } + .onFailure { log.warn("Failed to notify template change for plugin $pluginId", it) } + } + } } From f58733a936dee3462d15af243257758dc5083fa5 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 21:01:40 -0800 Subject: [PATCH 10/13] ADFA-7160: Keep another loaded plugin's templates on uninstall Template cleanup and listing matched every .cgt starting with plugin__, so updating com.example.my deleted the templates of the loaded plugin com.example.my_tools. A shared helper, isTemplateOf, now excludes names owned by another loaded plugin whose ID extends this one's. PluginManager passes the other loaded IDs to PluginProjectManager and to IdeTemplateServiceImpl. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginManager.kt | 6 +- .../manager/project/PluginProjectManager.kt | 24 +++++- .../services/IdeTemplateServiceImpl.kt | 11 ++- .../manager/core/PluginTemplateCleanupTest.kt | 77 +++++++++++++++++++ .../manager/project/IsTemplateOfTest.kt | 42 ++++++++++ 5 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index 1a58f57d02..d8ff32e822 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -814,7 +814,7 @@ class PluginManager private constructor( } } - PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId) + PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId, loadedPlugins.keys - pluginId) PluginSnippetManager.getInstance().cleanupPlugin(pluginId) snippetRefreshListener?.invoke(pluginId) @@ -1171,7 +1171,7 @@ class PluginManager private constructor( private fun cleanupPluginContributions(loadedPlugin: LoadedPlugin) { val pluginId = loadedPlugin.manifest.id - runCatching { PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId) } + runCatching { PluginProjectManager.getInstance().cleanupPluginTemplates(pluginId, loadedPlugins.keys - pluginId) } .onFailure { logger.error("Failed to clean project templates for: $pluginId", it) } runCatching { PluginSnippetManager.getInstance().cleanupPlugin(pluginId) @@ -1570,6 +1570,7 @@ class PluginManager private constructor( pluginId = pluginId, permissions = permissions, onTemplatesChanged = { templateReloadListener?.invoke() }, + loadedPluginIds = { loadedPlugins.keys }, ) } @@ -1839,6 +1840,7 @@ class PluginManager private constructor( pluginId = pluginId, permissions = permissions, onTemplatesChanged = { templateReloadListener?.invoke() }, + loadedPluginIds = { loadedPlugins.keys }, ) } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt index 1ea1f1c9df..615820cf1f 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/project/PluginProjectManager.kt @@ -72,11 +72,13 @@ class PluginProjectManager private constructor() { } } - fun cleanupPluginTemplates(pluginId: String) { - val prefix = "${PLUGIN_CGT_PREFIX}${pluginId}_" + fun cleanupPluginTemplates( + pluginId: String, + otherPluginIds: Collection, + ) { val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + .listFiles { file -> isTemplateOf(pluginId, file.name, otherPluginIds) } ?.count { file -> file.delete().also { if (it) log.debug("Cleaned up plugin template: {}", file.name) } } @@ -88,3 +90,19 @@ class PluginProjectManager private constructor() { } } } + +/** + * Whether [fileName] in the templates folder is a template of [pluginId]: `plugin__.cgt`. + * Another plugin whose ID extends this one's (`_tools`) has names that start the same way, so + * its names are excluded. Templates exist only for loaded plugins, so [otherPluginIds] is the other + * loaded plugins. Undecidable: this plugin's own `tools_x.cgt` while `_tools` is loaded is + * kept, which leaves a file behind rather than deleting another plugin's. + */ +internal fun isTemplateOf( + pluginId: String, + fileName: String, + otherPluginIds: Collection, +): Boolean = + fileName.startsWith("plugin_${pluginId}_") && + fileName.substringAfterLast('.', "") == "cgt" && + otherPluginIds.none { it.startsWith("${pluginId}_") && fileName.startsWith("plugin_${it}_") } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt index e255b5f0b4..b02a2a1ac7 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/services/IdeTemplateServiceImpl.kt @@ -1,6 +1,7 @@ package com.itsaky.androidide.plugins.manager.services import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.manager.project.isTemplateOf import com.itsaky.androidide.plugins.services.IdeTemplateService import com.itsaky.androidide.plugins.templates.CgtTemplateBuilder import com.itsaky.androidide.utils.Environment @@ -11,6 +12,7 @@ class IdeTemplateServiceImpl( private val pluginId: String, private val permissions: Set, private val onTemplatesChanged: () -> Unit, + private val loadedPluginIds: () -> Collection, ) : IdeTemplateService { companion object { private val log = LoggerFactory.getLogger(IdeTemplateServiceImpl::class.java) @@ -66,21 +68,24 @@ class IdeTemplateServiceImpl( override fun getRegisteredTemplates(): List { val prefix = prefixedName("") + val others = otherPluginIds() return Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + .listFiles { file -> isTemplateOf(pluginId, file.name, others) } ?.map { it.name.removePrefix(prefix) } ?: emptyList() } + private fun otherPluginIds() = loadedPluginIds() - pluginId + override fun reloadTemplates() { onTemplatesChanged() } fun cleanupAllTemplates() { - val prefix = prefixedName("") + val others = otherPluginIds() val deleted = Environment.TEMPLATES_DIR - .listFiles { file -> file.name.startsWith(prefix) && file.extension == CGT_EXTENSION } + .listFiles { file -> isTemplateOf(pluginId, file.name, others) } ?.count { it.delete() } ?: 0 diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt new file mode 100644 index 0000000000..a1c5c442d7 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginTemplateCleanupTest.kt @@ -0,0 +1,77 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.PluginPermission +import com.itsaky.androidide.plugins.manager.project.PluginProjectManager +import com.itsaky.androidide.plugins.manager.services.IdeTemplateServiceImpl +import com.itsaky.androidide.utils.Environment +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * Updating or uninstalling plugin "com.example.my" deletes its own templates, not those of the + * loaded plugin "com.example.my_tools", whose names start with the same prefix. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginTemplateCleanupTest { + private val previous = Environment.TEMPLATES_DIR + private lateinit var templates: File + + @Before + fun pointTemplatesAtATempFolder() { + templates = File(RuntimeEnvironment.getApplication().filesDir, "templates").apply { mkdirs() } + Environment.TEMPLATES_DIR = templates + } + + @After + fun restoreTemplatesFolder() { + Environment.TEMPLATES_DIR = previous + } + + @Test + fun cleanupPluginTemplatesKeepsAnotherLoadedPluginsTemplates() { + val own = template("plugin_com.example.my_starter.cgt") + val other = template("plugin_com.example.my_tools_starter.cgt") + + PluginProjectManager.getInstance().cleanupPluginTemplates("com.example.my", listOf("com.example.my_tools")) + + assertThat(own.exists()).isFalse() + assertWithMessage("cleanup deleted another plugin's template").that(other.exists()).isTrue() + } + + @Test + fun cleanupAllTemplatesKeepsAnotherLoadedPluginsTemplates() { + val own = template("plugin_com.example.my_starter.cgt") + val other = template("plugin_com.example.my_tools_starter.cgt") + val service = service("com.example.my", loaded = listOf("com.example.my", "com.example.my_tools")) + + service.cleanupAllTemplates() + + assertThat(own.exists()).isFalse() + assertWithMessage("cleanup deleted another plugin's template").that(other.exists()).isTrue() + } + + @Test + fun getRegisteredTemplatesListsOnlyTheOwnTemplates() { + template("plugin_com.example.my_starter.cgt") + template("plugin_com.example.my_tools_starter.cgt") + val service = service("com.example.my", loaded = listOf("com.example.my", "com.example.my_tools")) + + assertThat(service.getRegisteredTemplates()).containsExactly("starter.cgt") + } + + private fun service( + id: String, + loaded: List, + ) = IdeTemplateServiceImpl(id, setOf(PluginPermission.FILESYSTEM_WRITE), {}, { loaded }) + + private fun template(name: String): File = File(templates, name).apply { writeText(name) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt new file mode 100644 index 0000000000..f8075f9575 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/project/IsTemplateOfTest.kt @@ -0,0 +1,42 @@ +package com.itsaky.androidide.plugins.manager.project + +import com.google.common.truth.Truth.assertThat +import org.junit.Test + +class IsTemplateOfTest { + @Test + fun aTemplateOfThePluginMatches() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_starter.cgt", emptyList())).isTrue() + } + + @Test + fun aTemplateOfAnotherLoadedPluginWhoseIdExtendsThisOneIsNotMatched() { + val others = listOf("com.example.my_tools") + + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_tools_starter.cgt", others)).isFalse() + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_starter.cgt", others)).isTrue() + } + + @Test + fun anExtendingIdThatIsNotLoadedDoesNotProtectItsName() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my_tools_starter.cgt", emptyList())).isTrue() + } + + @Test + fun anotherPluginsNameWithoutTheUnderscoreIsNotMatched() { + assertThat(isTemplateOf("com.example.my", "plugin_com.example.my2_starter.cgt", listOf("com.example.my2"))).isFalse() + } + + @Test + fun theOwnTemplateThatLooksLikeAnotherPluginsIsKept() { + assertThat(isTemplateOf("x", "plugin_x_tools_a.cgt", listOf("x_tools"))).isFalse() + } + + @Test + fun otherFilesAreNotMatched() { + assertThat(isTemplateOf("a", "plugin_a_starter.zip", emptyList())).isFalse() + assertThat(isTemplateOf("a", "plugin_a_starter", emptyList())).isFalse() + assertThat(isTemplateOf("a", "plugin_b_starter.cgt", emptyList())).isFalse() + assertThat(isTemplateOf("a", "core.cgt", emptyList())).isFalse() + } +} From b80f055230fa0ff39d37e1243bdafc9c4211398c Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 21:04:02 -0800 Subject: [PATCH 11/13] ADFA-7160: Refuse plugin IDs CoGo uses; close uninstall link edge cases - Refuse the IDs `oat` and `*.cgp` (any case): their data folder would be ART's compiled-code folder or another plugin's older-build file, and uninstall deletes it. Documented in PLUGIN_AUTHORING.md. - extractNativeLibs throws if a link at the plugin's libs folder survives its deletion, instead of extracting into the link's target. - Uninstall deletes a dangling link at the plugin's data folder (no exists() guard, which follows links). - Compiled code is kept for a plugin file under the stem in any extension case (.CGP), as loadPlugins loads those too. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- docs/PLUGIN_AUTHORING.md | 2 +- .../plugins/manager/core/PluginBuildFiles.kt | 5 +- .../plugins/manager/core/PluginIds.kt | 29 +++++++-- .../plugins/manager/core/PluginManager.kt | 7 +-- .../manager/core/PluginBuildFilesTest.kt | 21 +++++++ .../plugins/manager/core/PluginIdTest.kt | 39 ++++++++++++ .../manager/core/PluginUninstallTest.kt | 62 +++++++++++++++++++ 7 files changed, 155 insertions(+), 10 deletions(-) diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index 85e18dffb1..c51558dd79 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -112,7 +112,7 @@ these in `PluginLoader.getPluginMetadata()` | Key | Type | Required | Notes | |---------------------------|-------------------------|----------|-----------------------------------------------------------------------| -| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL; CoGo refuses such a plugin. | +| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL, and it can't be `oat` or end in `.cgp` (any case), which CoGo uses for its own files; CoGo refuses such a plugin. | | `plugin.name` | string | yes\* | Display name. | | `plugin.version` | string | yes\* | Defaults to `"1.0.0"`. Prefer `${pluginVersion}` from `pluginBuilder`. | | `plugin.description` | string | no | Shown in the plugin list. | diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 4f68f1b18a..aeaf58b891 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -58,7 +58,8 @@ class PluginBuildFiles internal constructor( ): Boolean { if (!isCompiledCodeOf(pluginId, file)) return false val stem = file.name.replace(Regex("\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)$"), "") - return !File(pluginsDir, "$stem.$PLUGIN_ARCHIVE_EXTENSION").exists() + // loadPlugins loads .CGP too, so any case of the extension keeps the code. + return pluginsDir.listFiles { f -> isPluginFile(f) && f.nameWithoutExtension == stem }.isNullOrEmpty() } /** Every plugin file whose manifest has [pluginId]. Its name can't tell: it names the build. */ @@ -106,6 +107,8 @@ class PluginBuildFiles internal constructor( if (Files.isSymbolicLink(pluginDir.toPath())) { log.warn("{} is a link; deleting the link and extracting into a folder", pluginDir) pluginDir.deleteWithoutFollowingLinks() + // Else the steps below run inside the link's target and empty it. + if (Files.isSymbolicLink(pluginDir.toPath())) throw IOException("Could not delete the link ${pluginDir.absolutePath}") } val key = buildKey(pluginId, build) // Before folderFor: a link left at the build's path would block the scratch folder's rename. diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt index 6c77fbc323..cd0910fe70 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIds.kt @@ -1,13 +1,34 @@ package com.itsaky.androidide.plugins.manager.core +import org.adfa.constants.PLUGIN_ARCHIVE_EXTENSION + /** * Fails when [id] can't be a plugin ID. The ID names the plugin's files and folders, so one that * is a path reaches outside them: installing `../files` would delete the app's whole files folder. + * So do two names CoGo uses itself in the plugins folder, where the data folder is ``: `oat` + * is ART's folder of every plugin's compiled code, and `x.cgp` is plugin `x`'s file from an older + * CoGo build. Uninstalling either would delete what it names. * Nullable because a `plugin.json` manifest without an ID parses to a null one. */ internal fun checkPluginId(id: String?): Result = - if (id.isNullOrEmpty() || id == "." || id == ".." || id.any { it == '/' || it == '\\' || it == '\u0000' }) { - Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": it must be a name, not a path")) - } else { - Result.success(id) + when { + id.isNullOrEmpty() || id == "." || id == ".." || id.any { it == '/' || it == '\\' || it == '\u0000' } -> { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": it must be a name, not a path")) + } + + id.equals("oat", ignoreCase = true) -> { + Result.failure(IllegalArgumentException("Invalid plugin ID \"$id\": \"oat\" is the folder of compiled plugin code")) + } + + id.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true) -> { + Result.failure( + IllegalArgumentException( + "Invalid plugin ID \"$id\": it must not end in .$PLUGIN_ARCHIVE_EXTENSION, which names a plugin file", + ), + ) + } + + else -> { + Result.success(id) + } } diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt index d8ff32e822..4f1dd0f8b7 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManager.kt @@ -1882,10 +1882,9 @@ class PluginManager private constructor( logger.debug("Cleaning up ALL files and cache for plugin: $pluginId") val pluginDir = File(pluginsDir, pluginId) - if (pluginDir.exists()) { - val deleted = pluginDir.deleteWithoutFollowingLinks() - logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") - } + // No exists() guard: it follows links, so a dangling link would survive. + val deleted = pluginDir.deleteWithoutFollowingLinks() + logger.debug("Deleted plugin directory: ${pluginDir.absolutePath} (success: $deleted)") File(context.getDir("plugin_icons", Context.MODE_PRIVATE), pluginId).let { dir -> dir.deleteWithoutFollowingLinks() diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index 87664ce7a3..5a1fcb1a4e 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -6,6 +6,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import java.io.File +import java.io.IOException import java.nio.file.Files import java.nio.file.LinkOption import java.util.concurrent.CountDownLatch @@ -251,6 +252,26 @@ class PluginBuildFilesTest { assertWithMessage("the libraries went through a link").that(Files.isSymbolicLink(File(nativeLibsDir, ID).toPath())).isFalse() } + @Test + fun aPluginFolderLinkThatCannotBeDeletedFailsExtractionInsteadOfEmptyingItsTarget() { + val victim = victim() + val link = File(nativeLibsDir, ID).also { Files.createSymbolicLink(it.toPath(), victim.toPath()) } + val build = installed(build("a.cgp", "libfoo.so" to "A")) + + // The link can't be unlinked from a read-only folder. + nativeLibsDir.setWritable(false) + val extracted = + try { + runCatching { files.extractNativeLibs(ID, build, ABI) } + } finally { + nativeLibsDir.setWritable(true) + } + + assertThat(extracted.exceptionOrNull()).isInstanceOf(IOException::class.java) + assertThat(extracted.exceptionOrNull()).hasMessageThat().contains(link.absolutePath) + assertWithMessage("extraction went through the link").that(victim.list()?.toList()).containsExactly("keep.txt") + } + @Test fun aLinkInThePluginFolderIsNotFollowedByUninstall() { val victim = victim() diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt index d259cd1950..a38a2bea1d 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginIdTest.kt @@ -55,6 +55,37 @@ class PluginIdTest { assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"../files\": it must be a name, not a path") } + @Test + fun theInstallerRefusesTheIdsOfCoGosOwnFiles() { + for ((id, message) in refusedCoGoIds) { + val result = pluginManager().getPluginValidation(plugin(id)) + + assertWithMessage("the installer accepted \"$id\"").that(result.isFailure).isTrue() + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": $message") + } + } + + @Test + fun loadingRefusesTheIdsOfCoGosOwnFiles() { + for ((id, message) in refusedCoGoIds) { + val result = pluginManager().loadPlugin(plugin(id)) + + assertThat(result.exceptionOrNull()).hasMessageThat().isEqualTo("Invalid plugin ID \"$id\": $message") + } + } + + @Test + fun idsThatOnlyLookLikeCoGosOwnFilesAreAccepted() { + for (id in listOf("oat.tools", "cgp", "my.cgp.tools", "oats", "x.cgpx")) { + assertThat( + pluginManager() + .getPluginValidation(plugin(id)) + .getOrThrow() + .manifest.id, + ).isEqualTo(id) + } + } + @Test fun aManifestWithoutAnIdIsRefused() { assertThat(checkPluginId(null).exceptionOrNull()) @@ -65,5 +96,13 @@ class PluginIdTest { @After fun forgetManager() = forgetPluginManager() + private val refusedCoGoIds = + listOf( + "oat" to "\"oat\" is the folder of compiled plugin code", + "OAT" to "\"oat\" is the folder of compiled plugin code", + "x.cgp" to "it must not end in .cgp, which names a plugin file", + "x.CGP" to "it must not end in .cgp, which names a plugin file", + ) + private fun plugin(id: String): File = fakePlugin(File(temp.newFolder(), "plugin.cgp"), id) } diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt index d7b03e66dc..5cfd44c6d3 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -11,6 +11,7 @@ import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config import java.io.File import java.nio.file.Files +import java.nio.file.LinkOption /** * Uninstalling a plugin deletes its own files only. Update uninstalls first, so the same goes for @@ -110,6 +111,65 @@ class PluginUninstallTest { .isEmpty() } + @Test + fun compiledCodeOfAnotherPluginInstalledWithAnUpperCaseExtensionIsKept() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + fakePlugin(File(pluginsDir, "a-0123456789abcdef.CGP"), "a-0123456789abcdef") + val other = file(oatDir, "arm64/a-0123456789abcdef.odex") + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" deleted the compiled code of a .CGP file").that(other.exists()).isTrue() + } + + @Test + fun aLinkAtThePluginsDataFolderIsDeletedNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val link = File(pluginsDir, "a").toPath() + Files.createSymbolicLink(link, victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertWithMessage("the link is still there").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aDanglingLinkAtThePluginsDataFolderIsDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val link = File(pluginsDir, "a").toPath() + Files.createSymbolicLink(link, File(RuntimeEnvironment.getApplication().filesDir.parentFile, "gone").toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the dangling link survived").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aLinkAtThePluginsIconsFolderIsDeletedNotFollowed() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val victim = victim() + val link = iconsDir("a").toPath() + Files.createSymbolicLink(link, victim.toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the link's target lost files").that(victim.list()?.toList()).containsExactly("keep.txt") + assertWithMessage("the link is still there").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + + @Test + fun aDanglingLinkAtThePluginsIconsFolderIsDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") + val link = iconsDir("a").toPath() + Files.createSymbolicLink(link, File(RuntimeEnvironment.getApplication().filesDir.parentFile, "gone").toPath()) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("the dangling link survived").that(Files.exists(link, LinkOption.NOFOLLOW_LINKS)).isFalse() + } + @Test fun aLinkInThePluginsDataIsNotFollowed() { fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") @@ -150,6 +210,8 @@ class PluginUninstallTest { .containsExactly("keep.txt", "a-0123456789012345.odex") } + private fun iconsDir(id: String): File = File(RuntimeEnvironment.getApplication().getDir("plugin_icons", Context.MODE_PRIVATE), id) + // Outside the folders an uninstall deletes, with a file that must survive. private fun victim(): File = File(RuntimeEnvironment.getApplication().filesDir.parentFile, "victim").apply { mkdirs() }.also { From 15fa6729c26b1495672626b714d402e8a54c8c04 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 21:55:45 -0800 Subject: [PATCH 12/13] ADFA-7160: Share the compiled-code suffixes; skip the unlink test as root From CodeRabbit. - isCompiledCodeOf and isLeftoverCompiledCodeOf each listed ART's suffixes. A suffix added to only one would leave the stem unmatched, and uninstall would delete another plugin's compiled code. Both now use one constant. - The test for a link that can't be unlinked relies on a read-only folder. Root, and file systems without POSIX permissions, ignore the write bit, so the test is skipped there instead of failing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../androidide/plugins/manager/core/PluginBuildFiles.kt | 9 +++++++-- .../plugins/manager/core/PluginBuildFilesTest.kt | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index aeaf58b891..86e5ce0124 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -57,7 +57,7 @@ class PluginBuildFiles internal constructor( file: File, ): Boolean { if (!isCompiledCodeOf(pluginId, file)) return false - val stem = file.name.replace(Regex("\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)$"), "") + val stem = file.name.replace(COMPILED_CODE_END, "") // loadPlugins loads .CGP too, so any case of the extension keeps the code. return pluginsDir.listFiles { f -> isPluginFile(f) && f.nameWithoutExtension == stem }.isNullOrEmpty() } @@ -232,6 +232,11 @@ class PluginBuildFiles internal constructor( private const val HASH_CHARS = 16 + // ART's files for `.cgp`. One list for matching and for finding the stem: a suffix + // missing from either would leave the stem unmatched and delete another plugin's code. + private const val COMPILED_CODE_SUFFIX = "(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)" + private val COMPILED_CODE_END = Regex("\\.$COMPILED_CODE_SUFFIX$") + // Not app_plugin_native_libs: an older CoGo build reuses its plugin's folder there whatever it // holds, so it would find these per-build folders and no libraries. Folder names as getDir() // makes them. @@ -276,7 +281,7 @@ class PluginBuildFiles internal constructor( file: File, ): Boolean = file.isFile && - Regex("${Regex.escape(pluginId)}(-[0-9a-f]{$HASH_CHARS})?\\.(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)") + Regex("${Regex.escape(pluginId)}(-[0-9a-f]{$HASH_CHARS})?\\.$COMPILED_CODE_SUFFIX") .matches(file.name) private fun sha256Hex(file: File): String { diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt index 5a1fcb1a4e..f2a467644f 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFilesTest.kt @@ -2,6 +2,7 @@ package com.itsaky.androidide.plugins.manager.core import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertWithMessage +import org.junit.Assume import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder @@ -262,6 +263,8 @@ class PluginBuildFilesTest { nativeLibsDir.setWritable(false) val extracted = try { + // Root, and file systems without POSIX permissions, ignore the write bit. + Assume.assumeFalse("the folder stayed writable", nativeLibsDir.canWrite()) runCatching { files.extractNativeLibs(ID, build, ABI) } } finally { nativeLibsDir.setWritable(true) From 5d19ce4d4736585203f1673d309622c82f9a3cff Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 22:25:15 -0800 Subject: [PATCH 13/13] ADFA-7160: Test the template fix's wiring; recognize .CGP profiles - Add PluginManagerTemplateWiringTest: with plugins "x" and "x_tools" really loaded, unloading or force-disabling "x" keeps x_tools's template, and x's template service does not list it. Emptying the four loaded-plugin-ID sites in PluginManager now fails these tests. - Match the archive extension of ART's .prof / .cur.prof files case-insensitively, so uninstalling a plugin installed as .CGP deletes its profiles. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZjbyUcUDporb95N9T --- .../plugins/manager/core/PluginBuildFiles.kt | 6 +- .../core/PluginManagerTemplateWiringTest.kt | 74 +++++++++++++++++++ .../manager/core/PluginUninstallTest.kt | 10 +++ .../manager/core/RobolectricPlugins.kt | 23 +++++- 4 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt index 86e5ce0124..7f413b6e85 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/core/PluginBuildFiles.kt @@ -232,9 +232,9 @@ class PluginBuildFiles internal constructor( private const val HASH_CHARS = 16 - // ART's files for `.cgp`. One list for matching and for finding the stem: a suffix - // missing from either would leave the stem unmatched and delete another plugin's code. - private const val COMPILED_CODE_SUFFIX = "(odex|vdex|art|$PLUGIN_ARCHIVE_EXTENSION\\.(cur\\.)?prof)" + // ART's files for `.cgp`, or `.CGP`: it names profiles after the file. One list for matching + // and for finding the stem: a suffix missing from either would leave the stem unmatched and delete another plugin's code. + private const val COMPILED_CODE_SUFFIX = "(odex|vdex|art|(?i:$PLUGIN_ARCHIVE_EXTENSION)\\.(cur\\.)?prof)" private val COMPILED_CODE_END = Regex("\\.$COMPILED_CODE_SUFFIX$") // Not app_plugin_native_libs: an older CoGo build reuses its plugin's folder there whatever it diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt new file mode 100644 index 0000000000..2d3cea44c1 --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginManagerTemplateWiringTest.kt @@ -0,0 +1,74 @@ +package com.itsaky.androidide.plugins.manager.core + +import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage +import com.itsaky.androidide.plugins.services.IdeTemplateService +import com.itsaky.androidide.utils.Environment +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +/** + * The plugin manager tells template cleanup which other plugins are loaded: unloading or disabling + * plugin "x" keeps the templates of the loaded plugin "x_tools", whose names start with the same prefix. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class PluginManagerTemplateWiringTest { + private val previous = Environment.TEMPLATES_DIR + private lateinit var templates: File + private lateinit var own: File + private lateinit var other: File + + @Before + fun loadTwoPluginsWithTemplates() { + templates = File(RuntimeEnvironment.getApplication().filesDir, "templates").apply { mkdirs() } + Environment.TEMPLATES_DIR = templates + own = template("plugin_x_starter.cgt") + other = template("plugin_x_tools_starter.cgt") + val pluginsDir = File(RuntimeEnvironment.getApplication().filesDir, "plugins") + val main = NoopPlugin::class.java.name + listOf("x", "x_tools").forEach { + val result = pluginManager().loadPlugin(fakePlugin(File(pluginsDir, "$it-0123456789012345.cgp"), it, main)) + assertWithMessage("plugin $it did not load: ${result.exceptionOrNull()}").that(result.isSuccess).isTrue() + } + assertThat(pluginManager().getLoadedPluginIds()).containsExactly("x", "x_tools") + } + + @After + fun restoreTemplatesFolder() { + Environment.TEMPLATES_DIR = previous + forgetPluginManager() + } + + @Test + fun unloadingAPluginKeepsTheTemplatesOfAnotherLoadedPlugin() { + assertThat(pluginManager().unloadPlugin("x")).isTrue() + + assertThat(own.exists()).isFalse() + assertWithMessage("unloading \"x\" deleted a template of \"x_tools\"").that(other.exists()).isTrue() + } + + @Test + fun forceDisablingAPluginKeepsTheTemplatesOfAnotherLoadedPlugin() { + pluginManager().forceDisablePlugin("x") + + assertThat(own.exists()).isFalse() + assertWithMessage("disabling \"x\" deleted a template of \"x_tools\"").that(other.exists()).isTrue() + } + + @Test + fun aPluginsTemplateServiceDoesNotListTheTemplatesOfAnotherLoadedPlugin() { + val plugin = pluginManager().getPlugin("x") as NoopPlugin + val service = plugin.context.services.get(IdeTemplateService::class.java)!! + + assertThat(service.getRegisteredTemplates()).containsExactly("starter.cgt") + } + + private fun template(name: String): File = File(templates, name).apply { writeText(name) } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt index 5cfd44c6d3..f09ad228e1 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/PluginUninstallTest.kt @@ -93,6 +93,16 @@ class PluginUninstallTest { assertWithMessage("uninstalling \"a\" left its own files").that(own.filter(File::exists)).isEmpty() } + @Test + fun theProfilesOfAPluginInstalledWithAnUpperCaseExtensionAreDeleted() { + fakePlugin(File(pluginsDir, "a-0123456789012345.CGP"), "a") + val own = listOf(file(oatDir, "a-0123456789012345.CGP.prof"), file(oatDir, "a-0123456789012345.CGP.cur.prof")) + + assertThat(pluginManager().uninstallPlugin("a")).isTrue() + + assertWithMessage("uninstalling \"a\" left the profiles of its .CGP file").that(own.filter(File::exists)).isEmpty() + } + @Test fun compiledCodeOfAnotherPluginWhoseIdLooksLikeThisPluginsFileNameIsKept() { fakePlugin(File(pluginsDir, "a-0123456789012345.cgp"), "a") diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt index 5188d120b3..b7f7f88b5e 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/core/RobolectricPlugins.kt @@ -3,6 +3,8 @@ package com.itsaky.androidide.plugins.manager.core import android.content.pm.ApplicationInfo import android.content.pm.PackageInfo import android.os.Bundle +import com.itsaky.androidide.plugins.IPlugin +import com.itsaky.androidide.plugins.PluginContext import com.itsaky.androidide.plugins.PluginLogger import org.robolectric.RuntimeEnvironment import org.robolectric.Shadows.shadowOf @@ -21,10 +23,11 @@ internal fun forgetPluginManager() { .set(null, null) } -/** Writes [file] as a plugin whose AndroidManifest states [id]: Robolectric can't read one from an APK. */ +/** Writes [file] as a plugin whose AndroidManifest states [id] and [mainClass]: Robolectric can't read one from an APK. */ internal fun fakePlugin( file: File, id: String, + mainClass: String = "org.example.plugin.Main", ): File { file.parentFile?.mkdirs() ZipOutputStream(file.outputStream()).use { it.putNextEntry(ZipEntry("classes.dex")) } @@ -36,7 +39,7 @@ internal fun fakePlugin( metaData = Bundle().apply { putString("plugin.id", id) - putString("plugin.main_class", "org.example.plugin.Main") + putString("plugin.main_class", mainClass) // Loading deletes and recreates the plugin's icon folder, named after its ID. putString("plugin.icon_day", "icon.png") } @@ -46,6 +49,22 @@ internal fun fakePlugin( return file } +/** A plugin that keeps its context. Name it as a fakePlugin's main class: the class loader finds it through its parent. */ +class NoopPlugin : IPlugin { + lateinit var context: PluginContext + + override fun initialize(context: PluginContext): Boolean { + this.context = context + return true + } + + override fun activate() = true + + override fun deactivate() = true + + override fun dispose() = Unit +} + internal object NoLogger : PluginLogger { override val pluginId = "test"