NAME¶
fcopy -
从一个通道向另一个复制数据
总览 SYNOPSIS¶
fcopy inchan outchan ?
-size size?
?
-command callback?
描述 DESCRIPTION¶
fcopy 命令从一个 I/O 通道
inchan 向另一个 I/O 通道
outchan 复制数据。
fcopy
命令在 Tcl I/O
系统中起到缓冲的杠杆作用(leverage),它避免额外的复制,并且在向慢速目标如网络套接口复制大文件的时候避免在主存中缓冲过多的数据。
fcopy 命令从
inchan
传输数据直到文件结束或传输完
size字节。如果没有给出
-size
参数,则复制持续到文件结束。从
inchan读的所有数据都复制到
outchan。如果没有
-command选项,在复制完成并返回写到
outchan 的字节数之前
fcopy
将阻塞。
-command 参数使
fcopy在后台工作。在这种情况下它立即返回,并在复制完成时调用
callback。调用
callback
加上一个或两个额外的参数来指示有多少字节被写到了
outchan。在后台复制期间如果发生了一个错误,第二个参数是与错误相关联的错误字符串。使用后台复制不需要把
inchan 或
outchan
转换成非阻塞模式;
fcopy
命令将自动关照这些。但是,需要使用
vwait 命令或使用 Tk
进入事件循环。
在后台复制期间不允许对
inchan 或
outchan 做其他 I/O
操作。如果在复制进行期间
inchan 或
outchan
中被关闭,停止当前的复制并且不做命令回调。如果
inchan被关闭,则写出为
outchan
而排队(queue)的所有数据。
注意在一个命令复制期间
inchan
可以变成可读的。在一个后台复制期间你应该关闭任何
fileevent句柄,这样这些句柄不与复制相触及(interfere)。通过一个
fileevent句柄的任何 I/O
尝试将得到一个 "channel
busy" 错误。
Fcopy 依据
inchan 和
outchan 的
-translation选项来转换它们中的文件行结束序列。参见
fconfigure的手册条目来得到
-translation
选项的详情。转换意味着从
inchan
读到的字节数与写到
outchan.的字节数可能不同。只报告写到
outchan中的字节数。要么作为同步的
fcopy
的返回值,要么作为给异步的
fcopy 的回调的参数。
范例 EXAMPLE¶
第一个例子展示了回调如何得到传递给它的传输了的字节数。它还使用
vwait
来使应用进入事件循环。当然,不使用回调也能做出这个简化了的例子。
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
第二个例子按块(chunk)复制并在命令回调中测试文件结束。
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 -command [list CopyMore $in $out $chunk] \
-size $chunk
}
}
set in [open $file1]
set out [socket $server $port]
set chunk 1024
set total 0
fcopy $in $out -command [list CopyMore $in $out $chunk] -size $chunk
vwait done
参见 SEE ALSO¶
eof(n), fblocked(n), fconfigure(n)
关键字 KEYWORDS¶
blocking, channel, end of line, end of file, nonblocking, read, translation
[中文版维护人]¶
寒蝉退士
[中文版最新更新]¶
2001/08/02
《中国 Linux 论坛 man 手册页翻译计划》:¶
http://cmpp.linuxforum.net