gluon-core: replace nixio with luaposix and luabitops

This commit is contained in:
Matthias Schiffer 2018-07-13 19:20:56 +02:00
parent 60a0e78a4a
commit fd10d7cbb0
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
8 changed files with 45 additions and 33 deletions

View File

@ -14,7 +14,7 @@ define Package/gluon-core
TITLE:=Base files of Gluon TITLE:=Base files of Gluon
DEPENDS:= \ DEPENDS:= \
+gluon-site +libgluonutil +libiwinfo-lua +lua-platform-info +lua-simple-uci +lua-hash +lua-jsonc \ +gluon-site +libgluonutil +libiwinfo-lua +lua-platform-info +lua-simple-uci +lua-hash +lua-jsonc \
+luci-lib-nixio +vxlan +odhcp6c +firewall +pretty-hostname +luabitop +luaposix +luci-lib-nixio +vxlan +odhcp6c +firewall +pretty-hostname
endef endef
define Package/gluon-core/description define Package/gluon-core/description

View File

@ -1,10 +1,10 @@
#!/usr/bin/lua #!/usr/bin/lua
local fs = require 'nixio.fs'
local sysconfig = require 'gluon.sysconfig' local sysconfig = require 'gluon.sysconfig'
local unistd = require 'posix.unistd'
if fs.stat('/lib/gluon/version/core') and not sysconfig.gluon_version then if unistd.access('/lib/gluon/version/core') and not sysconfig.gluon_version then
-- This isn't an initial upgrade, so set gluon_version -- This isn't an initial upgrade, so set gluon_version
sysconfig.gluon_version = '' sysconfig.gluon_version = ''
end end

View File

@ -1,9 +1,9 @@
#!/usr/bin/lua #!/usr/bin/lua
local fs = require 'nixio.fs' local unistd = require 'posix.unistd'
if not fs.access('/lib/gluon/domains/') then if not unistd.access('/lib/gluon/domains/') then
return return
end end
@ -11,7 +11,7 @@ end
local uci = require('simple-uci').cursor() local uci = require('simple-uci').cursor()
local domain = uci:get('gluon', 'core', 'domain') local domain = uci:get('gluon', 'core', 'domain')
if domain and not fs.access('/lib/gluon/domains/' .. domain .. '.json') then if domain and not unistd.access('/lib/gluon/domains/' .. domain .. '.json') then
io.stderr:write(string.format("Warning: invalid mesh domain '%s' configured, resetting to default...\n", domain)) io.stderr:write(string.format("Warning: invalid mesh domain '%s' configured, resetting to default...\n", domain))
domain = nil domain = nil
end end

View File

