Because they use the Lua io
interface, the built in
ltn12.source.file
and ltn12.sink.file
inherit C's stdio.h
semantics.
This may not be desirable, for example, if the source is stdin
connected to an interactive stream where we want to process chunks as
soon as possible, and we wish to avoid buffering
(stdin:setvbuf('no')
). In this case ltn12.source.file(stdin)
will
block until exactly ltn12.BLOCKSIZE
bytes (usually 2048) are read from
the stream. ltn12_posix.source(0)
will instead return a chunk as soon
as one is available for reading, however many bytes it may be, as per
POSIX read
semantics.
local ltn12 = require('ltn12')
local ltn12_posix = require('ltn12-posix')
local fcntl = require('posix.fcntl')
-- copy a file
ltn12.pump.all(
ltn12_posix.source(fcntl.open('original.png', 'r')),
ltn12_posix.sink(fcntl.open('copy.png', 'w'))
)