From 04818c170b2fb6d64eccdec5aa97673ad4419a9f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 17 Jan 2017 23:57:39 +0100 Subject: [PATCH] package: refactor add_to_set/remove_to_set to get rid of last LuCI patch --- .../upgrade/300-gluon-client-bridge-network | 16 ++- .../luasrc/usr/lib/lua/gluon/util.lua | 24 ++++ .../lua/luci/model/cbi/admin/portconfig.lua | 16 +-- .../310-gluon-mesh-batman-adv-core-mesh | 5 +- ...l.uci-add-add_to_set-remove_from_set.patch | 103 ------------------ 5 files changed, 47 insertions(+), 117 deletions(-) delete mode 100644 patches/packages/luci/0001-model.uci-add-add_to_set-remove_from_set.patch diff --git a/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/300-gluon-client-bridge-network b/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/300-gluon-client-bridge-network index 67d0c061..7540ecb5 100755 --- a/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/300-gluon-client-bridge-network +++ b/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/300-gluon-client-bridge-network @@ -2,6 +2,7 @@ local site = require 'gluon.site_config' local sysconfig = require 'gluon.sysconfig' +local util = require 'gluon.util' local ip = require 'luci.ip' local lutil = require 'luci.util' @@ -35,21 +36,24 @@ uci:delete('network', 'client', 'peerdns') uci:delete('network', 'client', 'sourcefilter') -local ifname = uci:get('network', 'client', 'ifname') +local interfaces = uci:get('network', 'client', 'ifname') or {} -if type(ifname) == 'string' then - uci:delete('network', 'client', 'ifname') - for x in ifname:gmatch("[^%s]+") do - uci:add_to_set('network', 'client', 'ifname', x) +if type(interfaces) == 'string' then + local ifname = interfaces + interfaces = {} + for iface in ifname:gmatch("[^%s]+") do + util.add_to_set(interfaces, iface) end end if sysconfig.lan_ifname and not ifname and not uci:get_bool('network', 'mesh_lan', 'auto') then for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do - uci:add_to_set('network', 'client', 'ifname', lanif) + util.add_to_set(interfaces, lanif) end end +uci:set_list('network', 'client', 'ifname', interfaces) + uci:save('network') diff --git a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua index f07f0ab1..f013ec23 100644 --- a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua +++ b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua @@ -38,8 +38,32 @@ local uci = require('luci.model.uci').cursor() local lutil = require 'luci.util' local fs = require 'nixio.fs' + module 'gluon.util' +function add_to_set(t, itm) + for _,v in ipairs(t) do + if v == itm then return false end + end + table.insert(t, itm) + return true +end + +function remove_from_set(t, itm) + local i = 1 + local changed = false + while i <= #t do + if t[i] == itm then + table.remove(t, i) + changed = true + else + i = i + 1 + end + end + return changed +end + + function exec(...) return os.execute(escape_args('', 'exec', ...)) end diff --git a/package/gluon-luci-portconfig/luasrc/usr/lib/lua/luci/model/cbi/admin/portconfig.lua b/package/gluon-luci-portconfig/luasrc/usr/lib/lua/luci/model/cbi/admin/portconfig.lua index 0e3458f5..2cab7606 100644 --- a/package/gluon-luci-portconfig/luasrc/usr/lib/lua/luci/model/cbi/admin/portconfig.lua +++ b/package/gluon-luci-portconfig/luasrc/usr/lib/lua/luci/model/cbi/admin/portconfig.lua @@ -15,6 +15,7 @@ $Id$ local uci = luci.model.uci.cursor() local lutil = require 'luci.util' local sysconfig = require 'gluon.sysconfig' +local util = require 'gluon.util' local wan = uci:get_all("network", "wan") local wan6 = uci:get_all("network", "wan6") @@ -127,16 +128,17 @@ function f.handle(self, state, data) if sysconfig.lan_ifname then uci:set("network", "mesh_lan", "auto", data.mesh_lan) - local doit - if data.mesh_lan == '1' then - doit = uci.remove_from_set - else - doit = uci.add_to_set - end + local interfaces = uci:get_list("network", "client", "ifname") for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do - doit(uci, "network", "client", "ifname", lanif) + if data.mesh_lan == '1' then + util.remove_from_set(interfaces, lanif) + else + util.add_to_set(interfaces, lanif) + end end + + uci:set_list("network", "client", "ifname", interfaces) end uci:save("network") diff --git a/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/310-gluon-mesh-batman-adv-core-mesh b/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/310-gluon-mesh-batman-adv-core-mesh index 9569df25..6558671c 100755 --- a/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/310-gluon-mesh-batman-adv-core-mesh +++ b/package/gluon-mesh-batman-adv-core/luasrc/lib/gluon/upgrade/310-gluon-mesh-batman-adv-core-mesh @@ -2,6 +2,7 @@ local sysconfig = require 'gluon.sysconfig' local site = require 'gluon.site_config' +local util = require 'gluon.util' local uci = require('luci.model.uci').cursor() @@ -34,6 +35,8 @@ uci:section('network', 'interface', 'bat0', } ) -uci:add_to_set('network', 'client', 'ifname', 'bat0') +local interfaces = uci:get_list('network', 'client', 'ifname') +util.add_to_set(interfaces, 'bat0') +uci:set_list('network', 'client', 'ifname', interfaces) uci:save('network') diff --git a/patches/packages/luci/0001-model.uci-add-add_to_set-remove_from_set.patch b/patches/packages/luci/0001-model.uci-add-add_to_set-remove_from_set.patch deleted file mode 100644 index b0adfc1e..00000000 --- a/patches/packages/luci/0001-model.uci-add-add_to_set-remove_from_set.patch +++ /dev/null @@ -1,103 +0,0 @@ -From: Nils Schneider -Date: Mon, 17 Aug 2015 20:39:58 +0200 -Subject: model.uci: add add_to_set / remove_from_set - -Adds two functions to simplify working with UCI lists: - -- add_to_set, which ensures a given value will be present in a list, and -- remove_from_set, which removes a value from list. - -I've called these methods "set" because they treat the list as a set, -i.e. duplicated values will be removed. Also, order is not preserved. - -Signed-off-by: Nils Schneider - -diff --git a/modules/luci-base/luasrc/model/uci.lua b/modules/luci-base/luasrc/model/uci.lua -index 577c6cde08eaa6e10887c97b26fed000f3289070..e77cacec5dfeb447085b13d6275edfe873beb3c4 100644 ---- a/modules/luci-base/luasrc/model/uci.lua -+++ b/modules/luci-base/luasrc/model/uci.lua -@@ -9,7 +9,7 @@ local table = require "table" - - local setmetatable, rawget, rawset = setmetatable, rawget, rawset - local require, getmetatable = require, getmetatable --local error, pairs, ipairs = error, pairs, ipairs -+local error, pairs, ipairs, next = error, pairs, ipairs, next - local type, tostring, tonumber, unpack = type, tostring, tonumber, unpack - - -- The typical workflow for UCI is: Get a cursor instance from the -@@ -150,6 +150,40 @@ function Cursor.set_list(self, config, section, option, value) - return false - end - -+function Cursor.add_to_set(self, config, section, option, value, remove) -+ local list = self:get_list(config, section, option) -+ -+ if not list then -+ return false -+ end -+ -+ local set = {} -+ for _, l in ipairs(list) do -+ set[l] = true -+ end -+ -+ if remove then -+ set[value] = nil -+ else -+ set[value] = true -+ end -+ -+ list = {} -+ for k, _ in pairs(set) do -+ table.insert(list, k) -+ end -+ -+ if next(list) == nil then -+ return self:delete(config, section, option) -+ else -+ return self:set(config, section, option, list) -+ end -+end -+ -+function Cursor.remove_from_set(self, config, section, option, value) -+ self:add_to_set(config, section, option, value, true) -+end -+ - -- Return a list of initscripts affected by configuration changes. - function Cursor._affected(self, configlist) - configlist = type(configlist) == "table" and configlist or {configlist} -diff --git a/modules/luci-base/luasrc/model/uci.luadoc b/modules/luci-base/luasrc/model/uci.luadoc -index 49093c7930128f1e6de6f739662b96adcc43fe74..cfec9eea7922da551cb8d2a4f2c540d479b8ccbe 100644 ---- a/modules/luci-base/luasrc/model/uci.luadoc -+++ b/modules/luci-base/luasrc/model/uci.luadoc -@@ -118,6 +118,30 @@ has the same effect as deleting the option. - ]] - - ---[[ -+Add a given value to a list of unique values. -+ -+@class function -+@name Cursor.add_to_set -+@param config UCI config -+@param section UCI section name -+@param option UCI option -+@param value UCI value -+@return Boolean whether operation succeeded -+]] -+ -+---[[ -+Remove a given value from a list of unique values. -+ -+@class function -+@name Cursor.add_to_set -+@param config UCI config -+@param section UCI section name -+@param option UCI option -+@param value UCI value -+@return Boolean whether operation succeeded -+]] -+ -+---[[ - Create a sub-state of this cursor. The sub-state is tied to the parent - - curser, means it the parent unloads or loads configs, the sub state will