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