Scroll to navigation

fcopy(3tcl) Tcl Built-In Commands fcopy(3tcl)


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。在後臺複製期間如果發生了一個錯誤,第二個參數是與錯誤相關聯的錯誤字符串。使用後臺複製不需要把 inchanoutchan 轉換成非阻塞模式;fcopy 命令將自動關照這些。但是,需要使用 vwait 命令或使用 Tk 進入事件循環。

在後臺複製期間不允許對 inchanoutchan 做其他 I/O 操作。如果在複製進行期間 inchanoutchan 中被關閉,停止當前的複製並且不做命令回調。如果 inchan被關閉,則寫出爲 outchan 而排隊(queue)的所有數據。

注意在一個命令複製期間 inchan 可以變成可讀的。在一個後臺複製期間你應該關閉任何 fileevent句柄,這樣這些句柄不與複製相觸及(interfere)。通過一個 fileevent句柄的任何 I/O 嘗試將得到一個 "channel busy" 錯誤。

Fcopy 依據 inchanoutchan -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

本頁面中文版由中文 man 手冊頁計劃提供。
中文 man 手冊頁計劃:https://github.com/man-pages-zh/manpages-zh
8.0 Tcl