File: RemoteAccess.tcl

package info (click to toggle)
ftools-fv 5.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,908 kB
  • ctags: 2,922
  • sloc: tcl: 48,319; ansic: 16,926; cpp: 169; makefile: 157; sh: 121; csh: 10; exp: 2
file content (146 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
itcl::class RemoteClass {

    constructor {args} {}
    destructor {}

    private variable address ""
    private variable userName ""
    private variable passwd  ""
    private variable isOpen  0 
    private variable pwd ""
    private variable beginPwd ""

    public method openConn {add user pass}
    public method list {pwd}
    public method cd {dir}
    public method get {rFile lFile}
    public method pwd { {full 1} }
    private method closeConn {}
} 

itcl::body RemoteClass::constructor {args} {
    
}
    
itcl::body RemoteClass::destructor {} {
    closeConn
}

itcl::body RemoteClass::openConn {add user pass} {
    if {$isOpen == 1} {
	FTP::Close
	set address ""
	set passwd "" 
	set userName ""
	set isOpen 0
    } 
    set address $add
    set passwd $pass
    set userName $user

    FTP::Open $add $user $pass
    if { ! [FTP::isConn] } {
	error "Cannot open connection to $add, please check your user name and password"
	return
    }
    set isOpen 1
    set beginPwd [FTP::Pwd]
    if { $beginPwd == "/" } { set beginPwd "" }
}

itcl::body RemoteClass::list {pwd} {
    if { $isOpen == 0 } return
    
    set contentList [FTP::List $pwd]
    set dln ""
    set fln ""
    set dls ""
    set fls ""
    set dld ""
    set fld ""
    foreach i $contentList {
        set name [lindex $i end]
	if { "d" == [string range [lindex $i 0] 0 0] } {
	    if { [lindex $i end] == "." || [lindex $i end] == ".."} {
# get rid of the . and ..
		; 
	    } else {
		lappend dln $name/
                lappend dls "(dir)"
                lappend dld [join [lrange $i 5 7]]
	    }
	} elseif { "l" == [string range [lindex $i 0] 0 0] } {
           # Should we treat this as a directory or file?
           set link [lindex [split $name "/"] end]
           set name [lindex $i 8]
           set extLen [string length [file extension $name]]
           set lnkLen [string length [file extension $link]]
           if {  $extLen<2 || $extLen>6 || $extLen==[string length $name] || \
                 $lnkLen<2 || $lnkLen>6 || $lnkLen==[string length $link] \
              } {
              # Directory
              lappend dln $name/
              lappend dls "(sym)"
              lappend dld [join [lrange $i 5 7]]
           } else {
              # File
              lappend fln $name
              lappend fls "(sym)"
              lappend fld [join [lrange $i 5 7]]
           }
	} else {
	    lappend fln $name
            lappend fls [calcSizeStr [lindex $i 4]]
            lappend fld [join [lrange $i 5 7]]
	} 
    }
    return [::list $dln $dls $dld $fln $fls $fld]
}

itcl::body RemoteClass::cd {dir} {
    if { $isOpen == 0 } return
    
    if { $dir == "" } return
    FTP::Cd $dir
    set pwd $dir
}
    
itcl::body RemoteClass::get {rFile lFile} {
   if {$isOpen == 0} return 

   FTP::Type binary
   FTP::Get $rFile $lFile
}

itcl::body RemoteClass::pwd { {full 1} } {

    if {$isOpen == 0} return 

    set dir [FTP::Pwd]
    if { $full } {
       return "ftp://${address}${dir}"
    }

    set crrntParts [split $dir '/']
    set beginParts [split $beginPwd '/']
    set same 1
    set common ""
    set backup ""
    for { set i 1 } { $i < [llength $beginParts] } { incr i } {
	if { $same && [lindex $beginParts $i]==[lindex $crrntParts $i] } {
	    set common "$common/[lindex $beginParts $i]"
	} else {
	    set backup "/..$backup"
	    set same 0
	}
    }
    set dir "$backup/.[string range $dir [string length $common] end]"

    set netdir "ftp://${address}${dir}"
    return $netdir
}

itcl::body RemoteClass::closeConn {} {
    if {$isOpen == 0} return 
    FTP::Close
}