base-files: uci-defaults mechanism to run code only after settings reset

uci-defaults runs scripts after settings are wiped but also after
sysupgrade. This works fine when existing settings need to be imported,
but is undesirable in situations where the intention is to provide a
settings default, as sysupgrade can potentially override the user's
custom settings back with the defaults. This is sometimes mitigated
in an ad-hoc manner by scripts, and sometimes not at all.

This change provides 2 standardized ways to cope with this situation:

- uci-defaults scripts receive a RESTORING_SETTINGS flag that helps them
  skip over defaults when earlier settings are being restored, eg with:

  [ "$RESTORING_SETTINGS" = "true" ] && exit 0

  (Note: if the script fails, the next invocation will not be marked
  with the RESTORING_SETTINGS flag.)

- uci-defaults scripts whose names end with '_RESET' will never be ran
  when settings are restored and will only run after settings reset,
  regardless of their eventual exit status.

The second option is the recommended way for scripts to set defaults.

Signed-off-by: Rodrigo Balerdi <lanchon@gmail.com>
This commit is contained in:
Rodrigo Balerdi 2024-04-30 10:33:46 -03:00
parent 47da1786e9
commit 8f2f0121dc

View File

@ -4,10 +4,18 @@
START=10
STOP=90
is_restoring_settings() {
[ -f /sysupgrade.tgz -o -f /tmp/sysupgrade.tar ]
}
uci_apply_defaults() {
. /lib/functions/system.sh
cd /etc/uci-defaults || return 0
is_restoring_settings && {
RESTORING_SETTINGS=true
rm -f *_RESET
}
files="$(ls)"
[ -z "$files" ] && return 0
mkdir -p /tmp/.uci