fix: refuse overwriting in-use component packages and clear unpacked store on replace (#1144)

* fix: refuse overwriting in-use component packages and clear unpacked store on replace

* fix: preserve component store until replacement succeeds

* fix: invalidate completed component cache before replace

* fix: constrain unpacked component cache path

* Test rejected package cache dot segments

---------

Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-25 17:50:03 -04:00
1 parent b4b7883648
commit e76489ccea
3 files changed
+197 -2

No files matched your search

@@ -780,6 +780,27 @@ object ComponentsManager {
true
}
private fun checkPackageUnused(context: Context, state: JSONObject, name: String) {
val installed = protons(context).distinctBy { it.id }.associateBy { it.id }
for (key in listOf("queued", "active")) {
val byProton = state.sub(key)
for (pid in byProton.keys()) {
val comps = byProton.optJSONObject(pid) ?: continue
for (comp in comps.keys()) {
val entry = comps.optJSONObject(comp) ?: continue
check(entry.optString("file") != name || !liveSelection(key, entry, installed[pid])) {
context.getString(R.string.cmgr_package_in_use, name, installed[pid]?.name ?: pid)
}
}
}
}
val profiles = state.sub("profiles")
for (scope in profiles.keys()) {
val profile = profiles.optJSONObject(scope) ?: continue
check(COMPONENTS.none { profile.optString(it) == name }) { context.getString(R.string.cmgr_package_in_use, name, scope) }
}
}
fun carryOver(context: Context, fromDir: String, toDir: String) {
synchronized(lock) {
val target = protons(context).firstOrNull { it.id == safeName(toDir) } ?: return
@@ -828,6 +849,33 @@ object ComponentsManager {
}
}
private fun storePackage(context: Context, part: File, dest: File) {
try {
if (dest.isFile && runCatching { Hashes.sha256(dest) }.getOrNull()?.equals(Hashes.sha256(part), true) == true) return
if (dest.exists()) checkPackageUnused(context, loadState(context), dest.name)
val store = File(launchDir(context), "store").canonicalFile
val cacheKey = unpackedCacheKey(dest.name)
val unpacked = File(store, cacheKey).canonicalFile
check(unpacked.parentFile == store) { "Invalid unpacked component cache path for ${dest.name}" }
val complete = File(unpacked, ".complete")
if (complete.exists()) check(complete.delete()) { "Could not invalidate unpacked component ${dest.name}" }
if (!part.renameTo(dest)) {
dest.delete()
check(part.renameTo(dest)) { "Could not store ${dest.name}" }
}
if (!unpacked.deleteRecursively() && unpacked.exists()) {
check(!complete.exists()) { "Could not invalidate unpacked component ${dest.name}" }
Log.w(TAG, "Could not remove stale unpacked files for ${dest.name}; cache marker is invalidated")
}
} finally {
part.delete()
}
}
internal fun unpackedCacheKey(filename: String): String = safeName(filename.removeSuffix(".wcp")).also {
require(it != "." && it != "..") { "Invalid component cache name" }
}
fun deletePackage(context: Context, file: String): String = synchronized(lock) {
val name = safeName(file)
val state = loadState(context)
@@ -877,7 +925,7 @@ object ComponentsManager {
fun importPackage(context: Context, source: File, name: String): Package = synchronized(lock) {
val info = packageInfo(context, source)
val dest = File(packagesDir(context), safeName(name).let { if (it.endsWith(".wcp")) it else "$it.wcp" })
source.copyTo(File(dest.parentFile, dest.name + ".part"), overwrite = true).renameTo(dest)
storePackage(context, source.copyTo(File(dest.parentFile, dest.name + ".part"), overwrite = true), dest)
info.copy(file = dest.name)
}
@@ -1030,7 +1078,7 @@ object ComponentsManager {
}
if (!Hashes.sha256(part).equals(item.digest.substringAfter(':'), true)) { part.delete(); error(context.getString(R.string.cmgr_checksum_mismatch)) }
val info = runCatching { packageInfo(context, part) }.getOrElse { part.delete(); throw it }
synchronized(lock) { part.renameTo(dest) }
synchronized(lock) { storePackage(context, part, dest) }
return info.copy(file = dest.name)
}
}