Cleanup eye-gouging madness around io.read calls (#7149)

* Don't reinvent the wheel when reading a one-line int or string from sysfs

* Simplify a whole other bunch of read calls
reviewable/pr7150/r1
NiLuJe 3 years ago committed by GitHub
parent f52fffd467
commit dffe86dfe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -290,11 +290,10 @@ local footerTextGeneratorMap = {
local prefix = symbol_prefix[symbol_type].mem_usage local prefix = symbol_prefix[symbol_type].mem_usage
local statm = io.open("/proc/self/statm", "r") local statm = io.open("/proc/self/statm", "r")
if statm then if statm then
local infos = statm:read("*all") local dummy, rss = statm:read("*number", "*number")
statm:close() statm:close()
local rss = infos:match("^%S+ (%S+) ")
-- we got the nb of 4Kb-pages used, that we convert to Mb -- we got the nb of 4Kb-pages used, that we convert to Mb
rss = math.floor(tonumber(rss) * 4096 / 1024 / 1024) rss = math.floor(rss * 4096 / 1024 / 1024)
return (prefix .. " %d"):format(rss) return (prefix .. " %d"):format(rss)
end end
return "" return ""

@ -7,7 +7,7 @@ local function no() return false end
local function getProductId() local function getProductId()
local ntxinfo_pcb = io.popen("/usr/bin/ntxinfo /dev/mmcblk0 | grep pcb | cut -d ':' -f2", "r") local ntxinfo_pcb = io.popen("/usr/bin/ntxinfo /dev/mmcblk0 | grep pcb | cut -d ':' -f2", "r")
if not ntxinfo_pcb then return 0 end if not ntxinfo_pcb then return 0 end
local product_id = tonumber(ntxinfo_pcb:read()) or 0 local product_id = ntxinfo_pcb:read("*number") or 0
ntxinfo_pcb:close() ntxinfo_pcb:close()
return product_id return product_id
end end
@ -20,23 +20,10 @@ local function isConnected()
if not file then return 0 end if not file then return 0 end
-- 0 means not connected, 1 connected -- 0 means not connected, 1 connected
local out = file:read("*all") local out = file:read("*number")
file:close() file:close()
-- strip NaN from file read (ie: line endings, error messages) return out or 0
local carrier
if type(out) ~= "number" then
carrier = tonumber(out)
else
carrier = out
end
-- finally return if we're connected or not
if type(carrier) == "number" then
return carrier
else
return 0
end
end end
local function isMassStorageSupported() local function isMassStorageSupported()

@ -192,7 +192,7 @@ function CervantesPowerD:getCapacityHW()
end end
function CervantesPowerD:isChargingHW() function CervantesPowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n" return self:read_str_file(self.status_file) == "Charging"
end end
function CervantesPowerD:beforeSuspend() function CervantesPowerD:beforeSuspend()

@ -95,20 +95,20 @@ function BasePowerD:turnOnFrontlight()
end end
function BasePowerD:read_int_file(file) function BasePowerD:read_int_file(file)
local fd = io.open(file, "r") local fd = io.open(file, "r")
if fd then if fd then
local int = fd:read("*all"):match("%d+") local int = fd:read("*number")
fd:close() fd:close()
return int and tonumber(int) or 0 return int or 0
else else
return 0 return 0
end end
end end
function BasePowerD:read_str_file(file) function BasePowerD:read_str_file(file)
local fd = io.open(file, "r") local fd = io.open(file, "r")
if fd then if fd then
local str = fd:read("*all") local str = fd:read("*line")
fd:close() fd:close()
return str return str
else else

@ -678,7 +678,7 @@ function KindleOasis:init()
-- get rotate dev by EV=d -- get rotate dev by EV=d
local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]'", "r") local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]'", "r")
if std_out then if std_out then
local rotation_dev = std_out:read() local rotation_dev = std_out:read("*line")
std_out:close() std_out:close()
if rotation_dev then if rotation_dev then
self.input.open("/dev/input/"..rotation_dev) self.input.open("/dev/input/"..rotation_dev)
@ -752,7 +752,7 @@ function KindleOasis2:init()
-- Get accelerometer device by looking for EV=d -- Get accelerometer device by looking for EV=d
local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]\\{1,2\\}'", "r") local std_out = io.popen("grep -e 'Handlers\\|EV=' /proc/bus/input/devices | grep -B1 'EV=d' | grep -o 'event[0-9]\\{1,2\\}'", "r")
if std_out then if std_out then
local rotation_dev = std_out:read() local rotation_dev = std_out:read("*line")
std_out:close() std_out:close()
if rotation_dev then if rotation_dev then
self.input.open("/dev/input/"..rotation_dev) self.input.open("/dev/input/"..rotation_dev)
@ -790,7 +790,7 @@ function KindlePaperWhite4:init()
-- So, look for a goodix TS input device (c.f., #5110)... -- So, look for a goodix TS input device (c.f., #5110)...
local std_out = io.popen("grep -e 'Handlers\\|Name=' /proc/bus/input/devices | grep -A1 'goodix-ts' | grep -o 'event[0-9]'", "r") local std_out = io.popen("grep -e 'Handlers\\|Name=' /proc/bus/input/devices | grep -A1 'goodix-ts' | grep -o 'event[0-9]'", "r")
if std_out then if std_out then
local goodix_dev = std_out:read() local goodix_dev = std_out:read("*line")
std_out:close() std_out:close()
if goodix_dev then if goodix_dev then
self.touch_dev = "/dev/input/" .. goodix_dev self.touch_dev = "/dev/input/" .. goodix_dev
@ -866,7 +866,7 @@ end
local kindle_sn_fd = io.open("/proc/usid", "r") local kindle_sn_fd = io.open("/proc/usid", "r")
if not kindle_sn_fd then return end if not kindle_sn_fd then return end
local kindle_sn = kindle_sn_fd:read() local kindle_sn = kindle_sn_fd:read("*line")
kindle_sn_fd:close() kindle_sn_fd:close()
-- NOTE: Attempt to sanely differentiate v1 from v2, -- NOTE: Attempt to sanely differentiate v1 from v2,
-- c.f., https://github.com/NiLuJe/FBInk/commit/8a1161734b3f5b4461247af461d26987f6f1632e -- c.f., https://github.com/NiLuJe/FBInk/commit/8a1161734b3f5b4461247af461d26987f6f1632e

@ -106,9 +106,9 @@ function KindlePowerD:getCapacityHW()
else else
local std_out = io.popen("gasgauge-info -c 2>/dev/null", "r") local std_out = io.popen("gasgauge-info -c 2>/dev/null", "r")
if std_out then if std_out then
local result = std_out:read("*all"):match("%d+") local result = std_out:read("*number")
std_out:close() std_out:close()
return result and tonumber(result) or 0 return result or 0
else else
return 0 return 0
end end

@ -495,7 +495,7 @@ function Kobo:getCodeName()
-- If that fails, run the script ourselves -- If that fails, run the script ourselves
if not codename then if not codename then
local std_out = io.popen("/bin/kobo_config.sh 2>/dev/null", "r") local std_out = io.popen("/bin/kobo_config.sh 2>/dev/null", "r")
codename = std_out:read() codename = std_out:read("*line")
std_out:close() std_out:close()
end end
return codename return codename
@ -506,7 +506,7 @@ function Kobo:getFirmwareVersion()
if not version_file then if not version_file then
self.firmware_rev = "none" self.firmware_rev = "none"
end end
local version_str = version_file:read() local version_str = version_file:read("*line")
version_file:close() version_file:close()
local i = 0 local i = 0
@ -527,7 +527,7 @@ local function getProductId()
if not version_file then if not version_file then
return "000" return "000"
end end
local version_str = version_file:read() local version_str = version_file:read("*line")
version_file:close() version_file:close()
product_id = string.sub(version_str, -3, -1) product_id = string.sub(version_str, -3, -1)

@ -318,7 +318,7 @@ function KoboPowerD:getCapacityHW()
end end
function KoboPowerD:isChargingHW() function KoboPowerD:isChargingHW()
return self:read_str_file(self.is_charging_file) == "Charging\n" return self:read_str_file(self.is_charging_file) == "Charging"
end end
function KoboPowerD:turnOffFrontlightHW() function KoboPowerD:turnOffFrontlightHW()

@ -19,7 +19,7 @@ function Remarkable_PowerD:getCapacityHW()
end end
function Remarkable_PowerD:isChargingHW() function Remarkable_PowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n" return self:read_str_file(self.status_file) == "Charging"
end end
return Remarkable_PowerD return Remarkable_PowerD

@ -25,7 +25,7 @@ function SonyPRSTUX_PowerD:getCapacityHW()
end end
function SonyPRSTUX_PowerD:isChargingHW() function SonyPRSTUX_PowerD:isChargingHW()
return self:read_str_file(self.status_file) == "Charging\n" return self:read_str_file(self.status_file) == "Charging"
end end
return SonyPRSTUX_PowerD return SonyPRSTUX_PowerD

@ -98,23 +98,11 @@ function NetworkListener:_getTxPackets()
-- file exists only when Wi-Fi module is loaded. -- file exists only when Wi-Fi module is loaded.
if not file then return nil end if not file then return nil end
local out = file:read("*all") local tx_packets = file:read("*number")
file:close() file:close()
-- strip NaN from file read (i.e.,: line endings, error messages) -- Will be nil if NaN, just like we want it
local tx_packets return tx_packets
if type(out) ~= "number" then
tx_packets = tonumber(out)
else
tx_packets = out
end
-- finally return it
if type(tx_packets) == "number" then
return tx_packets
else
return nil
end
end end
function NetworkListener:_unscheduleActivityCheck() function NetworkListener:_unscheduleActivityCheck()

@ -97,7 +97,7 @@ end
-- Say who we are to Wikipedia (see https://meta.wikimedia.org/wiki/User-Agent_policy) -- Say who we are to Wikipedia (see https://meta.wikimedia.org/wiki/User-Agent_policy)
local USER_AGENT = T("KOReader/%1 (https://koreader.rocks/) %2", local USER_AGENT = T("KOReader/%1 (https://koreader.rocks/) %2",
(lfs.attributes("git-rev", "mode") == "file" and io.open("git-rev", "r"):read() or "devel"), (lfs.attributes("git-rev", "mode") == "file" and io.open("git-rev", "r"):read("*line") or "devel"),
require('socket.http').USERAGENT:gsub(" ", "/") ) require('socket.http').USERAGENT:gsub(" ", "/") )
-- Codes that getUrlContent may get from requester.request() -- Codes that getUrlContent may get from requester.request()
@ -895,7 +895,7 @@ function Wikipedia:createEpub(epub_path, page, lang, with_images)
-- </guide> -- </guide>
local koreader_version = "KOReader" local koreader_version = "KOReader"
if lfs.attributes("git-rev", "mode") == "file" then if lfs.attributes("git-rev", "mode") == "file" then
koreader_version = "KOReader "..io.open("git-rev", "r"):read() koreader_version = "KOReader "..io.open("git-rev", "r"):read("*line")
end end
local content_opf_parts = {} local content_opf_parts = {}
-- head -- head

@ -622,13 +622,8 @@ function util.getFilesystemType(path)
local mounts = io.open("/proc/mounts", "r") local mounts = io.open("/proc/mounts", "r")
if not mounts then return nil end if not mounts then return nil end
local type local type
while true do for line in mounts:lines() do
local line
local mount = {} local mount = {}
line = mounts:read()
if line == nil then
break
end
for param in line:gmatch("%S+") do table.insert(mount, param) end for param in line:gmatch("%S+") do table.insert(mount, param) end
if string.match(path, mount[2]) then if string.match(path, mount[2]) then
type = mount[3] type = mount[3]

@ -10,7 +10,7 @@ function Version:getCurrentRevision()
if not self.rev then if not self.rev then
local rev_file = io.open("git-rev", "r") local rev_file = io.open("git-rev", "r")
if rev_file then if rev_file then
self.rev = rev_file:read() self.rev = rev_file:read("*line")
rev_file:close() rev_file:close()
end end
-- sanity check in case `git describe` failed -- sanity check in case `git describe` failed

@ -54,7 +54,7 @@ local function systemInfo()
do do
local stat = io.open("/proc/stat", "r") local stat = io.open("/proc/stat", "r")
if stat ~= nil then if stat ~= nil then
for line in util.gsplit(stat:read("*all"), "\n", false) do for line in stat:lines() do
local t = util.splitToArray(line, " ") local t = util.splitToArray(line, " ")
if #t >= 5 and string.lower(t[1]) == "cpu" then if #t >= 5 and string.lower(t[1]) == "cpu" then
local n1, n2, n3, n4 local n1, n2, n3, n4
@ -82,7 +82,7 @@ local function systemInfo()
local meminfo = io.open("/proc/meminfo", "r") local meminfo = io.open("/proc/meminfo", "r")
if meminfo ~= nil then if meminfo ~= nil then
result.memory = {} result.memory = {}
for line in util.gsplit(meminfo:read("*all"), "\n", false) do for line in meminfo:lines() do
local t = util.splitToArray(line, " ") local t = util.splitToArray(line, " ")
if #t >= 2 then if #t >= 2 then
if string.lower(t[1]) == "memtotal:" then if string.lower(t[1]) == "memtotal:" then
@ -142,7 +142,7 @@ function SystemStat:appendProcessInfo()
local stat = io.open("/proc/self/stat", "r") local stat = io.open("/proc/self/stat", "r")
if stat == nil then return end if stat == nil then return end
local t = util.splitToArray(stat:read("*all"), " ") local t = util.splitToArray(stat:read("*line"), " ")
stat:close() stat:close()
local n1, n2 local n1, n2
@ -197,7 +197,7 @@ function SystemStat:appendStorageInfo()
if not std_out then return end if not std_out then return end
self:put({_("Storage information"), ""}) self:put({_("Storage information"), ""})
for line in util.gsplit(std_out:read("*all"), "\n", false) do for line in std_out:lines() do
local t = util.splitToArray(line, "\t") local t = util.splitToArray(line, "\t")
if #t ~= 4 then if #t ~= 4 then
self:put({_(" Unexpected"), line}) self:put({_(" Unexpected"), line})

@ -24,10 +24,9 @@ local TimeSync = WidgetContainer:new{
local function currentTime() local function currentTime()
local std_out = io.popen("date") local std_out = io.popen("date")
if std_out then if std_out then
local result = std_out:read("*all") local result = std_out:read("*line")
std_out:close() std_out:close()
if result ~= nil then if result ~= nil then
result = result:gsub("\n", "")
return T(_("New time is %1."), result) return T(_("New time is %1."), result)
end end
end end

@ -227,7 +227,7 @@ describe("device module", function()
} }
elseif filename == "/sys/class/backlight/max77696-bl/brightness" then elseif filename == "/sys/class/backlight/max77696-bl/brightness" then
return { return {
read = function() return "12" end, read = function() return 12 end,
close = function() end close = function() end
} }
else else

Loading…
Cancel
Save