map-controller: uci-defaults: Assign unique ID to qos_rule sections

This commit is contained in:
Amin Ben Romdhane 2025-12-01 11:22:57 +01:00
parent d5044df134
commit a3e4a0f6e9
2 changed files with 66 additions and 2 deletions

View file

@ -0,0 +1,66 @@
#!/bin/sh
. /lib/functions.sh
cfg="mapcontroller"
config_load "$cfg"
used_ids=""
collect_used_ids() {
local section="$1"
local id
id=$(uci -q get ${cfg}.${section}.id)
if [ -n "$id" ] && printf "%s" "$id" | grep -qE '^[0-9]+$'; then
used_ids="$used_ids $id"
fi
}
# Find first available ID from 0 to INT32_MAX
find_first_available_id() {
local max_int=2147483647
local expected=0
local id
# Convert list to sorted unique list
sorted_ids=$(printf "%s\n" $used_ids | sort -n | uniq)
for id in $sorted_ids; do
if [ "$id" -eq "$expected" ]; then
expected=$((expected + 1))
elif [ "$id" -gt "$expected" ]; then
# Found a gap -> return the gap
echo "$expected"
return
fi
done
# If no gaps, next available is `expected`
if [ "$expected" -le "$max_int" ]; then
echo "$expected"
else
echo -1
fi
}
# Assign ID if missing
add_qos_rule_id() {
local section="$1"
local id
id=$(uci -q get ${cfg}.${section}.id)
if [ -z "$id" ]; then
new_id=$(find_first_available_id)
[ "$new_id" -ge 0 ] || return # No available ID
uci -q set ${cfg}.${section}.id="$new_id"
used_ids="$used_ids $new_id"
fi
}
# Step 1: Collect all existing IDs
config_foreach collect_used_ids qos_rule
# Step 2: Assign IDs to rules missing them
config_foreach add_qos_rule_id qos_rule

View file

@ -14,5 +14,3 @@ for sec in $sections; do
uci rename $cfg.$s=$sec uci rename $cfg.$s=$sec
done done
uci commit $cfg