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 "uci2"
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, getmetatable = require, getmetatable
34 --- LuCI UCI model library.
35 module("luci.model.uci")
38 APIVERSION = uci.APIVERSION
40 --- Creates a new statevalue cursor
42 function cursor_state()
43 return cursor(nil, "/var/state")
47 local Cursor = getmetatable(cursor())
49 --- Applies the new config
50 -- @param config UCI config
51 function Cursor.apply(self, config)
52 local conf = require "luci.config"
53 return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
56 --- Delete all sections of a given type that match certain criteria.
57 -- @param config UCI config
58 -- @param type UCI section type
59 -- @param comparator Function that will be called for each section and
60 -- returns a boolean whether to delete the current section (optional)
61 function Cursor.delete_all(self, config, type, comparator)
63 local function helper (section)
64 if not comparator or comparator(section) then
65 table.insert(del, section[".name"])
69 self:foreach(config, type, helper)
71 for i, j in ipairs(del) do
72 self:delete(config, j)
76 --- Create a new section and initialize it with data.
77 -- @param config UCI config
78 -- @param type UCI section type
79 -- @param name UCI section name (optional)
80 -- @param values Table of key - value pairs to initialize the section with
81 -- @return Name of created section
82 function Cursor.section(self, config, type, name, values)
85 stat = self:set(config, name, type)
87 name = self:add(config, type)
91 if stat and values then
92 stat = self:tset(config, name, values)
98 --- Updated the data of a section using data from a table.
99 -- @param config UCI config
100 -- @param section UCI section name (optional)
101 -- @param values Table of key - value pairs to update the section with
102 function Cursor.tset(self, config, section, values)
104 for k, v in pairs(values) do
105 if k:sub(1, 1) ~= "." then
106 stat = stat and self:set(config, section, k, v)
112 --- Get an option or list and return values as table.
113 -- @param config UCI config
114 -- @param section UCI section name
115 -- @param option UCI option
117 function Cursor.get_list(self, config, section, option)
118 if config and section and option then
119 local val = self:get(config, section, option)
120 return ( type(val) == "table" and val or { val } )
125 --- Set given values as list.
126 -- @param config UCI config
127 -- @param section UCI section name
128 -- @param option UCI option
129 -- @param value UCI value
130 -- @return Boolean whether operation succeeded
131 function Cursor.set_list(self, config, section, option, value)
132 if config and section and option then
134 config, section, option,
135 ( type(value) == "table" and value or { value } )
142 --- Add an anonymous section.
145 -- @param config UCI config
146 -- @param type UCI section type
147 -- @return Name of created section
149 --- Get a table of unsaved changes.
151 -- @name Cursor.changes
152 -- @param config UCI config
153 -- @return Table of changes
155 --- Commit unsaved changes.
157 -- @name Cursor.commit
158 -- @param config UCI config
159 -- @return Boolean whether operation succeeded
162 --- Deletes a section or an option.
164 -- @name Cursor.delete
165 -- @param config UCI config
166 -- @param section UCI section name
167 -- @param option UCI option (optional)
168 -- @return Boolean whether operation succeeded
170 --- Call a function for every section of a certain type.
172 -- @name Cursor.foreach
173 -- @param config UCI config
174 -- @param type UCI section type
175 -- @param callback Function to be called
176 -- @return Boolean whether operation succeeded
178 --- Get a section type or an option
181 -- @param config UCI config
182 -- @param section UCI section name
183 -- @param option UCI option (optional)
186 --- Get all sections of a config or all values of a section.
188 -- @name Cursor.get_all
189 -- @param config UCI config
190 -- @param section UCI section name (optional)
191 -- @return Table of UCI sections or table of UCI values
193 --- Manually load a config.
194 -- Warning: This function is unsave! You should use load_config or load_state if possible.
197 -- @param config UCI config
198 -- @return Boolean whether operation succeeded
204 --- Revert unsaved changes.
206 -- @name Cursor.revert
207 -- @param config UCI config
208 -- @return Boolean whether operation succeeded
211 --- Saves changes made to a config to make them committable.
214 -- @param config UCI config
215 -- @return Boolean whether operation succeeded
219 --- Set a value or create a named section.
222 -- @param config UCI config
223 -- @param section UCI section name
224 -- @param option UCI option or UCI section type
225 -- @param value UCI value or nil if you want to create a section
226 -- @return Boolean whether operation succeeded
228 --- Get the configuration directory.
230 -- @name Cursor.get_confdir
231 -- @return Configuration directory
233 --- Get the directory for uncomitted changes.
235 -- @name Cursor.get_savedir
236 -- @return Save directory
238 --- Set the configuration directory.
240 -- @name Cursor.set_confdir
241 -- @param directory UCI configuration directory
242 -- @return Boolean whether operation succeeded
244 --- Set the directory for uncommited changes.
246 -- @name Cursor.set_savedir
247 -- @param directory UCI changes directory
248 -- @return Boolean whether operation succeeded
250 --- Discard changes made to a config.
252 -- @name Cursor.unload
253 -- @param config UCI config
254 -- @return Boolean whether operation succeeded