The fcopy command copies data from one I/O channel, inchan, to another I/O channel, outchan. The fcopy
command leverages the buffering in the Tcl I/O system to avoid extra copies and to avoid buffering too
much data in main memory when copying large files to destinations like network sockets.
DATAQUANTITY
All data until EOF is copied. In addition, the quantity of copied data may be specified by the option
-size. The given size is in bytes, if the input channel is in binary mode. Otherwise, it is in
characters.
Depreciated feature: the transfer is treated as a binary transfer, if the encoding profile is set to
“tcl8” and the input encoding matches the output encoding. In this case, eventual encoding errors are
not handled. An eventually given size is in bytes in this case.
BLOCKINGOPERATIONMODE
Without the -command option, fcopy blocks until the copy is complete and returns the number of bytes or
characters (using the same rules as for the -size option) written to outchan.
BACKGROUNDOPERATIONMODE
The -command argument makes fcopy work in the background. In this case it returns immediately and the
callback is invoked later when the copy completes. The callback is called with one or two additional
arguments that indicates how many bytes were written to outchan. If an error occurred during the
background copy, the second argument is the error string associated with the error. With a background
copy, it is not necessary to put inchan or outchan into non-blocking mode; the fcopy command takes care
of that automatically. However, it is necessary to enter the event loop by using the vwait command or by
using Tk.
You are not allowed to do other input operations with inchan, or output operations with outchan, during a
background fcopy. The converse is entirely legitimate, as exhibited by the bidirectional fcopy example
below.
If either inchan or outchan get closed while the copy is in progress, the current copy is stopped and the
command callback is not made. If inchan is closed, then all data already queued for outchan is written
out.
Note that inchan can become readable during a background copy. You should turn off any fileevent
handlers during a background copy so those handlers do not interfere with the copy. Any wrong-sided I/O
attempted (by a fileevent handler or otherwise) will get a “channel busy” error.
CHANNELTRANSLATIONOPTIONSFcopy translates end-of-line sequences in inchan and outchan according to the -translation option for
these channels. See the manual entry for fconfigure for details on the -translation option. The
translations mean that the number of bytes read from inchan can be different than the number of bytes
written to outchan. Only the number of bytes written to outchan is reported, either as the return value
of a synchronous fcopy or as the argument to the callback for an asynchronous fcopy.
CHANNELENCODINGOPTIONSFcopy obeys the encodings, profiles and character translations configured for the channels. This means
that the incoming characters are converted internally first UTF-8 and then into the encoding of the
channel fcopy writes to. See the manual entry for fconfigure for details on the -encoding and -profile
options. No conversion is done if both channels are set to encoding “binary” and have matching
translations. If only the output channel is set to encoding “binary” the system will write the internal
UTF-8 representation of the incoming characters. If only the input channel is set to encoding “binary”
the system will assume that the incoming bytes are valid UTF-8 characters and convert them according to
the output encoding. The behaviour of the system for bytes which are not valid UTF-8 characters is
undefined in this case.
Fcopy may throw encoding errors (error code EILSEQ), if input or output channel is configured to the
“strict” encoding profile.
If an encoding error arises on the input channel, any data before the error byte is written to the output
channel. The input file pointer is located just before the values causing the encoding error. Error
inspection or recovery is possible by changing the encoding parameters and invoking a file command (read,
fcopy).
If an encoding error arises on the output channel, the erroneous data is lost. To make the difference
between the input error case and the output error case, only the error message may be inspected (read or
write), as both throw the error code EILSEQ.