From 031649b2705a1c45042fc1df19cc535d2ec2280a Mon Sep 17 00:00:00 2001 From: JGZYES Date: Sat, 5 Sep 2026 23:54:19 +0800 Subject: [PATCH] fix: correct download error message + fall back to /dists for legacy /packages base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - msg.err.download-failed now reads generic '下载 %s 失败' (was wrongly '下载语言包...'), so a failed .pdm download no longer claims it's a language pack. Synced zh/en packs. - registry lookup (install_from_registry + registry_fetch) now also tries the /dists subpath when a mirror base ends with .../mirror/packages, so a stale mirror.ini keeps working after the mirror was reformed to dists/+files/. --- src/i18n.c | 2 +- src/install.c | 32 ++++++++++++++++++++++---------- src/main.c | 22 ++++++++++++++++++---- web/mirror/lang/en-US.pjson | 2 +- web/mirror/lang/zh-CN.pjson | 2 +- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/i18n.c b/src/i18n.c index 663fac4..dc5cd23 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -58,7 +58,7 @@ static const char *kBuiltinZh = "\"msg.path-exists\":\"PATH 已包含 PMM 目录\",\"msg.err.registry-not-found\":\"找不到软件包 '%s'\"," "\"msg.pmm-self-replaced\":\"pmm 已更新到 %s\",\"msg.pmm-self-hint\":\"无法覆盖 %s(需 root/管理员);新版本在 %s。请将其加入 PATH 并在当前 shell 运行 hash -r\"," "\"msg.err.usage\":\"用法\",\"msg.err.unknown-cmd\":\"未知命令 '%s' (试试 'pmm help')\"," -"\"msg.err.download-failed\":\"下载语言包 %s 失败\",\"msg.err.invalid-locale\":\"无效语言 %s\"," +"\"msg.err.download-failed\":\"下载 %s 失败\",\"msg.err.invalid-locale\":\"无效语言 %s\"," "\"msg.err.not-found\":\"找不到软件包 '%s'\",\"msg.err.failed-install\":\"安装 %s 失败\",\"msg.err.conflict\":\"无法安装 %s:与已装的 %s 冲突\"," "\"msg.err.no-registry-mirror\":\"没有配置注册表镜像\",\"msg.err.cannot-write\":\"无法写入 %s\"," "\"desc.install\":\"安装\",\"desc.remove\":\"卸载\",\"desc.update\":\"刷新索引\"," diff --git a/src/install.c b/src/install.c index cb7f5c5..bca0575 100644 --- a/src/install.c +++ b/src/install.c @@ -814,16 +814,28 @@ int install_from_registry(const char *name, const char *spec, const char *mirror char *body = NULL; char *used_base = NULL; for (int i = 0; i < nb; i++) { - snprintf(url, sizeof(url), "%s/%s.json", bases[i], name); - pmm_info("%s", pmm_tr_fmt("msg.looking-up", name, bases[i])); - body = http_get(url, &status); - if (getenv("PMM_DEBUG")) fprintf(stderr, "[reg] %s -> body=%s status=%d\n", - bases[i], body ? "set" : "NULL", status); - /* accept any body that isn't an explicit not-found/server error; some - * curl builds don't emit the status marker the way we expect, so a - * non-NULL body with an indeterminate status should still be parsed */ - if (body && status != 404 && status != 403 && status != 503) { used_base = bases[i]; break; } - free(body); body = NULL; + char used = 0; + /* layout migration: an old .../mirror/packages base also falls back to + * .../mirror/dists, so stale mirror.ini keeps working after the reform. */ + char distsbase[2048]; + snprintf(distsbase, sizeof(distsbase), "%s", bases[i]); + size_t dbl = strlen(distsbase); + static const char *pkgsuf = "/packages"; + size_t dpl = strlen(pkgsuf); + int isold = (dbl >= dpl && strcmp(distsbase + dbl - dpl, pkgsuf) == 0); + if (isold) snprintf(distsbase + dbl - dpl, sizeof(distsbase) - (dbl - dpl), "/dists"); + + for (int attempt = 0; attempt < (isold ? 2 : 1); attempt++) { + const char *use = (attempt == 0) ? bases[i] : distsbase; + snprintf(url, sizeof(url), "%s/%s.json", use, name); + pmm_info("%s", pmm_tr_fmt("msg.looking-up", name, use)); + body = http_get(url, &status); + if (getenv("PMM_DEBUG")) fprintf(stderr, "[reg] %s -> body=%s status=%d\n", + use, body ? "set" : "NULL", status); + if (body && status != 404 && status != 403 && status != 503) { used_base = bases[i]; used = 1; break; } + free(body); body = NULL; + } + if (used) break; } if (!body) { pmm_error("%s", pmm_tr_fmt("msg.err.registry-not-found", name)); diff --git a/src/main.c b/src/main.c index 1194d20..794c488 100644 --- a/src/main.c +++ b/src/main.c @@ -197,10 +197,24 @@ static char *registry_fetch(const char *rel, int *outstatus) { bases[nb++] = ml->items[i].registry; char url[2048]; int status = 0; char *body = NULL; for (int i = 0; i < nb; i++) { - snprintf(url, sizeof(url), "%s/%s", bases[i], rel); - body = http_get(url, &status); - if (body && status != 404 && status != 403 && status != 503) break; - free(body); body = NULL; + /* layout migration: an old .../mirror/packages base also falls back to + * .../mirror/dists, so a stale mirror.ini keeps working after the reform. */ + char distsbase[2048]; + snprintf(distsbase, sizeof(distsbase), "%s", bases[i]); + size_t dbl = strlen(distsbase); + static const char *pkgsuf = "/packages"; + size_t dpl = strlen(pkgsuf); + int isold = (dbl >= dpl && strcmp(distsbase + dbl - dpl, pkgsuf) == 0); + if (isold) snprintf(distsbase + dbl - dpl, sizeof(distsbase) - (dbl - dpl), "/dists"); + + for (int attempt = 0; attempt < (isold ? 2 : 1); attempt++) { + const char *use = (attempt == 0) ? bases[i] : distsbase; + snprintf(url, sizeof(url), "%s/%s", use, rel); + body = http_get(url, &status); + if (body && status != 404 && status != 403 && status != 503) break; + free(body); body = NULL; + } + if (body) break; } mirrors_free(ml); if (outstatus) *outstatus = status; diff --git a/web/mirror/lang/en-US.pjson b/web/mirror/lang/en-US.pjson index 1c28509..4fd1b97 100644 --- a/web/mirror/lang/en-US.pjson +++ b/web/mirror/lang/en-US.pjson @@ -86,7 +86,7 @@ "msg.err.cannot-read": "cannot read %s", "msg.err.cannot-open": "cannot open file: %s", "msg.err.invalid-locale": "invalid language pack: %s", - "msg.err.download-failed": "failed to download language pack %s", + "msg.err.download-failed": "failed to download %s", "msg.err.no-registry-mirror": "no registry mirror configured. Add to ~/.pmm/mirror.ini", "msg.err.no-mirror": "no mirrors configured (use: pmm mirror add)", "msg.err.no-mirror-file": "no mirror file", diff --git a/web/mirror/lang/zh-CN.pjson b/web/mirror/lang/zh-CN.pjson index e5eca52..a479fb2 100644 --- a/web/mirror/lang/zh-CN.pjson +++ b/web/mirror/lang/zh-CN.pjson @@ -86,7 +86,7 @@ "msg.err.cannot-read": "无法读取 %s", "msg.err.cannot-open": "无法打开文件: %s", "msg.err.invalid-locale": "无效的语言包: %s", - "msg.err.download-failed": "下载语言包 %s 失败", + "msg.err.download-failed": "下载 %s 失败", "msg.err.no-registry-mirror": "没有配置注册表镜像。请添加到 ~/.pmm/mirror.ini", "msg.err.no-mirror": "没有配置镜像源(使用: pmm mirror add)", "msg.err.no-mirror-file": "没有镜像配置文件",