From 24af8eb090a17e7637b84e07c9d49697aeaf5774 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 25 Mar 2025 11:40:37 +0800 Subject: [PATCH] Handle `Debian` CDROM case [GitHub #185] --- src/chsrc-main.c | 43 +++++++++++++------------ src/recipe/os/APT/Debian.c | 66 ++++++++++++++++++++++++++++---------- src/recipe/os/APT/common.h | 36 ++++++++++++--------- 3 files changed, 92 insertions(+), 53 deletions(-) diff --git a/src/chsrc-main.c b/src/chsrc-main.c index d2c1f2e..b7b5d30 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -2,29 +2,30 @@ * Copyright © 2023-2025 Aoran Zeng, Heng Guo * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- - * Project Name : chsrc - * Project Authors : Aoran Zeng - * | Heng Guo <2085471348@qq.com> - * Contributors : Aaron Ruan - * | Rui Chen - * | Shengwei Chen <414685209@qq.com> - * | BlockLune - * | Mr. Will - * | Terrasse - * | Lontten - * | happy game - * | xuan - * | GnixAij - * | ChatGPT - * | czyt - * | zouri - * | yongxiang <1926885268@qq.com> - * | YU-7 <2747046473@qq.com> - * | juzeon - * | Jialin Lyu + * Project Name : chsrc + * Project Authors : Aoran Zeng + * | Heng Guo <2085471348@qq.com> + * Contributors : Aaron Ruan + * | Rui Chen + * | Shengwei Chen <414685209@qq.com> + * | BlockLune + * | Mr. Will + * | Terrasse + * | Lontten + * | happy game + * | xuan + * | GnixAij + * | ChatGPT + * | czyt + * | zouri + * | yongxiang <1926885268@qq.com> + * | YU-7 <2747046473@qq.com> + * | juzeon + * | Jialin Lyu + * | GitHub Copilot * | * Created On : <2023-08-28> - * Last Modified : <2025-03-17> + * Last Modified : <2025-03-25> * * chsrc: Change Source —— 全平台通用命令行换源工具 * ------------------------------------------------------------*/ diff --git a/src/recipe/os/APT/Debian.c b/src/recipe/os/APT/Debian.c index def94cf..2d8572b 100644 --- a/src/recipe/os/APT/Debian.c +++ b/src/recipe/os/APT/Debian.c @@ -1,12 +1,14 @@ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- - * File Authors : Aoran Zeng - * | Heng Guo <2085471348@qq.com> - * Contributors : Yangmoooo - * | - * Created On : <2023-09-02> - * Last Modified : <2024-12-18> + * File Authors : Aoran Zeng + * | Heng Guo <2085471348@qq.com> + * Contributors : Yangmoooo + * | GitHub Copilot + * | + * Created On : <2023-09-02> + * Major Revision : 3 + * Last Modified : <2025-03-25> * ------------------------------------------------------------*/ static SourceProvider_t os_debian_upstream = @@ -55,20 +57,30 @@ os_debian_getsrc (char *option) return; } + +static bool +os_debian_does_old_sourcelist_use_cdrom (void) +{ + /* 我们只检查旧版sourcelist,因为 common.h 中的填充只支持旧版 */ + char *cmd = xy_2strjoin ("grep -q '^deb cdrom:' ", OS_Apt_SourceList); + int ret = system (cmd); + bool use_cdrom = ret == 0; + + return use_cdrom; +} + + void os_debian_setsrc_for_deb822 (char *option) { chsrc_yield_source_and_confirm (os_debian); - chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:"); - puts ("apt install apt-transport-https ca-certificates"); - chsrc_backup (OS_Debian_SourceList_DEB822); char *cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian/?@", source.url, "@g' " OS_Debian_SourceList_DEB822); chsrc_run (cmd, RunOpt_Default); - // debian-security 源和其他源不一样 + /* debian-security 源和其他源不一样 */ cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian-security/?@", source.url, "-security@g' " OS_Debian_SourceList_DEB822); chsrc_run (cmd, RunOpt_Default); @@ -80,6 +92,8 @@ os_debian_setsrc_for_deb822 (char *option) /** + * 处理旧版(非DEB822) sourcelist 的换源 + * * Debian 10 Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 * apt install apt-transport-https ca-certificates */ @@ -90,27 +104,46 @@ os_debian_setsrc (char *option) if (chsrc_check_file (OS_Debian_SourceList_DEB822)) { - chsrc_note2 ("将基于新格式换源"); + chsrc_note2 ("将基于新格式(DEB822)换源"); os_debian_setsrc_for_deb822 (option); return; } + chsrc_note2 ("将基于旧格式(非DEB822)换源"); - // Docker环境下,Debian镜像可能不存在该文件 + /* Docker环境下,Debian镜像可能不存在该文件 */ bool sourcelist_exist = ensure_apt_sourcelist (OS_Is_Debian_Literally); + /** + * 处理带有CDROM源的sourcelist + * + * https://github.com/RubyMetric/chsrc/issues/185#issuecomment-2746072917 + */ + if (sourcelist_exist) + { + bool use_cdrom = os_debian_does_old_sourcelist_use_cdrom(); + if (use_cdrom) + { + chsrc_backup (OS_Debian_old_SourceList); + system ("rm " OS_Debian_old_SourceList); + chsrc_warn2 ("旧版源配置文件中使用了 CDROM 源,已删除(但备份)该配置文件,重新配置"); + /* 现在的情况是:系统中已经没有配置文件了 */ + sourcelist_exist = ensure_apt_sourcelist (OS_Is_Debian_Literally); + } + } + chsrc_yield_source_and_confirm (os_debian); chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:"); - puts ("apt install apt-transport-https ca-certificates"); + say ("apt install apt-transport-https ca-certificates"); - // 不存在的时候,用的是我们生成的无效文件,不要备份 + /* 不存在的时候,用的是我们生成的用来填充占位的无效文件,不要备份 */ if (sourcelist_exist) { - chsrc_backup (OS_Apt_SourceList); + chsrc_backup (OS_Debian_old_SourceList); } - char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " OS_Apt_SourceList); + char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " OS_Debian_old_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt update", RunOpt_No_Last_New_Line); @@ -145,4 +178,3 @@ os_debian_feat (char *option) } def_target_gsrf(os_debian); - diff --git a/src/recipe/os/APT/common.h b/src/recipe/os/APT/common.h index 3b487ed..6836820 100644 --- a/src/recipe/os/APT/common.h +++ b/src/recipe/os/APT/common.h @@ -1,18 +1,19 @@ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- - * File Authors : Aoran Zeng - * Contributors : happy game - * | - * Created On : <2024-06-14> - * Last Modified : <2024-12-12> + * File Authors : Aoran Zeng + * Contributors : happy game + * | + * Created On : <2024-06-14> + * Major Revision : 2 + * Last Modified : <2025-03-25> * ------------------------------------------------------------*/ #define OS_Apt_SourceList "/etc/apt/sources.list" #define OS_Apt_SourceList_D "/etc/apt/sources.list.d/" /** - * @note 从 Debian 12 开始,Debain 的软件源配置文件变更为 DEB822 格式, + * @note 从 Debian 12 (bookworm) 开始,Debain 的软件源配置文件变更为 DEB822 格式, * 路径为: /etc/apt/sources.list.d/debian.sources" * * @note 从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式, @@ -21,6 +22,9 @@ #define OS_Debian_SourceList_DEB822 "/etc/apt/sources.list.d/debian.sources" #define OS_Ubuntu_SourceList_DEB822 "/etc/apt/sources.list.d/ubuntu.sources" +#define OS_Debian_old_SourceList OS_Apt_SourceList +#define OS_Ubuntu_old_SourceList OS_Apt_SourceList + #define ETC_OS_RELEASE "/etc/os-release" @@ -58,7 +62,7 @@ ensure_apt_sourcelist (int debian_type) } else { - char *msg = CliOpt_InEnglish ? "Will generate a new source config file" + char *msg = CliOpt_InEnglish ? "Will generate a new source list file" : "将生成新的源配置文件"; chsrc_note2 (msg); } @@ -83,20 +87,22 @@ ensure_apt_sourcelist (int debian_type) } else { - if (version >= 12) + if (version >= 12) /* bookworm */ { - // https://wiki.debian.org/SourcesList - // https://mirrors.tuna.tsinghua.edu.cn/help/debian/ - // 从 Debian 12 开始,开始有一项 non-free-firmware + /** + * 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 "\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+ + /* 上述 debian-security 这种写法是和 Debian 10不同的,所以我们只能支持 Debian 11+ */ } - else if (version >= 11) + else if (version >= 11) /* bullseye */ { makeup = xy_strjoin (9, "# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" @@ -105,7 +111,7 @@ ensure_apt_sourcelist (int debian_type) "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) + else if (version >= 10) /* buster */ { makeup = xy_strjoin (9, "# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" @@ -113,7 +119,7 @@ ensure_apt_sourcelist (int debian_type) "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 不同的 + /* 上述 debian-security 这种写法是和 Debian 11 不同的 */ } else {