2019-08-23 17:53:53 +00:00
--[[--
Checks for updates on the specified nightly build server .
] ]
2020-01-04 00:18:51 +00:00
local BD = require ( " ui/bidi " )
2014-08-01 13:59:59 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2015-10-03 06:18:47 +00:00
local DataStorage = require ( " datastorage " )
2014-10-30 18:42:18 +00:00
local Device = require ( " device " )
2017-04-06 13:11:21 +00:00
local InfoMessage = require ( " ui/widget/infomessage " )
2019-01-01 19:06:55 +00:00
local MultiConfirmBox = require ( " ui/widget/multiconfirmbox " )
2017-04-06 13:11:21 +00:00
local NetworkMgr = require ( " ui/network/manager " )
local UIManager = require ( " ui/uimanager " )
2017-04-15 12:45:56 +00:00
local Version = require ( " version " )
2017-04-06 13:11:21 +00:00
local lfs = require ( " libs/libkoreader-lfs " )
2016-12-29 08:10:38 +00:00
local logger = require ( " logger " )
2014-08-01 13:59:59 +00:00
local _ = require ( " gettext " )
2017-04-06 13:11:21 +00:00
local T = require ( " ffi/util " ) . template
2014-08-01 04:36:32 +00:00
2015-10-03 06:18:47 +00:00
local ota_dir = DataStorage : getDataDir ( ) .. " /ota/ "
2014-08-01 04:36:32 +00:00
local OTAManager = {
2020-02-04 02:14:23 +00:00
-- NOTE: Each URL *MUST* end with a /
2014-08-06 12:28:09 +00:00
ota_servers = {
2020-02-04 02:14:23 +00:00
" http://ota.koreader.rocks/ " ,
2018-07-03 21:16:45 +00:00
--[[
2020-02-04 02:14:23 +00:00
-- NOTE: Seems down? Ping @chrox ;).
" http://vislab.bjmu.edu.cn/apps/koreader/ota/ " ,
2018-07-03 21:16:45 +00:00
--]]
2020-02-04 02:14:23 +00:00
" http://koreader-fr.ak-team.com/ " ,
" http://koreader-pl.ak-team.com/ " ,
" http://koreader-na.ak-team.com/ " ,
" http://koreader.ak-team.com/ " ,
2014-08-06 12:28:09 +00:00
} ,
ota_channels = {
2015-02-01 09:24:50 +00:00
" stable " ,
2014-08-06 12:28:09 +00:00
" nightly " ,
} ,
2019-02-21 07:26:48 +00:00
link_template = " koreader-%s-latest-%s " ,
2014-08-01 04:36:32 +00:00
zsync_template = " koreader-%s-latest-%s.zsync " ,
2016-03-13 15:42:58 +00:00
installed_package = ota_dir .. " koreader.installed.tar " ,
2014-08-01 04:36:32 +00:00
package_indexfile = " ota/package.index " ,
2016-03-13 15:42:58 +00:00
updated_package = ota_dir .. " koreader.updated.tar " ,
2018-09-05 23:35:48 +00:00
can_pretty_print = lfs.attributes ( " ./fbink " , " mode " ) == " file " and true or false ,
2014-08-01 04:36:32 +00:00
}
2015-02-01 09:24:50 +00:00
local ota_channels = {
2016-12-14 19:08:57 +00:00
stable = _ ( " Stable " ) ,
nightly = _ ( " Development " ) ,
2015-02-01 09:24:50 +00:00
}
2019-07-29 07:54:28 +00:00
local function showRestartMessage ( )
UIManager : show ( ConfirmBox : new {
text = _ ( " KOReader will be updated on next restart. \n Would you like to restart now? " ) ,
ok_text = _ ( " Restart " ) ,
ok_callback = function ( )
local savequit_caller
local save_quit = function ( )
Device : saveSettings ( )
UIManager : quit ( )
UIManager._exit_code = 85
end
local FileManager = require ( " apps/filemanager/filemanager " )
if FileManager.instance then
savequit_caller = FileManager.instance . menu
end
local ReaderUI = require ( " apps/reader/readerui " )
local readerui_instance = ReaderUI : _getRunningInstance ( )
if readerui_instance then
savequit_caller = readerui_instance.menu
end
savequit_caller : exitOrRestart ( save_quit )
end ,
} )
end
2019-01-20 18:08:44 +00:00
-- Try to detect WARIO+ Kindle boards (i.MX6 & i.MX7)
function OTAManager : _isKindleWarioOrMore ( )
local cpu_hw = nil
-- Parse cpuinfo line by line, until we find the Hardware description
for line in io.lines ( " /proc/cpuinfo " ) do
if line : find ( " ^Hardware " ) then
cpu_hw = line : match ( " ^Hardware%s*:%s([%g%s]*)$ " )
end
end
-- NOTE: I couldn't dig up a cpuinfo dump from an Oasis 2 to check the CPU part value,
-- but for Wario (Cortex A9), matching that to 0xc09 would work, too.
-- On the other hand, I'm already using the Hardware match in MRPI, so, that sealed the deal ;).
-- If we've got a Hardware string, check if it mentions an i.MX 6 or 7...
if cpu_hw then
2019-02-01 14:37:15 +00:00
if cpu_hw : find ( " i.MX%s?[6-7] " ) then
2019-01-20 18:08:44 +00:00
return true
else
return false
end
else
return false
end
end
2018-12-15 09:23:51 +00:00
-- "x86", "x64", "arm", "arm64", "ppc", "mips" or "mips64".
local arch = jit.arch
2014-08-01 04:36:32 +00:00
function OTAManager : getOTAModel ( )
2018-10-31 22:48:36 +00:00
if Device : isAndroid ( ) then
2018-12-15 09:23:51 +00:00
if arch == " x86 " then
return " android-x86 "
end
2018-10-31 22:48:36 +00:00
return " android "
2019-02-21 07:26:48 +00:00
elseif Device : isSDL ( ) then
return " appimage "
2018-10-31 22:48:36 +00:00
elseif Device : isCervantes ( ) then
return " cervantes "
elseif Device : isKindle ( ) then
2019-01-20 18:11:20 +00:00
if Device : isTouchDevice ( ) or Device.model == " Kindle4 " then
2019-01-20 18:08:44 +00:00
if self : _isKindleWarioOrMore ( ) then
return " kindlepw2 "
else
return " kindle "
end
2016-11-08 05:43:10 +00:00
else
return " kindle-legacy "
end
2014-08-01 04:36:32 +00:00
elseif Device : isKobo ( ) then
return " kobo "
2015-01-19 16:07:36 +00:00
elseif Device : isPocketBook ( ) then
return " pocketbook "
2020-02-08 00:58:10 +00:00
elseif Device : isRemarkable ( ) then
return " remarkable "
2018-10-06 05:55:35 +00:00
elseif Device : isSonyPRSTUX ( ) then
return " sony-prstux "
2014-08-01 04:36:32 +00:00
else
return " "
end
end
2019-02-21 07:26:48 +00:00
function OTAManager : getOTAType ( )
local ota_model = self : getOTAModel ( )
if ota_model == " " then return end
if ota_model : find ( " android " ) or ota_model : find ( " appimage " ) then
return " link "
end
return " ota "
end
2014-08-06 12:28:09 +00:00
function OTAManager : getOTAServer ( )
return G_reader_settings : readSetting ( " ota_server " ) or self.ota_servers [ 1 ]
end
function OTAManager : setOTAServer ( server )
2016-12-29 08:10:38 +00:00
logger.dbg ( " Set OTA server: " , server )
2014-08-06 12:28:09 +00:00
G_reader_settings : saveSetting ( " ota_server " , server )
end
2014-08-01 04:36:32 +00:00
function OTAManager : getOTAChannel ( )
2014-08-06 12:28:09 +00:00
return G_reader_settings : readSetting ( " ota_channel " ) or self.ota_channels [ 1 ]
2014-08-01 04:36:32 +00:00
end
function OTAManager : setOTAChannel ( channel )
2016-12-29 08:10:38 +00:00
logger.dbg ( " Set OTA channel: " , channel )
2014-08-06 12:28:09 +00:00
G_reader_settings : saveSetting ( " ota_channel " , channel )
2014-08-01 04:36:32 +00:00
end
2019-02-21 07:26:48 +00:00
function OTAManager : getLinkFilename ( )
return self.link_template : format ( self : getOTAModel ( ) , self : getOTAChannel ( ) )
end
2014-08-01 04:36:32 +00:00
function OTAManager : getZsyncFilename ( )
return self.zsync_template : format ( self : getOTAModel ( ) , self : getOTAChannel ( ) )
end
function OTAManager : checkUpdate ( )
local http = require ( " socket.http " )
local ltn12 = require ( " ltn12 " )
2019-02-21 07:26:48 +00:00
local update_file = ( self : getOTAType ( ) == " link " ) and self : getLinkFilename ( ) or self : getZsyncFilename ( )
local ota_update_file = self : getOTAServer ( ) .. update_file
local local_update_file = ota_dir .. update_file
2014-08-01 04:36:32 +00:00
-- download zsync file from OTA server
2019-02-21 07:26:48 +00:00
logger.dbg ( " downloading update file " , ota_update_file )
2016-02-16 02:08:04 +00:00
local _ , c , _ = http.request {
2019-02-21 07:26:48 +00:00
url = ota_update_file ,
sink = ltn12.sink . file ( io.open ( local_update_file , " w " ) ) }
2015-02-01 09:24:50 +00:00
if c ~= 200 then
2019-02-21 07:26:48 +00:00
logger.warn ( " cannot find update file " , c )
2015-02-01 09:24:50 +00:00
return
end
2014-08-20 07:46:43 +00:00
-- parse OTA package version
2019-02-21 07:26:48 +00:00
local link , ota_package
local update_info = io.open ( local_update_file , " r " )
if update_info then
if OTAManager : getOTAType ( ) == " link " then
local i = 0
for line in update_info : lines ( ) do
i = i + 1
if i == 1 then
ota_package = line
link = self : getOTAServer ( ) .. ota_package
end
end
else
for line in update_info : lines ( ) do
ota_package = line : match ( " ^Filename:%s*(.-)%s*$ " )
if ota_package then break end
end
2014-08-01 04:36:32 +00:00
end
2019-02-21 07:26:48 +00:00
update_info : close ( )
2014-08-01 04:36:32 +00:00
end
2014-11-10 12:21:25 +00:00
local local_ok , local_version = pcall ( function ( )
2017-04-15 12:45:56 +00:00
local rev = Version : getCurrentRevision ( )
if rev then return Version : getNormalizedVersion ( rev ) end
2014-11-10 12:21:25 +00:00
end )
local ota_ok , ota_version = pcall ( function ( )
2017-04-15 12:45:56 +00:00
return Version : getNormalizedVersion ( ota_package )
2014-11-10 12:21:25 +00:00
end )
2014-08-01 04:36:32 +00:00
-- return ota package version if package on OTA server has version
-- larger than the local package version
2014-11-10 12:21:25 +00:00
if local_ok and ota_ok and ota_version and local_version and
2015-02-01 09:24:50 +00:00
ota_version ~= local_version then
2019-02-23 13:25:29 +00:00
return ota_version , local_version , link , ota_package
2014-08-01 04:36:32 +00:00
elseif ota_version and ota_version == local_version then
return 0
end
end
2014-08-26 19:21:18 +00:00
function OTAManager : fetchAndProcessUpdate ( )
2019-02-23 13:25:29 +00:00
local ota_version , local_version , link , ota_package = OTAManager : checkUpdate ( )
2019-02-21 07:26:48 +00:00
2014-08-26 19:21:18 +00:00
if ota_version == 0 then
UIManager : show ( InfoMessage : new {
2017-04-06 13:11:21 +00:00
text = _ ( " KOReader is up to date. " ) ,
2014-08-26 19:21:18 +00:00
} )
elseif ota_version == nil then
2015-04-15 05:38:01 +00:00
local channel = ota_channels [ OTAManager : getOTAChannel ( ) ]
2014-08-26 19:21:18 +00:00
UIManager : show ( InfoMessage : new {
2019-02-21 07:26:48 +00:00
text = T ( _ ( " Update information not available on %1 channel. " ) , channel ) ,
2014-08-26 19:21:18 +00:00
} )
elseif ota_version then
2019-02-25 21:50:01 +00:00
local update_message = T ( _ ( " Do you want to update? \n Installed version: %1 \n Available version: %2 " ) ,
2020-01-04 00:18:51 +00:00
BD.ltr ( local_version ) ,
BD.ltr ( ota_version ) )
2019-02-25 21:50:01 +00:00
local update_ok_text = _ ( " Update " )
if ota_version < local_version then
update_message = T ( _ ( " The currently installed version is newer than the available version. \n Would you still like to continue and downgrade? \n Installed version: %1 \n Available version: %2 " ) ,
2020-01-04 00:18:51 +00:00
BD.ltr ( local_version ) ,
BD.ltr ( ota_version ) )
2019-02-25 21:50:01 +00:00
update_ok_text = _ ( " Downgrade " )
end
2019-02-21 07:26:48 +00:00
if OTAManager : getOTAType ( ) == " link " then
UIManager : show ( ConfirmBox : new {
2019-02-25 21:50:01 +00:00
text = update_message ,
ok_text = update_ok_text ,
2019-02-21 07:26:48 +00:00
ok_callback = function ( )
2019-02-23 13:25:29 +00:00
local isAndroid , android = pcall ( require , " android " )
if isAndroid then
2020-01-04 19:59:52 +00:00
-- try to download the package
local ok = android.download ( link , ota_package )
if ok == 1 then
android.notification ( T ( _ ( " The file %1 already exists. " ) , ota_package ) )
elseif ok == 0 then
android.notification ( T ( _ ( " Downloading %1 " ) , ota_package ) )
2019-02-23 13:25:29 +00:00
else
2020-01-04 19:59:52 +00:00
UIManager : show ( ConfirmBox : new {
text = _ ( " Your device seems to be unable to download packages. \n Retry using the browser? " ) ,
ok_text = _ ( " Retry " ) ,
ok_callback = function ( ) Device : openLink ( link ) end ,
} )
2019-02-23 13:25:29 +00:00
end
elseif Device : isSDL ( ) then
2019-03-20 15:58:32 +00:00
Device : openLink ( link )
2014-08-26 19:21:18 +00:00
end
2019-02-21 07:26:48 +00:00
end
} )
else
UIManager : show ( ConfirmBox : new {
2019-02-25 21:50:01 +00:00
text = update_message ,
ok_text = update_ok_text ,
2019-02-21 07:26:48 +00:00
ok_callback = function ( )
UIManager : show ( InfoMessage : new {
text = _ ( " Downloading may take several minutes… " ) ,
timeout = 3 ,
} )
UIManager : scheduleIn ( 1 , function ( )
if OTAManager : zsync ( ) == 0 then
2019-07-29 07:54:28 +00:00
showRestartMessage ( )
2019-02-21 07:26:48 +00:00
-- Make it clear that zsync is done
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
end
else
-- Make it clear that zsync is done
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
end
UIManager : show ( MultiConfirmBox : new {
text = _ ( " Failed to update KOReader. \n \n You can: \n Cancel, keeping temporary files. \n Retry the update process with a full download. \n Abort and cleanup all temporary files. " ) ,
choice1_text = _ ( " Retry " ) ,
choice1_callback = function ( )
UIManager : show ( InfoMessage : new {
text = _ ( " Downloading may take several minutes… " ) ,
timeout = 3 ,
} )
-- Clear the installed package, as well as the complete/incomplete update download
2020-02-09 04:08:46 +00:00
os.execute ( " rm -f " .. self.installed_package )
os.execute ( " rm -f " .. self.updated_package .. " * " )
-- As well as temporary files, in case zsync went kablooey too early...
os.execute ( " rm -f ./rcksum-* " )
2019-02-21 07:26:48 +00:00
-- And then relaunch zsync in full download mode...
UIManager : scheduleIn ( 1 , function ( )
if OTAManager : zsync ( true ) == 0 then
2019-07-29 07:54:28 +00:00
showRestartMessage ( )
2019-02-21 07:26:48 +00:00
-- Make it clear that zsync is done
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
end
else
-- Make it clear that zsync is done
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
end
UIManager : show ( ConfirmBox : new {
text = _ ( " Error updating KOReader. Would you like to delete temporary files? " ) ,
ok_callback = function ( )
2020-02-09 04:08:46 +00:00
os.execute ( " rm -f " .. ota_dir .. " ko* " )
2019-02-21 07:26:48 +00:00
end ,
} )
end
end )
end ,
choice2_text = _ ( " Abort " ) ,
choice2_callback = function ( )
2020-02-09 04:08:46 +00:00
os.execute ( " rm -f " .. ota_dir .. " ko* " )
os.execute ( " rm -f " .. self.updated_package .. " * " )
os.execute ( " rm -f ./rcksum-* " )
2019-02-21 07:26:48 +00:00
end ,
} )
end
end )
end
} )
end
2014-08-26 19:21:18 +00:00
end
end
2019-08-23 17:53:53 +00:00
---- Uses zsync and tar to prepare an update package.
2014-08-01 04:36:32 +00:00
function OTAManager : _buildLocalPackage ( )
2019-08-23 17:53:53 +00:00
--- @todo Validate the installed package?
2016-03-13 15:42:58 +00:00
local installed_package = self.installed_package
2014-10-14 05:04:49 +00:00
if lfs.attributes ( installed_package , " mode " ) == " file " then
return 0
end
2017-04-23 02:10:48 +00:00
if lfs.attributes ( self.package_indexfile , " mode " ) ~= " file " then
logger.err ( " Missing ota metadata: " , self.package_indexfile )
return nil
end
2019-02-21 07:26:48 +00:00
-- With visual feedback if supported...
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pmh 'Preparing local OTA package' " )
-- We need a vague idea of how much space the tarball we're creating will take to compute a proper percentage...
-- Get the size from the latest zsync package, which'll be a closer match than anything else we might come up with.
local update_file = self : getZsyncFilename ( )
local local_update_file = ota_dir .. update_file
local tarball_size = nil
local zsync = io.open ( local_update_file , " r " )
if zsync then
for line in zsync : lines ( ) do
tarball_size = line : match ( " ^Length: (%d*)$ " )
if tarball_size then break end
2018-09-05 23:35:48 +00:00
end
2019-02-21 07:26:48 +00:00
zsync : close ( )
end
-- Next, we need to compute the amount of tar blocks that'll take, knowing that tar's default blocksize is 20 * 512 bytes.
-- c.f., https://superuser.com/questions/168749 & http://www.noah.org/wiki/tar_notes
-- Defaults to a sane-ish value as-of now, in case shit happens...
local blocks = 6405
if tarball_size then
blocks = tarball_size / ( 512 * 20 )
2018-07-03 21:16:45 +00:00
end
2019-02-21 07:26:48 +00:00
-- And since we want a percentage, devise the exact value we need for tar to spit out exactly 100 checkpoints ;).
local cpoints = blocks / 100
return os.execute ( string.format (
" ./tar --no-recursion -cf %s -C .. -T %s --checkpoint=%d --checkpoint-action=exec='./fbink -q -y -6 -P $(($TAR_CHECKPOINT/%d))' " ,
self.installed_package , self.package_indexfile , cpoints , cpoints ) )
else
return os.execute ( string.format (
" ./tar --no-recursion -cf %s -C .. -T %s " ,
self.installed_package , self.package_indexfile ) )
2016-03-13 15:42:58 +00:00
end
2014-08-01 04:36:32 +00:00
end
2019-01-01 19:06:55 +00:00
function OTAManager : zsync ( full_dl )
if full_dl or self : _buildLocalPackage ( ) == 0 then
2020-02-04 02:14:23 +00:00
local zsync_wrapper = " zsync2 "
2020-02-11 23:07:05 +00:00
local use_pipefail = true
2018-09-05 23:35:48 +00:00
-- With visual feedback if supported...
if self.can_pretty_print then
zsync_wrapper = " spinning_zsync "
2020-02-11 23:07:05 +00:00
-- And because everything is terrible, we can't check for pipefail's usability in spinning_zsync,
-- because older ash versions abort on set -o failures...
-- c.f., ko/#5844
-- So, instead, check from this side of the fence...
-- (remember, os.execute is essentially system(), it goes through sh)
use_pipefail = ( os.execute ( " set -o pipefail 2>/dev/null " ) == 0 )
2018-07-03 21:16:45 +00:00
end
2019-01-01 19:06:55 +00:00
-- If that's a full-download fallback, drop the input tarball
if full_dl then
return os.execute (
2020-02-11 23:07:05 +00:00
( " env WITH_PIPEFAIL='%s' ./%s -o '%s' -u '%s' '%s%s' " ) : format (
use_pipefail ,
2019-01-01 19:06:55 +00:00
zsync_wrapper ,
self.updated_package ,
self : getOTAServer ( ) ,
ota_dir ,
self : getZsyncFilename ( ) )
)
else
return os.execute (
2020-02-11 23:07:05 +00:00
( " env WITH_PIPEFAIL='%s' ./%s -i '%s' -o '%s' -u '%s' '%s%s' " ) : format (
use_pipefail ,
2019-01-01 19:06:55 +00:00
zsync_wrapper ,
self.installed_package ,
self.updated_package ,
self : getOTAServer ( ) ,
ota_dir ,
self : getZsyncFilename ( ) )
)
end
2014-08-01 04:36:32 +00:00
end
end
2014-08-06 12:28:09 +00:00
function OTAManager : genServerList ( )
local servers = { }
for _ , server in ipairs ( self.ota_servers ) do
local server_item = {
text = server ,
checked_func = function ( ) return self : getOTAServer ( ) == server end ,
callback = function ( ) self : setOTAServer ( server ) end
}
table.insert ( servers , server_item )
end
return servers
end
function OTAManager : genChannelList ( )
local channels = { }
for _ , channel in ipairs ( self.ota_channels ) do
local channel_item = {
2015-02-01 09:24:50 +00:00
text = ota_channels [ channel ] ,
2014-08-06 12:28:09 +00:00
checked_func = function ( ) return self : getOTAChannel ( ) == channel end ,
callback = function ( ) self : setOTAChannel ( channel ) end
}
table.insert ( channels , channel_item )
end
return channels
end
2014-08-01 14:23:08 +00:00
function OTAManager : getOTAMenuTable ( )
2014-08-01 13:59:59 +00:00
return {
2019-02-21 07:26:48 +00:00
text = _ ( " Update " ) ,
hold_callback = function ( )
if not NetworkMgr : isOnline ( ) then
NetworkMgr : promptWifiOn ( )
else
OTAManager : fetchAndProcessUpdate ( )
end
end ,
2014-08-06 12:28:09 +00:00
sub_item_table = {
{
2014-11-12 11:29:38 +00:00
text = _ ( " Check for update " ) ,
2014-08-06 12:28:09 +00:00
callback = function ( )
2016-11-28 00:40:47 +00:00
if not NetworkMgr : isOnline ( ) then
2014-08-24 12:34:38 +00:00
NetworkMgr : promptWifiOn ( )
2014-08-25 17:06:15 +00:00
else
2015-04-15 05:38:01 +00:00
OTAManager : fetchAndProcessUpdate ( )
2014-08-01 13:59:59 +00:00
end
2014-08-06 12:28:09 +00:00
end
} ,
{
text = _ ( " Settings " ) ,
sub_item_table = {
{
2019-02-21 07:26:48 +00:00
text = _ ( " Update server " ) ,
2014-08-06 12:28:09 +00:00
sub_item_table = self : genServerList ( )
} ,
{
2019-02-21 07:26:48 +00:00
text = _ ( " Update channel " ) ,
2014-08-06 12:28:09 +00:00
sub_item_table = self : genChannelList ( )
} ,
}
} ,
}
2014-08-01 13:59:59 +00:00
}
end
2014-08-01 04:36:32 +00:00
return OTAManager