fcopy



fcopy

NAME
SYNOPSIS
DESCRIPTION
EXAMPLES
SEE ALSO
KEYWORDS

___________________________

NAME

fcopy − Copy data from one channel to another

SYNOPSIS

fcopy inchan outchan ?−size size? ?−command callback? ___________________________

DESCRIPTION

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 slow destinations like network sockets.

The fcopy command transfers data from inchan until end of file or size bytes have been transferred. If no −size argument is given, then the copy goes until end of file. All the data read from inchan is copied to outchan. Without the −command option, fcopy blocks until the copy is complete and returns the number of bytes written to outchan.

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.

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

Fcopy obeys the encodings 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 −translation 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.

EXAMPLES

The first example transfers the contents of one channel exactly to another. Note that when copying one file to another, it is better to use file copy which also copies file metadata (e.g. the file access permissions) where possible.

fconfigure $in -translation binary
fconfigure $out -translation binary
fcopy
$in $out

This second example shows how the callback gets passed the number of bytes transferred. It also uses vwait to put the application into the event loop. Of course, this simplified example could be done without the command callback.

proc Cleanup {in out bytes {error {}}} {
global total
set total $bytes
close $in
close $out
if {[string length $error] != 0} {
# error occurred during the copy
}
}
set in [open $file1]
set out [socket $server $port]
fcopy
$in $out -command [list Cleanup $in $out]
vwait total

The third example copies in chunks and tests for end of file in the command callback.

proc CopyMore {in out chunk bytes {error {}}} {
global total done
incr total $bytes
if {([string length $error] != 0) || [eof $in]} {
set done $total
close $in
close $out
} else {
fcopy
$in $out -size $chunk \
-command [list CopyMore $in $out $chunk]
}
}
set in [open $file1]
set out [socket $server $port]
set chunk 1024
set total 0
fcopy
$in $out -size $chunk \
-command [list CopyMore $in $out $chunk]
vwait done

The fourth example starts an asynchronous, bidirectional fcopy between two sockets. Those could also be pipes from two [open "|hal 9000" r+] (though their conversation would remain secret to the script, since all four fileevent slots are busy).

set flows 2
proc Done {dir args} {
global flows done
puts "$dir is over."
incr flows -1
if {$flows<=0} {set done 1}
}
fcopy
$sok1 $sok2 -command [list Done UP]
fcopy
$sok2 $sok1 -command [list Done DOWN]
vwait done

SEE ALSO

eof(n), fblocked(n), fconfigure(n), file(n)

KEYWORDS

blocking, channel, end of line, end of file, nonblocking, read, translation




More Linux Commands

manpages/mytool.1.html
mytool(1) - manipulate map files for yudit, uniconv and unip
mytool is a my map file manipulation program in the yudit distribution. It can generates so-called binary nbit ( my ) map file that can map any sequences of byt

manpages/ObjCmdWrite.3.html
ObjCmdWrite(3) - Writing C language extensions to Tcl. '....
ObjCmdWrite.3 - This document is intended to help the programmer who wishes to extend Tcl with C language routines. It should also be useful to someone wishing

manpages/clear.3ncurses.html
clear(3ncurses) - clear all or part of a curses window......
The erase and werase routines copy blanks to every position in the window, clearing the screen. The clear and wclear routines are like erase and werase, but the

manpages/SDL_SetGamma.3.html
SDL_SetGamma(3) - Sets the color gamma function for the disp
Sets the gamma function for the display of each color component. Gamma controls the brightness/contrast of colors displayed on the screen. A gamma value of 1.0

manpages/immedok.3ncurses.html
immedok(3ncurses) - curses output options - Linux man page
These routines set options that change the style of output within curses. All options are initially FALSE, unless otherwise stated. It is not necessary to turn

manpages/perlfaq.1.html
perlfaq(1) - frequently asked questions about Perl (ManPage)
The perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming. Its divided by topic into nine major sect

manpages/Tcl_WaitForEvent.3.html
Tcl_WaitForEvent(3) - the event queue and notifier interface
The interfaces described here are used to customize the Tcl event loop. The two most common customizations are to add new sources of events and to merge Tcls ev

manpages/Tcl_GetObjResult.3.html
Tcl_GetObjResult(3) - manipulate Tcl result - Linux man page
The procedures described here are utilities for manipulating the result value in a Tcl interpreter. The interpreter result may be either a Tcl object or a strin

manpages/gnutls_pubkey_get_key_usage.3.html
gnutls_pubkey_get_key_usage(3) - API function (Man Page)....
This function will return the key usage of the public key. RETURNS On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error value. SINCE 2.12.0

manpages/xgi.4.html
xgi(4) - XGI video driver (Special files - Linux man page)
xgi is an XFree86 driver for XGI video chips. The driver is accelerated, and provides support for colordepths of 8, 16 and 24 bpp. XVideo, Render and other exte

manpages/tsort.1.html
tsort(1) - perform topological sort - Linux manual page.....
Write totally ordered list consistent with the partial ordering in FILE. With no FILE, or when FILE is -, read standard input. --help display this help and exit

manpages/Tcl_RegExpGetInfo.3.html
Tcl_RegExpGetInfo(3) - Pattern matching with regular express
Tcl_RegExpMatch determines whether its pattern argument matches regexp, where regexp is interpreted as a regular expression using the rules in the re_syntax ref





We can't live, work or learn in freedom unless the software we use is free.