From ae39d585c538b144323ab24d8f47bdf9aa24faa3 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Wed, 18 Jun 2014 15:10:05 +0200 Subject: [PATCH] Fix rotating logs when the result name contains a dash Do not rotate log if not required. General cleanup. bz#140 Signed-off-by: Tobias Ulmer --- Changelog | 1 + generic/e2lib.lua | 80 +++++++++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/Changelog b/Changelog index 80b2f7e..03db877 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ NEXT: + * Fix rotating logs when the result name contains a dash * Fix rsync silently skipping non-regular files and directories e2factory-2.3.14p0 diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 4e555cb..b24b17e 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -476,6 +476,9 @@ function e2lib.log(level, msg) end --- Rotate log file. +-- @param file Absolute path to log file. +-- @return True on success, false on error. +-- @return Error object on failure. function e2lib.rotate_log(file) local e = err.new("rotating logfile: %s", file) local rc, re @@ -486,48 +489,65 @@ function e2lib.rotate_log(file) return false, e:cat(string.format("%s: can't read directory", dir)) end local files = {} - for _,f in ipairs(dir) do - local match = f:match(string.format("%s.[0-9]+", logfile)) - if match then - table.insert(files, 1, match) + local dst + + if not e2util.stat(file) then + return true + end + + for f, re in e2lib.directory(logdir, false) do + local start, stop, extension + + if not f then + return false, e:cat(re) + end + + start, stop = string.find(f, logfile, 1, true) + if start and start == 1 then + extension = string.sub(f, stop+1) + if string.find(extension, "^%.[0-9]+$") then + table.insert(files, f) + end end end + -- sort in reverse order local function comp(a, b) - local na = a:match(string.format("%s.([0-9]+)", logfile)) - local nb = b:match(string.format("%s.([0-9]+)", logfile)) + local na = a:match("%.([0-9]+)$") + local nb = b:match("%.([0-9]+)$") return tonumber(na) > tonumber(nb) end + table.sort(files, comp) + for _,f in ipairs(files) do - local n = f:match(string.format("%s.([0-9]+)", logfile)) - if n then - n = tonumber(n) - if n >= e2lib.globals.logrotate - 1 then - local del = string.format("%s/%s.%d", logdir, logfile, n) - rc, re = e2lib.rm(del) - if not rc then - return false, e:cat(re) - end - else - local src = string.format("%s/%s.%d", logdir, logfile, n) - local dst = string.format("%s/%s.%d", logdir, logfile, n + 1) - rc, re = e2lib.mv(src, dst) - if not rc then - return false, e:cat(re) - end + local n, src, dst + + src = e2lib.join(logdir, f) + n = f:match("%.([0-9]+)$") + assert(n, "could not match logfile number") + n = tonumber(n) + if n >= e2lib.globals.logrotate - 1 then + rc, re = e2lib.rm(src) + if not rc then + return false, e:cat(re) + end + else + dst = string.format("%s/%s.%d", logdir, logfile, n + 1) + rc, re = e2lib.mv(src, dst) + if not rc then + return false, e:cat(re) end end end - local src = file - local dst = string.format("%s/%s.0", logdir, logfile) - if e2lib.isfile(src) then - rc, re = e2lib.mv(src, dst) - if not rc then - return false, e:cat(re) - end + + dst = string.format("%s/%s.0", logdir, logfile) + assert(not e2util.stat(dst), "did not expect logfile here: "..dst) + rc, re = e2lib.mv(file, dst) + if not rc then + return false, e:cat(re) end - return true, nil + return true end --- Clean up temporary files and directories, shut down plugins. -- 2.39.5