From: Tobias Ulmer Date: Mon, 11 Feb 2019 17:47:34 +0000 (+0100) Subject: eio: add fflush() X-Git-Tag: e2factory-2.3.18rc1~65 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=076aaa58e2eb910578223c149ba6051174d6218a;p=e2factory.git eio: add fflush() Signed-off-by: Tobias Ulmer --- diff --git a/generic/eio.lua b/generic/eio.lua index a668985..98eb2e7 100644 --- a/generic/eio.lua +++ b/generic/eio.lua @@ -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. diff --git a/generic/leio.c b/generic/leio.c index b035478..7539bcf 100644 --- a/generic/leio.c +++ b/generic/leio.c @@ -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 },