@ -11,8 +11,6 @@ end
local util = require 'gluon.util' local util = require 'gluon.util'
local platform = require 'gluon.platform' local platform = require 'gluon.platform'
local fs = require 'nixio.fs'
local try_files = { local try_files = {
'/sys/class/net/eth0/address' '/sys/class/net/eth0/address'
@ -52,7 +50,7 @@ end
for _, file in ipairs(try_files) do for _, file in ipairs(try_files) do
local addr = fs.readfile(file) local addr = util.readfile(file)
if addr then if addr then
sysconfig.primary_mac = util.trim(addr) sysconfig.primary_mac = util.trim(addr)

View File

@ -12,15 +12,15 @@ local util = require 'gluon.util'
local platform = require 'gluon.platform' local platform = require 'gluon.platform'
local site = require 'gluon.site' local site = require 'gluon.site'
local fs = require 'nixio.fs'
local uci = require('simple-uci').cursor() local uci = require('simple-uci').cursor()
local unistd = require 'posix.unistd'
local function iface_exists(ifaces) local function iface_exists(ifaces)
if not ifaces then return nil end if not ifaces then return nil end
for iface in ifaces:gmatch('%S+') do for iface in ifaces:gmatch('%S+') do
if fs.access('/sys/class/net/' .. iface:gsub('%..*$', '')) then if unistd.access('/sys/class/net/' .. iface:gsub('%..*$', '')) then
return ifaces return ifaces
end end
end end

View File

@ -1,8 +1,8 @@
#!/usr/bin/lua #!/usr/bin/lua
local fs = require 'nixio.fs' local unistd = require 'posix.unistd'
if not fs.access('/etc/opkg/distfeeds.conf') then if not unistd.access('/etc/opkg/distfeeds.conf') then
os.exit(0) os.exit(0)
end end
@ -22,8 +22,8 @@ subst['%%A'] = f:read()
f:close() f:close()
subst['%%GS'] = site.site_code() subst['%%GS'] = site.site_code()
subst['%%GV'] = util.trim(fs.readfile('/lib/gluon/gluon-version')) subst['%%GV'] = util.trim(util.readfile('/lib/gluon/gluon-version'))
subst['%%GR'] = util.trim(fs.readfile('/lib/gluon/release')) subst['%%GR'] = util.trim(util.readfile('/lib/gluon/release'))
local prefix = subst['%%d'] .. '_' local prefix = subst['%%d'] .. '_'

View File

@ -2,10 +2,9 @@
local sysconfig = require 'gluon.sysconfig' local sysconfig = require 'gluon.sysconfig'
local fs = require 'nixio.fs'
local util = require 'gluon.util' local util = require 'gluon.util'
-- Save the Gluon version in the sysconfig so we know which version we -- Save the Gluon version in the sysconfig so we know which version we
-- upgraded from after the next upgrade -- upgraded from after the next upgrade
sysconfig.gluon_version = util.trim(fs.readfile('/lib/gluon/gluon-version')) sysconfig.gluon_version = util.trim(util.readfile('/lib/gluon/gluon-version'))

View File

@ -22,11 +22,11 @@ local ipairs = ipairs
local pairs = pairs local pairs = pairs
local table = table local table = table
local nixio = require 'nixio' local bit = require 'bit'
local posix_glob = require 'posix.glob'
local hash = require 'hash' local hash = require 'hash'
local sysconfig = require 'gluon.sysconfig' local sysconfig = require 'gluon.sysconfig'
local site = require 'gluon.site' local site = require 'gluon.site'
local fs = require 'nixio.fs'
module 'gluon.util' module 'gluon.util'
@ -77,14 +77,24 @@ function replace_prefix(file, prefix, add)
os.rename(tmp, file) os.rename(tmp, file)
end end
function exec(command) local function readall(f)
local pp = io.popen(command) if not f then
local data = pp:read("*a") return nil
pp:close() end
local data = f:read('*a')
f:close()
return data return data
end end
function readfile(file)
return readall(io.open(file))
end
function exec(command)
return readall(io.popen(command))
end
function node_id() function node_id()
return string.gsub(sysconfig.primary_mac, ':', '') return string.gsub(sysconfig.primary_mac, ':', '')
end end
@ -120,20 +130,25 @@ function get_mesh_devices(uconn)
return devices return devices
end end
local function find_phy_by_path(path) -- Safe glob: returns an empty table when the glob fails because of
for phy in fs.glob('/sys/devices/' .. path .. '/ieee80211/phy*') do -- a non-existing path
return phy:match('([^/]+)$') function glob(pattern)
end return posix_glob.glob(pattern) or {}
end
for phy in fs.glob('/sys/devices/platform/' .. path .. '/ieee80211/phy*') do local function find_phy_by_path(path)
local phy = glob('/sys/devices/' .. path .. '/ieee80211/phy*')[1]
or glob('/sys/devices/platform/' .. path .. '/ieee80211/phy*')[1]
if phy then
return phy:match('([^/]+)$') return phy:match('([^/]+)$')
end end
end end
local function find_phy_by_macaddr(macaddr) local function find_phy_by_macaddr(macaddr)
local addr = macaddr:lower() local addr = macaddr:lower()
for file in fs.glob('/sys/class/ieee80211/*/macaddress') do for _, file in ipairs(glob('/sys/class/ieee80211/*/macaddress')) do
if trim(fs.readfile(file)) == addr then if trim(readfile(file)) == addr then
return file:match('([^/]+)/macaddress$') return file:match('([^/]+)/macaddress$')
end end
end end
@ -181,14 +196,14 @@ function generate_mac(i)
m1 = tonumber(m1, 16) m1 = tonumber(m1, 16)
m6 = tonumber(m6, 16) m6 = tonumber(m6, 16)
m1 = nixio.bit.bor(m1, 0x02) -- set locally administered bit m1 = bit.bor(m1, 0x02) -- set locally administered bit
m1 = nixio.bit.band(m1, 0xFE) -- unset the multicast bit m1 = bit.band(m1, 0xFE) -- unset the multicast bit
-- It's necessary that the first 45 bits of the MAC address don't -- It's necessary that the first 45 bits of the MAC address don't
-- vary on a single hardware interface, since some chips are using -- vary on a single hardware interface, since some chips are using
-- a hardware MAC filter. (e.g 'rt305x') -- a hardware MAC filter. (e.g 'rt305x')
m6 = nixio.bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting) m6 = bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting)
m6 = m6 + i -- add virtual interface id m6 = m6 + i -- add virtual interface id
return string.format('%02x:%s:%s:%s:%s:%02x', m1, m2, m3, m4, m5, m6) return string.format('%02x:%s:%s:%s:%s:%02x', m1, m2, m3, m4, m5, m6)