~ubuntu-branches/ubuntu/oneiric/dejagnu/oneiric

« back to all changes in this revision

Viewing changes to lib/ftp.exp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2004-02-09 15:07:58 UTC
  • Revision ID: james.westby@ubuntu.com-20040209150758-oaj7r5zrop60v8sb
Tags: upstream-1.4.4
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
 
2
# 2001, 2002, 2003 Free Software Foundation, Inc.
 
3
#
 
4
# This file is part of DejaGnu.
 
5
#
 
6
# DejaGnu is free software; you can redistribute it and/or modify it
 
7
# under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# DejaGnu is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
# General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with DejaGnu; if not, write to the Free Software Foundation,
 
18
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
# Open an FTP connection to HOST.
 
21
#
 
22
proc ftp_open {host} {
 
23
    set prompt "ftp>"
 
24
    global board_info
 
25
 
 
26
    if [board_info $host exists name] {
 
27
        set host [board_info $host name]
 
28
    }
 
29
 
 
30
    if [board_info $host exists ftp_fileid] {
 
31
        return [board_info $host ftp_fileid]
 
32
    }
 
33
 
 
34
    if [board_info $host exists hostname] {
 
35
        set remotehost [board_info $host hostname]
 
36
    } else {
 
37
        set remotehost $host
 
38
    }
 
39
 
 
40
    # LoseQVT tends to get stuck sometimes; we'll loop around a few million
 
41
    # times when it gets a "connection refused".
 
42
    set spawn_id -1
 
43
    set count 3
 
44
    while {$spawn_id < 0 && $count >= 0} {
 
45
        spawn ftp -n $remotehost
 
46
        expect {
 
47
            -i $spawn_id -re ".*220.*$prompt" { }
 
48
            -i $spawn_id -re ".*Connection refused.*$prompt" {
 
49
                sleep 2
 
50
                send "open $remotehost\n"
 
51
                exp_continue
 
52
            }
 
53
            -i $spawn_id default {
 
54
                close -i $spawn_id
 
55
                wait -i $spawn_id
 
56
                set spawn_id -1
 
57
            }
 
58
        }
 
59
        incr count -1
 
60
    }
 
61
    if {$spawn_id < 0} {
 
62
        return -1
 
63
    }
 
64
    set board_info($host,ftp_fileid) $spawn_id
 
65
    if [board_info $host exists ftp_username] {
 
66
        if [board_info $host exists ftp_password] {
 
67
            set command "user [board_info $host ftp_username] [board_info $host ftp_password]\n"
 
68
        } else {
 
69
            set command "user [board_info $host ftp_username]\n"
 
70
        }
 
71
        send "$command"
 
72
        expect {
 
73
            -i $spawn_id -re ".*230.*$prompt" { }
 
74
            -i $spawn_id default {
 
75
                close -i $spawn_id
 
76
                wait -i $spawn_id
 
77
                return -1
 
78
            }
 
79
        }
 
80
    }
 
81
    set timeout 15
 
82
    send -i $spawn_id "binary\n"
 
83
    expect {
 
84
        -i $spawn_id -re "200.*$prompt" { }
 
85
        -i $spawn_id timeout {
 
86
            close -i $spawn_id
 
87
            wait -i $spawn_id
 
88
            return -1
 
89
        }
 
90
    }
 
91
    if [board_info $host exists ftp_directory] {
 
92
        send "cd [board_info $host ftp_directory]\n"
 
93
        expect {
 
94
            -i $spawn_id -re "250.*$prompt" { }
 
95
            -i $spawn_id default {
 
96
                close -i $spawn_id
 
97
                wait -i $spawn_id
 
98
                return -1
 
99
            }
 
100
        }
 
101
    }
 
102
 
 
103
    if [board_info $host exists ftp_no_passive] {
 
104
        send "passive\n"
 
105
        expect {
 
106
            -i $spawn_id -re "Passive mode off.*$prompt" { }
 
107
            -i $spawn_id -re "Passive mode on.*$prompt" {
 
108
                send "passive\n"
 
109
                exp_continue
 
110
            }
 
111
            -i $spawn_id -re ".*$prompt" { }
 
112
        }
 
113
    }
 
114
 
 
115
    set board_info($host,ftp_fileid) $spawn_id
 
116
    return $spawn_id
 
117
}
 
