From 23cd7e24bb0d1ed63592411abec9749d222eb589 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sat, 19 Mar 2022 23:57:31 +0100 Subject: [PATCH] Minor Lua I/O cleanups (#8921) Mostly making sure we always explicitly close io handles. --- frontend/dbg.lua | 11 +++++++---- frontend/device/sysfs_light.lua | 2 +- frontend/gettext.lua | 1 + frontend/ui/wikipedia.lua | 3 ++- platform/kindle/koreader.sh | 5 +++++ plugins/exporter.koplugin/clip.lua | 1 + plugins/japanese.koplugin/deinflector.lua | 1 + plugins/statistics.koplugin/main.lua | 1 + plugins/terminal.koplugin/main.lua | 2 +- 9 files changed, 20 insertions(+), 7 deletions(-) diff --git a/frontend/dbg.lua b/frontend/dbg.lua index 99b7a28db..315533c6d 100644 --- a/frontend/dbg.lua +++ b/frontend/dbg.lua @@ -80,11 +80,14 @@ function Dbg:turnOn() --- @note: On Linux, use CLOEXEC to avoid polluting the fd table of our child processes. --- Otherwise, it can be problematic w/ wpa_supplicant & USBMS... --- Note that this is entirely undocumented, but at least LuaJIT passes the mode as-is to fopen, so, we're good. + local open_flags = "w" if jit.os == "Linux" then - self.ev_log = io.open("ev.log", "we") - else - self.ev_log = io.open("ev.log", "w") + -- Oldest Kindle devices are too old to support O_CLOEXEC... + if os.getenv("KINDLE_LEGACY") ~= "yes" then + open_flags = "we" + end end + self.ev_log = io.open("ev.log", open_flags) end --- Turn off debug mode. @@ -99,7 +102,7 @@ function Dbg:turnOff() return check end if self.ev_log then - io.close(self.ev_log) + self.ev_log:close() self.ev_log = nil end end diff --git a/frontend/device/sysfs_light.lua b/frontend/device/sysfs_light.lua index a113f588d..a090a1a3d 100644 --- a/frontend/device/sysfs_light.lua +++ b/frontend/device/sysfs_light.lua @@ -140,7 +140,7 @@ function SysfsLight:_write_value(file, value) return false end local ret, err_msg, err_code = f:write(value) - io.close(f) + f:close() if not ret then logger.err("Write error: ", err_msg, err_code) return false diff --git a/frontend/gettext.lua b/frontend/gettext.lua index 3f848b72b..796d8b0cc 100644 --- a/frontend/gettext.lua +++ b/frontend/gettext.lua @@ -280,6 +280,7 @@ function GetText_mt.__index.changeLang(new_lang) end end end + po:close() GetText.current_lang = new_lang end diff --git a/frontend/ui/wikipedia.lua b/frontend/ui/wikipedia.lua index a5e565dc7..51797269f 100644 --- a/frontend/ui/wikipedia.lua +++ b/frontend/ui/wikipedia.lua @@ -851,7 +851,8 @@ function Wikipedia:createEpub(epub_path, page, lang, with_images) -- local koreader_version = "KOReader" if lfs.attributes("git-rev", "mode") == "file" then - koreader_version = "KOReader "..io.open("git-rev", "r"):read("*line") + local Version = require("version") + koreader_version = "KOReader " .. Version:getCurrentRevision() end local content_opf_parts = {} -- head diff --git a/platform/kindle/koreader.sh b/platform/kindle/koreader.sh index d027a2d3f..9afe11753 100755 --- a/platform/kindle/koreader.sh +++ b/platform/kindle/koreader.sh @@ -226,6 +226,11 @@ fi # c.f., https://stackoverflow.com/a/37939589 version() { echo "$@" | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'; } +# Detect kernels w/ CLOEXEC support +if [ "$(version "$(uname -r | sed -n -r 's/^([[:digit:]\.]*)(.*?)$/\1/p')")" -lt "$(version "2.6.23")" ]; then + export KINDLE_LEGACY="yes" +fi + # There's no pillow if we stopped the framework, and it's only there on systems with upstart anyway if [ "${STOP_FRAMEWORK}" = "no" ] && [ "${INIT_TYPE}" = "upstart" ]; then # NOTE: If we were launched from KUAL, don't even try to deal with KPVBooklet-specific workarounds diff --git a/plugins/exporter.koplugin/clip.lua b/plugins/exporter.koplugin/clip.lua index 64f3e130d..e1e000722 100644 --- a/plugins/exporter.koplugin/clip.lua +++ b/plugins/exporter.koplugin/clip.lua @@ -85,6 +85,7 @@ function MyClipping:parseMyClippings() end index = index + 1 end + file:close() end return clippings diff --git a/plugins/japanese.koplugin/deinflector.lua b/plugins/japanese.koplugin/deinflector.lua index 68db9b323..eb1a8018e 100644 --- a/plugins/japanese.koplugin/deinflector.lua +++ b/plugins/japanese.koplugin/deinflector.lua @@ -58,6 +58,7 @@ local function parsePluginJson(filename) local file, err = io.open(jsonPath, "r") if file then local contents = file:read("*all") + file:close() local ok, parsed = pcall(JSON.decode, contents) if ok then return parsed diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index b265b1ef9..08aa4b2a7 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -454,6 +454,7 @@ function ReaderStatistics:partialMd5(file) break end end + file_handle:close() return update() end diff --git a/plugins/terminal.koplugin/main.lua b/plugins/terminal.koplugin/main.lua index 2375a19b7..ec7d8cdea 100644 --- a/plugins/terminal.koplugin/main.lua +++ b/plugins/terminal.koplugin/main.lua @@ -245,7 +245,7 @@ function Terminal:killShell(ask) return -1 end - local pid = tonumber(pid_file:read("*a")) + local pid = pid_file:read("*n") pid_file:close() if ask then