lua-users home
lua-l archive

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

u can implement fsync() easly on io lib.

fsync is POSIX (not ANSI)

---- Em Seg, 19 set 2022 13:40:15 -0300 Tomas Mudrunka <> escreveu ---

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()...

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);

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).

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

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 ='file.txt', 'w')
> f:write('hello')
> 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,
>> writes to it using :write() and closes it using :close().
>> I really need way to call fsync() on that file descriptor.
>> 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.