📄 ftp_lib.tcl
字号:
# Arguments:# filename - specifies the remote file name# # Returns:# clock - files date and time as a system-depentend integer# value in seconds (see tcls clock command) or {} in # error casesproc ModTime {{filename ""}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return {} } if { $filename == "" } { return {} } set ftp(File) $filename set ftp(DateTime) "" set ftp(State) modtime StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(File) if {$rc} { scan $ftp(DateTime) "%4s%2s%2s%2s%2s%2s" year month day hour min sec set clock [clock scan "$month/$day/$year $hour:$min:$sec" -gmt 1] unset year month day hour min sec return $clock } else { return {} }}############################################################################### Pwd --## PRINT WORKING DIRECTORY - Causes the name of the current working directory.# (exported)# # Arguments:# None.# # Returns:# current directory nameproc Pwd {} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return {} } set ftp(Dir) {} set ftp(State) pwd StateHandler # wait for synchronization set rc [WaitOrTimeout] if {$rc} { return $ftp(Dir) } else { return {} }}############################################################################### Cd --# # CHANGE DIRECTORY - Sets the working directory on the server host.# (exported)# # Arguments:# dir - pathname specifying a directoryproc Cd {dir} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if { $dir == "" } { set ftp(Dir) "" } else { set ftp(Dir) " $dir" } set ftp(State) cd StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(Dir) if {$rc} { return 1 } else { return 0 }}############################################################################### MkDir --## MAKE DIRECTORY - This command causes the directory specified in the $dir# to be created as a directory (if the $dir is absolute) or as a subdirectory# of the current working directory (if the $dir is relative).# (exported)# # Arguments:# dir - new directory name## Returns:# 0 - ERROR# 1 - OKproc MkDir {dir} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } set ftp(Dir) $dir set ftp(State) mkdir StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(Dir) if {$rc} { return 1 } else { return 0 }}############################################################################### RmDir --## REMOVE DIRECTORY - This command causes the directory specified in $dir to # be removed as a directory (if the $dir is absolute) or as a # subdirectory of the current working directory (if the $dir is relative).# (exported)## Arguments:# dir - directory name## Returns:# 0 - ERROR# 1 - OKproc RmDir {dir} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } set ftp(Dir) $dir set ftp(State) rmdir StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(Dir) if {$rc} { return 1 } else { return 0 }}############################################################################### Delete --## DELETE - This command causes the file specified in $file to be deleted at # the server site.# (exported)# # Arguments:# file - file name## Returns:# 0 - ERROR# 1 - OKproc Delete {file} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } set ftp(File) $file set ftp(State) delete StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(File) if {$rc} { return 1 } else { return 0 }}############################################################################### Rename --## RENAME FROM TO - This command causes the file specified in $from to be # renamed at the server site.# (exported)# # Arguments:# from - specifies the old file name of the file which # is to be renamed# to - specifies the new file name of the file # specified in the $from agument# Returns:# 0 - ERROR# 1 - OKproc Rename {from to} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } set ftp(RenameFrom) $from set ftp(RenameTo) $to set ftp(State) rename StateHandler # wait for synchronization set rc [WaitOrTimeout] unset ftp(RenameFrom) unset ftp(RenameTo) if {$rc} { return 1 } else { return 0 }}############################################################################### ElapsedTime --## Gets the elapsed time for file transfer# # Arguments:# stop_time - ending timeproc ElapsedTime {stop_time} {variable ftp set elapsed [expr $stop_time - $ftp(Start_Time)] if { $elapsed == 0 } { set elapsed 1} set persec [expr $ftp(Total) / $elapsed] DisplayMsg "$ftp(Total) bytes sent in $elapsed seconds ($persec Bytes/s)"}############################################################################### PUT --## STORE DATA - Causes the server to accept the data transferred via the data # connection and to store the data as a file at the server site. If the file# exists at the server site, then its contents shall be replaced by the data# being transferred. A new file is created at the server site if the file# does not already exist.# (exported)## Arguments:# source - local file name# dest - remote file name, if unspecified, ftp assigns# the local file name.# Returns:# 0 - file not stored# 1 - OKproc Put {source {dest ""}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if ![file exists $source] { DisplayMsg "File \"$source\" not exist" error return 0 } if { $dest == "" } { set dest $source } set ftp(LocalFilename) $source set ftp(RemoteFilename) $dest set ftp(SourceCI) [open $ftp(LocalFilename) r] if { $ftp(Type) == "ascii" } { fconfigure $ftp(SourceCI) -buffering line -blocking 1 } else { fconfigure $ftp(SourceCI) -buffering line -translation binary -blocking 1 } set ftp(State) put_$ftp(Mode) StateHandler # wait for synchronization set rc [WaitOrTimeout] if {$rc} { ElapsedTime [clock seconds] return 1 } else { CloseDataConn return 0 }}############################################################################### APPEND --## APPEND DATA - Causes the server to accept the data transferred via the data # connection and to store the data as a file at the server site. If the file# exists at the server site, then the data shall be appended to that file; # otherwise the file specified in the pathname shall be created at the# server site.# (exported)## Arguments:# source - local file name# dest - remote file name, if unspecified, ftp assigns# the local file name.# Returns:# 0 - file not stored# 1 - OKproc Append {source {dest ""}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if ![file exists $source] { DisplayMsg "File \"$source\" not exist" error return 0 } if { $dest == "" } { set dest $source } set ftp(LocalFilename) $source set ftp(RemoteFilename) $dest set ftp(SourceCI) [open $ftp(LocalFilename) r] if { $ftp(Type) == "ascii" } { fconfigure $ftp(SourceCI) -buffering line -blocking 1 } else { fconfigure $ftp(SourceCI) -buffering line -translation binary -blocking 1 } set ftp(State) append_$ftp(Mode) StateHandler # wait for synchronization set rc [WaitOrTimeout] if {$rc} { ElapsedTime [clock seconds] return 1 } else { CloseDataConn return 0 }}############################################################################### Get --## RETRIEVE DATA - Causes the server to transfer a copy of the specified file# to the local site at the other end of the data connection.# (exported)## Arguments:# source - remote file name# dest - local file name, if unspecified, ftp assigns# the remote file name.# Returns:# 0 - file not retrieved# 1 - OK## Updated by M.K. - 6 Nov 1997 - added read_callback option#proc Get {source {dest ""} {read_callback ""} {expected_size 0}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if { $dest == "" } { set dest $source } set ftp(ReadCallback) $read_callback set ftp(RemoteFilename) $source set ftp(LocalFilename) $dest set ftp(ExpectedSize) $expected_size set ftp(State) get_$ftp(Mode) StateHandler # wait for synchronization set rc [WaitOrTimeout] if {$rc} { ElapsedTime [clock seconds] return 1 } else { CloseDataConn return 0 } }############################################################################### Reget --## RESTART RETRIEVING DATA - Causes the server to transfer a copy of the specified file# to the local site at the other end of the data connection like get but skips over # the file to the specified data checkpoint. # (exported)## Arguments:# source - remote file name# dest - local file name, if unspecified, ftp assigns# the remote file name.# Returns:# 0 - file not retrieved# 1 - OKproc Reget {source {dest ""}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if { $dest == "" } { set dest $source } set ftp(RemoteFilename) $source set ftp(LocalFilename) $dest if [file exists $ftp(LocalFilename)] { set ftp(FileSize) [file size $ftp(LocalFilename)] } else { set ftp(FileSize) 0 } set ftp(State) reget_$ftp(Mode) StateHandler # wait for synchronization set rc [WaitOrTimeout] if {$rc} { ElapsedTime [clock seconds] return 1 } else { CloseDataConn return 0 }}############################################################################### Newer --## GET NEWER DATA - Get the file only if the modification time of the remote # file is more recent that the file on the current system. If the file does# not exist on the current system, the remote file is considered newer.# Otherwise, this command is identical to get. # (exported)## Arguments:# source - remote file name# dest - local file name, if unspecified, ftp assigns# the remote file name.## Returns:# 0 - file not retrieved# 1 - OKproc Newer {source {dest ""}} {variable ftp if ![info exists ftp(State)] { DisplayMsg "Not connected!" error return 0 } if { $dest == "" } { set dest $source } set ftp(RemoteFilename) $source set ftp(LocalFilename) $dest # get remote modification time set rmt [ModTime $ftp(RemoteFilename)] if { $rmt == "-1" } { return 0 } # get local modification time if [file exists $ftp(LocalFilename)] { set lmt [file mtime $ftp(LocalFilename)] } else { set lmt 0 } # remote file is older than local file if { $rmt < $lmt } { return 0 } # remote file is newer than local file or local file doesn't exist # get it set rc [Get $ftp(RemoteFilename) $ftp(LocalFilename)] return $rc }##############################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -