diff --git a/android/assets/bin/pkg.mjs b/android/assets/bin/pkg.mjs index fb3f0c7f..de9f33d4 100644 --- a/android/assets/bin/pkg.mjs +++ b/android/assets/bin/pkg.mjs @@ -534,8 +534,20 @@ function unpack(p, deb) { const st = fs.statSync(f); if (st.nlink > 1) { const b = fs.readFileSync(f); fs.unlinkSync(f); fs.writeFileSync(f, b, { mode: st.mode }); } // 硬链接拆开,各改各的 const b = fs.readFileSync(f); - const [o, k] = ZIP_EXT.some((e) => r.toLowerCase().endsWith(e)) ? rewriteZip(b) : swap(b); - if (k) { fs.writeFileSync(f, o); fs.chmodSync(f, st.mode & 0o7777); patched++; } + if (b.length > 32 && b.subarray(-4).toString("latin1") === "GBMB") { + // 带签名的 phar(如 composer):动任何字节 PHP 都会以签名不符拒跑;CLI phar 改名 .phar 并写绝对路径包装脚本,库 phar 原样保留 + if (b[0] === 0x23 && b[1] === 0x21 && (r.startsWith("bin/") || (st.mode & 0o111) !== 0) && !r.endsWith(".phar")) { + const pharRel = r + ".phar", pharAbs = f + ".phar"; + fs.renameSync(f, pharAbs); + fs.chmodSync(pharAbs, 0o755); + fs.writeFileSync(f, `#!${TARGET}files/usr/bin/sh\nexec "${TARGET}files/usr/bin/php" "${TARGET}files/usr/${pharRel}" "$@"\n`, { mode: 0o755 }); + files.push(pharRel); + patched++; + } + } else { + const [o, k] = ZIP_EXT.some((e) => r.toLowerCase().endsWith(e)) ? rewriteZip(b) : swap(b); + if (k) { fs.writeFileSync(f, o); fs.chmodSync(f, st.mode & 0o7777); patched++; } + } } else continue; files.push(r); } diff --git a/android/java/cc/codroid/GwRuntime.java b/android/java/cc/codroid/GwRuntime.java index c0fbaf3c..eecdb90f 100644 --- a/android/java/cc/codroid/GwRuntime.java +++ b/android/java/cc/codroid/GwRuntime.java @@ -204,6 +204,7 @@ private void fetchBase(java.util.List missing, Say say) throws Excepti File got = ins.download(Installer.DEVKIT_MIRROR + tag + "/" + file, base + file, new File(env.files, "gw-" + file), gz.optLong("bytes"), gz.optString("sha256")); File stage = new File(env.files, "gw-stage"); Installer.deleteTree(stage); + boolean ok = false; try { stage.mkdirs(); if (say != null) say.say(Tr.t("取回网关运行库:解包…")); @@ -217,13 +218,29 @@ private void fetchBase(java.util.List missing, Say say) throws Excepti swap = new byte[][] { a, b }; } for (String[] m : missing) put(new File(stage, m[1]), new File(m[0]), m[2], swap); + ok = true; } finally { Installer.deleteTree(stage); - got.delete(); new File(got.getPath() + ".sha256").delete(); + JSONObject pinGz = dp.optJSONObject("gz"); + String pinSha = pinGz == null ? "" : pinGz.optString("sha256", ""); + String instSha = env.devkitInfo().optString("sha256", ""); + // 工具包还没升级到当前钉住的版本时,把刚下好并核过 sha256 的基础模块包留给 Updater / Installer 复用,省掉重下 + File reuse = new File(env.files, file); + if (shouldKeepBaseForUpdater(pinSha, instSha, ok)) { + if (reuse.exists() && !reuse.delete()) got.delete(); + else if (!got.renameTo(reuse)) got.delete(); + } else { + got.delete(); + } } } + /** 工具包尚未装到钉住那版时保留基础模块压缩包给后续更新复用;已是同一版则立刻删掉免占空间 */ + static boolean shouldKeepBaseForUpdater(String pinSha, String installedSha, boolean ok) { + return ok && pinSha != null && !pinSha.isEmpty() && !pinSha.equals(installedSha); + } + // ── 哈希缓存:同一个文件(inode、大小、修改时间、ctime 都没变)不重算。ctime 用户态改不了,换内容一定会变 ── private static final java.util.Map CACHE = new java.util.concurrent.ConcurrentHashMap<>(); diff --git a/android/java/cc/codroid/Installer.java b/android/java/cc/codroid/Installer.java index 9918dd74..229fa862 100644 --- a/android/java/cc/codroid/Installer.java +++ b/android/java/cc/codroid/Installer.java @@ -632,10 +632,25 @@ void installModules(byte[] mjBytes, java.util.Collection want, String ba } static String moduleName(JSONObject m) { + String id = m.optString("id"); JSONObject n = m.optJSONObject("name"); - if (n == null) return m.optString("id"); - String s = n.optString(Tr.lang().startsWith("zh") ? "zh" : "en", ""); - return s.isEmpty() ? n.optString("zh", m.optString("id")) : s; + boolean zh = Tr.lang().startsWith("zh"); + String s = null; + if (n != null) { + s = n.optString(zh ? "zh" : "en", ""); + if (s.isEmpty()) s = n.optString("zh", ""); + } + return formatModuleName(id, m.optString("kind"), s, zh); + } + + /** 多个共享运行库(lib-clang / lib-guile / lib-libllvm / lib-openjdk-21)默认同名,按 id 后缀区分 */ + static String formatModuleName(String id, String kind, String base, boolean zh) { + String s = (base == null || base.isEmpty()) ? id : base; + if ("shared".equals(kind) && id != null && id.startsWith("lib-") && id.length() > 4) { + String tag = id.substring(4); + if (s != null && !s.contains(tag)) s = zh ? s + "(" + tag + ")" : s + " (" + tag + ")"; + } + return s; } /** 两次换名把新版换上;第二次失败就把旧版换回来 */ diff --git a/android/tests/cc/codroid/InstallerCarryTest.java b/android/tests/cc/codroid/InstallerCarryTest.java index af14507e..beb6abea 100644 --- a/android/tests/cc/codroid/InstallerCarryTest.java +++ b/android/tests/cc/codroid/InstallerCarryTest.java @@ -64,6 +64,19 @@ public static void main(String[] args) throws Exception { Installer.carryOver(old2, neu2, files, false, "", QUIET); check("换版本:组件文件不搬(旧工具包上的)", !new File(neu2, "bin/comp-tool").exists()); check("换版本:底座文件也不搬", !new File(neu2, "bin/base-tool").exists()); + + // 共享运行库名称消歧(v0.7.15 多共享层:lib-clang / lib-guile / lib-libllvm / lib-openjdk-21) + check("共享库中文消歧", "共享运行库(clang)".equals(Installer.formatModuleName("lib-clang", "shared", "共享运行库", true))); + check("共享库英文消歧", "Shared libraries (openjdk-21)".equals(Installer.formatModuleName("lib-openjdk-21", "shared", "Shared libraries", false))); + check("共享库已带后缀不重复拼接", "共享运行库(guile)".equals(Installer.formatModuleName("lib-guile", "shared", "共享运行库(guile)", true))); + check("普通模块原样返回", "C/C++ 构建".equals(Installer.formatModuleName("build", "module", "C/C++ 构建", true))); + + // GwRuntime 基础包复用判断:工具包未升级到钉住版本时保留,已是同一版或失败时删除 + check("GwRuntime 升级前保留基础包", GwRuntime.shouldKeepBaseForUpdater("sha-new", "sha-old", true)); + check("GwRuntime 首装无已装版本时保留基础包", GwRuntime.shouldKeepBaseForUpdater("sha-new", "", true)); + check("GwRuntime 已是同一版工具包时不保留", !GwRuntime.shouldKeepBaseForUpdater("sha-same", "sha-same", true)); + check("GwRuntime 提取失败时不保留", !GwRuntime.shouldKeepBaseForUpdater("sha-new", "sha-old", false)); + check("GwRuntime 缺钉子 sha 时不保留", !GwRuntime.shouldKeepBaseForUpdater("", "sha-old", true)); } finally { try (java.util.stream.Stream s = Files.walk(tmp)) { s.sorted(Comparator.reverseOrder()).forEach((p) -> { try { Files.delete(p); } catch (Exception ignored) {} }); diff --git a/devkit/devkit.mjs b/devkit/devkit.mjs index bb5b4f50..7a107cc8 100644 --- a/devkit/devkit.mjs +++ b/devkit/devkit.mjs @@ -300,15 +300,31 @@ async function unxzToFile(data, outPath) { return new Promise((ok, bad) => { const out = fs.createWriteStream(outPath); const child = spawn(XZ, ["-dc"], { env: XZENV, stdio: ["pipe", "pipe", "inherit"] }); - let done = false; - const fail = (e) => { if (!done) { done = true; bad(e); } }; + let done = false, outDone = false, exited = false, exitCode = null, exitSig = null; + const fail = (e) => { + if (done) return; + done = true; + out.destroy(); + try { child.kill(); } catch {} + bad(e); + }; + const maybeOk = () => { + if (done || !outDone || !exited) return; + if (exitCode !== 0) return fail(new Error(`xz -dc 退出码 ${exitCode ?? exitSig}`)); + done = true; + ok(); + }; child.on("error", fail); - child.stdin.on("error", fail); + child.stdin.on("error", (e) => { if (e.code !== "EPIPE") fail(e); }); // 坏输入下 xz 提前退出会触发 EPIPE,让 exit 报真正退出码 child.stdout.on("error", fail); child.stdout.pipe(out); // 不消费 stdout 的话 xz 会写满管道缓冲后卡死 out.on("error", fail); - out.on("finish", () => { if (!done) { done = true; ok(); } }); - child.on("exit", (code) => { if (code !== 0 && !done) fail(new Error(`xz -dc 退出码 ${code}`)); }); + out.on("finish", () => { outDone = true; maybeOk(); }); + child.on("exit", (code, sig) => { + exited = true; exitCode = code; exitSig = sig; + if (code !== 0) fail(new Error(`xz -dc 退出码 ${code ?? sig}`)); + else maybeOk(); + }); child.stdin.end(data); }); } @@ -1148,16 +1164,19 @@ if (cmd === "modules") { const mj = { // format:modules.json 自身的格式版本;compat 同 devkit.lock.json(外壳接口版本) format: 1, compat: 1, target, snapshot: plan.snapshot, base: plan.base, built: new Date().toISOString(), - modules: locks.map((l) => ({ - id: l.id, kind: l.kind, required: !!(def[l.id] || {}).required, deps: l.deps, users: l.users || [], - full: (def[l.id] || {}).full !== false, // false:整包里没有它,整包用户升级时不当成已装 - name: (def[l.id] || {}).name || { zh: "共享运行库", en: "Shared libraries" }, - desc: (def[l.id] || {}).desc || { zh: `被 ${l.users.join("、")} 共用的大库,没有模块用到时自动卸载。`, en: `Large library shared by ${l.users.join(", ")}; removed automatically when no module needs it.` }, - uses: (def[l.id] || {}).uses || [], dir: l.dir, - gz: l.gz, tar: { bytes: l.tar.bytes, sha256: l.tar.sha256 }, installed: l.installed, files: l.files, - commands: l.commands, key: ((def[l.id] || {}).key || []).filter((c) => l.commands.includes(c)), firstRun: l.firstRun, - packages: l.packages.map((p) => ({ name: p.name, version: p.version, source: p.source })), - })), + modules: locks.map((l) => { + const sharedTag = l.kind === "shared" && l.id.startsWith("lib-") && l.id.length > 4 ? l.id.slice(4) : ""; + return { + id: l.id, kind: l.kind, required: !!(def[l.id] || {}).required, deps: l.deps, users: l.users || [], + full: (def[l.id] || {}).full !== false, // false:整包里没有它,整包用户升级时不当成已装 + name: (def[l.id] || {}).name || (sharedTag ? { zh: `共享运行库(${sharedTag})`, en: `Shared libraries (${sharedTag})` } : { zh: "共享运行库", en: "Shared libraries" }), + desc: (def[l.id] || {}).desc || { zh: `被 ${l.users.join("、")} 共用的大库,没有模块用到时自动卸载。`, en: `Large library shared by ${l.users.join(", ")}; removed automatically when no module needs it.` }, + uses: (def[l.id] || {}).uses || [], dir: l.dir, + gz: l.gz, tar: { bytes: l.tar.bytes, sha256: l.tar.sha256 }, installed: l.installed, files: l.files, + commands: l.commands, key: ((def[l.id] || {}).key || []).filter((c) => l.commands.includes(c)), firstRun: l.firstRun, + packages: l.packages.map((p) => ({ name: p.name, version: p.version, source: p.source })), + }; + }), }; fs.writeFileSync(path.join(OUT, "modules.json"), JSON.stringify(mj, null, 2)); console.log(`\n✓ modules.json:${mj.modules.length} 块,下载合计 ${mb(mj.modules.reduce((s, m) => s + m.gz.bytes, 0))},共 ${owner.size} 个文件路径,跨块无冲突`); @@ -1238,16 +1257,23 @@ if (cmd === "build" || cmd === "component") { if (e.type === "1") { tw.add({ name: clean, type: "1", mode: e.mode, mtime: e.mtime, link: R.pathOf(e.link) || e.link }); continue; } if (e.type !== "0" && e.type !== "7") continue; // 带签名的 phar(文件尾有 GBMB 签名魔数,composer 就是):动任何字节 PHP 都会以「签名不符」拒跑 - // (首发 php 模块真机实测抓到)。原样保留、名字加 .phar,另放一个包装脚本在原路径上执行它(Debian 同款做法); - // shebang 不再改写——经 `php xxx.phar` 执行时它只是 PHP 注释,签名完好。 + // (首发 php 模块真机实测抓到)。可执行 CLI phar(带 shebang、非 .phar 扩展名)原样放成 .phar 并写绝对路径包装脚本; + // 库类 .phar(被 PHP require 加载的)原样保留、跳过前缀替换以免破坏签名。 if (e.data.length > 32 && e.data.subarray(-4).toString("latin1") === "GBMB") { // phar 布局:[签名][4 字节算法][GBMB] - tw.add({ name: clean + ".phar", type: "0", mode: 0o755, mtime: e.mtime, data: e.data }); - listing.push("/" + R.to.replace(/^\//, "") + "files/" + clean + ".phar"); - tw.add({ - name: clean, type: "0", mode: 0o755, mtime: e.mtime, - data: Buffer.from("#!" + R.to + "files/usr/bin/sh\nexec php \"" + R.to + "files/usr/" + clean.replace(/^usr\//, "") + ".phar\" \"$@\"\n"), - }); - files += 2; + const isCli = e.data[0] === 0x23 && e.data[1] === 0x21 && + (/^usr\/bin\//.test(clean) || ((e.mode || 0) & 0o111) !== 0) && !clean.endsWith(".phar"); + if (isCli) { + tw.add({ name: clean + ".phar", type: "0", mode: 0o755, mtime: e.mtime, data: e.data }); + listing.push("/" + R.to.replace(/^\//, "") + "files/" + clean + ".phar"); + tw.add({ + name: clean, type: "0", mode: 0o755, mtime: e.mtime, + data: Buffer.from("#!" + R.to + "files/usr/bin/sh\nexec \"" + R.to + "files/usr/bin/php\" \"" + R.to + "files/usr/" + clean.replace(/^usr\//, "") + ".phar\" \"$@\"\n"), + }); + files += 2; + } else { + tw.add({ name: clean, type: "0", mode: e.mode, mtime: e.mtime, data: e.data }); + files++; + } continue; } // zip 类归档要下潜到压缩条目内部再改一遍,flat 替换在里面找不到模式 diff --git a/gateway/gw/components.mjs b/gateway/gw/components.mjs index 07a67e19..fe14a23b 100644 --- a/gateway/gw/components.mjs +++ b/gateway/gw/components.mjs @@ -389,7 +389,7 @@ export async function breathe() { // 安装心跳的时间戳用开机以来的单调毫秒(/proc/uptime),和安卓侧 SystemClock.elapsedRealtime 同一基准: // 用户改系统时间(墙钟跳变)不再让全部心跳提前过期或集体新鲜(复核 P2-9)。字段名 at/phase/boot 与 // GatewayService.parseBeatAt 的解析口径一致,改字段要两头一起改(复核 P2-14 契约) -function uptimeMs() { +export function uptimeMs() { try { const ms = Math.round(Number(fs.readFileSync("/proc/uptime", "utf8").split(/\s+/)[0]) * 1000); if (ms > 0) return ms; @@ -399,14 +399,14 @@ function uptimeMs() { } let uptimeFallbackLogged = false; /** 开机代号:心跳里带上,重启后的旧心跳在 Java 侧直接判无效(复核 R2-P3-4) */ -function bootId() { +export function bootId() { try { const b = fs.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim(); if (b) return b; } catch {} return ""; } // 组件安装 / 模块下载 / 自检这些 detached 子进程,网关被 SIGKILL 后会继续写 $PREFIX(0.6.9 审计 195-T.4)。 // 起的时候落一份运行标记,正常退出删掉;新网关启动时按标记清掉上一任留下的。 -const COMP_RUN_DIR = PREFIX ? path.join(COMP_DIR, ".runs") : ""; +export const COMP_RUN_DIR = PREFIX ? path.join(COMP_DIR, ".runs") : ""; /** 落一份运行标记;office 的转换子进程也走这里(0.6.9 审计 195-T.4),别另起一套 */ export function markChild(child, cmd, args) { if (!COMP_RUN_DIR || !child.pid) return ""; @@ -991,7 +991,7 @@ if (PREFIX && LIVE) setTimeout(() => { compHealSkills(); compIndex(false).catch( // 分批递归删一棵树:大组件的暂存 / 备份有几百 MB 几千文件,同步 rm 会占死事件循环被看门狗杀(复核 R2-P2-4)。 // 删不掉的就留着(下次再清),别为清垃圾卡死。 -async function rmTreeBatched(p, beat = () => {}) { +export async function rmTreeBatched(p, beat = () => {}) { const ents = []; (function walk(rel) { let ds; try { ds = fs.readdirSync(path.join(p, rel), { withFileTypes: true }); } catch { return; } diff --git a/gateway/gw/modules.mjs b/gateway/gw/modules.mjs index a520434c..34f3ae3e 100644 --- a/gateway/gw/modules.mjs +++ b/gateway/gw/modules.mjs @@ -1,10 +1,14 @@ import fs from "node:fs"; import path from "node:path"; -import { execFile } from "node:child_process"; +import { spawn } from "node:child_process"; import { LAST_LANG, PREFIX, log, tr, tz } from "./config.mjs"; import { killTree } from "./claude.mjs"; import { NEXT, readBody, sendJson } from "./http.mjs"; -import { compBaseDeps, compJobsDeps, compNeeds, compRecords, compRun, devkitIdentity, sha256File, breathe } from "./components.mjs"; +import { termEnv } from "./terminal.mjs"; +import { + COMP_RUN_DIR, bootId, breathe, compBaseDeps, compJobsDeps, compNeeds, compRecords, compRun, + devkitIdentity, markChild, rmTreeBatched, sha256File, unmarkChild, uptimeMs, +} from "./components.mjs"; import { archiveProblem, parseTarListing } from "./archive-check.mjs"; // ── 模块化工具包(0.3.9 起)────────────────────────────────────────── @@ -89,10 +93,10 @@ function pkgRecords() { } /** 谁在用这一块:已装的模块(deps 含它)、已装的组件(依赖的底座包落在这块里、别的已装块又没有)、 * pkg 装的包(Depends 里有这块独有的包,#104 05-002)。组件判断不了依赖的(旧记录 + 没有索引)也算在用:宁可拦下,不能删坏 */ -function modDependents(mj, id) { - const mods = (mj.modules || []).filter((m) => m.id !== id && modInstalled(mj, m.id) && (m.deps || []).includes(id) && m.kind !== "shared").map((m) => m.id); +function modDependents(mj, id, ignoreMods = null) { + const mods = (mj.modules || []).filter((m) => m.id !== id && !(ignoreMods && ignoreMods.has(m.id)) && modInstalled(mj, m.id) && (m.deps || []).includes(id) && m.kind !== "shared").map((m) => m.id); const mine = new Set((blockOf(mj, id).packages || []).map((p) => p.name)); - for (const m of mj.modules || []) if (m.id !== id && modInstalled(mj, m.id)) for (const p of m.packages || []) mine.delete(p.name); + for (const m of mj.modules || []) if (m.id !== id && !(ignoreMods && ignoreMods.has(m.id)) && modInstalled(mj, m.id)) for (const p of m.packages || []) mine.delete(p.name); const comps = []; for (const [cid, rec] of compRecords()) { const deps = compBaseDeps(cid, rec.sha256, rec); @@ -168,7 +172,7 @@ async function modStart(id, repair) { for (const c of chain) { const b = blockOf(mj, c); if (!b.gz || !Number.isFinite(b.gz.bytes) || typeof b.gz.sha256 !== "string" || !b.gz.sha256) return { ok: false, message: tz("这个模块的下载信息不完整,先更新一次工具包") }; - try { fs.rmSync(path.join(STAGE, c), { recursive: true, force: true }); } catch {} + try { await rmTreeBatched(path.join(STAGE, c)); } catch {} } const need = modSpaceNeed(mj, chain); try { const st = fs.statfsSync(path.dirname(PREFIX)); const free = st.bavail * st.bsize; if (free < need) return { ok: false, message: tz("存储空间不够:需要约 {need} MB,只剩 {free} MB", { need: Math.ceil(need / 1048576), free: Math.floor(free / 1048576) }) }; } catch {} @@ -182,7 +186,7 @@ async function modStart(id, repair) { job.error = e.message === "cancelled" ? "" : e.message; // 失败的解包树(可能几百 MB)以前一直留在 devkit-stage,只有下次重装才会被开头那句清掉; // 用户不再重试就没有入口删。这里当场清掉。压缩包按组件安装的做法留着,重试时大小对得上就不重下(196-供应链.2) - try { if (job.stageTree) fs.rmSync(job.stageTree, { recursive: true, force: true }); } catch {} + try { if (job.stageTree) await rmTreeBatched(job.stageTree); } catch {} log(`[mod] ${job.current} 安装失败:${e.message}`); } finally { job.finished = true; job.child = null; @@ -193,6 +197,81 @@ async function modStart(id, repair) { return { r: { ok: true, chain }, job }; } +/** 模块安装 / 卸载心跳:与 components.mjs 同目录同字段,看门狗(GatewayService.installingBeat)据此放宽探活时限, + * 避免大模块(如 haskell 9k+ 文件、build+共享层 16k+ 文件)在搬移或清理时被 SIGKILL */ +function modHeart(id) { + const mark = COMP_RUN_DIR ? path.join(COMP_RUN_DIR, `mod-${id}.installing`) : ""; + if (mark) try { fs.mkdirSync(COMP_RUN_DIR, { recursive: true }); } catch {} + return { + beat(moved, phase = "move") { + if (!mark) return; + try { fs.writeFileSync(mark, JSON.stringify({ id: "mod-" + id, phase, moved, at: uptimeMs(), gw: process.pid, boot: bootId() })); } catch {} + }, + clear() { if (mark) try { fs.rmSync(mark, { force: true }); } catch {} }, + }; +} + +/** 列出 tar.gz 条目并挂到 job.child:扫描几百 MB 模块包时允许取消,崩溃后也可被 reapComponentRuns 清理 */ +function modTarList(job, gz) { + return new Promise((resolve, reject) => { + const cmd = path.join(PREFIX, "bin", "tar"), args = ["-tzv", "--quoting-style=c", "-f", gz]; + const child = spawn(cmd, args, { env: termEnv(), stdio: ["ignore", "pipe", "pipe"], detached: true }); + job.child = child; + const mark = markChild(child, cmd, args); + const chunks = []; + let bytes = 0, err = "", overflow = false; + child.stdout.on("data", (d) => { + bytes += d.length; + if (bytes > (256 << 20)) { overflow = true; killTree(child); return; } + chunks.push(d); + }); + child.stderr.on("data", (d) => { err = (err + d).slice(-2000); }); + child.on("error", (e) => { job.child = null; unmarkChild(mark); reject(e); }); + child.on("exit", (code) => { + job.child = null; unmarkChild(mark); + if (job.cancelled) return reject(new Error("cancelled")); + if (overflow) return reject(new Error("tar listing too large")); + if (code !== 0) return reject(new Error(err.trim() || `tar exit ${code}`)); + resolve(Buffer.concat(chunks).toString("utf8")); + }); + }); +} + +/** Guile 编译缓存(.go)必须严格比源码(.scm)新,否则 gdb / guile 启动陷入重编译死循环(同 Installer.java:670) */ +async function touchGuileGo(beat = () => {}) { + const libGuile = path.join(PREFIX, "lib", "guile"); + if (!fs.existsSync(libGuile)) return; + let maxScm = 0; + const scanScm = async (dir) => { + let ds; try { ds = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; } + for (const d of ds) { + const p = path.join(dir, d.name); + if (d.isDirectory()) { await breathe(); await scanScm(p); } + else if (d.name.endsWith(".scm")) { + try { const mt = fs.statSync(p).mtimeMs; if (mt > maxScm) maxScm = mt; } catch {} + } + } + }; + await scanScm(path.join(PREFIX, "share", "guile")); + await scanScm(libGuile); + const stamp = new Date(Math.max(Date.now(), maxScm + 1000)); + let n = 0; + const touchWalk = async (dir) => { + let ds; try { ds = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; } + for (const d of ds) { + const p = path.join(dir, d.name); + if (d.isDirectory()) { await breathe(); await touchWalk(p); } + else if (d.name.endsWith(".go")) { + try { fs.utimesSync(p, stamp, stamp); } catch {} + n++; + await breathe(); + if ((n & 255) === 0) beat(n, "switch"); + } + } + }; + await touchWalk(libGuile); +} + /** 这条链还要多少空间(压缩包 + 解开后的大小,留 10%):暂存区里大小对得上的压缩包不再重复要(0.6.9 审计 #186) */ export function modSpaceNeed(mj, chain) { let need = 0; @@ -257,12 +336,11 @@ async function modRunInstall(mj, m, base, job) { if (job.cancelled) throw new Error("cancelled"); // 解包阶段没有可靠的字节进度:不画永远 0% 的条(0.6.9 审计 195-I.8),解完按清单给总量 job.phase = "verifying"; job.done = 0; job.total = 0; - fs.rmSync(tree, { recursive: true, force: true }); + await rmTreeBatched(tree); fs.mkdirSync(tree, { recursive: true }); // 解之前按目录查(同组件包):MANIFEST 解完才核,经包内软链或 .. 写到暂存区外面的条目那时已经落地了(11-03 审查 L-8) try { - const list = await new Promise((ok, no) => execFile(path.join(PREFIX, "bin", "tar"), ["-tzv", "--quoting-style=c", "-f", gz], - { maxBuffer: 256 << 20 }, (e, out) => e ? no(e) : ok(String(out)))); + const list = await modTarList(job, gz); const bad = archiveProblem(parseTarListing(list)); if (bad) throw new Error(tz("这个压缩包里有条目会写到包外面({p}),为安全起见不解开", { p: String(bad.name).slice(0, 200) })); await compRun(job, path.join(PREFIX, "bin", "tar"), ["-xzf", gz, "-C", tree]); @@ -280,26 +358,35 @@ async function modRunInstall(mj, m, base, job) { } // 搬进去:.ok 最后写,中途被打断就是「没装」,重装会覆盖、卸载按 FILES 删 job.phase = "installing"; - fs.rmSync(path.join(dirOf(m), ".ok"), { force: true }); - const skipped = modMoveIn(path.join(tree, "usr")); - if (skipped.length) log(`[mod] ${m.id}:${skipped.length} 个路径和已有目录冲突,没放:${skipped.slice(0, 5).join(", ")}`); - const fr = path.join(dirOf(m), "first-run.sh"); - if (fs.existsSync(fr)) { - try { await compRun(job, path.join(PREFIX, "bin", "sh"), [fr]); } - catch (e) { - if (job.cancelled) throw new Error("cancelled"); - // first-run 失败不能写 .ok:以前照样写,模块显示已装但没有配置,也没有重试入口(0.6.9 审计 200-14-06)。 - // 不写 .ok 就是「没装」,重装会再跑一遍 - throw new Error(tz("模块「{name}」的首次配置没跑完({err}),再装一次会重试", { name: (m.name && m.name.zh) || m.id, err: String(e.message).slice(0, 200) })); + const hasGuile = m.id === "lib-guile" || man.some((e) => e.rel.startsWith("lib/guile/") || e.rel.startsWith("share/guile/")) || fs.existsSync(path.join(tree, "usr", "lib", "guile")); + const heart = modHeart(m.id); + heart.beat(0, "switch"); + try { + fs.rmSync(path.join(dirOf(m), ".ok"), { force: true }); + const skipped = await modMoveIn(path.join(tree, "usr"), "", [], heart.beat); + if (skipped.length) log(`[mod] ${m.id}:${skipped.length} 个路径和已有目录冲突,没放:${skipped.slice(0, 5).join(", ")}`); + const fr = path.join(dirOf(m), "first-run.sh"); + if (fs.existsSync(fr)) { + try { await compRun(job, path.join(PREFIX, "bin", "sh"), [fr]); } + catch (e) { + if (job.cancelled) throw new Error("cancelled"); + // first-run 失败不能写 .ok:以前照样写,模块显示已装但没有配置,也没有重试入口(0.6.9 审计 200-14-06)。 + // 不写 .ok 就是「没装」,重装会再跑一遍 + throw new Error(tz("模块「{name}」的首次配置没跑完({err}),再装一次会重试", { name: (m.name && m.name.zh) || m.id, err: String(e.message).slice(0, 200) })); + } } + if (hasGuile) await touchGuileGo(heart.beat); + fs.writeFileSync(path.join(dirOf(m), ".ok"), JSON.stringify({ sha256: m.gz.sha256, at: Date.now() }) + "\n"); + await rmTreeBatched(tree, (n) => heart.beat(n, "switch")); + fs.rmSync(gz, { force: true }); + } finally { + heart.clear(); } - fs.writeFileSync(path.join(dirOf(m), ".ok"), JSON.stringify({ sha256: m.gz.sha256, at: Date.now() }) + "\n"); - fs.rmSync(tree, { recursive: true, force: true }); - fs.rmSync(gz, { force: true }); } -/** 暂存区的 usr/ 逐个搬进 $PREFIX。已有同名文件直接换掉:同一快照里共用的小包内容一样;pkg 先装过的以模块为准 */ -function modMoveIn(stageUsr, rel = "", skipped = []) { +/** 暂存区的 usr/ 逐个搬进 $PREFIX。已有同名文件直接换掉:同一快照里共用的小包内容一样;pkg 先装过的以模块为准。 + * 分批让出事件循环并写看门狗心跳,避免大模块搬移时事件循环阻塞被杀 */ +async function modMoveIn(stageUsr, rel = "", skipped = [], beat = () => {}, state = { n: 0 }) { for (const d of fs.readdirSync(path.join(stageUsr, rel), { withFileTypes: true })) { const r = rel ? rel + "/" + d.name : d.name; const src = path.join(stageUsr, r), dst = path.join(PREFIX, r); @@ -307,11 +394,15 @@ function modMoveIn(stageUsr, rel = "", skipped = []) { if (d.isDirectory()) { if (st && !st.isDirectory()) { skipped.push(r); continue; } if (!st) fs.mkdirSync(dst, { recursive: true }); - modMoveIn(stageUsr, r, skipped); + await breathe(); + await modMoveIn(stageUsr, r, skipped, beat, state); continue; } if (st && st.isDirectory()) { skipped.push(r); continue; } fs.renameSync(src, dst); + state.n++; + await breathe(); + if ((state.n & 255) === 0) { beat(state.n, "move"); await new Promise((r2) => setImmediate(r2)); } } return skipped; } @@ -326,8 +417,8 @@ function modCancel(id) { } // ── 卸 ──────────────────────────────────────────────────────────────── -/** 卸 id;它依赖的共享层没有别的已装模块在用时一并卸掉 */ -function modRemove(id) { +/** 卸 id;它依赖的共享层没有别的已装模块、组件或 pkg 在用时一并卸掉 */ +async function modRemove(id) { if (!PREFIX) return { ok: false, message: "only available in cccodroid" }; const mj = modulesInfo(); if (!mj) return { ok: false, message: tz("当前是整包工具包,没有模块可卸") }; @@ -340,36 +431,60 @@ function modRemove(id) { const dep = modDependents(mj, id); if (dep.modules.length || dep.components.length || dep.pkgs.length) return { ok: false, usedBy: dep, message: tz("{names} 要用它,先卸载那些", { names: [...dep.modules, ...dep.components, ...dep.pkgs.map((n) => "pkg " + n)].join("、") }) }; const gone = [id]; + const ignore = new Set([id]); for (const l of (m.deps || []).map((d) => blockOf(mj, d)).filter((b) => b && b.kind === "shared" && modInstalled(mj, b.id))) { - const still = (mj.modules || []).some((o) => o.id !== id && o.kind === "module" && (o.deps || []).includes(l.id) && modInstalled(mj, o.id)); - if (!still) gone.push(l.id); + const stillMod = (mj.modules || []).some((o) => o.id !== id && o.kind === "module" && (o.deps || []).includes(l.id) && modInstalled(mj, o.id)); + if (stillMod) continue; + // 共享层可能被已装组件或 pkg 单独依赖(如卸 android 时仍有组件/pkg 用 lib-openjdk-21):有依赖就保留共享层 + const ldep = modDependents(mj, l.id, ignore); + if (ldep.components.length || ldep.pkgs.length) { + log(`[mod] 保留共享运行库 ${l.id}(仍被 ${[...ldep.components, ...ldep.pkgs.map((x) => "pkg " + x)].join("、")} 使用)`); + continue; + } + gone.push(l.id); } // 还要留下的:其余已装块的路径 + 组件要用的 + pkg 装的 const keep = new Set(); for (const o of mj.modules || []) if (!gone.includes(o.id) && modInstalled(mj, o.id)) for (const r of filesOf(o)) keep.add(r); for (const [cid, rec] of compRecords()) for (const r of compNeeds(cid, rec)) keep.add(r); for (const p of Object.values(pkgRecords())) for (const r of (p && p.files) || []) keep.add(r); + const removal = { phase: "removing", finished: false }; + MOD_JOBS.set(id, removal); + const heart = modHeart(id); + heart.beat(0, "switch"); let n = 0; - for (const g of gone) { - const b = blockOf(mj, g), files = filesOf(b); - if (!files.length) { log(`[mod] ${g} 没有 FILES,跳过删除`); continue; } - fs.rmSync(path.join(dirOf(b), ".ok"), { force: true }); // 先摘 .ok:删到一半被打断就是「没装」 - const dirs = new Set(); - for (const r of files) { - if (keep.has(r)) continue; - try { fs.unlinkSync(path.join(PREFIX, r)); n++; } catch {} - for (let d = path.dirname(r); d && d !== "."; d = path.dirname(d)) dirs.add(d); - // Python 运行时生成的 .pyc 不在清单里,跟着 .py 删 - if (r.endsWith(".py")) { - const pc = path.join(PREFIX, path.dirname(r), "__pycache__"), stem = path.basename(r, ".py") + "."; - try { for (const f of fs.readdirSync(pc)) if (f.startsWith(stem) && f.endsWith(".pyc")) fs.unlinkSync(path.join(pc, f)); } catch {} - dirs.add(path.join(path.dirname(r), "__pycache__")); + try { + for (const g of gone) { + const b = blockOf(mj, g), files = filesOf(b); + if (!files.length) { log(`[mod] ${g} 没有 FILES,跳过删除`); continue; } + fs.rmSync(path.join(dirOf(b), ".ok"), { force: true }); // 先摘 .ok:删到一半被打断就是「没装」 + const dirs = new Set(); + for (const r of files) { + if (keep.has(r)) continue; + try { fs.unlinkSync(path.join(PREFIX, r)); n++; } catch {} + for (let d = path.dirname(r); d && d !== "."; d = path.dirname(d)) dirs.add(d); + // Python 运行时生成的 .pyc 不在清单里,跟着 .py 删 + if (r.endsWith(".py")) { + const pc = path.join(PREFIX, path.dirname(r), "__pycache__"), stem = path.basename(r, ".py") + "."; + try { for (const f of fs.readdirSync(pc)) if (f.startsWith(stem) && f.endsWith(".pyc")) fs.unlinkSync(path.join(pc, f)); } catch {} + dirs.add(path.join(path.dirname(r), "__pycache__")); + } + await breathe(); + if ((n & 255) === 0) { heart.beat(n, "move"); await new Promise((r2) => setImmediate(r2)); } + } + await rmTreeBatched(dirOf(b), (k) => heart.beat(n + k, "switch")); + const sortedDirs = [...dirs].sort((a, c) => c.length - a.length); + for (let i = 0; i < sortedDirs.length; i++) { + try { fs.rmdirSync(path.join(PREFIX, sortedDirs[i])); } catch {} + if ((i & 255) === 0) { await breathe(); heart.beat(n, "switch"); } } } - fs.rmSync(dirOf(b), { recursive: true, force: true }); - for (const d of [...dirs].sort((a, c) => c.length - a.length)) { try { fs.rmdirSync(path.join(PREFIX, d)); } catch {} } + } finally { + heart.clear(); + removal.finished = true; + for (const g of gone) if (MOD_JOBS.get(g) === removal || MOD_JOBS.get(g)?.finished) MOD_JOBS.delete(g); + if (MOD_JOBS.get(id) === removal) MOD_JOBS.delete(id); } - for (const g of gone) MOD_JOBS.delete(g); log(`[mod] 已卸载 ${gone.join("、")}(删 ${n} 个路径)`); return { ok: true, removed: gone }; } @@ -384,9 +499,9 @@ export async function routeModules(req, res, u, p) { if (!/^[a-z0-9-]{1,40}$/.test(id)) return sendJson(res, 400, { ok: false, message: "bad id" }); if (p === "/api/devkit/modules/install") return sendJson(res, 200, await modInstall(id)); if (p === "/api/devkit/modules/cancel") return sendJson(res, 200, modCancel(id)); - if (p === "/api/devkit/modules/remove") return sendJson(res, 200, modRemove(id)); + if (p === "/api/devkit/modules/remove") return sendJson(res, 200, await modRemove(id)); return sendJson(res, 404, { error: "not found" }); } // 只给测试用(和 components.mjs 的 _compForTest 一个口径) -export const _modForTest = { modInstall, modRemove, MOD_JOBS }; +export const _modForTest = { modInstall, modRemove, modCancel, MOD_JOBS }; diff --git a/gateway/tests/a0610-g1-path-safety.test.mjs b/gateway/tests/a0610-g1-path-safety.test.mjs index 4a4ee3f5..3fc83f47 100644 --- a/gateway/tests/a0610-g1-path-safety.test.mjs +++ b/gateway/tests/a0610-g1-path-safety.test.mjs @@ -39,7 +39,7 @@ test("modules.json 里的 dir 必须是 $PREFIX 内的普通相对路径,坏 assert.deepEqual(modulesInfo().modules.map((m) => m.id), ["base", "modok"]); }); -test("modRemove 只删 FILES 里安全的相对路径,../ 与绝对路径不碰", () => { +test("modRemove 只删 FILES 里安全的相对路径,../ 与绝对路径不碰", async () => { fs.writeFileSync(OK, JSON.stringify({ sha256: "dk0610" })); fs.mkdirSync(path.join(MODDIR), { recursive: true }); fs.writeFileSync(path.join(MODDIR, "MANIFEST"), "h 1 bin/safe0610\n"); @@ -55,7 +55,7 @@ test("modRemove 只删 FILES 里安全的相对路径,../ 与绝对路径不 "a/../../b0610", ].join("\n") + "\n"); - const r = _modForTest.modRemove("modok"); + const r = await _modForTest.modRemove("modok"); assert.equal(r.ok, true, JSON.stringify(r)); assert.ok(!fs.existsSync(path.join(PREFIX, "bin", "safe0610")), "安全路径照常删"); assert.ok(!fs.existsSync(MODDIR), "模块目录照常删"); diff --git a/gateway/tests/v0715-devkit-modules-safety.test.mjs b/gateway/tests/v0715-devkit-modules-safety.test.mjs new file mode 100644 index 00000000..393c90f4 --- /dev/null +++ b/gateway/tests/v0715-devkit-modules-safety.test.mjs @@ -0,0 +1,249 @@ +// v0.7.15 发版前修补回归: +// 1. 大模块安装/卸载写看门狗心跳、分批让出事件循环,完成后清心跳 +// 2. 在线安装含 lib/guile 的模块后刷新 .go mtime(严格新于 .scm) +// 3. 卸载模块时,若共享运行库仍被已装组件或 pkg 包依赖,保留该共享层 +// 4. 共享运行库名称消歧(08-extensions.js / devkit.mjs) +// 5. devkit.mjs unxzToFile 退出码竞态修复 + devkit.mjs / pkg.mjs GBMB phar 保护(CLI 绝对路径包装、库原样保留) +import "../gw/config.mjs"; +import test, { after } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import crypto from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { PREFIX } from "../gw/config.mjs"; +import { _modForTest } from "../gw/modules.mjs"; + +const FILES = path.dirname(PREFIX); +const DK = path.join(PREFIX, "libexec/devkit"); +const STAGE = path.join(FILES, "devkit-stage"); +const COMP_DIR = path.join(FILES, "components"); +const RUNS = path.join(COMP_DIR, ".runs"); +const PKG_DIR = path.join(FILES, "pkg"); +const OK = path.join(PREFIX, ".devkit-ok"); +const savedOk = fs.existsSync(OK) ? fs.readFileSync(OK, "utf8") : null; +const TAR = spawnSync("sh", ["-c", "command -v tar"], { encoding: "utf8" }).stdout.trim(); +const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "v0715-mod-")); + +function linkBin(name, target) { + if (!target) return; + fs.mkdirSync(path.join(PREFIX, "bin"), { recursive: true }); + const p = path.join(PREFIX, "bin", name); + if (!fs.existsSync(p)) fs.symlinkSync(target, p); +} +if (TAR) linkBin("tar", TAR); + +after(() => { + fs.rmSync(path.join(DK, "modules.json"), { force: true }); + fs.rmSync(path.join(DK, "modules"), { recursive: true, force: true }); + fs.rmSync(path.join(PREFIX, "share", "v0715-bulk"), { recursive: true, force: true }); + fs.rmSync(path.join(PREFIX, "lib", "guile"), { recursive: true, force: true }); + fs.rmSync(path.join(PREFIX, "share", "guile"), { recursive: true, force: true }); + fs.rmSync(path.join(PREFIX, "bin", "v0715-and"), { force: true }); + fs.rmSync(path.join(PREFIX, "bin", "v0715-jdk"), { force: true }); + fs.rmSync(path.join(COMP_DIR, "comp-jdk-test.json"), { force: true }); + fs.rmSync(path.join(PKG_DIR, "installed.json"), { force: true }); + fs.rmSync(path.join(STAGE, "modhb"), { recursive: true, force: true }); + fs.rmSync(path.join(STAGE, "lib-guile"), { recursive: true, force: true }); + fs.rmSync(path.join(RUNS, "mod-modhb.installing"), { force: true }); + fs.rmSync(path.join(RUNS, "mod-lib-guile.installing"), { force: true }); + fs.rmSync(TMP, { recursive: true, force: true }); + if (savedOk == null) fs.rmSync(OK, { force: true }); + else fs.writeFileSync(OK, savedOk); +}); + +function buildModTar(id, dirRel, entries) { + const root = path.join(TMP, id); + fs.rmSync(root, { recursive: true, force: true }); + const manLines = []; + const fileLines = []; + for (const [rel, content, mtimeSec] of entries) { + const abs = path.join(root, "usr", rel); + fs.mkdirSync(path.dirname(abs), { recursive: true }); + const buf = Buffer.isBuffer(content) ? content : Buffer.from(content); + fs.writeFileSync(abs, buf); + if (mtimeSec) fs.utimesSync(abs, mtimeSec, mtimeSec); + const h = crypto.createHash("sha256").update(buf).digest("hex"); + manLines.push(`${h} ${buf.length} ${rel}`); + fileLines.push(rel); + } + const modDir = path.join(root, "usr", dirRel); + fs.mkdirSync(modDir, { recursive: true }); + fs.writeFileSync(path.join(modDir, "MANIFEST"), manLines.join("\n") + "\n"); + fs.writeFileSync(path.join(modDir, "FILES"), fileLines.join("\n") + "\n"); + const gzPath = path.join(TMP, `${id}.tar.gz`); + const r = spawnSync(TAR, ["-czf", gzPath, "-C", root, "usr"], { encoding: "utf8" }); + assert.equal(r.status, 0, r.stderr); + const gz = fs.readFileSync(gzPath); + const sha = crypto.createHash("sha256").update(gz).digest("hex"); + return { gz, sha }; +} + +test("大模块安装与卸载:写入看门狗心跳并分批让出,结束后清理心跳", { skip: TAR ? false : "无 tar" }, async () => { + const id = "modhb", dirRel = `libexec/devkit/modules/${id}`; + const entries = []; + for (let i = 0; i < 320; i++) entries.push([`share/v0715-bulk/f-${i}.txt`, `payload-${i}`]); + const { gz, sha } = buildModTar(id, dirRel, entries); + fs.mkdirSync(DK, { recursive: true }); + fs.mkdirSync(STAGE, { recursive: true }); + fs.writeFileSync(OK, JSON.stringify({ sha256: "dk-v0715", base: "https://example.invalid/download/v1/" })); + fs.writeFileSync(path.join(DK, "modules.json"), JSON.stringify({ + snapshot: "s-v0715", + modules: [ + { id: "base", kind: "base", dir: "libexec/devkit" }, + { id, kind: "module", dir: dirRel, deps: ["base"], installed: 65536, gz: { file: `${id}.tar.gz`, bytes: gz.length, sha256: sha } }, + ], + })); + const stageGz = path.join(STAGE, `${id}-${sha.slice(0, 12)}.tar.gz`); + fs.writeFileSync(stageGz, gz); + + const mark = path.join(RUNS, `mod-${id}.installing`); + let sawInstallMove = false; + const r = await _modForTest.modInstall(id); + assert.equal(r.ok, true, JSON.stringify(r)); + const job = _modForTest.MOD_JOBS.get(id); + const deadline = Date.now() + 15000; + while (!(job && job.finished) && Date.now() < deadline) { + if (fs.existsSync(mark)) { + try { + const j = JSON.parse(fs.readFileSync(mark, "utf8")); + if (j.phase === "move" && j.moved >= 256 && j.at > 0 && j.gw === process.pid) sawInstallMove = true; + } catch {} + } + await new Promise((ok) => setImmediate(ok)); + } + await job.done; + assert.ok(job && job.finished && !job.error, `安装失败: ${job && job.error}`); + assert.equal(sawInstallMove, true, "安装 >256 文件期间应写出 move 心跳"); + assert.equal(fs.existsSync(mark), false, "安装完成后心跳必须删掉"); + assert.ok(fs.existsSync(path.join(PREFIX, dirRel, ".ok")), "安装完成后应写 .ok"); + + // 卸载同样产生心跳并在结束后清理 + let sawRemoveMove = false; + let rmDone = false; + const rmPromise = _modForTest.modRemove(id).finally(() => { rmDone = true; }); + const rmDeadline = Date.now() + 15000; + while (!rmDone && Date.now() < rmDeadline) { + if (fs.existsSync(mark)) { + try { + const j = JSON.parse(fs.readFileSync(mark, "utf8")); + if (j.phase === "move" && j.moved >= 256) sawRemoveMove = true; + } catch {} + } + await new Promise((ok) => setImmediate(ok)); + } + const rm = await rmPromise; + assert.equal(rm.ok, true, JSON.stringify(rm)); + assert.equal(sawRemoveMove, true, "卸载 >256 文件期间应写出 move 心跳"); + assert.equal(fs.existsSync(mark), false, "卸载完成后心跳必须删掉"); + assert.equal(fs.existsSync(path.join(PREFIX, "share/v0715-bulk/f-0.txt")), false); +}); + +test("在线安装 lib-guile 后刷新 .go 缓存时间戳,严格晚于 .scm 源码", { skip: TAR ? false : "无 tar" }, async () => { + const id = "lib-guile", dirRel = `libexec/devkit/modules/${id}`; + // 故意让 .go 比 .scm 旧 10 秒,模拟 tar 包时间戳顺序问题 + const scmTime = Math.floor(Date.now() / 1000) - 5; + const goTime = scmTime - 10; + const { gz, sha } = buildModTar(id, dirRel, [ + ["share/guile/3.0/ice-9/boot-9.scm", "(define x 1)\n", scmTime], + ["lib/guile/3.0/ccache/ice-9/boot-9.go", "ELF-go-cache", goTime], + ]); + fs.mkdirSync(DK, { recursive: true }); + fs.mkdirSync(STAGE, { recursive: true }); + fs.writeFileSync(OK, JSON.stringify({ sha256: "dk-v0715", base: "https://example.invalid/download/v1/" })); + fs.writeFileSync(path.join(DK, "modules.json"), JSON.stringify({ + snapshot: "s-v0715", + modules: [ + { id: "base", kind: "base", dir: "libexec/devkit" }, + { id, kind: "shared", dir: dirRel, deps: ["base"], installed: 4096, gz: { file: `${id}.tar.gz`, bytes: gz.length, sha256: sha } }, + ], + })); + fs.writeFileSync(path.join(STAGE, `${id}-${sha.slice(0, 12)}.tar.gz`), gz); + + const r = await _modForTest.modInstall(id); + assert.equal(r.ok, true, JSON.stringify(r)); + const job = _modForTest.MOD_JOBS.get(id); + if (job) await job.done; + assert.ok(job && job.finished && !job.error, `安装失败: ${job && job.error}`); + + const scmSt = fs.statSync(path.join(PREFIX, "share/guile/3.0/ice-9/boot-9.scm")); + const goSt = fs.statSync(path.join(PREFIX, "lib/guile/3.0/ccache/ice-9/boot-9.go")); + assert.ok(goSt.mtimeMs > scmSt.mtimeMs, `.go (${goSt.mtimeMs}) 必须严格晚于 .scm (${scmSt.mtimeMs})`); +}); + +test("modRemove 卸载模块时:共享运行库仍被已装组件或 pkg 依赖则保留,无依赖时一起卸载", async () => { + fs.mkdirSync(DK, { recursive: true }); + fs.mkdirSync(COMP_DIR, { recursive: true }); + fs.mkdirSync(PKG_DIR, { recursive: true }); + fs.writeFileSync(OK, JSON.stringify({ sha256: "dk-v0715" })); + const setup = () => { + fs.writeFileSync(path.join(DK, "modules.json"), JSON.stringify({ + snapshot: "s-v0715", + modules: [ + { id: "base", kind: "base", dir: "libexec/devkit" }, + { id: "lib-openjdk-21", kind: "shared", dir: "libexec/devkit/modules/lib-openjdk-21", deps: ["base"], + packages: [{ name: "openjdk-21", version: "21" }] }, + { id: "android", kind: "module", dir: "libexec/devkit/modules/android", deps: ["base", "lib-openjdk-21"], + packages: [{ name: "apksigner", version: "1" }] }, + ], + })); + for (const [id, bin] of [["lib-openjdk-21", "bin/v0715-jdk"], ["android", "bin/v0715-and"]]) { + const d = path.join(DK, "modules", id); + fs.mkdirSync(d, { recursive: true }); + fs.writeFileSync(path.join(d, "MANIFEST"), `h 1 ${bin}\n`); + fs.writeFileSync(path.join(d, "FILES"), `${bin}\n`); + fs.writeFileSync(path.join(d, ".ok"), "{}\n"); + fs.mkdirSync(path.dirname(path.join(PREFIX, bin)), { recursive: true }); + fs.writeFileSync(path.join(PREFIX, bin), "x"); + } + }; + + // 场景 A:组件依赖 openjdk-21 → 卸 android 时必须保留 lib-openjdk-21 + setup(); + fs.writeFileSync(path.join(COMP_DIR, "comp-jdk-test.json"), JSON.stringify({ + id: "comp-jdk-test", state: "ok", base: "dk-v0715", files: [], shared: [], baseDeps: ["openjdk-21"], + })); + const r1 = await _modForTest.modRemove("android"); + assert.equal(r1.ok, true, JSON.stringify(r1)); + assert.deepEqual(r1.removed, ["android"]); + assert.ok(fs.existsSync(path.join(PREFIX, "bin/v0715-jdk")), "被组件依赖的共享库文件不能删"); + assert.ok(fs.existsSync(path.join(DK, "modules/lib-openjdk-21/.ok")), "共享库 .ok 应保留"); + fs.rmSync(path.join(COMP_DIR, "comp-jdk-test.json"), { force: true }); + + // 场景 B:pkg 依赖 openjdk-21 → 卸 android 时同样保留 lib-openjdk-21 + setup(); + fs.writeFileSync(path.join(PKG_DIR, "installed.json"), JSON.stringify({ + pkgs: { gradle: { deps: "openjdk-21 (>= 21)", files: [] } }, + })); + const r2 = await _modForTest.modRemove("android"); + assert.equal(r2.ok, true, JSON.stringify(r2)); + assert.deepEqual(r2.removed, ["android"]); + assert.ok(fs.existsSync(path.join(PREFIX, "bin/v0715-jdk")), "被 pkg 依赖的共享库文件不能删"); + fs.rmSync(path.join(PKG_DIR, "installed.json"), { force: true }); + + // 场景 C:无任何组件或 pkg 依赖 → 卸 android 时一并卸掉 lib-openjdk-21 + setup(); + const r3 = await _modForTest.modRemove("android"); + assert.equal(r3.ok, true, JSON.stringify(r3)); + assert.deepEqual(r3.removed, ["android", "lib-openjdk-21"]); + assert.equal(fs.existsSync(path.join(PREFIX, "bin/v0715-jdk")), false, "无依赖的共享库应一并卸载"); +}); + +test("源码回归:共享运行库消歧 + devkit unxzToFile 竞态修复 + devkit/pkg GBMB phar 保护", () => { + const extSrc = fs.readFileSync(new URL("../web/app/08-extensions.js", import.meta.url), "utf8"); + assert.match(extSrc, /const modLabel = \(m\) =>/, "08-extensions.js 应通过 modLabel 消歧共享运行库"); + assert.match(extSrc, /m\.kind === "shared" && \/\^lib-\/\.test\(m\.id\)/); + + const dkSrc = fs.readFileSync(new URL("../../devkit/devkit.mjs", import.meta.url), "utf8"); + assert.match(dkSrc, /let done = false, outDone = false, exited = false/, "unxzToFile 必须同时等待 out finish 与 child exit"); + assert.match(dkSrc, /if \(done \|\| !outDone \|\| !exited\) return;/); + assert.match(dkSrc, /exec \\"" \+ R\.to \+ "files\/usr\/bin\/php\\"/, "devkit phar 包装脚本必须使用绝对路径 php"); + assert.match(dkSrc, /!clean\.endsWith\("\.phar"\)/, "devkit phar 保护必须区分库 .phar 与 CLI phar"); + assert.match(dkSrc, /共享运行库(\$\{sharedTag\})/, "devkit modules.json 默认共享层名应带后缀"); + + const pkgSrc = fs.readFileSync(new URL("../../android/assets/bin/pkg.mjs", import.meta.url), "utf8"); + assert.match(pkgSrc, /subarray\(-4\)\.toString\("latin1"\) === "GBMB"/, "pkg.mjs 必须保护 GBMB 签名 phar"); + assert.match(pkgSrc, /exec "\$\{TARGET\}files\/usr\/bin\/php"/, "pkg.mjs phar 包装脚本必须使用绝对路径 php"); + assert.match(pkgSrc, /!r\.endsWith\("\.phar"\)/, "pkg.mjs 必须保留库 .phar 原样"); +}); diff --git a/gateway/web/app/08-extensions.js b/gateway/web/app/08-extensions.js index 5ba214cc..eb9e1a9a 100644 --- a/gateway/web/app/08-extensions.js +++ b/gateway/web/app/08-extensions.js @@ -59,7 +59,17 @@ $("mod-field").classList.toggle("hidden", !on); if (!on) return; const items = r.items || [], byId = Object.fromEntries(items.map((m) => [m.id, m])); - const nameOf = (id) => esc(pickLang((byId[id] || {}).name) || id); + // 多个共享运行库(lib-clang / lib-guile / lib-libllvm / lib-openjdk-21)在 modules.json 里默认同名,按 id 后缀区分 + const modLabel = (m) => { + if (!m) return ""; + const base = pickLang(m.name) || m.id; + if (m.kind === "shared" && /^lib-/.test(m.id) && m.id.length > 4) { + const tag = m.id.slice(4); + if (!base.includes(tag)) return isZh() ? `${base}(${tag})` : `${base} (${tag})`; + } + return base; + }; + const nameOf = (id) => esc(modLabel(byId[id]) || id); const inst = items.filter((m) => m.state === "ok"); $("mod-count").textContent = inst.length + "/" + items.length; ext.nMod = items.length; extSub(); @@ -107,7 +117,7 @@ + btn("remove", m.id, t("卸载"), ' class="btn sm ghost"' + (used.length ? ' data-usedby="' + listOf(used) + '"' : "")); const tag = m.required ? `${ei("lock")}${t("必装")}` : m.kind === "shared" ? `${t("共享运行库")}` : m.state === "ok" && modOff.has(m.id) ? `${t("已停用")}` : ""; - return rowItem(esc(pickLang(m.name) || m.id) + tag, '' + (m.key || []).map(esc).join(" ") + "", + return rowItem(esc(modLabel(m) || m.id) + tag, '' + (m.key || []).map(esc).join(" ") + "", '' + size + "" + acts, { more: desc, extra: bar }); }).join(""); if (moving && pageOn("ext")) modTimer = setTimeout(() => { if (live()) loadModules(); }, 1500);