2 LuCI - UCI libuci wrapper
5 Wrapper for the libuci Lua bindings
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.
27 module("luci.model.uci.libuci", package.seeall)
34 Session = luci.util.class()
36 -- Session constructor
37 function Session.__init__(self, savedir)
38 self.ucicmd = savedir and "uci -P " .. savedir or "uci"
39 self.savedir = savedir or luci.model.uci.savedir
42 function Session.add(self, config, section_type)
43 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
46 function Session.changes(self, config)
47 return self:_uci("changes " .. _path(config))
50 function Session.commit(self, config)
52 return self:t_commit(config)
55 function Session.del(self, config, section, option)
56 return self:_uci2("del " .. _path(config, section, option))
59 function Session.get(self, config, section, option)
61 return self:t_get(config, section, option)
64 function Session.revert(self, config)
66 return self:t_revert(config)
69 function Session.sections(self, config)
71 return self:t_sections(config)
74 function Session.set(self, config, section, option, value)
76 return self:t_set(config, section, option, value) and self:t_save(config)
79 function Session.synchronize(self)
80 return uci.set_savedir(self.savedir)
86 function Session.t_load(self, config)
87 return self:synchronize() and uci.load(config)
90 function Session.t_save(self, config)
91 return uci.save(config)
94 function Session.t_add(self, config, type)
96 local r = self:add(config, type)
101 function Session.t_commit(self, config)
102 return uci.commit(config)
105 function Session.t_del(self, config, section, option)
107 local r = self:del(config, section, option)
112 function Session.t_get(self, config, section, option)
114 return uci.get(config, section, option)
116 return uci.get(config, section)
120 function Session.t_revert(self, config)
121 return uci.revert(config)
124 function Session.t_sections(self, config)
125 local raw = uci.get_all(config)
133 for i, sec in ipairs(raw) do
134 table.insert(o, sec.name)
136 s[sec.name] = sec.options
137 s[sec.name][".type"] = sec.type
143 function Session.t_set(self, config, section, option, value)
145 return uci.set(config.."."..section.."."..option.."="..value)
147 return uci.set(config.."."..section.."="..value)
151 -- Internal functions --
154 function Session._uci(self, cmd)
155 local res = luci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
157 if res:len() == 0 then
160 return res:sub(1, res:len()-1)
164 function Session._uci2(self, cmd)
165 local res = luci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
167 if res:len() > 0 then
174 -- Build path (config.section.option=value) and prevent command injection
178 -- Not using ipairs because it is not reliable in case of nil arguments
180 for k,v in pairs(arg) do
184 result = "'" .. v:gsub("['.]", "") .. "'"
186 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
188 result = result .. "='" .. v:gsub("'", "") .. "'"