1fcopy(n) Tcl Built-In Commands fcopy(n)
2
3
4
5______________________________________________________________________________
6
8 fcopy - Copy data from one channel to another
9
11 fcopy inchan outchan ?-size size? ?-command callback?
12______________________________________________________________________________
13
14
16 The fcopy command copies data from one I/O channel, inchan to another
17 I/O channel, outchan. The fcopy command leverages the buffering in the
18 Tcl I/O system to avoid extra copies and to avoid buffering too much
19 data in main memory when copying large files to slow destinations like
20 network sockets.
21
22 The fcopy command transfers data from inchan until end of file or size
23 bytes or characters have been transferred; size is in bytes if the two
24 channels are using the same encoding, and is in characters otherwise.
25 If no -size argument is given, then the copy goes until end of file.
26 All the data read from inchan is copied to outchan. Without the -com‐
27 mand option, fcopy blocks until the copy is complete and returns the
28 number of bytes or characters (using the same rules as for the -size
29 option) written to outchan.
30
31 The -command argument makes fcopy work in the background. In this case
32 it returns immediately and the callback is invoked later when the copy
33 completes. The callback is called with one or two additional arguments
34 that indicates how many bytes were written to outchan. If an error
35 occurred during the background copy, the second argument is the error
36 string associated with the error. With a background copy, it is not
37 necessary to put inchan or outchan into non-blocking mode; the fcopy
38 command takes care of that automatically. However, it is necessary to
39 enter the event loop by using the vwait command or by using Tk.
40
41 You are not allowed to do other input operations with inchan, or output
42 operations with outchan, during a background fcopy. The converse is
43 entirely legitimate, as exhibited by the bidirectional fcopy example
44 below.
45
46 If either inchan or outchan get closed while the copy is in progress,
47 the current copy is stopped and the command callback is not made. If
48 inchan is closed, then all data already queued for outchan is written
49 out.
50
51 Note that inchan can become readable during a background copy. You
52 should turn off any fileevent handlers during a background copy so
53 those handlers do not interfere with the copy. Any wrong-sided I/O
54 attempted (by a fileevent handler or otherwise) will get a “channel
55 busy” error.
56
57 Fcopy translates end-of-line sequences in inchan and outchan according
58 to the -translation option for these channels. See the manual entry
59 for fconfigure for details on the -translation option. The transla‐
60 tions mean that the number of bytes read from inchan can be different
61 than the number of bytes written to outchan. Only the number of bytes
62 written to outchan is reported, either as the return value of a syn‐
63 chronous fcopy or as the argument to the callback for an asynchronous
64 fcopy.
65
66 Fcopy obeys the encodings and character translations configured for the
67 channels. This means that the incoming characters are converted inter‐
68 nally first UTF-8 and then into the encoding of the channel fcopy
69 writes to. See the manual entry for fconfigure for details on the
70 -encoding and -translation options. No conversion is done if both chan‐
71 nels are set to encoding “binary” and have matching translations. If
72 only the output channel is set to encoding “binary” the system will
73 write the internal UTF-8 representation of the incoming characters. If
74 only the input channel is set to encoding “binary” the system will
75 assume that the incoming bytes are valid UTF-8 characters and convert
76 them according to the output encoding. The behaviour of the system for
77 bytes which are not valid UTF-8 characters is undefined in this case.
78
80 The first example transfers the contents of one channel exactly to
81 another. Note that when copying one file to another, it is better to
82 use file copy which also copies file metadata (e.g. the file access
83 permissions) where possible.
84
85 fconfigure $in -translation binary
86 fconfigure $out -translation binary
87 fcopy $in $out
88
89 This second example shows how the callback gets passed the number of
90 bytes transferred. It also uses vwait to put the application into the
91 event loop. Of course, this simplified example could be done without
92 the command callback.
93
94 proc Cleanup {in out bytes {error {}}} {
95 global total
96 set total $bytes
97 close $in
98 close $out
99 if {[string length $error] != 0} {
100 # error occurred during the copy
101 }
102 }
103 set in [open $file1]
104 set out [socket $server $port]
105 fcopy $in $out -command [list Cleanup $in $out]
106 vwait total
107
108 The third example copies in chunks and tests for end of file in the
109 command callback.
110
111 proc CopyMore {in out chunk bytes {error {}}} {
112 global total done
113 incr total $bytes
114 if {([string length $error] != 0) || [eof $in]} {
115 set done $total
116 close $in
117 close $out
118 } else {
119 fcopy $in $out -size $chunk \
120 -command [list CopyMore $in $out $chunk]
121 }
122 }
123 set in [open $file1]
124 set out [socket $server $port]
125 set chunk 1024
126 set total 0
127 fcopy $in $out -size $chunk \
128 -command [list CopyMore $in $out $chunk]
129 vwait done
130
131 The fourth example starts an asynchronous, bidirectional fcopy between
132 two sockets. Those could also be pipes from two [open "|hal 9000" r+]
133 (though their conversation would remain secret to the script, since all
134 four fileevent slots are busy).
135
136 set flows 2
137 proc Done {dir args} {
138 global flows done
139 puts "$dir is over."
140 incr flows -1
141 if {$flows<=0} {set done 1}
142 }
143 fcopy $sok1 $sok2 -command [list Done UP]
144 fcopy $sok2 $sok1 -command [list Done DOWN]
145 vwait done
146
148 eof(n), fblocked(n), fconfigure(n), file(n)
149
151 blocking, channel, end of line, end of file, nonblocking, read, trans‐
152 lation
153
154
155
156Tcl 8.0 fcopy(n)