Skip to content

Commit 24af8eb

Browse files
committed
Handle Debian CDROM case
[GitHub #185]
1 parent d6834c3 commit 24af8eb

File tree

3 files changed

+92
-53
lines changed

3 files changed

+92
-53
lines changed

src/chsrc-main.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22
* Copyright © 2023-2025 Aoran Zeng, Heng Guo
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
* -------------------------------------------------------------
5-
* Project Name : chsrc
6-
* Project Authors : Aoran Zeng <[email protected]>
7-
* | Heng Guo <[email protected]>
8-
* Contributors : Aaron Ruan <[email protected]>
9-
* | Rui Chen <[email protected]>
10-
* | Shengwei Chen <[email protected]>
11-
* | BlockLune <[email protected]>
12-
* | Mr. Will <[email protected]>
13-
* | Terrasse <[email protected]>
14-
* | Lontten <[email protected]>
15-
* | happy game <[email protected]>
16-
* | xuan <[email protected]>
17-
* | GnixAij <[email protected]>
18-
* | ChatGPT <https://chatgpt.com>
19-
* | czyt <[email protected]>
20-
* | zouri <[email protected]>
21-
* | yongxiang <[email protected]>
22-
* | YU-7 <[email protected]>
23-
* | juzeon <[email protected]>
24-
* | Jialin Lyu <[email protected]>
5+
* Project Name : chsrc
6+
* Project Authors : Aoran Zeng <[email protected]>
7+
* | Heng Guo <[email protected]>
8+
* Contributors : Aaron Ruan <[email protected]>
9+
* | Rui Chen <[email protected]>
10+
* | Shengwei Chen <[email protected]>
11+
* | BlockLune <[email protected]>
12+
* | Mr. Will <[email protected]>
13+
* | Terrasse <[email protected]>
14+
* | Lontten <[email protected]>
15+
* | happy game <[email protected]>
16+
* | xuan <[email protected]>
17+
* | GnixAij <[email protected]>
18+
* | ChatGPT <https://chatgpt.com>
19+
* | czyt <[email protected]>
20+
* | zouri <[email protected]>
21+
* | yongxiang <[email protected]>
22+
* | YU-7 <[email protected]>
23+
* | juzeon <[email protected]>
24+
* | Jialin Lyu <[email protected]>
25+
* | GitHub Copilot <https://github.com/copilot>
2526
* |
2627
* Created On : <2023-08-28>
27-
* Last Modified : <2025-03-17>
28+
* Last Modified : <2025-03-25>
2829
*
2930
* chsrc: Change Source —— 全平台通用命令行换源工具
3031
* ------------------------------------------------------------*/

src/recipe/os/APT/Debian.c

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/** ------------------------------------------------------------
22
* SPDX-License-Identifier: GPL-3.0-or-later
33
* -------------------------------------------------------------
4-
* File Authors : Aoran Zeng <[email protected]>
5-
* | Heng Guo <[email protected]>
6-
* Contributors : Yangmoooo <[email protected]>
7-
* |
8-
* Created On : <2023-09-02>
9-
* Last Modified : <2024-12-18>
4+
* File Authors : Aoran Zeng <[email protected]>
5+
* | Heng Guo <[email protected]>
6+
* Contributors : Yangmoooo <[email protected]>
7+
* | GitHub Copilot <https://github.com/copilot>
8+
* |
9+
* Created On : <2023-09-02>
10+
* Major Revision : 3
11+
* Last Modified : <2025-03-25>
1012
* ------------------------------------------------------------*/
1113

1214
static SourceProvider_t os_debian_upstream =
@@ -55,20 +57,30 @@ os_debian_getsrc (char *option)
5557
return;
5658
}
5759

