forked from koreader/koreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userpatch.lua
106 lines (90 loc) · 4.35 KB
/
userpatch.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--[[--
Allows applying developer patches while running KOReader.
The contents in `koreader/patches/` are applied on calling `userpatch.applyPatches(priority)`.
--]]--
local isAndroid, android = pcall(require, "android")
local userpatch = {
-- priorities for user patches,
early_once = "0", -- to be started early on startup (once after an update)
early = "1", -- to be started early on startup (always, but after an `early_once`)
late = "2", -- to be started after UIManager is ready (always)
-- 3-7 are reserved for later use
before_exit = "8", -- to be started a bit before exit before settings are saved (always)
on_exit = "9", -- to be started right before exit (always)
-- hash table for patch execution status
-- key: name of the patch
-- value: true (success), false (failure), nil (not executed)
execution_status = {},
-- the patch function itself
applyPatches = function(priority) end, -- to be overwritten, if the device allows it.
}
if isAndroid and android.prop.flavor == "fdroid" then
return userpatch -- allows to use applyPatches as a no-op on F-Droid flavor
end
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local sort = require("sort")
local DataStorage = require("datastorage")
-- the directory KOReader is installed in (and runs from)
local package_dir = lfs.currentdir()
-- the directory where KOReader stores user data
local data_dir = DataStorage:getDataDir()
--- Run lua patches
-- Execution order order is alphanum-sort for humans version 4: `1-patch.lua` is executed before `10-patch.lua`
-- (see http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua)
-- string directory ... to scan through (flat no recursion)
-- string priority ... only files starting with `priority` followed by digits and a '-' will be processed.
-- return true if a patch was executed
local function runUserPatchTasks(dir, priority)
if lfs.attributes(dir, "mode") ~= "directory" then
return
end
local patches = {}
for entry in lfs.dir(dir) do
local mode = lfs.attributes(dir .. "/" .. entry, "mode")
if entry and mode == "file" and entry:match("^" .. priority .. "%d*%-") then
table.insert(patches, entry)
end
end
if #patches == 0 then
return -- nothing to do
end
table.sort(patches, sort.natsort_cmp())
for i, entry in ipairs(patches) do
local fullpath = dir .. "/" .. entry
if lfs.attributes(fullpath, "mode") == "file" then
if fullpath:match("%.lua$") then -- execute patch-files first
logger.info("Applying patch:", fullpath)
local ok, err = pcall(dofile, fullpath)
userpatch.execution_status[entry] = ok
if not ok then
logger.warn("Patching failed:", err)
-- Only show InfoMessage, when UIManager is working
if priority >= userpatch.late and priority < userpatch.before_exit then
-- Only developers (advanced users) will use this mechanism.
-- A warning on a patch failure after an OTA update will simplify troubleshooting.
local UIManager = require("ui/uimanager")
local InfoMessage = require("ui/widget/infomessage")
UIManager:show(InfoMessage:new{text = "Error applying patch:\n" .. fullpath}) -- no translate
end
end
end
end
end
return true
end
--- This function applies lua patches from `/koreader/patches`
---- @string priority ... one of the defined priorities in the userpatch hashtable
function userpatch.applyPatches(priority)
local patch_dir = data_dir .. "/patches"
local update_once_marker = package_dir .. "/update_once.marker"
local update_once_pending = lfs.attributes(update_once_marker, "mode") == "file"
if priority >= userpatch.early or update_once_pending then
local executed_something = runUserPatchTasks(patch_dir, priority)
if executed_something and update_once_pending then
-- Only delete update once marker if `early_once` updates have been applied.
os.remove(update_once_marker) -- Prevent another execution on a further starts.
end
end
end
return userpatch