Home Contents | # rawio:copy | Rawio | Core4 Lua Event System

## SYNOPSIS

srchandle:copy(dsthandle [, blocksize [, timeout]])

## DESCRIPTION

Copies data from srchandle to dsthandle. The handles must have been properly opened for at least reading and writing, respectively.

Each call to copy() will copy at most blocksize bytes. It may copy less due to buffering. The default is 64kB.

To copy a file to a destination, the copy operation must be called in a loop until it returns nil. Only this will make sure to copy the full file contents.

If the optional timeout value is given, the call will wait for this amount of milliseconds for data before it fails. This applies to both blocking and non-blocking mode.

## RETURN VALUE

On success, returns the number of bytes copied. On failure or end-of-file, returns nil. In case of an error, the error can be retrieved using srchandle:lastError().

## NOTES

Also returns nil on end-of-file. Calling srchandle:lastError() will return nil, too, in that case, indicating no error.

## EXAMPLE

> > > > > > > > > > > > > > > > >
local src = rawio.open("/media/storage/src.dat") local dst = rawio.open("/media/storage/dst.dat") while true; do local bytes = src:copy(dst) if (not bytes) then break end print(string.format("Copied %u bytes", bytes)) end local code, text = src:lastError() if (code) then print(string.format("Copy failed: %s", text)) end dst:close() src:close()

## SEE ALSO

rawio.attach, rawio:blocking, rawio:canReadLine, rawio:clearError, rawio:close, rawio:connect, rawio:detach, rawio:lastError, rawio:line, rawio:lines, rawio.access, rawio.open, rawio:read, rawio:slurp, rawio.stat, rawio.statfs, rawio:writet, rawio:writes, rawio:write