From: Marek VavrusÌŒa Date: Thu, 23 Nov 2017 09:02:15 +0000 (-0800) Subject: daemon/lua: fixed unused variables, whitespace, cleanup X-Git-Tag: v1.5.1~15^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3162da830b5ee95b52441685fcaf30b277476303;p=thirdparty%2Fknot-resolver.git daemon/lua: fixed unused variables, whitespace, cleanup Also fixed improper promotion of `ffi` to global variable. ``` $ luacheck --codes daemon/lua/ Checking daemon/lua/config.lua OK Checking daemon/lua/kres-gen.lua OK Checking daemon/lua/kres.lua OK Checking daemon/lua/sandbox.lua OK Checking daemon/lua/trust_anchors.lua OK Checking daemon/lua/zonefile.lua OK Total: 0 warnings / 0 errors in 6 files ``` --- diff --git a/.luacheckrc b/.luacheckrc index 72f12770c..c514ccb57 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -71,6 +71,7 @@ ignore = { -- Sandbox can set global variables files['daemon/lua'].ignore = {'111', '121', '122'} +files['daemon/lua/kres-gen.lua'].ignore = {'631'} -- Allow overly long lines -- Tests and scripts can use global variables files['scripts'].ignore = {'111', '112', '113'} files['tests'].ignore = {'111', '112', '113'} diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index 933ca9f7a..7d41beb38 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -1,3 +1,4 @@ +local ffi = require('ffi') --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[ typedef struct knot_dump_style knot_dump_style_t; diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index e4f3d949c..43ca87f5e 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -14,6 +14,7 @@ # - you also need gdb on $PATH +printf -- "local ffi = require('ffi')\n" printf -- "--[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[\n" ## Various types (mainly), from libknot and libkres diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 7ea3d1b34..9805da458 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -3,7 +3,7 @@ local kres -- the module -ffi = require('ffi') +local ffi = require('ffi') local bit = require('bit') local bor = bit.bor local band = bit.band @@ -229,7 +229,7 @@ ffi.metatype( knot_pkt_t, { end end return records - end, + end, begin = function (pkt, section) return knot.knot_pkt_begin(pkt, section) end, put = function (pkt, owner, ttl, rclass, rtype, rdata) return C.kr_pkt_put(pkt, owner, ttl, rclass, rtype, rdata, #rdata) @@ -241,7 +241,6 @@ ffi.metatype( knot_pkt_t, { }, }) -- Metatype for query -local ub_t = ffi.typeof('unsigned char *') local kr_query_t = ffi.typeof('struct kr_query') ffi.metatype( kr_query_t, { __index = { @@ -306,7 +305,7 @@ local function rr2str(rr, style) ret = ret:sub(1, -2) end if rr.comment then - ret = ret .. ' ;' .. rr.comment + ret = ret .. ' ;' .. rr.comment end end return ret @@ -332,7 +331,7 @@ kres = { end local fs = ffi.new(kr_qflags) - for k_, name in pairs(names) do + for _, name in pairs(names) do fs[name] = true end return fs diff --git a/daemon/lua/sandbox.lua b/daemon/lua/sandbox.lua index dd43c1474..7124e607b 100644 --- a/daemon/lua/sandbox.lua +++ b/daemon/lua/sandbox.lua @@ -78,7 +78,7 @@ end -- `env.VAR returns os.getenv(VAR)` env = {} setmetatable(env, { - __index = function (t, k) return os.getenv(k) end + __index = function (_, k) return os.getenv(k) end }) -- Quick access to interfaces @@ -109,8 +109,10 @@ setmetatable(net, { -- Syntactic sugar for module loading -- `modules. = ` setmetatable(modules, { - __newindex = function (t,k,v) - if type(k) == 'number' then k = v v = nil end + __newindex = function (_, k, v) + if type(k) == 'number' then + k, v = v, nil + end if not rawget(_G, k) then modules.load(k) k = string.match(k, '[%w_]+') @@ -180,7 +182,7 @@ local function make_sandbox(defined) return setmetatable({ __orig_name_list = nl }, { __index = defined, - __newindex = function (t, k, v) + __newindex = function (_, k, v) if __protected[k] then for k2,v2 in pairs(v) do defined[k][k2] = v2 @@ -213,7 +215,7 @@ function eval_cmd(line, raw) return load(code, nil, 't', _ENV) end end - local status, err, chunk + local err, chunk chunk, err = load_code(raw and 'return '..line or 'return table_print('..line..')') if err then chunk, err = load_code(line) @@ -229,7 +231,7 @@ end function table_print (tt, indent, done) done = done or {} indent = indent or 0 - result = "" + local result = "" -- Convert to printable string (escape unprintable) local function printable(value) value = tostring(value) diff --git a/daemon/lua/trust_anchors.lua.in b/daemon/lua/trust_anchors.lua.in index fcbf2486e..e933523ab 100644 --- a/daemon/lua/trust_anchors.lua.in +++ b/daemon/lua/trust_anchors.lua.in @@ -8,7 +8,7 @@ local function https_fetch(url, ca) return nil, 'luasec and luasocket needed for root TA bootstrap' end local resp = {} - local r, c, h, s = https.request{ + local r, c = https.request{ url = url, cafile = ca, verify = {'peer', 'fail_if_no_peer_cert' }, @@ -175,7 +175,7 @@ local active_refresh, update -- forwards -- Plan an event for refreshing the root DNSKEYs and re-scheduling itself local function refresh_plan(keyset, delay, priming, is_initial) local owner_str = kres.dname2str(keyset.owner) -- maybe fix converting back and forth? - keyset.refresh_ev = event.after(delay, function (ev) + keyset.refresh_ev = event.after(delay, function () resolve(owner_str, kres.type.DNSKEY, kres.class.IN, 'NO_CACHE', function (pkt) -- Schedule itself with updated timeout @@ -198,7 +198,7 @@ active_refresh = function (keyset, pkt, is_initial) if pkt:rcode() == kres.rcode.NOERROR then local records = pkt:section(kres.section.ANSWER) local new_keys = {} - for i, rr in ipairs(records) do + for _, rr in ipairs(records) do if rr.type == kres.type.DNSKEY then table.insert(new_keys, rr) end @@ -211,7 +211,7 @@ active_refresh = function (keyset, pkt, is_initial) end -- Calculate refresh/retry timer (RFC 5011, 2.3) local min_ttl = retry and day or 15 * day - for i, rr in ipairs(keyset) do -- 10 or 50% of the original TTL + for _, rr in ipairs(keyset) do -- 10 or 50% of the original TTL min_ttl = math.min(min_ttl, (retry and 100 or 500) * rr.ttl) end return math.max(hour, min_ttl) @@ -248,7 +248,7 @@ end -- For each RR, parse .state and .timer from .comment. local function keyset_parse_comments(tas, default_state) - for _k, ta in pairs(tas) do + for _, ta in pairs(tas) do ta.state = default_state if ta.comment then string.gsub(ta.comment, '^%s*(%a+):(%d*)', function (state, time) @@ -284,7 +284,7 @@ local function keyset_read(path) end end - for _k, ta in pairs(tas) do + for _, ta in pairs(tas) do ta.key_tag = C.kr_dnssec_key_tag(ta.type, ta.rdata, #ta.rdata) end return tas @@ -296,7 +296,7 @@ local function keyset_publish(keyset) local store = kres.context().trust_anchors local count = 0 C.kr_ta_del(store, keyset.owner) - for i, ta in ipairs(keyset) do + for _, ta in ipairs(keyset) do -- Key MAY be used as a TA only in these two states (RFC5011, 4.2) if ta.state == key_state.Valid or ta.state == key_state.Missing then if C.kr_ta_add(store, ta.owner, ta.type, ta.ttl, ta.rdata, #ta.rdata) == 0 then @@ -318,7 +318,7 @@ update = function (keyset, new_keys, is_initial) local hold_down = (keyset.hold_down_time or trust_anchors.hold_down_time) / 1000 local keepset = {} local keep_removed = keyset.keep_removed or trust_anchors.keep_removed - for i, ta in ipairs(keyset) do + for _, ta in ipairs(keyset) do local keep = true if not ta_find(new_keys, ta) then -- Ad-hoc: RFC 5011 doesn't mention removing a Missing key. @@ -338,17 +338,16 @@ update = function (keyset, new_keys, is_initial) end end -- 2: remove all TAs - other settings etc. will remain in the keyset - for i, ta in ipairs(keyset) do + for i, _ in ipairs(keyset) do keyset[i] = nil end -- 3: move TAs to be kept into the keyset (same indices) for k, ta in pairs(keepset) do keyset[k] = ta end - keepset = nil -- Evaluate new TAs - for i, rr in ipairs(new_keys) do + for _, rr in ipairs(new_keys) do if (rr.type == kres.type.DNSKEY or rr.type == kres.type.DS) and rr.rdata ~= nil then ta_present(keyset, rr, hold_down, is_initial) end @@ -402,7 +401,7 @@ local add_file = function (path, unmanaged) error('[ ta ] ERROR: failed to read anchors from path ' .. path) end local owner = keyset[1].owner - for i, ta in ipairs(keyset) do + for _, ta in ipairs(keyset) do if ta.owner ~= owner then error('[ ta ] ERROR: mixed owner names found in path ' .. path) end diff --git a/daemon/lua/zonefile.lua.in b/daemon/lua/zonefile.lua.in index 89b783f86..c954414eb 100644 --- a/daemon/lua/zonefile.lua.in +++ b/daemon/lua/zonefile.lua.in @@ -125,7 +125,6 @@ const char* zs_strerror(const int code); local zs_state = ffi.new('struct zs_state') -- Wrap scanner context -local const_char_t = ffi.typeof('const char *') local zs_scanner_t = ffi.typeof('struct scanner') ffi.metatype( zs_scanner_t, { __gc = function(zs) return libzscanner.zs_deinit(zs) end,