11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 local uci = require("uci")
27 local util = require("luci.util")
28 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
29 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
32 --- LuCI UCI model library.
33 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
35 savedir_default = "/tmp/.uci"
36 confdir_default = "/etc/config"
38 savedir_state = "/var/state"
40 --- Delete all sections of a given type that match certain criteria.
41 -- @param config UCI config
42 -- @param type UCI section type
43 -- @param comparator Function that will be called for each section and
44 -- returns a boolean whether to delete the current section (optional)
45 function delete_all(config, type, comparator)
47 local function helper (section)
48 if not comparator or comparator(section) then
49 table.insert(del, section[".name"])
53 foreach(config, type, helper)
55 for i, j in ipairs(del) do
60 --- Create a new section and initialize it with data.
61 -- @param config UCI config
62 -- @param type UCI section type
63 -- @param name UCI section name (optional)
64 -- @param values Table of key - value pairs to initialize the section with
65 -- @return Name of created section
66 function section(config, type, name, values)
69 stat = set(config, name, type)
71 name = add(config, type)
75 if stat and values then
76 stat = tset(config, name, values)
82 --- Get a certain state value.
83 -- @param ... Parameters passed to function get
86 function get_statevalue(...)
87 set_savedir(savedir_state)
88 local result = get(...)
89 set_savedir(savedir_default)
93 --- Updated the data of a section using data from a table.
94 -- @param config UCI config
95 -- @param section UCI section name (optional)
96 -- @param values Table of key - value pairs to update the section with
97 function tset(config, section, values)
99 for k, v in pairs(values) do
100 if k:sub(1, 1) ~= "." then
101 stat = stat and set(config, section, k, v)
108 --- Add an anonymous section.
111 -- @param config UCI config
112 -- @param type UCI section type
113 -- @return Name of created section
115 --- Get a table of unsaved changes.
118 -- @param config UCI config
119 -- @return Table of changes
121 --- Commit unsaved changes.
124 -- @param config UCI config
125 -- @return Boolean whether operation succeeded
128 --- Deletes a section or an option.
131 -- @param config UCI config
132 -- @param section UCI section name
133 -- @param option UCI option (optional)
134 -- @return Boolean whether operation succeeded
136 --- Call a function for every section of a certain type.
139 -- @param config UCI config
140 -- @param type UCI section type
141 -- @param callback Function to be called
142 -- @return Boolean whether operation succeeded
144 --- Get a section type or an option
147 -- @param config UCI config
148 -- @param section UCI section name
149 -- @param option UCI option (optional)
152 --- Get all sections of a config or all values of a section.
155 -- @param config UCI config
156 -- @param section UCI section name (optional)
157 -- @return Table of UCI sections or table of UCI values
159 --- Manually load a config.
162 -- @param config UCI config
163 -- @return Boolean whether operation succeeded
167 --- Revert unsaved changes.
170 -- @param config UCI config
171 -- @return Boolean whether operation succeeded
174 --- Saves changes made to a config to make them committable.
177 -- @param config UCI config
178 -- @return Boolean whether operation succeeded
182 --- Set a value or create a named section.
185 -- @param config UCI config
186 -- @param section UCI section name
187 -- @param option UCI option or UCI section type
188 -- @param value UCI value or nil if you want to create a section
189 -- @return Boolean whether operation succeeded
191 --- Set the configuration directory.
194 -- @param directory UCI configuration directory
195 -- @return Boolean whether operation succeeded
197 --- Set the directory for uncommited changes.
200 -- @param directory UCI changes directory
201 -- @return Boolean whether operation succeeded
203 --- Discard changes made to a config.
206 -- @param config UCI config
207 -- @return Boolean whether operation succeeded