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 os.execute(conf.uci_oncommit[config])
64 --- Delete all sections of a given type that match certain criteria.
65 -- @param config UCI config
66 -- @param type UCI section type
67 -- @param comparator Function that will be called for each section and
68 -- returns a boolean whether to delete the current section (optional)
69 function Cursor.delete_all(self, config, type, comparator)
71 local function helper (section)
72 if not comparator or comparator(section) then
73 table.insert(del, section[".name"])
77 self:foreach(config, type, helper)
79 for i, j in ipairs(del) do
80 self:delete(config, j)
84 --- Create a new section and initialize it with data.
85 -- @param config UCI config
86 -- @param type UCI section type
87 -- @param name UCI section name (optional)
88 -- @param values Table of key - value pairs to initialize the section with
89 -- @return Name of created section
90 function Cursor.section(self, config, type, name, values)
93 stat = self:set(config, name, type)
95 name = self:add(config, type)
99 if stat and values then
100 stat = self:tset(config, name, values)
106 --- Updated the data of a section using data from a table.
107 -- @param config UCI config
108 -- @param section UCI section name (optional)
109 -- @param values Table of key - value pairs to update the section with
110 function Cursor.tset(self, config, section, values)
112 for k, v in pairs(values) do
113 if k:sub(1, 1) ~= "." then
114 stat = stat and self:set(config, section, k, v)
120 --- Get an option or list and return values as table.
121 -- @param config UCI config
122 -- @param section UCI section name
123 -- @param option UCI option
125 function Cursor.get_list(self, config, section, option)
126 if config and section and option then
127 local val = self:get(config, section, option)
128 return ( type(val) == "table" and val or { val } )
133 --- Set given values as list.
134 -- @param config UCI config
135 -- @param section UCI section name
136 -- @param option UCI option
137 -- @param value UCI value
138 -- @return Boolean whether operation succeeded
139 function Cursor.set_list(self, config, section, option, value)
140 if config and section and option then
142 config, section, option,
143 ( type(value) == "table" and value or { value } )
150 --- Add an anonymous section.
153 -- @param config UCI config
154 -- @param type UCI section type
155 -- @return Name of created section
157 --- Get a table of unsaved changes.
159 -- @name Cursor.changes
160 -- @param config UCI config
161 -- @return Table of changes
163 --- Commit unsaved changes.
165 -- @name Cursor.commit
166 -- @param config UCI config
167 -- @return Boolean whether operation succeeded
168 -- @see Cursor.revert
170 --- Deletes a section or an option.
172 -- @name Cursor.delete
173 -- @param config UCI config
174 -- @param section UCI section name
175 -- @param option UCI option (optional)
176 -- @return Boolean whether operation succeeded
178 --- Call a function for every section of a certain type.
180 -- @name Cursor.foreach
181 -- @param config UCI config
182 -- @param type UCI section type
183 -- @param callback Function to be called
184 -- @return Boolean whether operation succeeded
186 --- Get a section type or an option
189 -- @param config UCI config
190 -- @param section UCI section name
191 -- @param option UCI option (optional)
194 --- Get all sections of a config or all values of a section.
196 -- @name Cursor.get_all
197 -- @param config UCI config
198 -- @param section UCI section name (optional)
199 -- @return Table of UCI sections or table of UCI values
201 --- Manually load a config.
204 -- @param config UCI config
205 -- @return Boolean whether operation succeeded
207 -- @see Cursor.unload
209 --- Revert unsaved changes.
211 -- @name Cursor.revert
212 -- @param config UCI config
213 -- @return Boolean whether operation succeeded
214 -- @see Cursor.commit
216 --- Saves changes made to a config to make them committable.
219 -- @param config UCI config
220 -- @return Boolean whether operation succeeded
222 -- @see Cursor.unload
224 --- Set a value or create a named section.
227 -- @param config UCI config
228 -- @param section UCI section name
229 -- @param option UCI option or UCI section type
230 -- @param value UCI value or nil if you want to create a section
231 -- @return Boolean whether operation succeeded
233 --- Get the configuration directory.
235 -- @name Cursor.get_confdir
236 -- @return Configuration directory
238 --- Get the directory for uncomitted changes.
240 -- @name Cursor.get_savedir
241 -- @return Save directory
243 --- Set the configuration directory.
245 -- @name Cursor.set_confdir
246 -- @param directory UCI configuration directory
247 -- @return Boolean whether operation succeeded
249 --- Set the directory for uncommited changes.
251 -- @name Cursor.set_savedir
252 -- @param directory UCI changes directory
253 -- @return Boolean whether operation succeeded
255 --- Discard changes made to a config.
257 -- @name Cursor.unload
258 -- @param config UCI config
259 -- @return Boolean whether operation succeeded