diff --git a/plugins/coverbrowser.koplugin/bookinfomanager.lua b/plugins/coverbrowser.koplugin/bookinfomanager.lua index 26f2d479f..f4f9f3a6e 100644 --- a/plugins/coverbrowser.koplugin/bookinfomanager.lua +++ b/plugins/coverbrowser.koplugin/bookinfomanager.lua @@ -308,7 +308,8 @@ function BookInfoManager:extractBookInfo(filepath, cover_specs) -- cre.initCache("", 1024*1024*32) -- empty path = no cache -- But it's best to use a cache for quicker and less memory -- usage when opening big books: - cre.initCache(self.tmpcr3cache, 0) -- 0 = previous book caches are removed when opening a book + cre.initCache(self.tmpcr3cache, 0, -- 0 = previous book caches are removed when opening a book + true, G_reader_settings:readSetting("cre_storage_size_factor")) -- avoid CRE WARNINGs self.cre_cache_overriden = true end @@ -440,6 +441,8 @@ function BookInfoManager:extractBookInfo(filepath, cover_specs) end end DocumentRegistry:closeDocument(filepath) + else + loaded = false end if not loaded then dbrow.unsupported = _("not readable by engine") @@ -513,7 +516,7 @@ function BookInfoManager:collectSubprocesses() local i = 1 while i <= #self.subprocesses_pids do -- clean in-place local pid = self.subprocesses_pids[i] - if xutil.isSubProcessDone(pid) then + if util.isSubProcessDone(pid) then table.remove(self.subprocesses_pids, i) else i = i + 1 @@ -551,7 +554,7 @@ end function BookInfoManager:terminateBackgroundJobs() logger.dbg("terminating", #self.subprocesses_pids, "subprocesses") for i=1, #self.subprocesses_pids do - xutil.terminateSubProcess(self.subprocesses_pids[i]) + util.terminateSubProcess(self.subprocesses_pids[i]) end end @@ -587,7 +590,7 @@ function BookInfoManager:extractInBackground(files) self.cleanup_needed = true -- so we will remove temporary cache directory created by subprocess -- Run task in sub-process, and remember its pid - local task_pid = xutil.runInSubProcess(task) + local task_pid = util.runInSubProcess(task) if not task_pid then logger.warn("Failed lauching background extraction sub-process (fork failed)") return false -- let caller know it failed diff --git a/plugins/coverbrowser.koplugin/xutil.lua b/plugins/coverbrowser.koplugin/xutil.lua index 270de476e..263734a97 100644 --- a/plugins/coverbrowser.koplugin/xutil.lua +++ b/plugins/coverbrowser.koplugin/xutil.lua @@ -1,56 +1,9 @@ local ffi = require("ffi") -local C = ffi.C -- Utilities functions needed by this plugin, but that may be added to -- existing base/ffi/ files local xutil = {} - --- Sub-process management (may be put into base/ffi/util.lua) -function xutil.runInSubProcess(func) - local pid = C.fork() - if pid == 0 then -- child process - -- Just run the provided lua code object in this new process, - -- and exit immediatly (so we do not release drivers and - -- resources still used by parent process) - func() - os.exit(0) - end - -- parent/main process, return pid of child - if pid == -1 then -- On failure, -1 is returned in the parent - return false - end - return pid -end - -function xutil.isSubProcessDone(pid) - local status = ffi.new('int[1]') - local ret = C.waitpid(pid, status, 1) -- 1 = WNOHANG : don't wait, just tell - -- status = tonumber(status[0]) - -- local logger = require("logger") - -- logger.dbg("waitpid for", pid, ":", ret, "/", status) - -- still running: ret = 0 , status = 0 - -- exited: ret = pid , status = 0 or 9 if killed - -- no more running: ret = -1 , status = 0 - if ret == pid or ret == -1 then - return true - end -end - -function xutil.terminateSubProcess(pid) - local done = xutil.isSubProcessDone(pid) - if not done then - -- local logger = require("logger") - -- logger.dbg("killing subprocess", pid) - -- we kill with signal 9/SIGKILL, which may be violent, but ensures - -- that it is terminated (a process may catch or ignore SIGTERM) - C.kill(pid, 9) - -- process will still have to be collected with calls to util.isSubProcessDone(), - -- which may still return false for some small amount of time after our kill() - end -end - - -- Data compression/decompression of strings thru zlib (may be put in a new base/ffi/zlib.lua) -- from http://luajit.org/ext_ffi_tutorial.html ffi.cdef[[ @@ -79,7 +32,6 @@ function xutil.zlib_uncompress(zdata, datalen) return ffi.string(buf, buflen[0]) end - -- Not provided by base/thirdparty/lua-ljsqlite3/init.lua -- Add a timeout to a lua-ljsqlite3 connection -- We need that if we have multiple processes accessing the same