Commit 94e3541e authored by WorldTeacher's avatar WorldTeacher
Browse files

fix(update, log): change path handling

parent 4b08a9c2
Loading
Loading
Loading
Loading
+60 −34
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ Keeps the last 3 log files.
@module koplugin.BookloreSync.file_logger
--]]--

local DataStorage = require("datastorage")
local logger = require("logger")

local FileLogger = {
@@ -31,13 +30,26 @@ Creates the log directory if it doesn't exist and sets up daily rotation.

@return boolean success
--]]
function FileLogger:init()
    -- Set log directory to plugin directory
    self.log_dir = DataStorage:getDataDir() .. "/plugins/bookloresync.koplugin/logs"
function FileLogger:init(plugin_dir)
    -- Detect plugin directory from this file's path if not provided
    if not plugin_dir then
        local source = debug.getinfo(1, "S").source
        -- source is "@/path/to/bookloresync.koplugin/booklore_file_logger.lua"
        plugin_dir = source:match("@(.*)/[^/]+$")
    end

    -- Set log directory inside the plugin folder
    self.log_dir = plugin_dir .. "/logs"

    -- Create logs directory if it doesn't exist
    local mkdir_cmd = "mkdir -p " .. self.log_dir
    os.execute(mkdir_cmd)
    -- Create logs directory using lfs if available, else fall back to os.execute
    local lfs_ok, lfs = pcall(require, "libs/libkoreader-lfs")
    if lfs_ok and lfs then
        if not lfs.attributes(self.log_dir) then
            lfs.mkdir(self.log_dir)
        end
    else
        os.execute("mkdir -p " .. self.log_dir)
    end

    -- Check if directory was created successfully
    local test_file = io.open(self.log_dir .. "/.test", "w")
@@ -73,25 +85,52 @@ function FileLogger:getLogFilePath(date)
end

--[[--
Rotate old log files, keeping only the last N files
List all log files in the log directory, sorted newest first.

@return boolean success
Tries lfs directory iteration first (works on Android); falls back to
io.popen("find ...") on platforms where lfs is unavailable.

@return table Array of full log file paths sorted descending by name
--]]
function FileLogger:rotateLogs()
    -- Get all log files in the directory
function FileLogger:_listLogFiles()
    local log_files = {}
    local find_cmd = "find " .. self.log_dir .. " -name 'booklore-*.log' -type f | sort -r"
    local handle = io.popen(find_cmd)

    if not handle then
        logger.warn("BookloreSync FileLogger: Failed to list log files for rotation")
        return false
    local lfs_ok, lfs = pcall(require, "libs/libkoreader-lfs")
    if lfs_ok and lfs then
        -- lfs-based iteration — no shell required
        for entry in lfs.dir(self.log_dir) do
            if entry:match("^booklore%-%d%d%d%d%-%d%d%-%d%d%.log$") then
                table.insert(log_files, self.log_dir .. "/" .. entry)
            end
    
        end
        table.sort(log_files, function(a, b) return a > b end)
    else
        -- Shell fallback for platforms without lfs
        local find_cmd = "find " .. self.log_dir .. " -name 'booklore-*.log' -type f | sort -r"
        local handle = io.popen(find_cmd)
        if handle then
            for file in handle:lines() do
                table.insert(log_files, file)
            end
            handle:close()
        end
    end

    return log_files
end

--[[--
Rotate old log files, keeping only the last N files

@return boolean success
--]]
function FileLogger:rotateLogs()
    -- Get all log files in the directory
    local log_files = self:_listLogFiles()
    if not log_files then
        logger.warn("BookloreSync FileLogger: Failed to list log files for rotation")
        return false
    end
    
    -- Delete files beyond max_files
    if #log_files > self.max_files then
@@ -194,20 +233,7 @@ Get the list of existing log files
@return table Array of log file paths (sorted newest first)
--]]
function FileLogger:getLogFiles()
    local log_files = {}
    local find_cmd = "find " .. self.log_dir .. " -name 'booklore-*.log' -type f | sort -r"
    local handle = io.popen(find_cmd)
    
    if not handle then
        return log_files
    end
    
    for file in handle:lines() do
        table.insert(log_files, file)
    end
    handle:close()
    
    return log_files
    return self:_listLogFiles()
end

--[[--
+2 −2
Original line number Diff line number Diff line
@@ -55,8 +55,8 @@ function Updater:init(plugin_dir, db)
    -- Set up backup directory
    self.backup_dir = DataStorage:getDataDir() .. "/booklore-backups"
    
    -- Set up temp directory
    self.temp_dir = "/tmp/booklore-update-" .. tostring(os.time())
    -- Set up temp directory inside KOReader data dir (writable on all platforms including Android)
    self.temp_dir = DataStorage:getDataDir() .. "/booklore-temp-" .. tostring(os.time())
    
    logger.info("BookloreSync Updater: Initialized")
    logger.info("BookloreSync Updater: Plugin dir:", self.plugin_dir)