Skip to content

Commit 129018f

Browse files
committed
fix: use /etc/os-release choose os
1 parent 92af471 commit 129018f

3 files changed

Lines changed: 117 additions & 49 deletions

File tree

src/usr/lib/rsetup/cli/system.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ set_radxa_mirror() {
425425
change_sources_set_radxa_mirror "$mirror"
426426
}
427427

428-
set_debian_mirror() {
428+
set_debian_ubuntu_mirror() {
429429
__parameter_count_check 1 "$@"
430430
local mirror="$1"
431431
change_sources_set_debian_mirror "$mirror"

src/usr/lib/rsetup/mod/change_sources.sh

Lines changed: 97 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# Output: One URL per line, first is official source, rest are mirrors
1010
change_sources_list_radxa_urls() {
1111
local urls=(
12-
"https://radxa-repo.github.io"
1312
"https://mirrors.aghost.cn/radxa-deb"
1413
"https://mirrors.lzu.edu.cn/radxa-deb"
1514
"https://mirrors.hust.edu.cn/radxa-deb"
@@ -35,41 +34,89 @@ change_sources_list_debian_urls() {
3534
printf '%s\n' "${urls[@]}"
3635
}
3736

38-
# Check if target value exists in list
39-
# Args: $1 - target value, $@ - list
40-
# Returns: 0 if found, 1 if not found
41-
__change_sources_contains() {
42-
local target="$1" item
43-
shift
44-
for item in "$@"; do
45-
[[ "$target" == "$item" ]] && return 0
46-
done
47-
return 1
48-
}
49-
5037
# Get all Radxa APT source configuration file paths
5138
# Output: One file path per line
5239
__change_sources_get_radxa_lists() {
5340
shopt -s nullglob
5441
local files=(/etc/apt/sources.list.d/*radxa*.list)
5542
shopt -u nullglob
56-
43+
5744
(( ${#files[@]} == 0 )) && return 0
58-
45+
5946
printf '%s\n' "${files[@]}"
6047
}
6148

49+
# Get current Radxa mirror URL
50+
# Output: Current mirror URL if it's in known list, or empty otherwise
51+
change_sources_get_current_radxa_mirror() {
52+
local file url
53+
while IFS= read -r file; do
54+
[[ -n "$file" && -f "$file" ]] || continue
55+
56+
# Extract URL from deb/deb-src lines
57+
url="$(grep -E '^[[:space:]]*(deb|deb-src)' "$file" | head -n1 | awk '{print $2}')"
58+
if [[ -n "$url" ]]; then
59+
# Validate URL is in known mirror list (including official source)
60+
local known_mirrors=("https://radxa-repo.github.io")
61+
local mirror_list=()
62+
mapfile -t mirror_list < <(change_sources_list_radxa_urls)
63+
known_mirrors+=("${mirror_list[@]}")
64+
65+
if __in_array "$url" "${known_mirrors[@]}"; then
66+
echo "$url"
67+
return 0
68+
fi
69+
fi
70+
done < <(__change_sources_get_radxa_lists)
71+
}
72+
73+
# Get current Debian/Ubuntu mirror URL
74+
# Output: Current mirror URL if it's in known list, or empty otherwise
75+
change_sources_get_current_debian_mirror() {
76+
shopt -s nullglob
77+
local lists=(/etc/apt/sources.list /etc/apt/sources.list.d/*.list)
78+
shopt -u nullglob
79+
80+
local file url
81+
for file in "${lists[@]}"; do
82+
[[ -f "$file" ]] || continue
83+
84+
# Skip Radxa source files
85+
[[ "$file" == *radxa*.list ]] && continue
86+
87+
# Extract URL from deb/deb-src lines, get only the base URL (hostname)
88+
url="$(grep -E '^[[:space:]]*(deb|deb-src)' "$file" | head -n1 | awk '{print $2}' | sed 's|^\(https\?://[^/]*\).*|\1|')"
89+
if [[ -n "$url" ]]; then
90+
# Validate URL is in known mirror list (including official sources)
91+
local known_mirrors=(
92+
"https://deb.debian.org"
93+
"https://deb.ubuntu.com"
94+
)
95+
local mirror_list=()
96+
mapfile -t mirror_list < <(change_sources_list_debian_urls)
97+
known_mirrors+=("${mirror_list[@]}")
98+
99+
if __in_array "$url" "${known_mirrors[@]}"; then
100+
echo "$url"
101+
return 0
102+
fi
103+
fi
104+
done
105+
}
106+
62107
# Apply Radxa mirror replacement
63108
# Args: $1 - new mirror URL
64109
__change_sources_apply_radxa_mirror() {
65110
local new_mirror="$1"
66-
local all_mirrors=()
67-
mapfile -t all_mirrors < <(change_sources_list_radxa_urls)
111+
local all_mirrors=("https://radxa-repo.github.io")
112+
local mirror_list=()
113+
mapfile -t mirror_list < <(change_sources_list_radxa_urls)
114+
all_mirrors+=("${mirror_list[@]}")
68115

69116
local file
70117
while IFS= read -r file; do
71118
[[ -n "$file" ]] || continue
72-
119+
73120
# Replace all known Radxa mirrors with new mirror
74121
local old_mirror
75122
for old_mirror in "${all_mirrors[@]}"; do
@@ -86,7 +133,7 @@ change_sources_set_radxa_mirror() {
86133
local all_mirrors=()
87134
mapfile -t all_mirrors < <(change_sources_list_radxa_urls)
88135

89-
if ! __change_sources_contains "$mirror" "${all_mirrors[@]}"; then
136+
if ! __in_array "$mirror" "${all_mirrors[@]}"; then
90137
echo "Unsupported Radxa mirror: $mirror" >&2
91138
return 1
92139
fi
@@ -135,7 +182,7 @@ change_sources_set_debian_mirror() {
135182
local all_mirrors=()
136183
mapfile -t all_mirrors < <(change_sources_list_debian_urls)
137184

138-
if ! __change_sources_contains "$mirror" "${all_mirrors[@]}"; then
185+
if ! __in_array "$mirror" "${all_mirrors[@]}"; then
139186
echo "Unsupported Debian/Ubuntu mirror: $mirror" >&2
140187
return 1
141188
fi
@@ -147,11 +194,31 @@ change_sources_set_debian_mirror() {
147194
# Restore official APT sources
148195
# Restores both Radxa and Debian/Ubuntu sources to official sources
149196
change_sources_restore() {
150-
local radxa_urls=()
151-
mapfile -t radxa_urls < <(change_sources_list_radxa_urls)
152-
153197
# Restore Radxa to official source
154-
__change_sources_apply_radxa_mirror "${radxa_urls[0]}"
198+
__change_sources_apply_radxa_mirror "https://radxa-repo.github.io"
199+
200+
# Determine system type from /etc/os-release
201+
local os_id=""
202+
if [[ -f /etc/os-release ]]; then
203+
# shellcheck disable=SC1091
204+
source /etc/os-release
205+
os_id="$ID"
206+
fi
207+
208+
# Determine official mirror based on OS
209+
local official_mirror
210+
case "$os_id" in
211+
ubuntu)
212+
official_mirror="https://deb.ubuntu.com"
213+
;;
214+
debian)
215+
official_mirror="https://deb.debian.org"
216+
;;
217+
*)
218+
echo "Unable to determine OS type from /etc/os-release" >&2
219+
return 1
220+
;;
221+
esac
155222

156223
# Restore Debian/Ubuntu sources to official sources
157224
local all_mirrors=(
@@ -166,29 +233,18 @@ change_sources_restore() {
166233
local lists=(/etc/apt/sources.list /etc/apt/sources.list.d/*.list)
167234
shopt -u nullglob
168235

169-
local file
236+
local file old_mirror old_mirror_norm
170237
for file in "${lists[@]}"; do
171238
[[ -f "$file" ]] || continue
172239

173240
# Skip Radxa source files
174241
[[ "$file" == *radxa*.list ]] && continue
175242

176-
# Check if file contains Ubuntu or Debian sources and restore accordingly
177-
if grep -q "ubuntu" "$file"; then
178-
# Restore Ubuntu sources
179-
local old_mirror old_mirror_norm
180-
for old_mirror in "${all_mirrors[@]}"; do
181-
old_mirror_norm="${old_mirror%/}"
182-
sed -i "s|$old_mirror_norm|https://deb.ubuntu.com|g" "$file"
183-
done
184-
else
185-
# Restore Debian sources
186-
local old_mirror old_mirror_norm
187-
for old_mirror in "${all_mirrors[@]}"; do
188-
old_mirror_norm="${old_mirror%/}"
189-
sed -i "s|$old_mirror_norm|https://deb.debian.org|g" "$file"
190-
done
191-
fi
243+
# Restore to official mirror based on detected OS
244+
for old_mirror in "${all_mirrors[@]}"; do
245+
old_mirror_norm="${old_mirror%/}"
246+
sed -i "s|$old_mirror_norm|$official_mirror|g" "$file"
247+
done
192248
done
193249

194250
echo "APT sources restored to official sources" >&2

src/usr/lib/rsetup/tui/system/system.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,17 @@ __system_change_sources() {
258258
__system_change_sources_radxa_select() {
259259
radiolist_init
260260

261-
local m selected
261+
local m selected current_mirror
262+
current_mirror="$(change_sources_get_current_radxa_mirror)"
263+
262264
# Radxa mirrors come from mod/change_sources.sh
263265
while IFS= read -r m
264266
do
265-
radiolist_add "$m" "OFF"
267+
if [[ "$m" == "$current_mirror" ]]; then
268+
radiolist_add "$m" "ON"
269+
else
270+
radiolist_add "$m" "OFF"
271+
fi
266272
done < <(change_sources_list_radxa_urls)
267273
radiolist_emptymsg "No Radxa mirror is available."
268274

@@ -273,7 +279,7 @@ __system_change_sources_radxa_select() {
273279

274280
selected="$(radiolist_getitem "${RTUI_RADIOLIST_STATE_NEW[0]}")"
275281

276-
if enable_unique_config set_radxa_mirror "$selected"
282+
if set_radxa_mirror "$selected"
277283
then
278284
msgbox "Radxa sources updated to:\n$selected"
279285
else
@@ -284,11 +290,17 @@ __system_change_sources_radxa_select() {
284290
__system_change_sources_debian_select() {
285291
radiolist_init
286292

287-
local h selected
293+
local h selected current_mirror
294+
current_mirror="$(change_sources_get_current_debian_mirror)"
295+
288296
# Debian/Ubuntu mirrors come from mod/change_sources.sh
289297
while IFS= read -r h
290298
do
291-
radiolist_add "$h" "OFF"
299+
if [[ "$h" == "$current_mirror" ]]; then
300+
radiolist_add "$h" "ON"
301+
else
302+
radiolist_add "$h" "OFF"
303+
fi
292304
done < <(change_sources_list_debian_urls)
293305
radiolist_emptymsg "No Debian/Ubuntu mirror is available."
294306

@@ -299,7 +311,7 @@ __system_change_sources_debian_select() {
299311

300312
selected="$(radiolist_getitem "${RTUI_RADIOLIST_STATE_NEW[0]}")"
301313

302-
if enable_unique_config set_debian_mirror "$selected"
314+
if set_debian_ubuntu_mirror "$selected"
303315
then
304316
msgbox "Debian/Ubuntu sources updated to:\n$selected"
305317
else
@@ -310,7 +322,7 @@ __system_change_sources_debian_select() {
310322
__system_change_sources_restore() {
311323
if yesno "Restore official Radxa and Debian/Ubuntu sources?"
312324
then
313-
if enable_unique_config restore_sources
325+
if restore_sources
314326
then
315327
msgbox "APT sources have been restored to official hosts."
316328
else

0 commit comments

Comments
 (0)