2 FFLuCI - 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("ffluci.model.uci.wrapper", package.seeall)
32 require("ffluci.util")
40 Session = ffluci.util.class()
42 -- Session constructor
43 function Session.__init__(self, path, uci)
46 self.ucicmd = uci .. " -P " .. path
52 function Session.add(self, config, section_type)
53 return self:_uci("add " .. _path(config) .. " " .. _path(section_type))
56 function Session.changes(self, config)
57 return self:_uci("changes " .. _path(config))
60 function Session.commit(self, config)
61 return self:_uci2("commit " .. _path(config))
64 function Session.del(self, config, section, option)
65 return self:_uci2("del " .. _path(config, section, option))
68 function Session.get(self, config, section, option)
69 return self:_uci("get " .. _path(config, section, option))
72 function Session.revert(self, config)
73 return self:_uci2("revert " .. _path(config))
76 function Session.sections(self, config)
81 local r1, r2 = self:_uci3("show " .. _path(config))
82 if type(r1) == "table" then
89 function Session.set(self, config, section, option, value)
90 return self:_uci2("set " .. _path(config, section, option, value))
95 -- Internal functions --
98 function Session._uci(self, cmd)
99 local res = ffluci.sys.exec(self.ucicmd .. " 2>/dev/null " .. cmd)
101 if res:len() == 0 then
104 return res:sub(1, res:len()-1)
108 function Session._uci2(self, cmd)
109 local res = ffluci.sys.exec(self.ucicmd .. " 2>&1 " .. cmd)
111 if res:len() > 0 then
118 function Session._uci3(self, cmd)
119 local res = ffluci.sys.execl(self.ucicmd .. " 2>&1 " .. cmd)
120 if res[1] and res[1]:sub(1, self.ucicmd:len()+1) == self.ucicmd..":" then
126 for k,line in pairs(res) do
127 c, s, t = line:match("^([^.]-)%.([^.]-)=(.-)$")
129 tbl[c] = tbl[c] or {}
130 tbl[c][".order"] = tbl[c][".order"] or {}
133 table.insert(tbl[c][".order"], s)
134 tbl[c][s][".type"] = t
137 c, s, o, v = line:match("^([^.]-)%.([^.]-)%.([^.]-)=(.-)$")
146 -- Build path (config.section.option=value) and prevent command injection
150 -- Not using ipairs because it is not reliable in case of nil arguments
152 for k,v in pairs(arg) do
156 result = "'" .. v:gsub("['.]", "") .. "'"
158 result = result .. ".'" .. v:gsub("['.]", "") .. "'"
160 result = result .. "='" .. v:gsub("'", "") .. "'"