]> git.e2factory.org Git - e2factory.git/commitdiff
eio: add fflush()
authorTobias Ulmer <tu@emlix.com>
Mon, 11 Feb 2019 17:47:34 +0000 (18:47 +0100)
committerTobias Ulmer <tu@emlix.com>
Mon, 11 Feb 2019 17:47:34 +0000 (18:47 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/eio.lua
generic/leio.c

index a6689859f20406141e038ef91ca08de8421f2121..98eb2e76c5c1ed9a905f8270308938f6abc4305a 100644 (file)
@@ -129,6 +129,35 @@ function eio.fclose(file)
     return true
 end
 
+--- Flush file object user space buffers.
+-- @param file File object (or nil).
+-- @return True on success, false on error.
+-- @return Error object on failure.
+function eio.fflush(file)
+    local rc, re, errstring, handle
+
+    if file then
+        rc, re = is_eio_object(file)
+        if not rc then
+            return false, re
+        end
+    end
+
+    if file then
+        rc, errstring = leio.fflush(file.handle)
+        if not rc then
+            return false, err.new("error flushing file %s: %s", file.finfo, errstring)
+        end
+    else
+        rc, errstring = leio.fflush(nil)
+        if not rc then
+            return false, err.new("error flushing files: %s", errstring)
+        end
+    end
+
+    return true
+end
+
 --- Close a file descriptor.
 -- @param fd File descriptor.
 -- @return True on success, false on error.
index b03547815960b8be07aaeadba9990deb98af48fa..7539bcfe8c44c322b17c1bbbc1ba2c93d6ffa921 100644 (file)
@@ -79,6 +79,22 @@ eio_fclose(lua_State *lua)
        return 1;
 }
 
+static int
+eio_fflush(lua_State *lua)
+{
+       FILE *f;
+       f = lua_touserdata(lua, 0); /* May be NULL */
+
+       if (fflush(f) == EOF) {
+               lua_pushboolean(lua, 0);
+               lua_pushstring(lua, strerror(errno));
+               return 2;
+       }
+
+       lua_pushboolean(lua, 1);
+       return 1;
+}
+
 static int
 eio_close(lua_State *L)
 {
@@ -464,6 +480,7 @@ static luaL_Reg lib[] = {
   { "fclose", eio_fclose },
   { "fdopen", eio_fdopen },
   { "feof", eio_feof },
+  { "fflush", eio_fflush },
   { "fgetc", eio_fgetc },
   { "fileno", eio_fileno },
   { "fopen", eio_fopen },