2015-06-15 08:46:43 +00:00
|
|
|
-- need low-level mechnism to detect android to avoid recursive dependency
|
|
|
|
local isAndroid = 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 = {}
|
|
|
|
|
|
|
|
function DataStorage:getDataDir()
|
2015-10-03 06:18:47 +00:00
|
|
|
local data_dir
|
2015-06-15 08:46:43 +00:00
|
|
|
if isAndroid then
|
2015-10-03 06:18:47 +00:00
|
|
|
data_dir = "/sdcard/koreader/"
|
|
|
|
elseif os.getenv("UBUNTU_APPLICATION_ISOLATION") then
|
|
|
|
local app_id = os.getenv("APP_ID")
|
|
|
|
local package_name = app_id:match("^(.-)_")
|
|
|
|
-- confinded ubuntu app has write access to this dir
|
|
|
|
data_dir = os.getenv("XDG_DATA_HOME") .. "/" .. package_name
|
2015-06-15 08:46:43 +00:00
|
|
|
else
|
2015-10-03 06:18:47 +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
|
|
|
|
return data_dir
|
2015-06-15 08:46:43 +00:00
|
|
|
end
|
|
|
|
|
2015-10-03 06:18:47 +00:00
|
|
|
local function initDataDir()
|
|
|
|
local data_dir = DataStorage:getDataDir()
|
|
|
|
local sub_data_dirs = {"cache", "clipboard", "data", "history", "ota", "screenshots"}
|
|
|
|
for _, dir in ipairs(sub_data_dirs) do
|
|
|
|
local sub_data_dir = data_dir .. "/" .. dir
|
|
|
|
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
|