60+
61+
static bool
62+
os_debian_does_old_sourcelist_use_cdrom (void)
63+
{
64+
/* 我们只检查旧版sourcelist,因为 common.h 中的填充只支持旧版 */
65+
char *cmd = xy_2strjoin ("grep -q '^deb cdrom:' ", OS_Apt_SourceList);
66+
int ret = system (cmd);
67+
bool use_cdrom = ret == 0;
68+
69+
return use_cdrom;
70+
}
71+
72+
5873
void
5974
os_debian_setsrc_for_deb822 (char *option)
6075
{
6176
chsrc_yield_source_and_confirm (os_debian);
6277

63-
chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:");
64-
puts ("apt install apt-transport-https ca-certificates");
65-
6678
chsrc_backup (OS_Debian_SourceList_DEB822);
6779

6880
char *cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian/?@", source.url, "@g' " OS_Debian_SourceList_DEB822);
6981
chsrc_run (cmd, RunOpt_Default);
7082

71-
// debian-security 源和其他源不一样
83+
/* debian-security 源和其他源不一样 */
7284
cmd = xy_strjoin (3, "sed -E -i 's@https?://.*/debian-security/?@", source.url, "-security@g' " OS_Debian_SourceList_DEB822);
7385
chsrc_run (cmd, RunOpt_Default);
7486

@@ -80,6 +92,8 @@ os_debian_setsrc_for_deb822 (char *option)
8092

8193

8294
/**
95+
* 处理旧版(非DEB822) sourcelist 的换源
96+
*
8397
* Debian 10 Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装
8498
* apt install apt-transport-https ca-certificates
8599
*/
@@ -90,27 +104,46 @@ os_debian_setsrc (char *option)
90104

91105
if (chsrc_check_file (OS_Debian_SourceList_DEB822))
92106
{
93-
chsrc_note2 ("将基于新格式换源");
107+
chsrc_note2 ("将基于新格式(DEB822)换源");
94108
os_debian_setsrc_for_deb822 (option);
95109
return;
96110
}
97111

112+
chsrc_note2 ("将基于旧格式(非DEB822)换源");
98113

99-
// Docker环境下,Debian镜像可能不存在该文件
114+
/* Docker环境下,Debian镜像可能不存在该文件 */
100115
bool sourcelist_exist = ensure_apt_sourcelist (OS_Is_Debian_Literally);
101116

117+
/**
118+
* 处理带有CDROM源的sourcelist
119+
*
120+
* https://github.com/RubyMetric/chsrc/issues/185#issuecomment-2746072917
121+
*/
122+
if (sourcelist_exist)
123+
{
124+
bool use_cdrom = os_debian_does_old_sourcelist_use_cdrom();
125+
if (use_cdrom)
126+
{
127+
chsrc_backup (OS_Debian_old_SourceList);
128+
system ("rm " OS_Debian_old_SourceList);
129+
chsrc_warn2 ("旧版源配置文件中使用了 CDROM 源,已删除(但备份)该配置文件,重新配置");
130+
/* 现在的情况是:系统中已经没有配置文件了 */
131+
sourcelist_exist = ensure_apt_sourcelist (OS_Is_Debian_Literally);
132+
}
133+
}
134+
102135
chsrc_yield_source_and_confirm (os_debian);
103136

104137
chsrc_note2 ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:");
105-
puts ("apt install apt-transport-https ca-certificates");
138+
say ("apt install apt-transport-https ca-certificates");
106139

107-
// 不存在的时候,用的是我们生成的无效文件,不要备份
140+
/* 不存在的时候,用的是我们生成的用来填充占位的无效文件,不要备份 */
108141
if (sourcelist_exist)
109142
{
110-
chsrc_backup (OS_Apt_SourceList);
143+
chsrc_backup (OS_Debian_old_SourceList);
111144
}
112145

113-
char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " OS_Apt_SourceList);
146+
char *cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " OS_Debian_old_SourceList);
114147

115148
chsrc_run (cmd, RunOpt_Default);
116149
chsrc_run ("apt update", RunOpt_No_Last_New_Line);
@@ -145,4 +178,3 @@ os_debian_feat (char *option)
145178
}
146179

147180
def_target_gsrf(os_debian);
148-

src/recipe/os/APT/common.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/** ------------------------------------------------------------
22
* SPDX-License-Identifier: GPL-3.0-or-later
33
* -------------------------------------------------------------
4-
* File Authors : Aoran Zeng <[email protected]>
5-
* Contributors : happy game <[email protected]>
6-
* |
7-
* Created On : <2024-06-14>
8-
* Last Modified : <2024-12-12>
4+
* File Authors : Aoran Zeng <[email protected]>
5+
* Contributors : happy game <[email protected]>
6+
* |
7+
* Created On : <2024-06-14>
8+
* Major Revision : 2
9+
* Last Modified : <2025-03-25>
910
* ------------------------------------------------------------*/
1011

