lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


io.flush() serves different purpose. it does only flush userspace buffers implemented in stdio.h, not underlying OS/filesystem cache. And fclose() is also triggering flush. So there's no reason to call io.flush() immediately before io.close()...


https://github.com/lua/lua/blob/master/liolib.c#L725

static int io_flush (lua_State *L) {
  return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
}


static int f_flush (lua_State *L) {
  return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
}

https://man7.org/linux/man-pages/man3/fflush.3.html

Note that fflush() flushes only the user-space buffers provided
       by the C library.  To ensure that the data is physically stored
       on disk the kernel buffers must be flushed too, for example, with
       sync(2) or fsync(2).

https://github.com/lua/lua/blob/master/liolib.c#L242

static int io_fclose (lua_State *L) {
  LStream *p = tolstream(L);
  int res = fclose(p->f);
  return luaL_fileresult(L, (res == 0), NULL);
}

https://man7.org/linux/man-pages/man3/fclose.3.html

       The fclose() function flushes the stream pointed to by stream
       (writing any buffered output data using fflush(3)) and closes the
       underlying file descriptor.


Dne 2022-09-19 18:06, DENIS napsal:
fsync is not avaliable in io.*

try to use flush()

local f = io.open('file.txt', 'w')

f:write('hello')

F:FLUSH()

f:close()

f = nil

---- Em Seg, 19 set 2022 12:40:15 -0300 TOMAS MUDRUNKA
<MUDRUNKA@SPOJE.NET> escreveu ---

Hello,
i have Lua script on Linux that opens file handle using io.open(),
writes to it using :write() and closes it using :close().

I really need way to call fsync() on that file descriptor.
https://man7.org/linux/man-pages/man2/fsync.2.html

Either as an argument, eg.: file:write(true) file:close(true)
or as separate call file:sync(). Is there way to do that?
If not, can you see this being added in the future?

I know as a workaround i can call os.execute("sync");
But that triggers system wide sync of all files and filesystems,
which
is not really desirable.

--
S pozdravem
Best regards
Tomáš Mudruňka

--
S pozdravem
Best regards
     Tomáš Mudruňka - SPOJE.NET s.r.o.