3535
3636static int chsrc_get_cpucore ();
3737
38+ /* 定义于 dish.c,core.c 在 combo 源解析时需要提前声明 */
39+ bool dish_has_source_from_mirror (Dish_t * dish , const char * code );
40+
3841
3942/* Global Program Mode */
4043struct
4346 bool MeasureMode ;
4447 bool ResetMode ;
4548
46- // 内部实现
47- bool DishGroupMode ;
48-
4949 // 用户命令选项
5050 bool Ipv6Mode ;
5151 Scope_t Scope ;
@@ -57,7 +57,6 @@ ProgMode =
5757{
5858 .MeasureMode = false,
5959 .ResetMode = false,
60- .DishGroupMode = false,
6160 .Ipv6Mode = false,
6261 .Scope = ImplementationDefinedScope ,
6362 .EnglishMode = false,
@@ -66,10 +65,9 @@ ProgMode =
6665};
6766
6867/* recipe 相关 mode */
69- bool chsrc_in_dish_group_mode () {return ProgMode .DishGroupMode ;}
70- // 并非作为 sub dish,而是自身作为一个独立的 dish 执行
71- bool chsrc_in_standalone_mode () {return !ProgMode .DishGroupMode ;}
72- void chsrc_set_dish_group_mode (){ProgMode .DishGroupMode = true;}
68+ // combo dish 递归不再复用旧的 DishGroupMode;每个 leaf dish 仍负责自己的
69+ // 确认和收尾输出。
70+ bool chsrc_in_standalone_mode () {return true;}
7371
7472bool chsrc_in_reset_mode (){return ProgMode .ResetMode ;}
7573/* 默认换源作用域就是 ImplementationDefinedScope */
@@ -113,35 +111,31 @@ typedef enum ChgType_t
113111 ChgType_Untested
114112} ChgType_t ;
115113
114+ #define MaxComboStackDepth 16
116115
117116/* Global Program Status */
118117struct
119118{
120- int leader_selected_index ; /* combo dish 选中的索引 */
121- ChgType_t chgtype ; /* 换源实现的类型 */
119+ ChgType_t chgtype ; /* 换源实现的类型 */
122120
123121 /* 此时 chsrc_run() 不再是recipe中指定要运行的一个外部命令,而是作为一个功能实现的支撑 */
124122 bool chsrc_run_faas ;
125123 char * user_agent ;
124+
125+ /* combo dish 源解析上下文。用户只给一个 code/URL,但不同 sub dish 可能
126+ * 使用不同的源列表;该栈记录当前正在处理的 combo dish。 */
127+ Dish_t * ComboStack [MaxComboStackDepth ];
128+ XySeq_t * ComboBackedUpPaths [MaxComboStackDepth ];
129+ int ComboStackDepth ;
126130}
127131ProgStatus =
128132{
129- .leader_selected_index = -1 ,
130133 .chgtype = ChgType_Auto ,
131134 .chsrc_run_faas = false,
132135 .user_agent = "chsrc/" Chsrc_Version ,
136+ .ComboStackDepth = 0 ,
133137};
134138
135- /* Combo dish source resolution context.
136- * When a combo dish is being handled, each sub dish may use a different
137- * source list, but the user only supplied one mirror code. This context lets
138- * the framework decide whether a sub dish should use the code directly, or
139- * auto-select its fastest mirror because the code belongs to a sibling. */
140- #define MaxComboSourceResolutionDepth 16
141- static Dish_t * ComboSourceResolutionDishStack [MaxComboSourceResolutionDepth ];
142- static char * ComboBackedUpPaths [MaxComboSourceResolutionDepth ];
143- static int ComboSourceResolutionDepth = 0 ;
144-
145139
146140/* Global Program Store */
147141struct
@@ -186,27 +180,28 @@ ProgStore =
186180bool
187181chsrc_in_combo_source_resolution (void )
188182{
189- return ComboSourceResolutionDepth > 0 ;
183+ return ProgStatus . ComboStackDepth > 0 ;
190184}
191185
192186void
193- chsrc_begin_combo_source_resolution (Dish_t * dish , char * option )
187+ chsrc_begin_combo_source_resolution (Dish_t * dish )
194188{
195- if (ComboSourceResolutionDepth >= MaxComboSourceResolutionDepth )
189+ if (ProgStatus . ComboStackDepth >= MaxComboStackDepth )
196190 chsrc_breakdown ("combo dish 嵌套层级过深" );
197191
198- ComboSourceResolutionDishStack [ComboSourceResolutionDepth ] = dish ;
199- ComboBackedUpPaths [ComboSourceResolutionDepth ] = NULL ;
200- ComboSourceResolutionDepth ++ ;
192+ int depth = ProgStatus .ComboStackDepth ;
193+ ProgStatus .ComboStack [depth ] = dish ;
194+ ProgStatus .ComboBackedUpPaths [depth ] = xy_seq_new ();
195+ ProgStatus .ComboStackDepth ++ ;
201196}
202197
203198void
204199chsrc_end_combo_source_resolution (void )
205200{
206- if (ComboSourceResolutionDepth <= 0 )
201+ if (ProgStatus . ComboStackDepth <= 0 )
207202 chsrc_breakdown ("combo dish 解析上下文已为空" );
208203
209- ComboSourceResolutionDepth -- ;
204+ ProgStatus . ComboStackDepth -- ;
210205}
211206
212207#define faint (str ) xy_str2faint(str)
@@ -1074,27 +1069,12 @@ source_has_empty_url (Source_t *source)
10741069}
10751070
10761071static bool
1077- combo_subdish_has_source_code (Dish_t * dish , const char * code )
1072+ subdish_sibling_has_source_from_mirror (Dish_t * current , const char * code )
10781073{
1079- if (! dish || ! dish -> sources || ! code )
1074+ if (ProgStatus . ComboStackDepth <= 0 )
10801075 return false;
10811076
1082- for (int i = 0 ; i < dish -> sources_n ; i ++ )
1083- {
1084- if (xy_streql (dish -> sources [i ].mirror -> code , code ))
1085- return true;
1086- }
1087-
1088- return false;
1089- }
1090-
1091- static bool
1092- combo_sibling_has_source_code (Dish_t * current , const char * code )
1093- {
1094- if (ComboSourceResolutionDepth <= 0 )
1095- return false;
1096-
1097- Dish_t * combo = ComboSourceResolutionDishStack [ComboSourceResolutionDepth - 1 ];
1077+ Dish_t * combo = ProgStatus .ComboStack [ProgStatus .ComboStackDepth - 1 ];
10981078 if (!combo || !combo -> sub_dishes )
10991079 return false;
11001080
@@ -1107,7 +1087,7 @@ combo_sibling_has_source_code (Dish_t *current, const char *code)
11071087 if (!sub_dish -> inited )
11081088 sub_dish -> preparefn ();
11091089
1110- if (combo_subdish_has_source_code (sub_dish , code ))
1090+ if (dish_has_source_from_mirror (sub_dish , code ))
11111091 return true;
11121092 }
11131093
@@ -1124,13 +1104,16 @@ combo_sibling_has_source_code (Dish_t *current, const char *code)
11241104int
11251105chsrc_resolve_combo_subdish_source (Dish_t * dish , char * option )
11261106{
1127- if (combo_subdish_has_source_code (dish , option ))
1107+ if (dish_has_source_from_mirror (dish , option ))
11281108 return query_mirror_exist (dish -> sources , dish -> sources_n , dish -> aliases , option );
11291109
1130- if (combo_sibling_has_source_code (dish , option ))
1110+ if (subdish_sibling_has_source_from_mirror (dish , option ))
11311111 return auto_select_mirror (dish -> sources , dish -> sources_n , dish -> aliases );
11321112
1133- return query_mirror_exist (dish -> sources , dish -> sources_n , dish -> aliases , option );
1113+ chsrc_breakdown (ENGLISH
1114+ ? "This combo sub dish has no source from the requested mirror"
1115+ : "该 combo sub dish 没有所请求镜像站的源" );
1116+ return 0 ;
11341117}
11351118
11361119
@@ -1145,10 +1128,9 @@ chsrc_resolve_combo_subdish_source (Dish_t *dish, char *option)
11451128 * 3. 用户什么都没指定, 即 chsrc set <dish>
11461129 * 4. 用户正在重置源, 即 chsrc reset <dish>
11471130 *
1148- * 如果处于 Dish Group 模式下,combo dish 已经测速过了,sub dish
1149- * 不能再次测速,而是直接选择 combo dish 测过的结果
1150- *
1151- * 5. combo dish 测速出来的某个源
1131+ * 如果处于 combo source resolution 上下文中,则按 sub dish 与 sibling
1132+ * 的源列表集中决定:code 命中本 dish 则直接使用;命中 sibling 则本 dish
1133+ * 自动测速;传入 URL 时,支持自定义 URL 的 sub dish 使用 URL,其余自动测速。
11521134 *
11531135 */
11541136Source_t
@@ -1163,19 +1145,29 @@ chsrc_yield_source (Dish_t *dish, char *option)
11631145 if (!dish -> inited ) dish -> preparefn ();
11641146
11651147 Source_t source ;
1166- if (chsrc_in_combo_source_resolution () && option && !hp_is_url (option ))
1167- {
1168- int index = chsrc_resolve_combo_subdish_source (dish , option );
1169- source = dish -> sources [index ];
1170- }
1171- else if (chsrc_in_dish_group_mode () && ProgStatus .leader_selected_index == -1 )
1148+ if (chsrc_in_combo_source_resolution () && option )
11721149 {
1173- ProgStatus .leader_selected_index = use_specific_mirror_or_auto_select (option , dish );
1174- source = dish -> sources [ProgStatus .leader_selected_index ];
1175- }
1176- else if (chsrc_in_dish_group_mode () && ProgStatus .leader_selected_index != -1 )
1177- {
1178- source = dish -> sources [ProgStatus .leader_selected_index ];
1150+ if (hp_is_url (option ))
1151+ {
1152+ /* combo dish 传入 URL 时,支持该 sub dish 自定义 URL 的则直接使用;
1153+ * 不支持则自动选择该 sub dish 的最快源。这样 uv 可只把 URL 当作
1154+ * PyPI index,而 python-install-mirror 仍自动选择。 */
1155+ if (dish -> can_user_define )
1156+ {
1157+ Source_t tmp = { & UserDefinedProvider , option };
1158+ source = tmp ;
1159+ }
1160+ else
1161+ {
1162+ int index = auto_select_mirror (dish -> sources , dish -> sources_n , dish -> aliases );
1163+ source = dish -> sources [index ];
1164+ }
1165+ }
1166+ else
1167+ {
1168+ int index = chsrc_resolve_combo_subdish_source (dish , option );
1169+ source = dish -> sources [index ];
1170+ }
11791171 }
11801172 else if (hp_is_url (option ))
11811173 {
@@ -2075,11 +2067,16 @@ chsrc_backup (const char *path)
20752067 * 已经保留原始内容后,后续 sub dish 不应再覆盖该备份。 */
20762068 if (chsrc_in_combo_source_resolution ())
20772069 {
2078- int depth = ComboSourceResolutionDepth - 1 ;
2079- if (ComboBackedUpPaths [depth ] && xy_streql (ComboBackedUpPaths [depth ], path ))
2080- goto log_anyway ;
2070+ int depth = ProgStatus .ComboStackDepth - 1 ;
2071+ XySeq_t * backed_up_paths = ProgStatus .ComboBackedUpPaths [depth ];
2072+
2073+ for (size_t i = 0 ; i < xy_seq_len (backed_up_paths ); i ++ )
2074+ {
2075+ if (xy_streql (xy_seq_at (backed_up_paths , i ), path ))
2076+ goto log_anyway ;
2077+ }
20812078
2082- ComboBackedUpPaths [ depth ] = xy_strdup (path );
2079+ xy_seq_push ( backed_up_paths , xy_strdup (path ) );
20832080 }
20842081
20852082 if (xy .on_bsd || xy .on_macos )
0 commit comments