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 = {
2014-08-06 12:28:09 +00:00
ota_servers = {
2016-11-08 05:17:01 +00:00
" http://ota.koreader.rocks:80/ " ,
2014-08-22 09:21:32 +00:00
" http://vislab.bjmu.edu.cn:80/apps/koreader/ota/ " ,
2018-07-03 21:16:45 +00:00
--[[
-- NOTE: Because we can't have nice things,
-- the HTTP frontend of these OpenStack storage containers doesn't actually properly support
-- HTTP/1.1 Range requests when multiple byte ranges are requested: they return bogus data when doing so,
-- which confuses zsync, causing it to retry indefinitely instead of aborting...
-- c.f., https://github.com/koreader/koreader-base/pull/699
2018-04-21 11:34:07 +00:00
" http://koreader-fr.ak-team.com:80/ " ,
" http://koreader-pl.ak-team.com:80/ " ,
2016-02-16 19:45:37 +00:00
" http://koreader-na.ak-team.com:80/ " ,
2018-07-03 21:16:45 +00:00
--]]
2014-08-22 09:21:32 +00:00
" http://koreader.ak-team.com:80/ " ,
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 " ,
} ,
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-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
if cpu_hw : find ( " i.MX [6-7] " ) then
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 "
elseif Device : isCervantes ( ) then
return " cervantes "
elseif Device : isKindle ( ) then
2016-11-08 05:43:10 +00:00
if Device : isTouchDevice ( ) 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 "
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
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
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 " )
local zsync_file = self : getZsyncFilename ( )
2014-08-06 12:28:09 +00:00
local ota_zsync_file = self : getOTAServer ( ) .. zsync_file
2015-10-03 06:18:47 +00:00
local local_zsync_file = ota_dir .. zsync_file
2014-08-01 04:36:32 +00:00
-- download zsync file from OTA server
2016-12-29 08:10:38 +00:00
logger.dbg ( " downloading zsync file " , ota_zsync_file )
2016-02-16 02:08:04 +00:00
local _ , c , _ = http.request {
2014-08-01 04:36:32 +00:00
url = ota_zsync_file ,
sink = ltn12.sink . file ( io.open ( local_zsync_file , " w " ) ) }
2015-02-01 09:24:50 +00:00
if c ~= 200 then
2016-12-29 08:10:38 +00:00
logger.warn ( " cannot find zsync file " , c )
2015-02-01 09:24:50 +00:00
return
end
2014-08-20 07:46:43 +00:00
-- parse OTA package version
2014-08-01 04:36:32 +00:00
local ota_package = nil
local zsync = io.open ( local_zsync_file , " r " )
if zsync then
for line in zsync : lines ( ) do
ota_package = line : match ( " ^Filename:%s*(.-)%s*$ " )
if ota_package then break end
end
zsync : close ( )
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
2014-11-13 06:43:16 +00:00
return ota_version , local_version
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 ( )
2014-11-13 06:43:16 +00:00
local ota_version , local_version = OTAManager : checkUpdate ( )
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 {
2015-04-15 05:38:01 +00:00
text = T ( _ ( " OTA package is not available on %1 channel. " ) , channel ) ,
2014-08-26 19:21:18 +00:00
} )
elseif ota_version then
UIManager : show ( ConfirmBox : new {
2014-11-28 11:48:15 +00:00
text = T (
_ ( " Do you want to update? \n Installed version: %1 \n Available version: %2 " ) ,
local_version ,
ota_version
) ,
2017-04-04 13:31:13 +00:00
ok_text = _ ( " Update " ) ,
2014-08-26 19:21:18 +00:00
ok_callback = function ( )
UIManager : show ( InfoMessage : new {
2016-04-15 13:04:41 +00:00
text = _ ( " Downloading may take several minutes… " ) ,
2014-08-26 19:21:18 +00:00
timeout = 3 ,
} )
UIManager : scheduleIn ( 1 , function ( )
if OTAManager : zsync ( ) == 0 then
UIManager : show ( InfoMessage : new {
2014-11-12 11:29:38 +00:00
text = _ ( " KOReader will be updated on next restart. " ) ,
2014-08-26 19:21:18 +00:00
} )
2018-07-04 17:25:55 +00:00
-- Make it clear that zsync is done
2018-09-05 23:35:48 +00:00
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
2018-07-04 17:25:55 +00:00
end
2014-08-26 20:18:00 +00:00
else
2018-09-05 23:35:48 +00:00
-- Make it clear that zsync is done
if self.can_pretty_print then
os.execute ( " ./fbink -q -y -7 -pm ' ' ' ' " )
end
2019-01-01 19:06:55 +00:00
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
os.execute ( " rm " .. self.installed_package )
os.execute ( " rm " .. self.updated_package .. " * " )
-- And then relaunch zsync in full download mode...
UIManager : scheduleIn ( 1 , function ( )
if OTAManager : zsync ( true ) == 0 then
UIManager : show ( InfoMessage : new {
text = _ ( " KOReader will be updated on next restart. " ) ,
} )
-- 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 ( )
os.execute ( " rm " .. ota_dir .. " ko* " )
end ,
} )
end
end )
end ,
choice2_text = _ ( " Abort " ) ,
choice2_callback = function ( )
2016-03-13 15:42:58 +00:00
os.execute ( " rm " .. ota_dir .. " ko* " )
2014-09-14 15:07:19 +00:00
end ,
2014-08-26 20:18:00 +00:00
} )
2014-08-26 19:21:18 +00:00
end
end )
end
} )
end
end
2014-08-01 04:36:32 +00:00
function OTAManager : _buildLocalPackage ( )
2014-10-14 05:04:49 +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
2016-03-13 15:42:58 +00:00
if Device : isAndroid ( ) then
return os.execute ( string.format (
2018-08-10 15:19:50 +00:00
" ./tar --no-recursion -cf %s -T %s " ,
2016-03-13 15:42:58 +00:00
self.installed_package , self.package_indexfile ) )
else
2018-07-03 21:16:45 +00:00
-- With visual feedback if supported...
2018-09-05 23:35:48 +00:00
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 zsync_file = self : getZsyncFilename ( )
local local_zsync_file = ota_dir .. zsync_file
local tarball_size = nil
local zsync = io.open ( local_zsync_file , " r " )
if zsync then
for line in zsync : lines ( ) do
tarball_size = line : match ( " ^Length: (%d*)$ " )
if tarball_size then break end
end
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 )
end
-- And since we want a percentage, devise the exact value we need for tar to spit out exactly 100 checkpoints ;).
local cpoints = blocks / 100
2018-07-03 21:16:45 +00:00
return os.execute ( string.format (
2018-09-05 23:35:48 +00:00
" ./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 ) )
2018-07-03 21:16:45 +00:00
else
return os.execute ( string.format (
2018-08-10 15:19:50 +00:00
" ./tar --no-recursion -cf %s -C .. -T %s " ,
2018-07-03 21:16:45 +00:00
self.installed_package , self.package_indexfile ) )
end
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
2018-09-05 23:35:48 +00:00
local zsync_wrapper = " zsync "
-- With visual feedback if supported...
if self.can_pretty_print then
zsync_wrapper = " spinning_zsync "
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 (
( " ./%s -o '%s' -u '%s' '%s%s' " ) : format (
zsync_wrapper ,
self.updated_package ,
self : getOTAServer ( ) ,
ota_dir ,
self : getZsyncFilename ( ) )
)
else
return os.execute (
( " ./%s -i '%s' -o '%s' -u '%s' '%s%s' " ) : format (
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 {
2014-08-06 12:28:09 +00:00
text = _ ( " OTA update " ) ,
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 = {
{
text = _ ( " OTA server " ) ,
sub_item_table = self : genServerList ( )
} ,
{
text = _ ( " OTA channel " ) ,
sub_item_table = self : genChannelList ( )
} ,
}
} ,
}
2014-08-01 13:59:59 +00:00
}
end
2014-08-01 04:36:32 +00:00
return OTAManager