118
 
 
119
# Fetch REMOTEFILE from HOST and store it as LOCALFILE.
 
120
#
 
121
proc ftp_upload {host remotefile localfile} {
 
122
    set prompt "ftp>"
 
123
 
 
124
    verbose "ftping $remotefile from $host to $localfile"
 
125
    set timeout 15
 
126
    set spawn_id [ftp_open $host]
 
127
    if {$spawn_id < 0} {
 
128
        return ""
 
129
    }
 
130
    set loop 1
 
131
 
 
132
    while {$loop} {
 
133
        send -i $spawn_id "get $remotefile $localfile\n"
 
134
        expect {
 
135
            -i $spawn_id -re ".*Too many open files.*$prompt" {
 
136
                ftp_close $host
 
137
            }
 
138
            -i $spawn_id -re ".*No such file or directory.*$prompt" {
 
139
                set loop 0
 
140
                set remotefile ""
 
141
            }
 
142
            -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" {set loop 0}
 
143
            -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
 
144
                set loop 0
 
145
                set remotefile ""
 
146
            }
 
147
            -i $spawn_id default {
 
148
                ftp_close $host
 
149
            }
 
150
        }
 
151
        if {$loop} {
 
152
            set spawn_id [ftp_open $host]
 
153
            if {$spawn_id < 0} {
 
154
                return ""
 
155
            }
 
156
        }
 
157
    }
 
158
    return $localfile
 
159
}
 
160
 
 
161
# Download LOCALFILE to HOST as REMOTEFILE.
 
162
#
 
163
proc ftp_download {host localfile remotefile} {
 
164
    set prompt "ftp>"
 
165
 
 
166
    verbose "putting $localfile $remotefile"
 
167
 
 
168
    if [board_info $host exists hostname] {
 
169
        set remotehost [board_info $host hostname]
 
170
    } else {
 
171
        set remotehost $host
 
172
    }
 
173
 
 
174
    set spawn_id [ftp_open $host]
 
175
    if {$spawn_id < 0} {
 
176
        return ""
 
177
    }
 
178
    set loop 1
 
179
 
 
180
    while {$loop} {
 
181
        send -i $spawn_id "put $localfile $remotefile\n"
 
182
        expect {
 
183
            -i $spawn_id -re ".*Too many open files.*$prompt" {
 
184
                ftp_close $host
 
185
            }
 
186
            -i $spawn_id -re ".*No such file or directory.*$prompt" {
 
187
                set loop 0
 
188
                set remotefile ""
 
189
            }
 
190
            -re "(^|\[\r\n\])150.*connection for (.*) \[(\]\[0-9.,\]+\\)\[\r\n\]" {
 
191
                set remotefile $expect_out(2,string)
 
192
                exp_continue
 
193
            }
 
194
            -i $spawn_id -re "(^|\[\r\n\])226.*$prompt" {
 
195
                set loop 0
 
196
            }
 
197
            -i $spawn_id -re "Timeout.*$prompt" {
 
198
                ftp_close $host
 
199
            }
 
200
            -i $spawn_id -re "(^|\[\r\n\])\[0-9\]\[0-9\]\[0-9\].*$prompt" {
 
201
                set loop 0
 
202
                set remotefile ""
 
203
            }
 
204
            -i $spawn_id default {
 
205
                ftp_close $host
 
206
            }
 
207
        }
 
208
        if {$loop} {
 
209
            set spawn_id [ftp_open $host]
 
210
            if {$spawn_id < 0} {
 
211
                return ""
 
212
            }
 
213
        }
 
214
    }
 
215
    return $remotefile
 
216
}
 
217
 
 
218
# Close the FTP connection to HOST.
 
219
#
 
220
proc ftp_close {host} {
 
221
    global board_info
 
222
 
 
223
    if [board_info $host exists name] {
 
224
        set host [board_info $host name]
 
225
    }
 
226
 
 
227
    if ![board_info $host exists ftp_fileid] {
 
228
        return ""
 
229
    }
 
230
 
 
231
    set spawn_id [board_info $host ftp_fileid]
 
232
    unset board_info($host,ftp_fileid)
 
233
 
 
234
    send -i $spawn_id "quit\n"
 
235
    close -i $spawn_id
 
236
    wait -i $spawn_id
 
237
    return ""
 
238
}