diff --git a/datastorage.lua b/datastorage.lua index 14966c5e2..7d9d10ae7 100644 --- a/datastorage.lua +++ b/datastorage.lua @@ -1,4 +1,4 @@ --- need low-level mechnism to detect android to avoid recursive dependency +-- need low-level mechanism to detect android to avoid recursive dependency local isAndroid, android = pcall(require, "android") local lfs = require("libs/libkoreader-lfs") @@ -60,13 +60,23 @@ end local function initDataDir() local sub_data_dirs = { - "cache", "clipboard", - "data", "data/dict", "data/tessdata", "docsettings", - "history", "ota", "patches", "plugins", - "screenshots", "settings", "styletweaks", + "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", } + local datadir = DataStorage:getDataDir() for _, dir in ipairs(sub_data_dirs) do - local sub_data_dir = string.format("%s/%s", DataStorage:getDataDir(), dir) + local sub_data_dir = string.format("%s/%s", datadir, dir) if lfs.attributes(sub_data_dir, "mode") ~= "directory" then lfs.mkdir(sub_data_dir) end diff --git a/frontend/readhistory.lua b/frontend/readhistory.lua index ab9a4a96d..cdafe2af3 100644 --- a/frontend/readhistory.lua +++ b/frontend/readhistory.lua @@ -125,10 +125,12 @@ function ReadHistory:_read(force_read) end end ---- Reads history from legacy history folder. +--- Reads history from legacy history folder and remove it iff empty. +-- Legacy history file is deleted when respective book is opened or deleted. function ReadHistory:_readLegacyHistory() - local history_updated local history_dir = DataStorage:getHistoryDir() + if not lfs.attributes(history_dir) then return end + local history_updated for f in lfs.dir(history_dir) do local legacy_history_file = joinPath(history_dir, f) if lfs.attributes(legacy_history_file, "mode") == "file" then @@ -145,6 +147,7 @@ function ReadHistory:_readLegacyHistory() self:_reduce() self:_flush() end + os.remove(history_dir) end function ReadHistory:_init() diff --git a/spec/unit/commonrequire.lua b/spec/unit/commonrequire.lua index 5757994ab..8ea09bd4f 100644 --- a/spec/unit/commonrequire.lua +++ b/spec/unit/commonrequire.lua @@ -42,8 +42,10 @@ require("dbg"):turnOff() local logger = require("logger") logger:setLevel(logger.levels.warn) --- global defaults local DataStorage = require("datastorage") +require("libs/libkoreader-lfs").mkdir(DataStorage:getHistoryDir()) -- for legacy history tests + +-- global defaults os.remove(DataStorage:getDataDir() .. "/defaults.tests.lua") os.remove(DataStorage:getDataDir() .. "/defaults.tests.lua.old") G_defaults = require("luadefaults"):open(DataStorage:getDataDir() .. "/defaults.tests.lua") diff --git a/spec/unit/filemanager_spec.lua b/spec/unit/filemanager_spec.lua index 552e09770..e6a511ff8 100644 --- a/spec/unit/filemanager_spec.lua +++ b/spec/unit/filemanager_spec.lua @@ -89,6 +89,7 @@ describe("FileManager module", function() local tmpsf = io.open(tmp_sidecar_file, "w") tmpsf:write("{}") tmpsf:close() + lfs.mkdir(require("datastorage"):getHistoryDir()) local tmp_history = docsettings:getHistoryPath(tmp_fn) local tmpfp = io.open(tmp_history, "w") tmpfp:write("{}") diff --git a/spec/unit/readhistory_spec.lua b/spec/unit/readhistory_spec.lua index 3707dab4b..91e542958 100644 --- a/spec/unit/readhistory_spec.lua +++ b/spec/unit/readhistory_spec.lua @@ -92,6 +92,7 @@ describe("ReadHistory module", function() local h = reload() now = now + 61 h:addItem(test_file("a"), now) + mkdir(DataStorage:getHistoryDir()) touch(legacy_history_file("b")) h = reload() assert.is.same(2, #h.hist) @@ -113,6 +114,7 @@ describe("ReadHistory module", function() local h = reload() now = now + 61 h:addItem(test_file("f"), now) + mkdir(DataStorage:getHistoryDir()) touch(legacy_history_file("c")) touch(legacy_history_file("b")) now = now + 61