]> git.e2factory.org Git - e2factory.git/commitdiff
e2lib: implement rename(), use in rotate_log()
authorTobias Ulmer <tu@emlix.com>
Wed, 3 May 2017 15:06:33 +0000 (17:06 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 25 Apr 2018 15:39:52 +0000 (17:39 +0200)
This makes it independent of the "mv" command, allowing to open and
rotate the log file before the tools module is initialized.

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

index 2072302a2cbb5530621429cce89c62ec2b747ffd..6b7a5bd58f169ae47c01af93206ed01acafa0073 100644 (file)
@@ -260,6 +260,20 @@ function e2lib.poll(timeout, fdvec)
     return pollvec
 end
 
+--- Rename file or directory using the rename syscall.
+-- @param src Source path.
+-- @param dst Destination path.
+-- @return True on success, false on error.
+-- @return Error object on failure.
+-- @return Errno number on failure.
+function e2lib.rename(src, dst)
+    local rc, re, errno = le2lib.rename(src, dst)
+    if rc then
+        return rc
+    end
+    return false, err.new("renaming %q to %q failed: %s", src, dst, re), errno
+end
+
 --- Set file descriptor in non-blocking mode.
 -- @param fd Open file descriptor.
 function e2lib.unblock(fd)
@@ -676,7 +690,7 @@ function e2lib.rotate_log(file)
             end
         else
             dst = string.format("%s/%s.%d", logdir, logfile, n + 1)
-            rc, re = e2lib.mv(src, dst)
+            rc, re = e2lib.rename(src, dst)
             if not rc then
                 return false, e:cat(re)
             end
@@ -684,8 +698,7 @@ function e2lib.rotate_log(file)
     end
 
     dst = string.format("%s/%s.0", logdir, logfile)
-    assert(not e2lib.stat(dst), "did not expect logfile here: "..dst)
-    rc, re = e2lib.mv(file, dst)
+    rc, re = e2lib.rename(file, dst)
     if not rc then
         return false, e:cat(re)
     end