ftp_geturl.tcl

Go to the documentation of this file.
00001 /*  ftp_geturl.tcl --*/
00002 /* */
00003 /*  Copyright (c) 2001 by Andreas Kupries <andreas_kupries@users.sourceforge.net>*/
00004 /* */
00005 /*  ftp::geturl url*/
00006 
00007 package require ftp
00008 package require uri
00009 
00010 namespace ::ftp {
00011     namespace export geturl
00012 }
00013 
00014 /*  ::ftp::geturl*/
00015 /* */
00016 /*  Command useable by uri to retrieve the contents of an ftp url.*/
00017 /*  Returns the contents of the requested url.*/
00018 
00019 ret  ::ftp::geturl (type url) {
00020     # FUTURE: -validate to validate existence of url, but no download
00021     # of contents.
00022 
00023     array set urlparts [uri::split $url]
00024 
00025     if {$urlparts(user) == {}} {
00026         set urlparts(user) "anonymous"
00027     }
00028     if {$urlparts(pwd) == {}} {
00029         set urlparts(pwd) "user@localhost.localdomain"
00030     }
00031     if {$urlparts(port) == {}} {
00032         set urlparts(port) 21
00033     }
00034 
00035     set fdc [ftp::Open $urlparts(host) $urlparts(user) $urlparts(pwd) \
00036                  -port $urlparts(port)]
00037     if {$fdc < 0} {
00038     return -code error "Cannot reach host for url \"$url\""
00039     }
00040 
00041     # We have reached the host, now get on to retrieve the item.
00042     # We are very careful in accessing the item because we don't know
00043     # if it is a file, directory or link. So we change into the
00044     # directory containing the item, get a list of all entries and
00045     # then determine if the item actually exists and what type it is,
00046     # and what actions to perform.
00047 
00048     set ftp_dir  [file dirname $urlparts(path)]
00049     set ftp_file [file tail    $urlparts(path)]
00050 
00051     set result [ftp::Cd $fdc $ftp_dir]
00052     if { $result == 0 } {
00053     ftp::Close $fdc
00054     return -code error "Cannot reach directory of url \"$url\""
00055     }
00056 
00057     # Fix for the tkcon List enhancements in ftp.tcl
00058     set List ::ftp::List_org
00059     if {[info command $List] == {}} {
00060         set List ::ftp::List 
00061     }
00062 
00063     # The result of List is a list of entries in the given directory.
00064     # Note that it is in 'ls -l format. We parse that into a more
00065     # readable array.
00066 
00067     #array set flist [ftp::ParseList [$List $fdc ""]]
00068     #if {![info exists flist($ftp_file)]} {}
00069     set flist [$List $fdc $ftp_file]
00070     if {$flist == {}} {
00071     ftp::Close $fdc
00072     return -code error "Cannot reach item of url \"$url\""
00073     }
00074 
00075     # The item exists, what is it ?
00076     # File     : Download the contents.
00077     # Directory: Download a listing, this is its contents.
00078     # Link     : For now we do not follow the link but return the
00079     #            meta information, i.e. the path it is pointing to.
00080 
00081     #switch -exact -- [lindex $flist($ftp_file) 0] {}
00082     switch -exact -- [string index [lindex $flist 0] 0] {
00083     - {
00084         ftp::Get $fdc $ftp_file -variable contents
00085     }
00086     d {
00087         set contents [ftp::NList $fdc $ftp_file]
00088     }
00089     l {
00090         set contents $flist
00091     }
00092         default {
00093             ftp::Close $fdc
00094             return -code error "File information \"$flist\" not recognised"
00095         }
00096     }
00097 
00098     ftp::Close $fdc
00099     return $contents
00100 }
00101 
00102 /*  Internal helper to parse a directory listing into something which*/
00103 /*  can be better handled by tcl than raw ls -l format.*/
00104 
00105 ret  ::ftp::ParseList (type flist) {
00106     array set data {}
00107     foreach item $flist {
00108     foreach {mode dummy owner group size month day yrtime name} $item break
00109 
00110     if {[string first : $yrtime] >=0} {
00111         set date "$month/$day/[clock format [clock seconds] -format %Y] $yrtime"
00112     } else {
00113         set date "$month/$day/$yrtime 00:00"
00114     }
00115     set info [list owner $owner group $group size $size date $date]
00116 
00117     switch -exact -- [string index $mode 0] {
00118         - {set type file}
00119         d {set type dir}
00120         l {set type link ; lappend info link [lindex $item end]}
00121     }
00122 
00123     set data($name) [list $type $info]
00124     }
00125     array get data
00126 }
00127 
00128 /*  ==================================================================*/
00129 /*  At last, everything is fine, we can provide the package.*/
00130 
00131 package provide ftp::geturl [lindex {Revision: 0.2} 1]
00132 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1