Handle Debian CDROM case

[GitHub #185]
This commit is contained in:
Aoran Zeng 2025-03-25 11:40:37 +08:00
parent d6834c3d5a
commit 24af8eb090
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
3 changed files with 92 additions and 53 deletions

View File

@ -22,9 +22,10 @@
* | YU-7 <2747046473@qq.com> * | YU-7 <2747046473@qq.com>
* | juzeon <skyjuzheng@gmail.com> * | juzeon <skyjuzheng@gmail.com>
* | Jialin Lyu <jialinlvcn@aliyun.com> * | Jialin Lyu <jialinlvcn@aliyun.com>
* | GitHub Copilot <https://github.com/copilot>
* | * |
* Created On : <2023-08-28> * Created On : <2023-08-28>
* Last Modified : <2025-03-17> * Last Modified : <2025-03-25>
* *
* chsrc: Change Source * chsrc: Change Source
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/

View File

@ -4,9 +4,11 @@
* File Authors : Aoran Zeng <ccmywish@qq.com> * File Authors : Aoran Zeng <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com> * | Heng Guo <2085471348@qq.com>
* Contributors : Yangmoooo <yangmoooo@outlook.com> * Contributors : Yangmoooo <yangmoooo@outlook.com>
* | GitHub Copilot <https://github.com/copilot>
* | * |
* Created On : <2023-09-02> * Created On : <2023-09-02>
* Last Modified : <2024-12-18> * Major Revision : 3
* Last Modified : <2025-03-25>
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
static SourceProvider_t os_debian_upstream = static SourceProvider_t os_debian_upstream =
@ -55,20 +57,30 @@ os_debian_getsrc (char *option)
return; 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 void
os_debian_setsrc_for_deb822 (char *option) os_debian_setsrc_for_deb822 (char *option)
{ {
chsrc_yield_source_and_confirm (os_debian); 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); chsrc_backup (OS_Debian_SourceList_DEB822);
char *cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian/?@", source.url, "@g' " 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); 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); cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian-security/?@", source.url, "-security@g' " OS_Debian_SourceList_DEB822);
chsrc_run (cmd, RunOpt_Default); chsrc_run (cmd, RunOpt_Default);
@ -80,6 +92,8 @@ os_debian_setsrc_for_deb822 (char *option)
/** /**
* (DEB822) sourcelist
*
* Debian 10 Buster HTTPS HTTPS 使 HTTP * Debian 10 Buster HTTPS HTTPS 使 HTTP
* apt install apt-transport-https ca-certificates * apt install apt-transport-https ca-certificates
*/ */
@ -90,27 +104,46 @@ os_debian_setsrc (char *option)
if (chsrc_check_file (OS_Debian_SourceList_DEB822)) if (chsrc_check_file (OS_Debian_SourceList_DEB822))
{ {
chsrc_note2 ("将基于新格式换源"); chsrc_note2 ("将基于新格式(DEB822)换源");
os_debian_setsrc_for_deb822 (option); os_debian_setsrc_for_deb822 (option);
return; return;
} }
chsrc_note2 ("将基于旧格式(非DEB822)换源");
// Docker环境下Debian镜像可能不存在该文件 /* Docker环境下Debian镜像可能不存在该文件 */
bool sourcelist_exist = ensure_apt_sourcelist (OS_Is_Debian_Literally); 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_yield_source_and_confirm (os_debian);
chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:"); chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:");
puts ("apt install apt-transport-https ca-certificates"); say ("apt install apt-transport-https ca-certificates");
// 不存在的时候,用的是我们生成的无效文件,不要备份 /* 不存在的时候,用的是我们生成的用来填充占位的无效文件,不要备份 */
if (sourcelist_exist) 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 (cmd, RunOpt_Default);
chsrc_run ("apt update", RunOpt_No_Last_New_Line); chsrc_run ("apt update", RunOpt_No_Last_New_Line);
@ -145,4 +178,3 @@ os_debian_feat (char *option)
} }
def_target_gsrf(os_debian); def_target_gsrf(os_debian);

View File

@ -5,14 +5,15 @@
* Contributors : happy game <happygame1024@gmail.com> * Contributors : happy game <happygame1024@gmail.com>
* | * |
* Created On : <2024-06-14> * Created On : <2024-06-14>
* Last Modified : <2024-12-12> * Major Revision : 2
* Last Modified : <2025-03-25>
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
#define OS_Apt_SourceList "/etc/apt/sources.list" #define OS_Apt_SourceList "/etc/apt/sources.list"
#define OS_Apt_SourceList_D "/etc/apt/sources.list.d/" #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" * : /etc/apt/sources.list.d/debian.sources"
* *
* @note Ubuntu 24.04 Ubuntu DEB822 * @note Ubuntu 24.04 Ubuntu DEB822
@ -21,6 +22,9 @@
#define OS_Debian_SourceList_DEB822 "/etc/apt/sources.list.d/debian.sources" #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_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" #define ETC_OS_RELEASE "/etc/os-release"
@ -58,7 +62,7 @@ ensure_apt_sourcelist (int debian_type)
} }
else 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); chsrc_note2 (msg);
} }
@ -83,20 +87,22 @@ ensure_apt_sourcelist (int debian_type)
} }
else else
{ {
if (version >= 12) if (version >= 12) /* bookworm */
{ {
// https://wiki.debian.org/SourcesList /**
// https://mirrors.tuna.tsinghua.edu.cn/help/debian/ * https://wiki.debian.org/SourcesList
// 从 Debian 12 开始,开始有一项 non-free-firmware * https://mirrors.tuna.tsinghua.edu.cn/help/debian/
* Debian 12 non-free-firmware
*/
makeup = xy_strjoin (9, makeup = xy_strjoin (9,
"# Generated by chsrc " Chsrc_Version "\n\n" "# 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, " 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, "-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 ", 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"); "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, makeup = xy_strjoin (9,
"# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" "# 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 ", codename, "-backports main contrib non-free\n"
"deb " Chsrc_Maintain_URL "/debian-security ", codename, "-security 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, makeup = xy_strjoin (9,
"# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n" "# 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, "-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, "/updates 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 else
{ {