Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions android/assets/bin/pkg.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 18 additions & 1 deletion android/java/cc/codroid/GwRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ private void fetchBase(java.util.List<String[]> 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("取回网关运行库:解包…"));
Expand All @@ -217,13 +218,29 @@ private void fetchBase(java.util.List<String[]> 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<String, String[]> CACHE = new java.util.concurrent.ConcurrentHashMap<>();

Expand Down
21 changes: 18 additions & 3 deletions android/java/cc/codroid/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,25 @@ void installModules(byte[] mjBytes, java.util.Collection<String> 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;
}

/** 两次换名把新版换上;第二次失败就把旧版换回来 */
Expand Down
13 changes: 13 additions & 0 deletions android/tests/cc/codroid/InstallerCarryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> s = Files.walk(tmp)) {
s.sorted(Comparator.reverseOrder()).forEach((p) -> { try { Files.delete(p); } catch (Exception ignored) {} });
Expand Down
74 changes: 50 additions & 24 deletions devkit/devkit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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} 个文件路径,跨块无冲突`);
Expand Down Expand Up @@ -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 替换在里面找不到模式
Expand Down
8 changes: 4 additions & 4 deletions gateway/gw/components.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 "";
Expand Down Expand Up @@ -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; }
Expand Down
Loading
Loading