2 LuCI - UCI wrapper library
5 Wrapper for the /sbin/uci application, syntax of implemented functions
6 is comparable to the syntax of the uci application
8 Any return value of false or nil can be interpreted as an error
14 Copyright 2008 Steven Barth <steven@midlink.org>
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
20 http://www.apache.org/licenses/LICENSE-2.0
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
30 module("luci.model.uci.wrapper", package.seeall)
36 Session = luci.util.class()
38 -- Session constructor
39 function Session.__init__(self, savedir)
40 self.ucicmd = savedir and "uci -P " .. savedir or "uci"
43 function Session.add(self, config, section_type)
44 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
47 function Session.changes(self, config)
48 return self:_uci("changes " .. _path(config))
51 function Session.commit(self, config)
52 return self:_uci2("commit " .. _path(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)
60 return self:_uci("get " .. _path(config, section, option))
63 function Session.revert(self, config)
64 return self:_uci2("revert " .. _path(config))
67 function Session.sections(self, config)
72 local r1, r2 = self:_uci3("show " .. _path(config))
73 if type(r1) == "table" then
80 function Session.set(self, config, section, option, value)
81 return self:_uci2("set " .. _path(config, section, option, value))
84 function Session.synchronize(self) end
86 -- Dummy transaction functions
88 function Session.t_load(self) end
89 function Session.t_save(self) end
91 Session.t_add = Session.add
92 Session.t_commit = Session.commit
93 Session.t_del = Session.del
94 Session.t_get = Session.get
95 Session.t_revert = Session.revert
96 Session.t_sections = Session.sections
97 Session.t_set = Session.set
103 -- Internal functions --
106 function Session._uci(self, cmd)
107 local res = luci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
109 if res:len() == 0 then
112 return res:sub(1, res:len()-1)
116 function Session._uci2(self, cmd)
117 local res = luci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
119 if res:len() > 0 then
126 function Session._uci3(self, cmd)
127 local res = luci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd)
128 if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then
135 for k,line in pairs(res) do
136 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
143 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
152 -- Build path (config.section.option=value) and prevent command injection
156 -- Not using ipairs because it is not reliable in case of nil arguments
158 for k,v in pairs(arg) do
162 result = "'" .. v:gsub("['.]", "") .. "'"
164 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
166 result = result .. "='" .. v:gsub("'", "") .. "'"