1112
#define OS_Apt_SourceList "/etc/apt/sources.list"
1213
#define OS_Apt_SourceList_D "/etc/apt/sources.list.d/"
1314

1415
/**
15-
* @note 从 Debian 12 开始,Debain 的软件源配置文件变更为 DEB822 格式,
16+
* @note 从 Debian 12 (bookworm) 开始,Debain 的软件源配置文件变更为 DEB822 格式,
1617
* 路径为: /etc/apt/sources.list.d/debian.sources"
1718
*
1819
* @note 从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式,
@@ -21,6 +22,9 @@
2122
#define OS_Debian_SourceList_DEB822 "/etc/apt/sources.list.d/debian.sources"
2223
#define OS_Ubuntu_SourceList_DEB822 "/etc/apt/sources.list.d/ubuntu.sources"
2324

25+
#define OS_Debian_old_SourceList OS_Apt_SourceList
26+
#define OS_Ubuntu_old_SourceList OS_Apt_SourceList
27+
2428

2529
#define ETC_OS_RELEASE "/etc/os-release"
2630

@@ -58,7 +62,7 @@ ensure_apt_sourcelist (int debian_type)
5862
}
5963
else
6064
{
61-
char *msg = CliOpt_InEnglish ? "Will generate a new source config file"
65+
char *msg = CliOpt_InEnglish ? "Will generate a new source list file"
6266
: "将生成新的源配置文件";
6367
chsrc_note2 (msg);
6468
}
@@ -83,20 +87,22 @@ ensure_apt_sourcelist (int debian_type)
8387
}
8488
else
8589
{
86-
if (version >= 12)
90+
if (version >= 12) /* bookworm */
8791
{
88-
// https://wiki.debian.org/SourcesList
89-
// https://mirrors.tuna.tsinghua.edu.cn/help/debian/
90-
// 从 Debian 12 开始,开始有一项 non-free-firmware
92+
/**
93+
* https://wiki.debian.org/SourcesList
94+
* https://mirrors.tuna.tsinghua.edu.cn/help/debian/
95+
* 从 Debian 12 开始,开始有一项 non-free-firmware
96+
*/
9197
makeup = xy_strjoin (9,
9298
"# Generated by chsrc " Chsrc_Version "\n\n"
9399
"deb " Chsrc_Maintain_URL "/debian ", codename, " main contrib non-free non-free-firmware\n"
94100
"deb " Chsrc_Maintain_URL "/debian ", codename, "-updates main contrib non-free non-free-firmware\n"
95101
"deb " Chsrc_Maintain_URL "/debian ", codename, "-backports main contrib non-free non-free-firmware\n"
96102
"deb " Chsrc_Maintain_URL "/debian-security ", codename, "-security main contrib non-free non-free-firmware\n");
97-
// 上述 debian-security 这种写法是和 Debian 10不同的,所以我们只能支持 Debian 11+
103+
/* 上述 debian-security 这种写法是和 Debian 10不同的,所以我们只能支持 Debian 11+ */
98104
}
99-
else if (version >= 11)
105+
else if (version >= 11) /* bullseye */
100106
{
101107
makeup = xy_strjoin (9,
102108
"# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n"
@@ -105,15 +111,15 @@ ensure_apt_sourcelist (int debian_type)
105111
"deb " Chsrc_Maintain_URL "/debian ", codename, "-backports main contrib non-free\n"
106112
"deb " Chsrc_Maintain_URL "/debian-security ", codename, "-security main contrib non-free\n");
107113
}
108-
else if (version >= 10)
114+
else if (version >= 10) /* buster */
109115
{
110116
makeup = xy_strjoin (9,
111117
"# Generated by chsrc " Chsrc_Version "(" Chsrc_Maintain_URL ")\n\n"
112118
"deb " Chsrc_Maintain_URL "/debian ", codename, " main contrib non-free\n"
113119
"deb " Chsrc_Maintain_URL "/debian ", codename, "-updates main contrib non-free\n"
114120
"deb " Chsrc_Maintain_URL "/debian ", codename, "-backports main contrib non-free\n"
115121
"deb " Chsrc_Maintain_URL "/debian-security ", codename, "/updates main contrib non-free\n");
116-
// 上述 debian-security 这种写法是和 Debian 11 不同的
122+
/* 上述 debian-security 这种写法是和 Debian 11 不同的 */
117123
}
118124
else
119125
{

0 commit comments

Comments
 (0)