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

@ -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 <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com>
* Contributors : Aaron Ruan <aaron212cn@outlook.com>
* | Rui Chen <rui@chenrui.dev>
* | Shengwei Chen <414685209@qq.com>
* | BlockLune <blocklune@gmail.com>
* | Mr. Will <mr.will.com@outlook.com>
* | Terrasse <terrasse@qq.com>
* | Lontten <lontten@163.com>
* | happy game <happygame1024@gmail.com>
* | xuan <wick.dynex@qq.com>
* | GnixAij <gaojiaxing0220@gmail.com>
* | ChatGPT <https://chatgpt.com>
* | czyt <czyt.go@gmail.com>
* | zouri <guoshuaisun@outlook.com>
* | yongxiang <1926885268@qq.com>
* | YU-7 <2747046473@qq.com>
* | juzeon <skyjuzheng@gmail.com>
* | Jialin Lyu <jialinlvcn@aliyun.com>
* Project Name : chsrc
* Project Authors : Aoran Zeng <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com>
* Contributors : Aaron Ruan <aaron212cn@outlook.com>
* | Rui Chen <rui@chenrui.dev>
* | Shengwei Chen <414685209@qq.com>
* | BlockLune <blocklune@gmail.com>
* | Mr. Will <mr.will.com@outlook.com>
* | Terrasse <terrasse@qq.com>
* | Lontten <lontten@163.com>
* | happy game <happygame1024@gmail.com>
* | xuan <wick.dynex@qq.com>
* | GnixAij <gaojiaxing0220@gmail.com>
* | ChatGPT <https://chatgpt.com>
* | czyt <czyt.go@gmail.com>
* | zouri <guoshuaisun@outlook.com>
* | yongxiang <1926885268@qq.com>
* | YU-7 <2747046473@qq.com>
* | juzeon <skyjuzheng@gmail.com>
* | Jialin Lyu <jialinlvcn@aliyun.com>
* | GitHub Copilot <https://github.com/copilot>
* |
* Created On : <2023-08-28>
* Last Modified : <2025-03-17>
* Last Modified : <2025-03-25>
*
* chsrc: Change Source
* ------------------------------------------------------------*/

View File

@ -1,12 +1,14 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com>
* Contributors : Yangmoooo <yangmoooo@outlook.com>
* |
* Created On : <2023-09-02>
* Last Modified : <2024-12-18>
* File Authors : Aoran Zeng <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com>
* Contributors : Yangmoooo <yangmoooo@outlook.com>
* | GitHub Copilot <https://github.com/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);

View File

@ -1,18 +1,19 @@
/** ------------------------------------------------------------
* SPDX-License-Identifier: GPL-3.0-or-later
* -------------------------------------------------------------
* File Authors : Aoran Zeng <ccmywish@qq.com>
* Contributors : happy game <happygame1024@gmail.com>
* |
* Created On : <2024-06-14>
* Last Modified : <2024-12-12>
* File Authors : Aoran Zeng <ccmywish@qq.com>
* Contributors : happy game <happygame1024@gmail.com>
* |
* 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
{