Fixed io.lines() bug

In Lua 5.2 io.lines() has to be used to iterate until EOF, otherwise
the fd will not be closed and eventually tons of naughty messages
will start to pop up saying that no more fd can be opened.

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org>
This commit is contained in:
Arvydas Sidorenko 2012-06-16 12:54:51 +02:00 committed by Adrian C. (anrxc)
parent 8e35a983bf
commit 363c03e79c
2 changed files with 9 additions and 4 deletions

View File

@ -7,7 +7,7 @@
-- {{{ Grab environment -- {{{ Grab environment
local ipairs = ipairs local ipairs = ipairs
local io = { lines = io.lines } local io = { open = io.open }
local setmetatable = setmetatable local setmetatable = setmetatable
local math = { floor = math.floor } local math = { floor = math.floor }
local table = { insert = table.insert } local table = { insert = table.insert }
@ -15,6 +15,7 @@ local string = {
sub = string.sub, sub = string.sub,
gmatch = string.gmatch gmatch = string.gmatch
} }
local naught = require("naughty")
-- }}} -- }}}
@ -33,7 +34,8 @@ local function worker(format)
local cpu_lines = {} local cpu_lines = {}
-- Get CPU stats -- Get CPU stats
for line in io.lines("/proc/stat") do local fd = io.open("/proc/stat")
for line in fd:lines() do
if string.sub(line, 1, 3) ~= "cpu" then break end if string.sub(line, 1, 3) ~= "cpu" then break end
cpu_lines[#cpu_lines+1] = {} cpu_lines[#cpu_lines+1] = {}
@ -42,6 +44,7 @@ local function worker(format)
table.insert(cpu_lines[#cpu_lines], i) table.insert(cpu_lines[#cpu_lines], i)
end end
end end
fd:close()
-- Ensure tables are initialized correctly -- Ensure tables are initialized correctly
for i = #cpu_total + 1, #cpu_lines do for i = #cpu_total + 1, #cpu_lines do

View File

@ -4,7 +4,7 @@
----------------------------------------------------- -----------------------------------------------------
-- {{{ Grab environment -- {{{ Grab environment
local io = { lines = io.lines } local io = { open = io.open }
local setmetatable = setmetatable local setmetatable = setmetatable
local string = { local string = {
len = string.len, len = string.len,
@ -33,7 +33,8 @@ local function worker(format, warg)
} }
-- Linux manual page: md(4) -- Linux manual page: md(4)
for line in io.lines("/proc/mdstat") do local fd = io.open("/proc/mdstat")
for line in fd:lines() do
if mddev[warg]["found"] then if mddev[warg]["found"] then
local updev = string.match(line, "%[[_U]+%]") local updev = string.match(line, "%[[_U]+%]")
@ -50,6 +51,7 @@ local function worker(format, warg)
end end
end end
end end
fd:close()
return {mddev[warg]["assigned"], mddev[warg]["active"]} return {mddev[warg]["assigned"], mddev[warg]["active"]}
end end