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 table = require "table"
30 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
31 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
32 local require = require
34 --- LuCI UCI model library.
35 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
37 savedir_default = "/tmp/.uci"
38 confdir_default = "/etc/config"
40 savedir_state = "/var/state"
43 --- Applies the new config
44 -- @param config UCI config
45 function apply(config)
46 local conf = require "luci.config"
47 return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
50 --- Delete all sections of a given type that match certain criteria.
51 -- @param config UCI config
52 -- @param type UCI section type
53 -- @param comparator Function that will be called for each section and
54 -- returns a boolean whether to delete the current section (optional)
55 function delete_all(config, type, comparator)
57 local function helper (section)
58 if not comparator or comparator(section) then
59 table.insert(del, section[".name"])
63 foreach(config, type, helper)
65 for i, j in ipairs(del) do
70 --- Create a new section and initialize it with data.
71 -- @param config UCI config
72 -- @param type UCI section type
73 -- @param name UCI section name (optional)
74 -- @param values Table of key - value pairs to initialize the section with
75 -- @return Name of created section
76 function section(config, type, name, values)
79 stat = set(config, name, type)
81 name = add(config, type)
85 if stat and values then
86 stat = tset(config, name, values)
92 --- Savely load the configuration.
93 -- @param config Configuration to load
94 -- @return Sucess status
97 function load_config(...)
98 set_confdir(confdir_default)
99 set_savedir(savedir_default)
103 --- Savely load state values.
104 -- @param config Configuration to load
105 -- @return Sucess status
108 function load_state(config)
109 set_confdir(confdir_default)
110 set_savedir(savedir_state)
114 --- Save changes to config values.
115 -- @param config Configuration to save
116 -- @return Sucess status
119 function save_config(config)
120 set_savedir(savedir_default)
124 --- Save changes to state values.
125 -- @param config Configuration to save
126 -- @return Sucess status
129 function save_state(config)
130 set_savedir(savedir_state)
134 --- Updated the data of a section using data from a table.
135 -- @param config UCI config
136 -- @param section UCI section name (optional)
137 -- @param values Table of key - value pairs to update the section with
138 function tset(config, section, values)
140 for k, v in pairs(values) do
141 if k:sub(1, 1) ~= "." then
142 stat = stat and set(config, section, k, v)
148 --- Get an option or list and return values as table.
149 -- @param config UCI config
150 -- @param section UCI section name
151 -- @param option UCI option
153 function get_list(config, section, option)
154 if config and section and option then
155 local val = get(config, section, option)
156 return ( type(val) == "table" and val or { val } )
161 --- Set given values as list.
162 -- @param config UCI config
163 -- @param section UCI section name
164 -- @param option UCI option
165 -- @param value UCI value
166 -- @return Boolean whether operation succeeded
167 function set_list(config, section, option, value)
168 if config and section and option then
170 config, section, option,
171 ( type(value) == "table" and value or { value } )
178 --- Add an anonymous section.
181 -- @param config UCI config
182 -- @param type UCI section type
183 -- @return Name of created section
185 --- Get a table of unsaved changes.
188 -- @param config UCI config
189 -- @return Table of changes
191 --- Commit unsaved changes.
194 -- @param config UCI config
195 -- @return Boolean whether operation succeeded
198 --- Deletes a section or an option.
201 -- @param config UCI config
202 -- @param section UCI section name
203 -- @param option UCI option (optional)
204 -- @return Boolean whether operation succeeded
206 --- Call a function for every section of a certain type.
209 -- @param config UCI config
210 -- @param type UCI section type
211 -- @param callback Function to be called
212 -- @return Boolean whether operation succeeded
214 --- Get a section type or an option
217 -- @param config UCI config
218 -- @param section UCI section name
219 -- @param option UCI option (optional)
222 --- Get all sections of a config or all values of a section.
225 -- @param config UCI config
226 -- @param section UCI section name (optional)
227 -- @return Table of UCI sections or table of UCI values
229 --- Manually load a config.
230 -- Warning: This function is unsave! You should use load_config or load_state if possible.
233 -- @param config UCI config
234 -- @return Boolean whether operation succeeded
240 --- Revert unsaved changes.
243 -- @param config UCI config
244 -- @return Boolean whether operation succeeded
247 --- Saves changes made to a config to make them committable.
250 -- @param config UCI config
251 -- @return Boolean whether operation succeeded
255 --- Set a value or create a named section.
258 -- @param config UCI config
259 -- @param section UCI section name
260 -- @param option UCI option or UCI section type
261 -- @param value UCI value or nil if you want to create a section
262 -- @return Boolean whether operation succeeded
264 --- Set the configuration directory.
267 -- @param directory UCI configuration directory
268 -- @return Boolean whether operation succeeded
270 --- Set the directory for uncommited changes.
273 -- @param directory UCI changes directory
274 -- @return Boolean whether operation succeeded
276 --- Discard changes made to a config.
279 -- @param config UCI config
280 -- @return Boolean whether operation succeeded