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 os = require "os"
27 local uci = require "uci"
28 local util = require "luci.util"
29 local table = require "table"
32 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
33 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
34 local require, getmetatable = require, getmetatable
36 --- LuCI UCI model library.
38 module "luci.model.uci"
40 --- Create a new UCI-Cursor.
46 APIVERSION = uci.APIVERSION
48 --- Create a new Cursor initialized to the state directory.
50 function cursor_state()
51 return cursor(nil, "/var/state")
55 local Cursor = getmetatable(cursor())
57 --- Applies the new config
58 -- @param config UCI config
59 function Cursor.apply(self, config)
60 local conf = require "luci.config"
61 return conf.uci_oncommit[config] and
62 os.execute(conf.uci_oncommit[config] .. " >/dev/null 2>&1")
65 --- Delete all sections of a given type that match certain criteria.
66 -- @param config UCI config
67 -- @param type UCI section type
68 -- @param comparator Function that will be called for each section and
69 -- returns a boolean whether to delete the current section (optional)
70 function Cursor.delete_all(self, config, type, comparator)
72 local function helper (section)
73 if not comparator or comparator(section) then
74 table.insert(del, section[".name"])
78 self:foreach(config, type, helper)
80 for i, j in ipairs(del) do
81 self:delete(config, j)
85 --- Create a new section and initialize it with data.
86 -- @param config UCI config
87 -- @param type UCI section type
88 -- @param name UCI section name (optional)
89 -- @param values Table of key - value pairs to initialize the section with
90 -- @return Name of created section
91 function Cursor.section(self, config, type, name, values)
94 stat = self:set(config, name, type)
96 name = self:add(config, type)
100 if stat and values then
101 stat = self:tset(config, name, values)
107 --- Updated the data of a section using data from a table.
108 -- @param config UCI config
109 -- @param section UCI section name (optional)
110 -- @param values Table of key - value pairs to update the section with
111 function Cursor.tset(self, config, section, values)
113 for k, v in pairs(values) do
114 if k:sub(1, 1) ~= "." then
115 stat = stat and self:set(config, section, k, v)
121 --- Get an option or list and return values as table.
122 -- @param config UCI config
123 -- @param section UCI section name
124 -- @param option UCI option
126 function Cursor.get_list(self, config, section, option)
127 if config and section and option then
128 local val = self:get(config, section, option)
129 return ( type(val) == "table" and val or { val } )
134 --- Set given values as list.
135 -- @param config UCI config
136 -- @param section UCI section name
137 -- @param option UCI option
138 -- @param value UCI value
139 -- @return Boolean whether operation succeeded
140 function Cursor.set_list(self, config, section, option, value)
141 if config and section and option then
143 config, section, option,
144 ( type(value) == "table" and value or { value } )
151 Cursor._changes = Cursor.changes
152 function Cursor.changes(self, config)
154 return Cursor._changes(self, config)
156 local changes = Cursor._changes(self)
157 util.copcall(function()
158 for k,v in pairs(require "luci.fs".dir(self:get_savedir())) do
159 if v ~= "." and v ~= ".." then
160 util.update(changes, Cursor._changes(self, v))
169 --- Add an anonymous section.
172 -- @param config UCI config
173 -- @param type UCI section type
174 -- @return Name of created section
176 --- Get a table of unsaved changes.
178 -- @name Cursor.changes
179 -- @param config UCI config
180 -- @return Table of changes
182 --- Commit unsaved changes.
184 -- @name Cursor.commit
185 -- @param config UCI config
186 -- @return Boolean whether operation succeeded
187 -- @see Cursor.revert
189 --- Deletes a section or an option.
191 -- @name Cursor.delete
192 -- @param config UCI config
193 -- @param section UCI section name
194 -- @param option UCI option (optional)
195 -- @return Boolean whether operation succeeded
197 --- Call a function for every section of a certain type.
199 -- @name Cursor.foreach
200 -- @param config UCI config
201 -- @param type UCI section type
202 -- @param callback Function to be called
203 -- @return Boolean whether operation succeeded
205 --- Get a section type or an option
208 -- @param config UCI config
209 -- @param section UCI section name
210 -- @param option UCI option (optional)
213 --- Get all sections of a config or all values of a section.
215 -- @name Cursor.get_all
216 -- @param config UCI config
217 -- @param section UCI section name (optional)
218 -- @return Table of UCI sections or table of UCI values
220 --- Manually load a config.
223 -- @param config UCI config
224 -- @return Boolean whether operation succeeded
226 -- @see Cursor.unload
228 --- Revert unsaved changes.
230 -- @name Cursor.revert
231 -- @param config UCI config
232 -- @return Boolean whether operation succeeded
233 -- @see Cursor.commit
235 --- Saves changes made to a config to make them committable.
238 -- @param config UCI config
239 -- @return Boolean whether operation succeeded
241 -- @see Cursor.unload
243 --- Set a value or create a named section.
246 -- @param config UCI config
247 -- @param section UCI section name
248 -- @param option UCI option or UCI section type
249 -- @param value UCI value or nil if you want to create a section
250 -- @return Boolean whether operation succeeded
252 --- Get the configuration directory.
254 -- @name Cursor.get_confdir
255 -- @return Configuration directory
257 --- Get the directory for uncomitted changes.
259 -- @name Cursor.get_savedir
260 -- @return Save directory
262 --- Set the configuration directory.
264 -- @name Cursor.set_confdir
265 -- @param directory UCI configuration directory
266 -- @return Boolean whether operation succeeded
268 --- Set the directory for uncommited changes.
270 -- @name Cursor.set_savedir
271 -- @param directory UCI changes directory
272 -- @return Boolean whether operation succeeded
274 --- Discard changes made to a config.
276 -- @name Cursor.unload
277 -- @param config UCI config
278 -- @return Boolean whether operation succeeded