reader.lua: rework file/directory argument handling. (#7053)

platform: do not pass a directory on the command line.
The home directory will be properly set by Device.home_dir.

It was sometimes crashing when opened with no args.

Fixes: #7049
pull/7096/head
yparitcher 3 years ago committed by GitHub
parent e157395660
commit dc964f3941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -54,7 +54,7 @@ local external = require("device/thirdparty"):new{
local Device = Generic:new{ local Device = Generic:new{
model = "SDL", model = "SDL",
isSDL = yes, isSDL = yes,
home_dir = os.getenv("HOME"), home_dir = os.getenv("XDG_DOCUMENTS_DIR") or os.getenv("HOME"),
hasBattery = SDL.getPowerInfo(), hasBattery = SDL.getPowerInfo(),
hasKeyboard = yes, hasKeyboard = yes,
hasKeys = yes, hasKeys = yes,
@ -125,7 +125,6 @@ local Emulator = Device:new{
local UbuntuTouch = Device:new{ local UbuntuTouch = Device:new{
model = "UbuntuTouch", model = "UbuntuTouch",
hasFrontlight = yes, hasFrontlight = yes,
home_dir = nil,
} }
function Device:init() function Device:init()

@ -15,6 +15,7 @@ local SonyPRSTUX = Generic:new{
canReboot = yes, canReboot = yes,
canPowerOff = yes, canPowerOff = yes,
usbPluggedIn = false, usbPluggedIn = false,
home_dir = nil,
} }

@ -853,11 +853,9 @@ TARGET:
KOREADER_COMMAND="${valgrind} env LD_LIBRARY_PATH=${KO_LD_LIBRARY_PATH} ./luajit reader.lua ${KOREADER_ARGS}" KOREADER_COMMAND="${valgrind} env LD_LIBRARY_PATH=${KO_LD_LIBRARY_PATH} ./luajit reader.lua ${KOREADER_ARGS}"
fi fi
echo "[*] Running KOReader with arguments: $*..." echo "[*] Running KOReader with arguments: $* ..."
pushd "${EMU_DIR}" && { pushd "${EMU_DIR}" && {
if [ $# -lt 1 ]; then if [ $# -ge 1 ]; then
args="${CURDIR}/test"
else
args="$*" args="$*"
[[ "${args}" != /* ]] && args="${CURDIR}/${args}" [[ "${args}" != /* ]] && args="${CURDIR}/${args}"
fi fi

@ -75,9 +75,9 @@ C.setenv("TESSDATA_PREFIX", path.."/koreader/data", 1)
-- create fake command-line arguments -- create fake command-line arguments
-- luacheck: ignore 121 -- luacheck: ignore 121
if android.isDebuggable() then if android.isDebuggable() then
arg = {"-d", file or path} arg = {"-d", file}
else else
arg = {file or path} arg = {file}
end end
dofile(android.dir.."/reader.lua") dofile(android.dir.."/reader.lua")

@ -12,19 +12,8 @@ export LD_LIBRARY_PATH=${KOREADER_DIR}/libs:${LD_LIBRARY_PATH}
RETURN_VALUE=85 RETURN_VALUE=85
if [ $# -eq 0 ]; then
# no arguments
if [ -n "${XDG_DOCUMENTS_DIR+x}" ]; then
start_path=${XDG_DOCUMENTS_DIR}
else
start_path=$(pwd)
fi
else
start_path="$*"
fi
while [ ${RETURN_VALUE} -eq 85 ]; do while [ ${RETURN_VALUE} -eq 85 ]; do
./reader.lua "${start_path}" ./reader.lua "$@"
RETURN_VALUE=$? RETURN_VALUE=$?
done done

@ -39,19 +39,12 @@ ko_update_check() {
fi fi
} }
# if no args were passed to the script, start the FM on public partition.
if [ "$#" -eq 0 ]; then
args="/mnt/public"
else
args="$*"
fi
# NOTE: Keep doing an initial update check, in addition to one during the restart loop, so we can pickup potential updates of this very script... # NOTE: Keep doing an initial update check, in addition to one during the restart loop, so we can pickup potential updates of this very script...
ko_update_check ko_update_check
# If an update happened, and was successful, reexec # If an update happened, and was successful, reexec
if [ -n "${fail}" ] && [ "${fail}" -eq 0 ]; then if [ -n "${fail}" ] && [ "${fail}" -eq 0 ]; then
# By now, we know we're in the right directory, and our script name is pretty much set in stone, so we can forgo using $0 # By now, we know we're in the right directory, and our script name is pretty much set in stone, so we can forgo using $0
exec ./koreader.sh "${args}" exec ./koreader.sh "$@"
fi fi
# load our own shared libraries if possible # load our own shared libraries if possible
@ -101,7 +94,7 @@ while [ "${RETURN_VALUE}" -ge "${RESTART_KOREADER}" ]; do
ko_update_check ko_update_check
# run KOReader # run KOReader
./reader.lua "${args}" >>crash.log 2>&1 ./reader.lua "$@" >>crash.log 2>&1
RETURN_VALUE=$? RETURN_VALUE=$?
# check if KOReader requested to enter in mass storage mode. # check if KOReader requested to enter in mass storage mode.

@ -4,14 +4,10 @@ export LC_ALL="en_US.UTF-8"
# writable storage: ${HOME}/.config/koreader. # writable storage: ${HOME}/.config/koreader.
export KO_MULTIUSER=1 export KO_MULTIUSER=1
if [ -z "${1}" ]; then if [ $# -eq 1 ] && [ -e "$(pwd)/${1}" ]; then
ARGS="${HOME}" ARGS="$(pwd)/${1}"
else else
if [ $# -eq 1 ] && [ -e "$(pwd)/${1}" ]; then ARGS="${*}"
ARGS="$(pwd)/${1}"
else
ARGS="${*}"
fi
fi fi
# working directory of koreader # working directory of koreader

@ -5,54 +5,26 @@
"priority": 0, "priority": 0,
"items": [ "items": [
{ {
"name": "Start the filemanager", "name": "Start KOReader",
"if": "\"KindleVoyage\" -m!",
"priority": 1,
"action": "/mnt/us/koreader/koreader.sh",
"params": "--kual /mnt/us/documents",
"status": false,
"internal": "status Start KOReader on the File Manager"
},
{
"name": "Start the filemanager",
"if": "\"KindleVoyage\" -m",
"priority": 1,
"action": "/mnt/us/koreader/koreader.sh",
"params": "--kual /mnt/us/documents",
"exitmenu": false,
"status": false,
"internal": "status Start KOReader on the File Manager"
},
{
"name": "Open the last document",
"if": "\"KindleVoyage\" -m!", "if": "\"KindleVoyage\" -m!",
"priority": 2, "priority": 2,
"action": "/mnt/us/koreader/koreader.sh", "action": "/mnt/us/koreader/koreader.sh",
"params": "--kual", "params": "--kual",
"status": false, "status": false,
"internal": "status Start KOReader on the last document" "internal": "status Start KOReader"
}, },
{ {
"name": "Open the last document", "name": "Start KOReader",
"if": "\"KindleVoyage\" -m", "if": "\"KindleVoyage\" -m",
"priority": 2, "priority": 2,
"action": "/mnt/us/koreader/koreader.sh", "action": "/mnt/us/koreader/koreader.sh",
"params": "--kual", "params": "--kual",
"exitmenu": false, "exitmenu": false,
"status": false, "status": false,
"internal": "status Start KOReader on the last document" "internal": "status Start KOReader"
}, },
{ {
"name": "Start the filemanager (no framework)", "name": "Start KOReader (no framework)",
"if": "\"Kindle2\" -m \"KindleDX\" -m \"KindleDXG\" -m \"Kindle3\" -m \"Kindle4\" -m || || || ||",
"priority": 3,
"action": "/mnt/us/koreader/koreader.sh",
"params": "--kual --framework_stop /mnt/us/documents",
"status": false,
"internal": "status Kill the framework and start KOReader's FM"
},
{
"name": "Open the last document (no framework)",
"if": "\"Kindle2\" -m \"KindleDX\" -m \"KindleDXG\" -m \"Kindle3\" -m \"Kindle4\" -m || || || ||", "if": "\"Kindle2\" -m \"KindleDX\" -m \"KindleDXG\" -m \"Kindle3\" -m \"Kindle4\" -m || || || ||",
"priority": 4, "priority": 4,
"action": "/mnt/us/koreader/koreader.sh", "action": "/mnt/us/koreader/koreader.sh",
@ -61,42 +33,23 @@
"internal": "status Kill the framework and start KOReader" "internal": "status Kill the framework and start KOReader"
}, },
{ {
"name": "Start the filemanager (ASAP)", "name": "Start KOReader (ASAP)",
"if": "\"KindleVoyage\" -m!",
"priority": 5,
"action": "/mnt/us/koreader/koreader.sh",
"params": "--kual --asap /mnt/us/documents",
"status": false,
"internal": "status Start KOreader on the File Manager ASAP"
},
{
"name": "Start the filemanager (ASAP)",
"if": "\"KindleVoyage\" -m",
"priority": 5,
"action": "/mnt/us/koreader/koreader.sh",
"params": "--kual --asap /mnt/us/documents",
"exitmenu": false,
"status": false,
"internal": "status Start KOreader on the File Manager ASAP"
},
{
"name": "Open the last document (ASAP)",
"if": "\"KindleVoyage\" -m!", "if": "\"KindleVoyage\" -m!",
"priority": 6, "priority": 6,
"action": "/mnt/us/koreader/koreader.sh", "action": "/mnt/us/koreader/koreader.sh",
"params": "--kual --asap", "params": "--kual --asap",
"status": false, "status": false,
"internal": "status Start KOreader on the last document ASAP" "internal": "status Start KOReader ASAP"
}, },
{ {
"name": "Open the last document (ASAP)", "name": "Start KOReader (ASAP)",
"if": "\"KindleVoyage\" -m", "if": "\"KindleVoyage\" -m",
"priority": 6, "priority": 6,
"action": "/mnt/us/koreader/koreader.sh", "action": "/mnt/us/koreader/koreader.sh",
"params": "--kual --asap", "params": "--kual --asap",
"exitmenu": false, "exitmenu": false,
"status": false, "status": false,
"internal": "status Start KOreader on the last document ASAP" "internal": "status Start KOReader ASAP"
}, },
{ {
"name": "Tools", "name": "Tools",
@ -122,7 +75,7 @@
"checked": true, "checked": true,
"refresh": false, "refresh": false,
"status": false, "status": false,
"internal": "status Try to install KOreader from scratch . . ." "internal": "status Try to install KOReader from scratch . . ."
} }
] ]
} }

@ -198,13 +198,6 @@ if [ "${VIA_NICKEL}" = "true" ]; then
fi fi
fi fi
# fallback for old fmon, KFMon and advboot users (-> if no args were passed to the script, start the FM)
if [ "$#" -eq 0 ]; then
args="/mnt/onboard"
else
args="$*"
fi
# check whether PLATFORM & PRODUCT have a value assigned by rcS # check whether PLATFORM & PRODUCT have a value assigned by rcS
if [ -z "${PRODUCT}" ]; then if [ -z "${PRODUCT}" ]; then
# shellcheck disable=SC2046 # shellcheck disable=SC2046
@ -333,7 +326,7 @@ while [ ${RETURN_VALUE} -ne 0 ]; do
ko_do_dns ko_do_dns
fi fi
./reader.lua "${args}" >>crash.log 2>&1 ./reader.lua "$@" >>crash.log 2>&1
RETURN_VALUE=$? RETURN_VALUE=$?
# Did we crash? # Did we crash?

@ -175,7 +175,6 @@ rm -rf cache clipboard history ota \
sed '1d' reader.lua >tempfile sed '1d' reader.lua >tempfile
sed -i.backup 's/.\/reader.lua/koreader/' tempfile sed -i.backup 's/.\/reader.lua/koreader/' tempfile
sed -i.backup 's/the last viewed document will be opened"/" .. os.getenv("HOME") .. " will be opened"/' tempfile
mv tempfile reader.lua mv tempfile reader.lua
rm -f tempfile* rm -f tempfile*
chmod -x reader.lua chmod -x reader.lua

@ -65,13 +65,6 @@ export TESSDATA_PREFIX="data"
# export dict directory # export dict directory
export STARDICT_DATA_DIR="data/dict" export STARDICT_DATA_DIR="data/dict"
# shellcheck disable=2000
if [ "$(echo "$@" | wc -c)" -eq 1 ]; then
args="/mnt/ext1/"
else
args="$*"
fi
# we keep at maximum 500K worth of crash log # we keep at maximum 500K worth of crash log
if [ -e crash.log ]; then if [ -e crash.log ]; then
tail -c 500000 crash.log >crash.log.new tail -c 500000 crash.log >crash.log.new
@ -94,7 +87,7 @@ while [ "${RETURN_VALUE}" -ne 0 ]; do
ko_update_check ko_update_check
fi fi
./reader.lua "${args}" >>crash.log 2>&1 ./reader.lua "$@" >>crash.log 2>&1
# Account for the fact a hard crash may have prevented the KO_EXIT_CODE file from being written to... # Account for the fact a hard crash may have prevented the KO_EXIT_CODE file from being written to...
if [ -f "${KO_EXIT_CODE}" ]; then if [ -f "${KO_EXIT_CODE}" ]; then

@ -132,12 +132,6 @@ if [ -e crash.log ]; then
mv -f crash.log.new crash.log mv -f crash.log.new crash.log
fi fi
if [ "$#" -eq 0 ]; then
args="/home/root"
else
args="$*"
fi
CRASH_COUNT=0 CRASH_COUNT=0
CRASH_TS=0 CRASH_TS=0
CRASH_PREV_TS=0 CRASH_PREV_TS=0
@ -152,7 +146,7 @@ while [ ${RETURN_VALUE} -ne 0 ]; do
ko_do_fbdepth ko_do_fbdepth
fi fi
./reader.lua "${args}" >>crash.log 2>&1 ./reader.lua "$@" >>crash.log 2>&1
RETURN_VALUE=$? RETURN_VALUE=$?
# Did we crash? # Did we crash?

@ -22,7 +22,7 @@ export SDL_FULLSCREEN=1
RETURN_VALUE=85 RETURN_VALUE=85
while [ ${RETURN_VALUE} -eq 85 ]; do while [ ${RETURN_VALUE} -eq 85 ]; do
./reader.lua -d ~/Documents ./reader.lua -d
RETURN_VALUE=$? RETURN_VALUE=$?
done done

@ -80,12 +80,31 @@ local function showusage()
print("If you give the name of a directory instead of a file path, a file") print("If you give the name of a directory instead of a file path, a file")
print("chooser will show up and let you select a file") print("chooser will show up and let you select a file")
print("") print("")
print("If you don't pass any path, the last viewed document will be opened") print("If you don't pass any path, the File Manager will be opened")
print("") print("")
print("This software is licensed under the AGPLv3.") print("This software is licensed under the AGPLv3.")
print("See http://github.com/koreader/koreader for more info.") print("See http://github.com/koreader/koreader for more info.")
end end
local function getPathFromURI(str)
local hexToChar = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hexToChar)
end
local prefix = "file://"
if str:sub(1, #prefix) ~= prefix then
return str
end
return unescape(str):sub(#prefix+1)
end
local lfs = require("libs/libkoreader-lfs")
local file
local directory
local Profiler = nil local Profiler = nil
local ARGV = arg local ARGV = arg
local argidx = 1 local argidx = 1
@ -109,8 +128,14 @@ while argidx <= #ARGV do
Profiler = require("jit.p") Profiler = require("jit.p")
Profiler.start("la") Profiler.start("la")
else else
-- not a recognized option, should be a filename -- not a recognized option, should be a filename or directory
argidx = argidx - 1 local sanitized_path = getPathFromURI(arg)
local mode = lfs.attributes(sanitized_path, "mode")
if mode == "file" then
file = sanitized_path
elseif mode == "directory" or mode == "link" then
directory = sanitized_path
end
break break
end end
end end
@ -206,87 +231,69 @@ if G_reader_settings:isTrue("color_rendering") and not Device:hasColorScreen() t
}) })
end end
-- Get which file to start with
local last_file = G_reader_settings:readSetting("lastfile")
local start_with = G_reader_settings:readSetting("start_with") or "filemanager"
-- Helpers -- Helpers
local lfs = require("libs/libkoreader-lfs")
local function retryLastFile() local function retryLastFile()
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
return ConfirmBox:new{ return ConfirmBox:new{
text = _("Cannot open last file.\nThis could be because it was deleted or because external storage is still being mounted.\nDo you want to retry?"), text = _("Cannot open last file.\nThis could be because it was deleted or because external storage is still being mounted.\nDo you want to retry?"),
ok_callback = function() ok_callback = function()
local last_file = G_reader_settings:readSetting("lastfile") if lfs.attributes(last_file, "mode") ~= "file" then
if lfs.attributes(last_file, "mode") == "file" then
local ReaderUI = require("apps/reader/readerui")
UIManager:nextTick(function()
ReaderUI:showReader(last_file)
end)
else
UIManager:show(retryLastFile()) UIManager:show(retryLastFile())
end end
end, end,
cancel_callback = function()
start_with = "filemanager"
end,
} }
end end
local function getPathFromURI(str) -- Start app
local hexToChar = function(x) local exit_code
return string.char(tonumber(x, 16)) if file then
end local ReaderUI = require("apps/reader/readerui")
UIManager:nextTick(function()
local unescape = function(url) ReaderUI:showReader(file)
return url:gsub("%%(%x%x)", hexToChar) end)
end exit_code = UIManager:run()
elseif directory then
local prefix = "file://" local FileManager = require("apps/filemanager/filemanager")
if str:sub(1, #prefix) ~= prefix then UIManager:nextTick(function()
return str FileManager:setRotationMode(true)
end FileManager:showFiles(directory)
return unescape(str):sub(#prefix+1) end)
end exit_code = UIManager:run()
-- Get which file to start with
local last_file = G_reader_settings:readSetting("lastfile")
local start_with = G_reader_settings:readSetting("start_with")
local open_last = start_with == "last"
if open_last and last_file and lfs.attributes(last_file, "mode") ~= "file" then
UIManager:show(retryLastFile())
last_file = nil
else else
local QuickStart = require("ui/quickstart") local QuickStart = require("ui/quickstart")
if not QuickStart:isShown() then if not QuickStart:isShown() then
open_last = true start_with = "last"
last_file = QuickStart:getQuickStart() last_file = QuickStart:getQuickStart()
end end
end
-- Start app if start_with == "last" and last_file and lfs.attributes(last_file, "mode") ~= "file" then
local exit_code UIManager:show(retryLastFile())
if ARGV[argidx] and ARGV[argidx] ~= "" then -- no exit code as something else will be run after this.
local file UIManager:run()
local sanitized_path = getPathFromURI(ARGV[argidx])
if lfs.attributes(sanitized_path, "mode") == "file" then
file = sanitized_path
elseif open_last and last_file then
file = last_file
end end
-- if file is given in command line argument or open last document is set if start_with == "last" and last_file then
-- true, the given file or the last file is opened in the reader
if file and file ~= "" then
local ReaderUI = require("apps/reader/readerui") local ReaderUI = require("apps/reader/readerui")
UIManager:nextTick(function() UIManager:nextTick(function()
ReaderUI:showReader(file) ReaderUI:showReader(last_file)
end) end)
-- we assume a directory is given in command line argument exit_code = UIManager:run()
-- the filemanger will show the files in that path
else else
local FileManager = require("apps/filemanager/filemanager") local FileManager = require("apps/filemanager/filemanager")
local home_dir = local home_dir =
G_reader_settings:readSetting("home_dir") or ARGV[argidx] G_reader_settings:readSetting("home_dir") or Device.home_dir or lfs.currentdir()
UIManager:nextTick(function() UIManager:nextTick(function()
FileManager:setRotationMode(true) FileManager:setRotationMode(true)
FileManager:showFiles(home_dir) FileManager:showFiles(home_dir)
end) end)
-- always open history on top of filemanager so closing history -- Always open history on top of filemanager so closing history
-- doesn't result in exit -- doesn't result in exit.
if start_with == "history" then if start_with == "history" then
local FileManagerHistory = require("apps/filemanager/filemanagerhistory") local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
UIManager:nextTick(function() UIManager:nextTick(function()
@ -307,16 +314,8 @@ if ARGV[argidx] and ARGV[argidx] ~= "" then
}:onShowFolderShortcutsDialog() }:onShowFolderShortcutsDialog()
end) end)
end end
exit_code = UIManager:run()
end end
exit_code = UIManager:run()
elseif last_file then
local ReaderUI = require("apps/reader/readerui")
UIManager:nextTick(function()
ReaderUI:showReader(last_file)
end)
exit_code = UIManager:run()
else
return showusage()
end end
-- Exit -- Exit

Loading…
Cancel
Save