]> git.e2factory.org Git - e2factory.git/commitdiff
Fix rotating logs when the result name contains a dash
authorTobias Ulmer <tu@emlix.com>
Wed, 18 Jun 2014 13:10:05 +0000 (15:10 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 7 Oct 2015 18:41:56 +0000 (20:41 +0200)
Do not rotate log if not required. General cleanup.

bz#140

Signed-off-by: Tobias Ulmer <tu@emlix.com>
Changelog
generic/e2lib.lua

index 80b2f7eecb198ad55a4748c39d49059ddb2933af..03db8775f3fa52f131f80b59e315774aa63b8790 100644 (file)
--- 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
index 4e555cbe8dabc3f6c9ffed08e51dcf3636cb1116..b24b17ed1919f2d41468fd1464e4897e4e35a8a0 100644 (file)
@@ -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.