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 "uci2"
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")
40 APIVERSION = uci.APIVERSION
42 --- Creates a new statevalue cursor
44 function cursor_state()
45 return cursor(nil, "/var/state")
49 local Cursor = getmetatable(cursor())
51 --- Applies the new config
52 -- @param config UCI config
53 function Cursor.apply(self, config)
54 local conf = require "luci.config"
55 return conf.uci_oncommit[config] and os.execute(conf.uci_oncommit[config])
58 --- Delete all sections of a given type that match certain criteria.
59 -- @param config UCI config
60 -- @param type UCI section type
61 -- @param comparator Function that will be called for each section and
62 -- returns a boolean whether to delete the current section (optional)
63 function Cursor.delete_all(self, config, type, comparator)
65 local function helper (section)
66 if not comparator or comparator(section) then
67 table.insert(del, section[".name"])
71 self:foreach(config, type, helper)
73 for i, j in ipairs(del) do
74 self:delete(config, j)
78 --- Create a new section and initialize it with data.
79 -- @param config UCI config
80 -- @param type UCI section type
81 -- @param name UCI section name (optional)
82 -- @param values Table of key - value pairs to initialize the section with
83 -- @return Name of created section
84 function Cursor.section(self, config, type, name, values)
87 stat = self:set(config, name, type)
89 name = self:add(config, type)
93 if stat and values then
94 stat = self:tset(config, name, values)
100 --- Updated the data of a section using data from a table.
101 -- @param config UCI config
102 -- @param section UCI section name (optional)
103 -- @param values Table of key - value pairs to update the section with
104 function Cursor.tset(self, config, section, values)
106 for k, v in pairs(values) do
107 if k:sub(1, 1) ~= "." then
108 stat = stat and self:set(config, section, k, v)
114 --- Get an option or list and return values as table.
115 -- @param config UCI config
116 -- @param section UCI section name
117 -- @param option UCI option
119 function Cursor.get_list(self, config, section, option)
120 if config and section and option then
121 local val = self:get(config, section, option)
122 return ( type(val) == "table" and val or { val } )
127 --- Set given values as list.
128 -- @param config UCI config
129 -- @param section UCI section name
130 -- @param option UCI option
131 -- @param value UCI value
132 -- @return Boolean whether operation succeeded
133 function Cursor.set_list(self, config, section, option, value)
134 if config and section and option then
136 config, section, option,
137 ( type(value) == "table" and value or { value } )
144 --- Add an anonymous section.
147 -- @param config UCI config
148 -- @param type UCI section type
149 -- @return Name of created section
151 --- Get a table of unsaved changes.
153 -- @name Cursor.changes
154 -- @param config UCI config
155 -- @return Table of changes
157 --- Commit unsaved changes.
159 -- @name Cursor.commit
160 -- @param config UCI config
161 -- @return Boolean whether operation succeeded
164 --- Deletes a section or an option.
166 -- @name Cursor.delete
167 -- @param config UCI config
168 -- @param section UCI section name
169 -- @param option UCI option (optional)
170 -- @return Boolean whether operation succeeded
172 --- Call a function for every section of a certain type.
174 -- @name Cursor.foreach
175 -- @param config UCI config
176 -- @param type UCI section type
177 -- @param callback Function to be called
178 -- @return Boolean whether operation succeeded
180 --- Get a section type or an option
183 -- @param config UCI config
184 -- @param section UCI section name
185 -- @param option UCI option (optional)
188 --- Get all sections of a config or all values of a section.
190 -- @name Cursor.get_all
191 -- @param config UCI config
192 -- @param section UCI section name (optional)
193 -- @return Table of UCI sections or table of UCI values
195 --- Manually load a config.
196 -- Warning: This function is unsave! You should use load_config or load_state if possible.
199 -- @param config UCI config
200 -- @return Boolean whether operation succeeded
206 --- Revert unsaved changes.
208 -- @name Cursor.revert
209 -- @param config UCI config
210 -- @return Boolean whether operation succeeded
213 --- Saves changes made to a config to make them committable.
216 -- @param config UCI config
217 -- @return Boolean whether operation succeeded
221 --- Set a value or create a named section.
224 -- @param config UCI config
225 -- @param section UCI section name
226 -- @param option UCI option or UCI section type
227 -- @param value UCI value or nil if you want to create a section
228 -- @return Boolean whether operation succeeded
230 --- Get the configuration directory.
232 -- @name Cursor.get_confdir
233 -- @return Configuration directory
235 --- Get the directory for uncomitted changes.
237 -- @name Cursor.get_savedir
238 -- @return Save directory
240 --- Set the configuration directory.
242 -- @name Cursor.set_confdir
243 -- @param directory UCI configuration directory
244 -- @return Boolean whether operation succeeded
246 --- Set the directory for uncommited changes.
248 -- @name Cursor.set_savedir
249 -- @param directory UCI changes directory
250 -- @return Boolean whether operation succeeded
252 --- Discard changes made to a config.
254 -- @name Cursor.unload
255 -- @param config UCI config
256 -- @return Boolean whether operation succeeded