base-files, uci: properly revert list state on config_load This fixes a bug where...
[openwrt/openwrt.git] / package / uci / files / lib / config / uci.sh
1 #!/bin/sh
2 # Shell script compatibility wrappers for /sbin/uci
3 #
4 # Copyright (C) 2008-2010 OpenWrt.org
5 # Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 CONFIG_APPEND=
22 uci_load() {
23 local PACKAGE="$1"
24 local DATA
25 local RET
26 local VAR
27
28 _C=0
29 if [ -z "$CONFIG_APPEND" ]; then
30 for VAR in $CONFIG_LIST_STATE; do
31 export ${NO_EXPORT:+-n} CONFIG_${VAR}=
32 export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
33 done
34 export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
35 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
36 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
37 export ${NO_EXPORT:+-n} CONFIG_SECTION=
38 fi
39
40 DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
41 RET="$?"
42 [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
43 unset DATA
44
45 ${CONFIG_SECTION:+config_cb}
46 return "$RET"
47 }
48
49 uci_set_default() {
50 local PACKAGE="$1"
51 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q show "$PACKAGE" > /dev/null && return 0
52 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} import "$PACKAGE"
53 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit "$PACKAGE"
54 }
55
56 uci_revert_state() {
57 local PACKAGE="$1"
58 local CONFIG="$2"
59 local OPTION="$3"
60
61 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
62 }
63
64 uci_set_state() {
65 local PACKAGE="$1"
66 local CONFIG="$2"
67 local OPTION="$3"
68 local VALUE="$4"
69
70 [ "$#" = 4 ] || return 0
71 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
72 }
73
74 uci_set() {
75 local PACKAGE="$1"
76 local CONFIG="$2"
77 local OPTION="$3"
78 local VALUE="$4"
79
80 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
81 }
82
83 uci_get_state() {
84 uci_get "$1" "$2" "$3" "$4" "/var/state"
85 }
86
87 uci_get() {
88 local PACKAGE="$1"
89 local CONFIG="$2"
90 local OPTION="$3"
91 local DEFAULT="$4"
92 local STATE="$5"
93
94 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${STATE:+-P $STATE} -q get "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
95 RET="$?"
96 [ "$RET" -ne 0 ] && [ -n "$DEFAULT" ] && echo "$DEFAULT"
97 return "$RET"
98 }
99
100 uci_add() {
101 local PACKAGE="$1"
102 local TYPE="$2"
103 local CONFIG="$3"
104
105 if [ -z "$CONFIG" ]; then
106 export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
107 else
108 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG=$TYPE"
109 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
110 fi
111 }
112
113 uci_rename() {
114 local PACKAGE="$1"
115 local CONFIG="$2"
116 local VALUE="$3"
117
118 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} rename "$PACKAGE.$CONFIG=$VALUE"
119 }
120
121 uci_remove() {
122 local PACKAGE="$1"
123 local CONFIG="$2"
124 local OPTION="$3"
125
126 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
127 }
128
129 uci_commit() {
130 local PACKAGE="$1"
131 /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit $PACKAGE
132 }