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 --- LuCI UCI model library.
33 module("luci.model.uci", function(m) setmetatable(m, {__index = uci}) end)
35 savedir_default = "/tmp/.uci"
36 confdir_default = "/etc/config"
38 savedir_state = "/var/state"
40 --- Delete all sections of a given type that match certain criteria.
41 -- @param config UCI config
42 -- @param type UCI section type
43 -- @param comparator Function that will be called for each section and
44 -- returns a boolean whether to delete the current section (optional)
45 function delete_all(config, type, comparator)
47 local function helper (section)
48 if not comparator or comparator(section) then
49 table.insert(del, section[".name"])
53 foreach(config, type, helper)
55 for i, j in ipairs(del) do
60 --- Create a new section and initialize it with data.
61 -- @param config UCI config
62 -- @param type UCI section type
63 -- @param name UCI section name (optional)
64 -- @param values Table of key - value pairs to initialize the section with
65 -- @return Name of created section
66 function section(config, type, name, values)
69 stat = set(config, name, type)
71 name = add(config, type)
75 if stat and values then
76 stat = tset(config, name, values)
82 --- Savely load the configuration.
83 -- @param config Configuration to load
84 -- @return Sucess status
87 function load_config(...)
88 set_confdir(confdir_default)
89 set_savedir(savedir_default)
93 --- Savely load state values.
94 -- @param config Configuration to load
95 -- @return Sucess status
98 function load_state(config)
99 set_confdir(confdir_default)
100 set_savedir(savedir_state)
104 --- Save changes to config values.
105 -- @param config Configuration to save
106 -- @return Sucess status
109 function save_config(config)
110 set_savedir(savedir_default)
114 --- Save changes to state values.
115 -- @param config Configuration to save
116 -- @return Sucess status
119 function save_state(config)
120 set_savedir(savedir_state)
124 --- Updated the data of a section using data from a table.
125 -- @param config UCI config
126 -- @param section UCI section name (optional)
127 -- @param values Table of key - value pairs to update the section with
128 function tset(config, section, values)
130 for k, v in pairs(values) do
131 if k:sub(1, 1) ~= "." then
132 stat = stat and set(config, section, k, v)
139 --- Add an anonymous section.
142 -- @param config UCI config
143 -- @param type UCI section type
144 -- @return Name of created section
146 --- Get a table of unsaved changes.
149 -- @param config UCI config
150 -- @return Table of changes
152 --- Commit unsaved changes.
155 -- @param config UCI config
156 -- @return Boolean whether operation succeeded
159 --- Deletes a section or an option.
162 -- @param config UCI config
163 -- @param section UCI section name
164 -- @param option UCI option (optional)
165 -- @return Boolean whether operation succeeded
167 --- Call a function for every section of a certain type.
170 -- @param config UCI config
171 -- @param type UCI section type
172 -- @param callback Function to be called
173 -- @return Boolean whether operation succeeded
175 --- Get a section type or an option
178 -- @param config UCI config
179 -- @param section UCI section name
180 -- @param option UCI option (optional)
183 --- Get all sections of a config or all values of a section.
186 -- @param config UCI config
187 -- @param section UCI section name (optional)
188 -- @return Table of UCI sections or table of UCI values
190 --- Manually load a config.
191 -- Warning: This function is unsave! You should use load_config or load_state if possible.
194 -- @param config UCI config
195 -- @return Boolean whether operation succeeded
201 --- Revert unsaved changes.
204 -- @param config UCI config
205 -- @return Boolean whether operation succeeded
208 --- Saves changes made to a config to make them committable.
211 -- @param config UCI config
212 -- @return Boolean whether operation succeeded
216 --- Set a value or create a named section.
217 -- Warning: This function is unsave! You should use save_config or save_state if possible.
220 -- @param config UCI config
221 -- @param section UCI section name
222 -- @param option UCI option or UCI section type
223 -- @param value UCI value or nil if you want to create a section
224 -- @return Boolean whether operation succeeded
226 --- Set the configuration directory.
229 -- @param directory UCI configuration directory
230 -- @return Boolean whether operation succeeded
232 --- Set the directory for uncommited changes.
235 -- @param directory UCI changes directory
236 -- @return Boolean whether operation succeeded
238 --- Discard changes made to a config.
241 -- @param config UCI config
242 -- @return Boolean whether operation succeeded