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 --- Add an anonymous section.
154 -- @param config UCI config
155 -- @param type UCI section type
156 -- @return Name of created section
158 --- Get a table of unsaved changes.
160 -- @name Cursor.changes
161 -- @param config UCI config
162 -- @return Table of changes
164 --- Commit unsaved changes.
166 -- @name Cursor.commit
167 -- @param config UCI config
168 -- @return Boolean whether operation succeeded
169 -- @see Cursor.revert
171 --- Deletes a section or an option.
173 -- @name Cursor.delete
174 -- @param config UCI config
175 -- @param section UCI section name
176 -- @param option UCI option (optional)
177 -- @return Boolean whether operation succeeded
179 --- Call a function for every section of a certain type.
181 -- @name Cursor.foreach
182 -- @param config UCI config
183 -- @param type UCI section type
184 -- @param callback Function to be called
185 -- @return Boolean whether operation succeeded
187 --- Get a section type or an option
190 -- @param config UCI config
191 -- @param section UCI section name
192 -- @param option UCI option (optional)
195 --- Get all sections of a config or all values of a section.
197 -- @name Cursor.get_all
198 -- @param config UCI config
199 -- @param section UCI section name (optional)
200 -- @return Table of UCI sections or table of UCI values
202 --- Manually load a config.
205 -- @param config UCI config
206 -- @return Boolean whether operation succeeded
208 -- @see Cursor.unload
210 --- Revert unsaved changes.
212 -- @name Cursor.revert
213 -- @param config UCI config
214 -- @return Boolean whether operation succeeded
215 -- @see Cursor.commit
217 --- Saves changes made to a config to make them committable.
220 -- @param config UCI config
221 -- @return Boolean whether operation succeeded
223 -- @see Cursor.unload
225 --- Set a value or create a named section.
228 -- @param config UCI config
229 -- @param section UCI section name
230 -- @param option UCI option or UCI section type
231 -- @param value UCI value or nil if you want to create a section
232 -- @return Boolean whether operation succeeded
234 --- Get the configuration directory.
236 -- @name Cursor.get_confdir
237 -- @return Configuration directory
239 --- Get the directory for uncomitted changes.
241 -- @name Cursor.get_savedir
242 -- @return Save directory
244 --- Set the configuration directory.
246 -- @name Cursor.set_confdir
247 -- @param directory UCI configuration directory
248 -- @return Boolean whether operation succeeded
250 --- Set the directory for uncommited changes.
252 -- @name Cursor.set_savedir
253 -- @param directory UCI changes directory
254 -- @return Boolean whether operation succeeded
256 --- Discard changes made to a config.
258 -- @name Cursor.unload
259 -- @param config UCI config
260 -- @return Boolean whether operation succeeded