Compare commits

...

4 Commits

3 changed files with 203 additions and 9 deletions

View File

@@ -30,8 +30,8 @@ android {
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
* 改小了会装不上)。
*/
versionCode = 222
versionName = "2.0.22"
versionCode = 226
versionName = "2.0.26"
ndk {
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份

View File

@@ -48,7 +48,7 @@ object TermuxInstaller {
* 把戳的值带上版本号,**对不上就只重跑修正式步骤**(不重新解压,
* 解压要几十秒)。以后再加这类"装完之后要修一下"的逻辑,把这里 +1。
*/
private const val STAMP_VALUE = "ok:7"
private const val STAMP_VALUE = "ok:16"
/**
* 安装日志。
@@ -88,6 +88,11 @@ object TermuxInstaller {
progress("正在修正内置环境(升级后的一次性修复)…")
val fixed = rewritePrefixes(context)
trace(context, "补跑改写:$fixed 个文件")
// ★ apt.conf 是新增的,老设备上肯定没有 → 必须补写。
// 不写的话 apt 会去 /data/data/com.termux/... 找 apt.conf.d,
// 报 "Unable to determine a suitable packaging system type"。
writeAptConf(context)
fixBrokenSymlinks(context).let { trace(context, "修了 $it 个断链") }
// ★ 顺手把源也重设一次。两个原因:
// 1. 老设备上 bootstrap 自带的是俄罗斯源,国内连不上
// 2. v2.0.20 之前只写了 sources.list,而 **pkg 不认那个文件** ——
@@ -137,6 +142,8 @@ object TermuxInstaller {
home(context).mkdirs()
writeBashRc(context)
writeAptConf(context)
fixBrokenSymlinks(context).let { trace(context, "修了 $it 个断链") }
progress("正在改写写死的 com.termux 路径…")
val fixed = rewritePrefixes(context)
trace(context, "改写了 $fixed 个文件里的硬编码路径")
@@ -475,6 +482,113 @@ object TermuxInstaller {
"-i",
)
/**
* ★★ 写一份 `etc/apt/apt.conf`,把所有 `Dir::` 指到我们自己的 prefix。
*
* 为什么必须写:`apt` **二进制里编译死**了这些路径(都是真 Termux 的包名):
*
* Dir::Etc::parts = /data/data/com.termux/files/usr/etc/apt/apt.conf.d/
* Dir::Etc::sourcelist = /data/data/com.termux/files/usr/etc/apt/sources.list
* Dir::State = /data/data/com.termux/files/usr/var/lib/apt
* …
*
* 读不到 `apt.conf.d` 它就直接罢工:
*
* W: Unable to read /data/data/com.termux/files/usr/etc/apt/apt.conf.d/
* E: Unable to determine a suitable packaging system type
*
* —— `pkg update` 到此为止,什么都装不了。
*
* **`LD_PRELOAD=libtermux-exec.so` 也救不了**(实测带不带一模一样):
* termux-exec 改写的是它认识的那些路径,apt 这套 `Dir::` 不在里面。
*
* 正路是 apt 自己的机制:环境变量 `APT_CONFIG` 指定主配置文件的**绝对路径**
* (见 environment() 里那一项),apt 会先读它,里面重设 `Dir::` 就覆盖了
* 编译死的默认值。
*
* ★ 有个坑:`APT_CONFIG` 指向的文件**必须存在**,否则 apt 只打个警告就
* 回退到编译死的路径 —— 我第一版就是这样,白试了一轮。
*/
private fun writeAptConf(context: Context) {
val prefix = prefix(context).absolutePath
val conf = File(prefix, "etc/apt/apt.conf")
conf.parentFile?.mkdirs()
conf.writeText(
"""
// 由豆包 App 生成 —— 把 apt 的目录全部指到我们自己的 prefix。
//
// 不写这份文件的话,apt 会去 /data/data/com.termux/... (真 Termux 的包名)
// 找 apt.conf.d,读不到就报 "Unable to determine a suitable packaging
// system type" 然后拒绝工作。
Dir::Etc::parts "$prefix/etc/apt/apt.conf.d";
Dir::Etc::main "$prefix/etc/apt/apt.conf";
Dir::Etc::sourcelist "$prefix/etc/apt/sources.list";
Dir::Etc::sourceparts "$prefix/etc/apt/sources.list.d";
Dir::Etc::preferences "$prefix/etc/apt/preferences";
Dir::Etc::preferencesparts "$prefix/etc/apt/preferences.d";
Dir::Etc::trusted "$prefix/etc/apt/trusted.gpg";
Dir::Etc::trustedparts "$prefix/etc/apt/trusted.gpg.d";
Dir::State "$prefix/var/lib/apt";
Dir::State::status "$prefix/var/lib/dpkg/status";
Dir::State::lists "$prefix/var/lib/apt/lists";
Dir::Cache "$prefix/var/cache/apt";
Dir::Cache::archives "$prefix/var/cache/apt/archives";
// ★ `Dir::Bin::*` 也全是编译死的路径,一个都不能漏。
// 漏了 methods 的话 apt 能读索引了,但一取包就报:
// E: The method driver /data/data/com.termux/files/usr/lib/apt/methods/https
// could not be found.
Dir::Bin::methods "$prefix/lib/apt/methods";
Dir::Bin::solvers "$prefix/lib/apt/solvers";
Dir::Bin::dpkg "$prefix/bin/dpkg";
// ★★ 验签用的那几个可执行文件,路径也是编译死的。
//
// 漏了 apt-key 的话 apt 能下载索引、能读证书,但一验签就报:
// Err:1 ...: Couldn't execute
// /data/data/com.termux/files/usr/bin/apt-key to check .../InRelease
// E: The repository ... is not signed.
// —— 看着像"源没签名",其实是找不到 apt-key 这个程序。
Dir::Bin::apt-key "$prefix/bin/apt-key";
Dir::Bin::gpg "$prefix/bin/gpg";
Dir::Bin::gpgv "$prefix/bin/gpgv";
Dir::Bin::apt-get "$prefix/bin/apt-get";
Dir::Bin::apt-cache "$prefix/bin/apt-cache";
Dir::Bin::apt-config "$prefix/bin/apt-config";
DPkg::options:: "--force-confnew";
// ★★ apt 的 HTTPS 要单独告诉它证书在哪。
//
// `SSL_CERT_FILE` / `CURL_CA_BUNDLE` 管的是 **curl**;
// apt 走自己的 https method(GnuTLS),不看那两个变量,
// 只认 apt 自己的 `Acquire::https::CaInfo`。
//
// 不加这两行的话 apt 能读索引、能建依赖树,但一取包就报:
// W: ...: No system certificates available. Try installing ca-certificates.
// W: Failed to fetch ...: Certificate verification failed:
// The certificate is NOT trusted. The certificate issuer is unknown.
Acquire::https::CaInfo "$prefix/etc/tls/cert.pem";
Acquire::https::Verify-Peer "true";
Acquire::https::Verify-Host "true";
// ★★ 临时目录也必须重设。
//
// 漏了它的话 apt 能下载索引,但验证签名时建不了临时文件:
// Err:1 ...: Couldn't create temporary file
// /data/data/com.termux/files/usr/tmp/apt.conf.XXXX for passing config to apt-key
// E: The repository ... is not signed.
// —— 看起来像签名问题,其实是临时目录路径错了。
Dir::Temp "$prefix/tmp";
Dir::Log "$prefix/var/log/apt";
Dir::Log::Terminal "$prefix/var/log/apt/term.log";
Dir::Log::History "$prefix/var/log/apt/history.log";
Dir::Media::MountPath "$prefix/media";
""".trimIndent() + "\n",
Charsets.UTF_8,
)
// apt 要往这些目录写东西,先建好
for (d in listOf("var/lib/apt/lists", "var/cache/apt/archives", "etc/apt/sources.list.d")) {
File(prefix, d).mkdirs()
}
trace(context, "已写入 apt.conf(Dir:: 全指向 $prefix)")
}
/** 装完之后给内核/工具用的环境变量(Termux 的标准一套) */
fun environment(context: Context): Map<String, String> {
val prefix = prefix(context).absolutePath
@@ -485,7 +599,14 @@ object TermuxInstaller {
"PREFIX" to prefix,
"HOME" to home,
"TMPDIR" to "$prefix/tmp",
"PATH" to "$prefix/bin:$prefix/bin/applets:/system/bin:/system/xbin",
// ★ PATH 里多带一条 /data/data/<包名>/files/usr/bin。
//
// dpkg 是 Termux 编译出来的,内部有一份"预期程序"清单
// (sh / rm / tar / diff / dpkg-deb / start-stop-daemon),
// 路径来自**编译时的 $PREFIX**。Android 上
// /data/data/<包名> 是指向 /data/user/0/<包名> 的符号链接,
// 所以这条路径真实存在 —— 多带一条,成本为零。
"PATH" to "$prefix/bin:$prefix/bin/applets:$legacy/usr/bin:/system/bin:/system/xbin",
/**
* ★★ 证书路径 —— 不设这个,**所有 HTTPS 都连不上**。
*
@@ -522,6 +643,20 @@ object TermuxInstaller {
* 那个文件的目录去找 `apt.conf.d/`,绕开编译死的路径。
*/
"APT_CONFIG" to "$prefix/etc/apt/apt.conf",
/**
* ★★ dpkg 的数据库目录也得告诉它。
*
* dpkg 二进制里编译死了 admindir(真 Termux 的包名),
* 装包时直接拒绝:
*
* dpkg: error: cannot create the dpkg database directory
* /data/data/com.termux/files/usr/var/lib/dpkg: Permission denied
* E: Sub-process .../bin/dpkg returned an error code (2)
*
* —— 包都下载完了,卡在最后一步,很冤。
* dpkg 认 DPKG_ADMINDIR 这个环境变量,指过去就行。
*/
"DPKG_ADMINDIR" to "$prefix/var/lib/dpkg",
"LD_LIBRARY_PATH" to "$prefix/lib",
// ★ 这个 LD_PRELOAD 是 Termux 能在 Android 上正常跑命令的关键:
// 它把二进制里写死的 /data/data/com.termux/files/usr 重写到我们自己的 prefix。
@@ -551,4 +686,63 @@ object TermuxInstaller {
"TERMUX_MAIN_PACKAGE_FORMAT" to "debian",
)
}
/**
* ★★ 修**断掉的符号链接**(链接目标和路径改写无关,得单独处理)。
*
* bootstrap 的 SYMLINKS.txt 里有一部分链接是**绝对路径**,指向真 Termux 的包名:
*
* etc/apt/trusted.gpg.d/termux-autobuilds.gpg
* -> /data/data/com.termux/files/usr/share/termux-keyring/termux-autobuilds.gpg
*
* 我们建出来就是**断链**。apt 读钥匙串时找不到公钥:
*
* Err:1 ...: The following signatures could not be verified because
* the public key is not available: NO_PUBKEY 5A897D96E57CF20C
* E: The repository ... is not signed.
*
* rewritePrefixes 救不了 —— 它改的是**文件内容**,链接目标存在 inode 里。
* 这里扫一遍:凡是「软链 + 当前是断的 + 目标含 com.termux」的,
* 把目标里的包名换成我们的,重建链接。
*/
private fun fixBrokenSymlinks(context: Context): Int {
val prefix = prefix(context)
val oldPkg = "/data/data/com.termux"
val newPkg = context.applicationInfo.dataDir
var fixed = 0
val all = try {
prefix.walkTopDown().toList()
} catch (t: Throwable) {
trace(context, "遍历 prefix 失败:${t.message}")
return 0
}
for (entry in all) {
val path = entry.toPath()
val isLink = try {
java.nio.file.Files.isSymbolicLink(path)
} catch (_: Throwable) {
false
}
if (!isLink) continue
if (entry.exists()) continue // 还好的链接不动
val target = try {
java.nio.file.Files.readSymbolicLink(path).toString()
} catch (_: Throwable) {
continue
}
if (!target.contains(oldPkg)) continue
val newTarget = target.replace(oldPkg, newPkg)
try {
entry.delete()
java.nio.file.Files.createSymbolicLink(
entry.toPath(), java.io.File(newTarget).toPath(),
)
fixed += 1
} catch (t: Throwable) {
trace(context, "修链接失败 ${entry.name}:${t.message}")
}
}
return fixed
}
}

View File

@@ -33,23 +33,23 @@ object TermuxMirror {
*/
val MIRRORS = listOf(
Mirror(
"tuna", "清华 TUNA", "https://mirrors.tuna.tsinghua.edu.cn/termux",
"tuna", "清华 TUNA", "http://mirrors.tuna.tsinghua.edu.cn/termux",
"国内最快最稳的一个,推荐",
),
Mirror(
"ustc", "中科大 USTC", "https://mirrors.ustc.edu.cn/termux",
"ustc", "中科大 USTC", "http://mirrors.ustc.edu.cn/termux",
"同样完整同步,教育网更快",
),
Mirror(
"aliyun", "阿里云", "https://mirrors.aliyun.com/termux",
"aliyun", "阿里云", "http://mirrors.aliyun.com/termux",
"公网覆盖好,偶尔缺 science 仓库",
),
Mirror(
"bfsu", "北外 BFSU", "https://mirrors.bfsu.edu.cn/termux",
"bfsu", "北外 BFSU", "http://mirrors.bfsu.edu.cn/termux",
"清华的备份站",
),
Mirror(
"official", "官方源", "https://packages.termux.dev/apt",
"official", "官方源", "http://packages.termux.dev/apt",
"国内经常连不上,镜像挂了才用它",
),
)