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 uci = require("uci")
27 local util = require("luci.util")
28 local setmetatable, rawget, rawset = setmetatable, rawget, rawset
29 local error, pairs, ipairs, tostring = error, pairs, ipairs, tostring
32 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
34 savedir_default = "/tmp/.uci"
35 confdir_default = "/etc/config"
37 savedir_state = "/var/state"
39 --- Delete all sections of a given type that match certain criteria.
40 -- @param config UCI config
41 -- @param type UCI section type
42 -- @param comparator Function that will be called for each section and
43 -- returns a boolean whether to delete the current section (optional)
44 function delete_all(config, type, comparator)
46 local function helper (section)
47 if not comparator or comparator(section) then
48 table.insert(del, section[".name"])
52 foreach(config, type, helper)
54 for i, j in ipairs(del) do
59 --- Create a new section and initialize it with data.
60 -- @param config UCI config
61 -- @param type UCI section type
62 -- @param name UCI section name (optional)
63 -- @param values Table of key - value pairs to initialize the section with
64 -- @return Name of created section
65 function section(config, type, name, values)
68 stat = set(config, name, type)
70 name = add(config, type)
74 if stat and values then
75 stat = tset(config, name, values)
81 --- Get a certain state value.
82 -- @param ... Parameters passed to function get
85 function get_statevalue(...)
86 set_savedir(savedir_state)
87 local result = get(...)
88 set_savedir(savedir_default)
92 --- Updated the data of a section using data from a table.
93 -- @param config UCI config
94 -- @param section UCI section name (optional)
95 -- @param values Table of key - value pairs to update the section with
96 function tset(config, section, values)
98 for k, v in pairs(values) do
99 if k:sub(1, 1) ~= "." then
100 stat = stat and set(config, section, k, v)
106 --- Add an anonymous section.
109 -- @param config UCI config
110 -- @param type UCI section type
111 -- @return Name of created section
113 --- Get a table of unsaved changes.
116 -- @param config UCI config
117 -- @return Table of changes
119 --- Commit unsaved changes.
122 -- @param config UCI config
123 -- @return Boolean whether operation succeeded
126 --- Deletes a section or an option.
129 -- @param config UCI config
130 -- @param section UCI section name
131 -- @param option UCI option (optional)
132 -- @return Boolean whether operation succeeded
134 --- Call a function for every section of a certain type.
137 -- @param config UCI config
138 -- @param type UCI section type
139 -- @param callback Function to be called
140 -- @return Boolean whether operation succeeded
142 --- Get a section type or an option
145 -- @param config UCI config
146 -- @param section UCI section name
147 -- @param option UCI option (optional)
150 --- Get all sections of a config or all values of a section.
153 -- @param config UCI config
154 -- @param section UCI section name (optional)
155 -- @return Table of UCI sections or table of UCI values
157 --- Manually load a config.
160 -- @param config UCI config
161 -- @return Boolean whether operation succeeded
165 --- Revert unsaved changes.
168 -- @param config UCI config
169 -- @return Boolean whether operation succeeded
172 --- Saves changes made to a config to make them committable.
175 -- @param config UCI config
176 -- @return Boolean whether operation succeeded
180 --- Set a value or create a named section.
183 -- @param config UCI config
184 -- @param section UCI section name
185 -- @param option UCI option or UCI section type
186 -- @param value UCI value or nil if you want to create a section
187 -- @return Boolean whether operation succeeded
189 --- Set the configuration directory.
192 -- @param directory UCI configuration directory
193 -- @return Boolean whether operation succeeded
195 --- Set the directory for uncommited changes.
198 -- @param directory UCI changes directory
199 -- @return Boolean whether operation succeeded
201 --- Discard changes made to a config.
204 -- @param config UCI config
205 -- @return Boolean whether operation succeeded