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.
37 module("luci.model.uci")
39 --- Create a new UCI-Cursor.
45 APIVERSION = uci.APIVERSION
47 --- Create a new Cursor initialized to the state directory.
49 function cursor_state()
50 return cursor(nil, "/var/state")
56 -- @name luci.model.uci.Cursor
57 local Cursor = getmetatable(cursor())
59 --- Applies the new config
60 -- @param config UCI config
61 function Cursor.apply(self, config)
62 local conf = require "luci.config"
63 return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
66 --- Delete all sections of a given type that match certain criteria.
67 -- @param config UCI config
68 -- @param type UCI section type
69 -- @param comparator Function that will be called for each section and
70 -- returns a boolean whether to delete the current section (optional)
71 function Cursor.delete_all(self, config, type, comparator)
73 local function helper (section)
74 if not comparator or comparator(section) then
75 table.insert(del, section[".name"])
79 self:foreach(config, type, helper)
81 for i, j in ipairs(del) do
82 self:delete(config, j)
86 --- Create a new section and initialize it with data.
87 -- @param config UCI config
88 -- @param type UCI section type
89 -- @param name UCI section name (optional)
90 -- @param values Table of key - value pairs to initialize the section with
91 -- @return Name of created section
92 function Cursor.section(self, config, type, name, values)
95 stat = self:set(config, name, type)
97 name = self:add(config, type)
101 if stat and values then
102 stat = self:tset(config, name, values)
108 --- Updated the data of a section using data from a table.
109 -- @param config UCI config
110 -- @param section UCI section name (optional)
111 -- @param values Table of key - value pairs to update the section with
112 function Cursor.tset(self, config, section, values)
114 for k, v in pairs(values) do
115 if k:sub(1, 1) ~= "." then
116 stat = stat and self:set(config, section, k, v)
122 --- Get an option or list and return values as table.
123 -- @param config UCI config
124 -- @param section UCI section name
125 -- @param option UCI option
127 function Cursor.get_list(self, config, section, option)
128 if config and section and option then
129 local val = self:get(config, section, option)
130 return ( type(val) == "table" and val or { val } )
135 --- Set given values as list.
136 -- @param config UCI config
137 -- @param section UCI section name
138 -- @param option UCI option
139 -- @param value UCI value
140 -- @return Boolean whether operation succeeded
141 function Cursor.set_list(self, config, section, option, value)
142 if config and section and option then
144 config, section, option,
145 ( type(value) == "table" and value or { value } )
152 --- Add an anonymous section.
155 -- @param config UCI config
156 -- @param type UCI section type
157 -- @return Name of created section
159 --- Get a table of unsaved changes.
161 -- @name Cursor.changes
162 -- @param config UCI config
163 -- @return Table of changes
165 --- Commit unsaved changes.
167 -- @name Cursor.commit
168 -- @param config UCI config
169 -- @return Boolean whether operation succeeded
170 -- @see Cursor.revert
172 --- Deletes a section or an option.
174 -- @name Cursor.delete
175 -- @param config UCI config
176 -- @param section UCI section name
177 -- @param option UCI option (optional)
178 -- @return Boolean whether operation succeeded
180 --- Call a function for every section of a certain type.
182 -- @name Cursor.foreach
183 -- @param config UCI config
184 -- @param type UCI section type
185 -- @param callback Function to be called
186 -- @return Boolean whether operation succeeded
188 --- Get a section type or an option
191 -- @param config UCI config
192 -- @param section UCI section name
193 -- @param option UCI option (optional)
196 --- Get all sections of a config or all values of a section.
198 -- @name Cursor.get_all
199 -- @param config UCI config
200 -- @param section UCI section name (optional)
201 -- @return Table of UCI sections or table of UCI values
203 --- Manually load a config.
206 -- @param config UCI config
207 -- @return Boolean whether operation succeeded
209 -- @see Cursor.unload
211 --- Revert unsaved changes.
213 -- @name Cursor.revert
214 -- @param config UCI config
215 -- @return Boolean whether operation succeeded
216 -- @see Cursor.commit
218 --- Saves changes made to a config to make them committable.
221 -- @param config UCI config
222 -- @return Boolean whether operation succeeded
224 -- @see Cursor.unload
226 --- Set a value or create a named section.
229 -- @param config UCI config
230 -- @param section UCI section name
231 -- @param option UCI option or UCI section type
232 -- @param value UCI value or nil if you want to create a section
233 -- @return Boolean whether operation succeeded
235 --- Get the configuration directory.
237 -- @name Cursor.get_confdir
238 -- @return Configuration directory
240 --- Get the directory for uncomitted changes.
242 -- @name Cursor.get_savedir
243 -- @return Save directory
245 --- Set the configuration directory.
247 -- @name Cursor.set_confdir
248 -- @param directory UCI configuration directory
249 -- @return Boolean whether operation succeeded
251 --- Set the directory for uncommited changes.
253 -- @name Cursor.set_savedir
254 -- @param directory UCI changes directory
255 -- @return Boolean whether operation succeeded
257 --- Discard changes made to a config.
259 -- @name Cursor.unload
260 -- @param config UCI config
261 -- @return Boolean whether operation succeeded