From 7439f522e236bd4e21cbc8fa99674c44fd3c72a4 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 11 Jun 2024 21:02:24 +0800 Subject: [PATCH] Ensure Debian's source.list too [GitHub link #23] --- src/chsrc.c | 68 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/src/chsrc.c b/src/chsrc.c index 8fb9824..c9855a9 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -843,20 +843,24 @@ pl_julia_setsrc (char *option) #define ETC_APT_SOURCELIST "/etc/apt/sources.list" #define ETC_OSRELEASE "/etc/os-release" +#define Debian_debian 1 +#define Debian_deriv_ubuntu 2 /** - * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu + * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu 和 Debian * 因为其它的 Debian 变体可能不使用 ETC_APT_SOURCELIST,也可能并不适用 `VERSION_CODENAME` + * + * @return 文件是否存在 */ -void -makeup_etc_apt_sourcelist () +bool +ensure_apt_sourcelist (int debian_type) { bool exist = xy_file_exist (ETC_APT_SOURCELIST); if (exist) { chsrc_infolog_remarkably (ETC_APT_SOURCELIST " 存在"); - return; + return true; } else { @@ -866,16 +870,32 @@ makeup_etc_apt_sourcelist () char *codename = xy_run ("sed -nr 's/VERSION_CODENAME=(.*)/\1/p' " ETC_OSRELEASE, 0, NULL); codename = xy_str_delete_suffix (codename, "\n"); - char *makeup = xy_strjoin (9, - "# Generated by " Chsrc_Version "\n" - "deb " Chsrc_Maintain_URL, codename, " main restricted universe multiverse\n" - "deb " Chsrc_Maintain_URL, codename, "-updates main restricted universe multiverse\n" - "deb " Chsrc_Maintain_URL, codename, "-backports main restricted universe multiverse\n" - "deb " Chsrc_Maintain_URL, codename, "-security main restricted universe multiverse\n"); + char *makeup = NULL; + + if (debian_type == Debian_deriv_ubuntu) + { + makeup = xy_strjoin (9, + "# Generated by chsrc" Chsrc_Version "\n" + "deb " Chsrc_Maintain_URL "/ubuntu", codename, " main restricted universe multiverse\n" + "deb " Chsrc_Maintain_URL "/ubuntu", codename, "-updates main restricted universe multiverse\n" + "deb " Chsrc_Maintain_URL "/ubuntu", codename, "-backports main restricted universe multiverse\n" + "deb " Chsrc_Maintain_URL "/ubuntu", codename, "-security main restricted universe multiverse\n"); + } + else + { + // https://wiki.debian.org/SourcesList + makeup = xy_strjoin (9, + "# Generated by chsrc" Chsrc_Version "\n" + "deb " Chsrc_Maintain_URL "/debian", codename, " main contrib non-free\n" + "deb " Chsrc_Maintain_URL "/debian", codename, "-security main contrib non-free\n" + "deb " Chsrc_Maintain_URL "/debian", codename, "-updates main contrib non-free\n" + "deb " Chsrc_Maintain_URL "/debian", codename, "-backports main contrib non-free\n"); + } FILE *f = fopen (ETC_APT_SOURCELIST, "w"); - fwrite (makeup, strlen(makeup), 1, f); + fwrite (makeup, strlen (makeup), 1, f); fclose (f); + return false; } @@ -894,24 +914,28 @@ os_ubuntu_setsrc (char *option) { chsrc_ensure_root (); - makeup_etc_apt_sourcelist (); + bool sourcelist_exist = ensure_apt_sourcelist (Debian_deriv_ubuntu); int index = use_specific_mirror_or_auto_select (option, os_ubuntu); SourceInfo source = os_ubuntu_sources[index]; chsrc_confirm_selection (&source); - chsrc_backup (ETC_APT_SOURCELIST); + // 不存在的时候,用的是我们生成的无效文件,不要备份 + if (sourcelist_exist) + { + chsrc_backup (ETC_APT_SOURCELIST); + } char *arch = xy_run ("arch", 0, NULL); char *cmd = NULL; if (strncmp (arch, "x86_64", 6)==0) { - cmd = xy_strjoin (3, "sed -E -i \'s@^https?://.*$@", source.url, "@g\' " ETC_APT_SOURCELIST); + cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu/?@", source.url, "@g\' " ETC_APT_SOURCELIST); } else { - cmd = xy_strjoin (3, "sed -E -i \'s@^https?://.*$@", source.url, "-ports@g\' " ETC_APT_SOURCELIST); + cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url, "-ports@g\' " ETC_APT_SOURCELIST); } chsrc_run (cmd); @@ -968,6 +992,9 @@ os_debian_setsrc (char *option) { chsrc_ensure_root (); + // Docker环境下,Debian镜像可能不存在该文件 + bool sourcelist_exist = ensure_apt_sourcelist (Debian_debian); + int index = use_specific_mirror_or_auto_select (option, os_debian); SourceInfo source = os_debian_sources[index]; @@ -976,12 +1003,13 @@ os_debian_setsrc (char *option) chsrc_note_remarkably ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:"); puts ("apt install apt-transport-https ca-certificates"); - chsrc_backup (ETC_APT_SOURCELIST); + // 不存在的时候,用的是我们生成的无效文件,不要备份 + if (sourcelist_exist) + { + chsrc_backup (ETC_APT_SOURCELIST); + } - char *cmd = xy_strjoin(3, - "sed -E -i \'s@https?://.*/debian/?@", - source.url, - "@g\' /etc/apt/sources.list"); + char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " ETC_APT_SOURCELIST); chsrc_run (cmd); chsrc_run ("apt update");