2023-03-02 06:39:09 +00:00
|
|
|
-- need low-level mechanism to detect android to avoid recursive dependency
|
2016-09-16 17:04:51 +00:00
|
|
|
local isAndroid, android = pcall(require, "android")
|
2015-10-03 06:18:47 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2015-06-15 08:46:43 +00:00
|
|
|
|
|
|
|
local DataStorage = {}
|
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
local data_dir
|
2017-10-27 21:00:49 +00:00
|
|
|
local full_data_dir
|
|
|
|
|
2015-06-15 08:46:43 +00:00
|
|
|
function DataStorage:getDataDir()
|
2016-06-26 00:53:08 +00:00
|
|
|
if data_dir then return data_dir end
|
|
|
|
|
2015-06-15 08:46:43 +00:00
|
|
|
if isAndroid then
|
2019-12-10 21:26:45 +00:00
|
|
|
data_dir = android.getExternalStoragePath() .. "/koreader"
|
2015-10-03 06:18:47 +00:00
|
|
|
elseif os.getenv("UBUNTU_APPLICATION_ISOLATION") then
|
|
|
|
local app_id = os.getenv("APP_ID")
|
|
|
|
local package_name = app_id:match("^(.-)_")
|
2018-04-08 20:39:52 +00:00
|
|
|
-- confined ubuntu app has write access to this dir
|
2017-10-26 10:05:41 +00:00
|
|
|
data_dir = string.format("%s/%s", os.getenv("XDG_DATA_HOME"), package_name)
|
2019-01-03 17:21:35 +00:00
|
|
|
elseif os.getenv("APPIMAGE") or os.getenv("KO_MULTIUSER") then
|
2021-12-05 20:16:22 +00:00
|
|
|
if os.getenv("XDG_CONFIG_HOME") then
|
|
|
|
data_dir = string.format("%s/%s", os.getenv("XDG_CONFIG_HOME"), "koreader")
|
|
|
|
else
|
|
|
|
local user_rw = jit.os == "OSX" and "Library/Application Support" or ".config"
|
|
|
|
data_dir = string.format("%s/%s/%s", os.getenv("HOME"), user_rw, "koreader")
|
|
|
|
end
|
2015-06-15 08:46:43 +00:00
|
|
|
else
|
2016-01-03 08:47:01 +00:00
|
|
|
data_dir = "."
|
2015-06-15 08:46:43 +00:00
|
|
|
end
|
2015-10-03 06:18:47 +00:00
|
|
|
if lfs.attributes(data_dir, "mode") ~= "directory" then
|
|
|
|
lfs.mkdir(data_dir)
|
|
|
|
end
|
2016-06-26 00:53:08 +00:00
|
|
|
|
2015-10-03 06:18:47 +00:00
|
|
|
return data_dir
|
2015-06-15 08:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function DataStorage:getHistoryDir()
|
|
|
|
return self:getDataDir() .. "/history"
|
|
|
|
end
|
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
function DataStorage:getSettingsDir()
|
|
|
|
return self:getDataDir() .. "/settings"
|
|
|
|
end
|
|
|
|
|
2023-02-17 06:35:49 +00:00
|
|
|
function DataStorage:getDocSettingsDir()
|
|
|
|
return self:getDataDir() .. "/docsettings"
|
|
|
|
end
|
2017-10-27 21:00:49 +00:00
|
|
|
|
|
|
|
function DataStorage:getFullDataDir()
|
|
|
|
if full_data_dir then return full_data_dir end
|
|
|
|
|
|
|
|
if string.sub(self:getDataDir(), 1, 1) == "/" then
|
|
|
|
full_data_dir = self:getDataDir()
|
|
|
|
elseif self:getDataDir() == "." then
|
|
|
|
full_data_dir = lfs.currentdir()
|
|
|
|
end
|
|
|
|
|
|
|
|
return full_data_dir
|
|
|
|
end
|
|
|
|
|
2015-10-03 06:18:47 +00:00
|
|
|
local function initDataDir()
|
2016-06-26 00:53:08 +00:00
|
|
|
local sub_data_dirs = {
|
2023-03-02 06:39:09 +00:00
|
|
|
"cache",
|
|
|
|
"clipboard",
|
|
|
|
"data",
|
|
|
|
"data/dict",
|
|
|
|
"data/tessdata",
|
|
|
|
-- "docsettings", -- created when needed
|
|
|
|
-- "history", -- legacy/obsolete sidecar files
|
|
|
|
"ota",
|
|
|
|
-- "patches", -- must be created manually by the interested user
|
|
|
|
"plugins",
|
|
|
|
"screenshots",
|
|
|
|
"settings",
|
|
|
|
"styletweaks",
|
2016-06-26 00:53:08 +00:00
|
|
|
}
|
2023-03-02 06:39:09 +00:00
|
|
|
local datadir = DataStorage:getDataDir()
|
2015-10-03 06:18:47 +00:00
|
|
|
for _, dir in ipairs(sub_data_dirs) do
|
2023-03-02 06:39:09 +00:00
|
|
|
local sub_data_dir = string.format("%s/%s", datadir, dir)
|
2015-10-03 06:18:47 +00:00
|
|
|
if lfs.attributes(sub_data_dir, "mode") ~= "directory" then
|
|
|
|
lfs.mkdir(sub_data_dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
initDataDir()
|
|
|
|
|
2015-06-15 08:46:43 +00:00
|
|
|
return DataStorage
|