From b816717aeb99fb3afe883ef78ca73a934bc01356 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Fri, 14 Jun 2024 14:34:02 +0800 Subject: [PATCH] Support DEB822 for Debian and support different versions of it [Gitee link #IA5F6P] [GitHub link #23] --- src/chsrc.c | 111 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 14 deletions(-) diff --git a/src/chsrc.c b/src/chsrc.c index afde135..400fce9 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -830,6 +830,10 @@ ensure_apt_sourcelist (int debian_type) char *codename = xy_run ("sed -nr 's/VERSION_CODENAME=(.*)/\\1/p' " ETC_OSRELEASE, 0, NULL); codename = xy_str_delete_suffix (codename, "\n"); + char *version_id = xy_run ("sed -nr 's/VERSION_ID=(.*)/\\1/p' " ETC_OSRELEASE, 0, NULL); + version_id = xy_str_delete_suffix (codename, "\n"); + double version = atof (version_id); + char *makeup = NULL; if (debian_type == Debian_deriv_ubuntu) @@ -843,13 +847,43 @@ ensure_apt_sourcelist (int debian_type) } else { - // https://wiki.debian.org/SourcesList - makeup = xy_strjoin (9, + if (version >= 12) + { + // https://wiki.debian.org/SourcesList + // https://mirrors.tuna.tsinghua.edu.cn/help/debian/ + // 从 Debian 12 开始,开始有一项 non-free-firmware + makeup = xy_strjoin (9, + "# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" + "deb " Chsrc_Maintain_URL "/debian ", codename, " main contrib non-free non-free-firmware\n" + "deb " Chsrc_Maintain_URL "/debian ", codename, "-updates main contrib non-free non-free-firmware\n" + "deb " Chsrc_Maintain_URL "/debian ", codename, "-backports main contrib non-free non-free-firmware\n" + "deb " Chsrc_Maintain_URL "/debian-security ", codename, "-security main contrib non-free non-free-firmware\n"); + // 上述 debian-security 这种写法是和 Debian 10不同的,所以我们只能支持 Debian 11+ + } + else if (version >= 11) + { + makeup = xy_strjoin (9, "# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\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"); + "deb " Chsrc_Maintain_URL "/debian ", codename, "-backports main contrib non-free\n" + "deb " Chsrc_Maintain_URL "/debian-security ", codename, "-security main contrib non-free\n"); + } + else if (version >= 10) + { + makeup = xy_strjoin (9, + "# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" + "deb " Chsrc_Maintain_URL "/debian ", codename, " 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" + "deb " Chsrc_Maintain_URL "/debian-security ", codename, "/updates main contrib non-free\n"); + // 上述 debian-security 这种写法是和 Debian 11 不同的 + } + else + { + chsrc_error ("您的Debian版本过低(<10),暂不支持换源"); + exit (2); + } } FILE *f = fopen (ETC_APT_SOURCELIST, "w"); @@ -861,17 +895,21 @@ ensure_apt_sourcelist (int debian_type) /** - * @note 从 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式, + * @note 从 Debian 12 开始,Debain 的软件源配置文件变更为 DEB822 格式, + * 路径为: /etc/apt/sources.list.d/debian.sources" + * + * @note 从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式, * 路径为: /etc/apt/sources.list.d/ubuntu.sources */ -#define ETC_APT_DEB822_UBUNTU_SOURCES "/etc/apt/sources.list.d/ubuntu.sources" +#define ETC_APT_DEB822_Debian_Sources "/etc/apt/sources.list.d/debian.sources" +#define ETC_APT_DEB822_Ubuntu_Sources "/etc/apt/sources.list.d/ubuntu.sources" void os_ubuntu_getsrc (char *option) { - if (query_file_exist (ETC_APT_DEB822_UBUNTU_SOURCES)) + if (query_file_exist (ETC_APT_DEB822_Ubuntu_Sources)) { - chsrc_take_a_look_at_file (ETC_APT_DEB822_UBUNTU_SOURCES); + chsrc_take_a_look_at_file (ETC_APT_DEB822_Ubuntu_Sources); return; } @@ -896,17 +934,17 @@ os_ubuntu_setsrc_for_deb822 (char *option) chsrc_yield_source (os_ubuntu); chsrc_confirm_selection (&source); - chsrc_backup (ETC_APT_DEB822_UBUNTU_SOURCES); + chsrc_backup (ETC_APT_DEB822_Ubuntu_Sources); 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?://.*/ubuntu/?@", source.url, "@g\' " ETC_APT_DEB822_UBUNTU_SOURCES); + cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu/?@", source.url, "@g\' " ETC_APT_DEB822_Ubuntu_Sources); } else { - cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url, "-ports@g\' " ETC_APT_DEB822_UBUNTU_SOURCES); + cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url, "-ports@g\' " ETC_APT_DEB822_Ubuntu_Sources); } chsrc_run (cmd); @@ -922,7 +960,7 @@ os_ubuntu_setsrc (char *option) { chsrc_ensure_root (); - if (query_file_exist (ETC_APT_DEB822_UBUNTU_SOURCES)) + if (query_file_exist (ETC_APT_DEB822_Ubuntu_Sources)) { chsrc_note_remarkably ("将基于新格式换源"); os_ubuntu_setsrc_for_deb822 (option); @@ -993,11 +1031,48 @@ os_mint_setsrc (char *option) void os_debian_getsrc (char *option) { - chsrc_take_a_look_at_file (ETC_APT_SOURCELIST); + if (query_file_exist (ETC_APT_DEB822_Debian_Sources)) + { + chsrc_take_a_look_at_file (ETC_APT_DEB822_Debian_Sources); + return; + } + + if (query_file_exist (ETC_APT_SOURCELIST)) + { + chsrc_take_a_look_at_file (ETC_APT_SOURCELIST); + return; + } + + chsrc_error_remarkably ("缺少源配置文件!但仍可直接通过 chsrc set debian 来添加使用新的源"); + return; } +void +os_debian_setsrc_for_deb822 (char *option) +{ + SourceInfo source; + chsrc_yield_source (os_debian); + chsrc_confirm_selection (&source); + + chsrc_note_remarkably ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:"); + puts ("apt install apt-transport-https ca-certificates"); + + chsrc_backup (ETC_APT_DEB822_Debian_Sources); + + char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " ETC_APT_DEB822_Debian_Sources); + chsrc_run (cmd); + + // debian-security 源和其他源不一样 + cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian-security/?@", source.url, "-security", "@g\' " ETC_APT_DEB822_Debian_Sources); + chsrc_run (cmd); + + chsrc_run ("apt update"); + chsrc_say_lastly (&source, ChsrcTypeAuto); +} + + /** - * Debian Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 + * Debian 10 Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 * apt install apt-transport-https ca-certificates */ void @@ -1005,6 +1080,14 @@ os_debian_setsrc (char *option) { chsrc_ensure_root (); + if (query_file_exist (ETC_APT_DEB822_Debian_Sources)) + { + chsrc_note_remarkably ("将基于新格式换源"); + os_debian_setsrc_for_deb822 (option); + return; + } + + // Docker环境下,Debian镜像可能不存在该文件 bool sourcelist_exist = ensure_apt_sourcelist (Debian_debian);