1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it 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 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/04/07 07:14:56 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.ssh;
|
---|
22 |
|
---|
23 | import java.awt.*;
|
---|
24 | import java.awt.event.*;
|
---|
25 | import java.io.File;
|
---|
26 |
|
---|
27 | import mindbright.gui.ProgressBar;
|
---|
28 | import mindbright.util.AWTConvenience;
|
---|
29 |
|
---|
30 | public final class SSHSCPGUIThread extends Thread implements SSHSCPIndicator {
|
---|
31 | String curDir, localFile, remoteFile;
|
---|
32 | String remoteHost;
|
---|
33 | int remotePort;
|
---|
34 | SSHAuthenticator authenticator;
|
---|
35 | SSHClientUser mainUser;
|
---|
36 | SSHInteractor interactor;
|
---|
37 | boolean recursive, background, toRemote;
|
---|
38 | Frame parent;
|
---|
39 |
|
---|
40 | String[] localFileList;
|
---|
41 |
|
---|
42 | Dialog copyIndicator;
|
---|
43 | ProgressBar progress;
|
---|
44 | SSHSCP scp;
|
---|
45 | Thread copyThread;
|
---|
46 | Label srcLbl, dstLbl, sizeLbl, nameLbl, speedLbl;
|
---|
47 | Button cancB;
|
---|
48 | long startTime;
|
---|
49 | long lastTime;
|
---|
50 | int totTransSize;
|
---|
51 | int fileTransSize;
|
---|
52 | int curFileSize;
|
---|
53 | int lastSize;
|
---|
54 | int fileCnt;
|
---|
55 | boolean doneCopying;
|
---|
56 |
|
---|
57 | public SSHSCPGUIThread(String remoteHost, int remotePort,
|
---|
58 | SSHAuthenticator authenticator,
|
---|
59 | SSHClientUser mainUser, SSHInteractor interactor,
|
---|
60 | Frame parent,
|
---|
61 | String curDir, String localFile, String remoteFile,
|
---|
62 | boolean recursive, boolean background, boolean toRemote) throws Exception {
|
---|
63 | if(SSH.NETSCAPE_SECURITY_MODEL) {
|
---|
64 | try {
|
---|
65 | netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");
|
---|
66 | } catch (netscape.security.ForbiddenTargetException e) {
|
---|
67 | // !!!
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | localFileList = spaceSplit(localFile);
|
---|
72 |
|
---|
73 | if(localFileList == null) {
|
---|
74 | throw new Exception("Unbalanced quotes in local files list");
|
---|
75 | }
|
---|
76 |
|
---|
77 | File lf = new File(localFileList[0]);
|
---|
78 | if(lf.isAbsolute()) {
|
---|
79 | curDir = lf.getParent();
|
---|
80 | if(curDir == null)
|
---|
81 | curDir = lf.getAbsolutePath();
|
---|
82 | }
|
---|
83 |
|
---|
84 | localFileList = starExpand(localFileList, curDir);
|
---|
85 |
|
---|
86 | if(localFileList.length > 1 && !toRemote) {
|
---|
87 | throw new Exception("Ambiguos local target");
|
---|
88 | }
|
---|
89 |
|
---|
90 | if(!toRemote) {
|
---|
91 | localFile = localFileList[0];
|
---|
92 | }
|
---|
93 |
|
---|
94 | this.remoteHost = remoteHost;
|
---|
95 | this.curDir = curDir;
|
---|
96 | this.remotePort = remotePort;
|
---|
97 | this.authenticator = authenticator;
|
---|
98 | this.mainUser = mainUser;
|
---|
99 | this.interactor = interactor;
|
---|
100 | this.parent = parent;
|
---|
101 | this.localFile = localFile;
|
---|
102 | this.remoteFile = remoteFile;
|
---|
103 | this.recursive = recursive;
|
---|
104 | this.background = background;
|
---|
105 | this.toRemote = toRemote;
|
---|
106 | this.fileCnt = 0;
|
---|
107 | this.doneCopying = false;
|
---|
108 | this.startTime = 0;
|
---|
109 | this.lastTime = 0;
|
---|
110 | this.totTransSize = 0;
|
---|
111 | this.fileTransSize = 0;
|
---|
112 | this.lastSize = 0;
|
---|
113 | this.start();
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void run() {
|
---|
117 | String sourceFile = "localhost:" + localFile;
|
---|
118 | String destFile = remoteHost + ":" + remoteFile;
|
---|
119 |
|
---|
120 | if(!toRemote) {
|
---|
121 | String tmp;
|
---|
122 | tmp = sourceFile;
|
---|
123 | sourceFile = destFile;
|
---|
124 | destFile = tmp;
|
---|
125 | }
|
---|
126 |
|
---|
127 | copyIndicator = new Dialog(parent, "MindTerm - File Transfer", false);
|
---|
128 |
|
---|
129 | GridBagLayout grid = new GridBagLayout();
|
---|
130 | GridBagConstraints gridc = new GridBagConstraints();
|
---|
131 | Label lbl;
|
---|
132 | Button b;
|
---|
133 |
|
---|
134 | copyIndicator.setLayout(grid);
|
---|
135 |
|
---|
136 | gridc.fill = GridBagConstraints.HORIZONTAL;
|
---|
137 | gridc.anchor = GridBagConstraints.WEST;
|
---|
138 | gridc.gridy = 0;
|
---|
139 | gridc.gridwidth = 1;
|
---|
140 | gridc.insets = new Insets(4, 4, 4, 4);
|
---|
141 |
|
---|
142 | lbl = new Label("Source:");
|
---|
143 | grid.setConstraints(lbl, gridc);
|
---|
144 | copyIndicator.add(lbl);
|
---|
145 |
|
---|
146 | gridc.gridwidth = 4;
|
---|
147 | srcLbl = new Label(cutName(sourceFile, 32));
|
---|
148 | grid.setConstraints(srcLbl, gridc);
|
---|
149 | copyIndicator.add(srcLbl);
|
---|
150 |
|
---|
151 | gridc.gridy = 1;
|
---|
152 | gridc.gridwidth = 1;
|
---|
153 |
|
---|
154 | lbl = new Label("Destination:");
|
---|
155 | grid.setConstraints(lbl, gridc);
|
---|
156 | copyIndicator.add(lbl);
|
---|
157 |
|
---|
158 | gridc.gridwidth = 4;
|
---|
159 | dstLbl = new Label(cutName(destFile, 32));
|
---|
160 | grid.setConstraints(dstLbl, gridc);
|
---|
161 | copyIndicator.add(dstLbl);
|
---|
162 |
|
---|
163 | gridc.gridy = 2;
|
---|
164 |
|
---|
165 | gridc.gridwidth = 1;
|
---|
166 | lbl= new Label("Current:");
|
---|
167 | grid.setConstraints(lbl, gridc);
|
---|
168 | copyIndicator.add(lbl);
|
---|
169 |
|
---|
170 | gridc.gridwidth = 3;
|
---|
171 | nameLbl= new Label("connecting...");
|
---|
172 | grid.setConstraints(nameLbl, gridc);
|
---|
173 | copyIndicator.add(nameLbl);
|
---|
174 |
|
---|
175 | gridc.gridwidth = 1;
|
---|
176 | sizeLbl= new Label("");
|
---|
177 | grid.setConstraints(sizeLbl, gridc);
|
---|
178 | copyIndicator.add(sizeLbl);
|
---|
179 |
|
---|
180 | gridc.gridy = 3;
|
---|
181 | gridc.gridwidth = 3;
|
---|
182 | gridc.fill = GridBagConstraints.NONE;
|
---|
183 | gridc.anchor = GridBagConstraints.CENTER;
|
---|
184 | gridc.insets = new Insets(4, 12, 4, 4);
|
---|
185 | progress = new ProgressBar(512, 160, 20);
|
---|
186 | grid.setConstraints(progress, gridc);
|
---|
187 | copyIndicator.add(progress);
|
---|
188 |
|
---|
189 | gridc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
190 | gridc.insets = new Insets(4, 4, 4, 4);
|
---|
191 | gridc.fill = GridBagConstraints.HORIZONTAL;
|
---|
192 | speedLbl = new Label("0.0 kB/sec", Label.CENTER);
|
---|
193 | grid.setConstraints(speedLbl, gridc);
|
---|
194 | copyIndicator.add(speedLbl);
|
---|
195 |
|
---|
196 | gridc.gridy = 4;
|
---|
197 | cancB = new Button("Cancel");
|
---|
198 | cancB.addActionListener(new ActionListener() {
|
---|
199 | public void actionPerformed(ActionEvent e) {
|
---|
200 | if(!doneCopying) {
|
---|
201 | if(copyThread != null)
|
---|
202 | copyThread.stop();
|
---|
203 | if(scp != null)
|
---|
204 | scp.abort();
|
---|
205 | }
|
---|
206 | copyIndicator.setVisible(false);
|
---|
207 | }
|
---|
208 | });
|
---|
209 |
|
---|
210 | gridc.fill = GridBagConstraints.NONE;
|
---|
211 | gridc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
212 | gridc.anchor = GridBagConstraints.CENTER;
|
---|
213 | gridc.ipady = 2;
|
---|
214 | gridc.ipadx = 2;
|
---|
215 | grid.setConstraints(cancB, gridc);
|
---|
216 | copyIndicator.add(cancB);
|
---|
217 |
|
---|
218 | AWTConvenience.setBackgroundOfChildren(copyIndicator);
|
---|
219 |
|
---|
220 | Dimension d = speedLbl.getSize();
|
---|
221 | d.width += d.width * 2;
|
---|
222 | speedLbl.setSize(d);
|
---|
223 | sizeLbl.setSize(d);
|
---|
224 |
|
---|
225 | copyIndicator.setResizable(true);
|
---|
226 | copyIndicator.pack();
|
---|
227 | AWTConvenience.placeDialog(copyIndicator);
|
---|
228 |
|
---|
229 | copyThread = new Thread(new Runnable() {
|
---|
230 | public void run() {
|
---|
231 | if(SSH.NETSCAPE_SECURITY_MODEL) {
|
---|
232 | try {
|
---|
233 | netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");
|
---|
234 | } catch (netscape.security.ForbiddenTargetException e) {
|
---|
235 | // !!!
|
---|
236 | }
|
---|
237 | try {
|
---|
238 | netscape.security.PrivilegeManager.enablePrivilege("TerminalEmulator");
|
---|
239 | } catch (netscape.security.ForbiddenTargetException e) {
|
---|
240 | // !!!
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | try {
|
---|
245 | scp = new SSHSCP(remoteHost, remotePort, authenticator, new File(curDir),
|
---|
246 | recursive, false);
|
---|
247 | scp.setClientUser(mainUser);
|
---|
248 | scp.setInteractor(interactor);
|
---|
249 | scp.setIndicator(SSHSCPGUIThread.this);
|
---|
250 | if(toRemote) {
|
---|
251 | scp.copyToRemote(localFileList, remoteFile);
|
---|
252 | } else {
|
---|
253 | scp.copyToLocal(localFile, remoteFile);
|
---|
254 | }
|
---|
255 | copyThread.setPriority(Thread.NORM_PRIORITY);
|
---|
256 | Toolkit.getDefaultToolkit().beep();
|
---|
257 | } catch (Exception e) {
|
---|
258 | interactor.alert("SCP Error: " + e.getMessage());
|
---|
259 | if(SSH.DEBUGMORE) {
|
---|
260 | System.out.println("SCP Error:");
|
---|
261 | e.printStackTrace();
|
---|
262 | }
|
---|
263 | }
|
---|
264 | nameLbl.setText("Copied " + fileCnt + " file" + (fileCnt != 1 ? "s" : "") + ".");
|
---|
265 | double kSize = (double)totTransSize / 1024;
|
---|
266 | sizeLbl.setText(round(kSize) + " kB");
|
---|
267 | doneCopying = true;
|
---|
268 | cancB.setLabel("Done");
|
---|
269 |
|
---|
270 | AWTConvenience.setKeyListenerOfChildren(copyIndicator,
|
---|
271 | new AWTConvenience.OKCancelAdapter(cancB, cancB),
|
---|
272 | null);
|
---|
273 |
|
---|
274 | }
|
---|
275 | });
|
---|
276 |
|
---|
277 | if(background) {
|
---|
278 | copyThread.setPriority(Thread.MIN_PRIORITY);
|
---|
279 | }
|
---|
280 |
|
---|
281 | copyThread.start();
|
---|
282 |
|
---|
283 | copyIndicator.setVisible(true);
|
---|
284 | }
|
---|
285 |
|
---|
286 | public static String[] spaceSplit(String str) {
|
---|
287 | int l = 0, r, cnt = 0;
|
---|
288 | String[] list = new String[str.length() / 2];
|
---|
289 | boolean lastIsQuoted = false;
|
---|
290 | str = str.trim();
|
---|
291 | while((r = str.indexOf(' ', l)) >= 0) {
|
---|
292 | if(str.charAt(l) == '"') {
|
---|
293 | l += 1;
|
---|
294 | r = str.indexOf('"', l);
|
---|
295 | if(r == -1)
|
---|
296 | return null;
|
---|
297 | }
|
---|
298 | String name = str.substring(l, r);
|
---|
299 | if(name.endsWith(File.separator))
|
---|
300 | name = name.substring(0, name.length() - 1);
|
---|
301 | list[cnt++] = name;
|
---|
302 |
|
---|
303 |
|
---|
304 | l = r;
|
---|
305 | do {
|
---|
306 | l++;
|
---|
307 | if(l == str.length()) {
|
---|
308 | lastIsQuoted = true;
|
---|
309 | break;
|
---|
310 | }
|
---|
311 | } while(str.charAt(l) == ' ');
|
---|
312 | }
|
---|
313 |
|
---|
314 | if(!lastIsQuoted) {
|
---|
315 | if(str.charAt(l) == '"') {
|
---|
316 | l += 1;
|
---|
317 | r = str.indexOf('"', l);
|
---|
318 | if(r == -1)
|
---|
319 | return null;
|
---|
320 | }
|
---|
321 | String name = str.substring(l);
|
---|
322 | if(name.endsWith(File.separator))
|
---|
323 | name = name.substring(0, name.length() - 1);
|
---|
324 | list[cnt++] = name;
|
---|
325 | }
|
---|
326 |
|
---|
327 | String[] tmp = list;
|
---|
328 | list = new String[cnt];
|
---|
329 | System.arraycopy(tmp, 0, list, 0, cnt);
|
---|
330 |
|
---|
331 | return list;
|
---|
332 | }
|
---|
333 |
|
---|
334 | public static String[] starExpand(String[] fileList, String curDir) {
|
---|
335 | int i, j, n, cnt = 0;
|
---|
336 | String[] newList = new String[4096]; // !!! Ouch...
|
---|
337 | String[] curDirList = (new File(curDir)).list();
|
---|
338 | String path, curFile;
|
---|
339 |
|
---|
340 | for(i = 0; i < fileList.length; i++) {
|
---|
341 | curFile = fileList[i];
|
---|
342 | path = "";
|
---|
343 | n = curFile.indexOf('*');
|
---|
344 | if(n == -1) {
|
---|
345 | cnt = addUnique(newList, curFile, cnt);
|
---|
346 | continue;
|
---|
347 | }
|
---|
348 | String[] dirList;
|
---|
349 | File f = new File(curFile);
|
---|
350 | if(!f.isAbsolute()) {
|
---|
351 | dirList = curDirList;
|
---|
352 | } else {
|
---|
353 | String dir = f.getParent();
|
---|
354 | if(dir == null)
|
---|
355 | dir = new String(File.separator); // !!! Ouch...
|
---|
356 | dirList = (new File(dir)).list();
|
---|
357 | curFile = f.getName();
|
---|
358 | path = dir + File.separator;
|
---|
359 | n = curFile.indexOf('*');
|
---|
360 | }
|
---|
361 |
|
---|
362 | String pre = curFile.substring(0, n);
|
---|
363 | String post = curFile.substring(n + 1);
|
---|
364 | for(j = 0; j < dirList.length; j++) {
|
---|
365 | String name = dirList[j];
|
---|
366 | if(name.startsWith(pre) && name.endsWith(post)) {
|
---|
367 | cnt = addUnique(newList, path + name, cnt);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | String[] tmp = newList;
|
---|
372 | newList = new String[cnt];
|
---|
373 | System.arraycopy(tmp, 0, newList, 0, cnt);
|
---|
374 | return newList;
|
---|
375 | }
|
---|
376 |
|
---|
377 | static int addUnique(String[] list, String str, int last) {
|
---|
378 | int i;
|
---|
379 | for(i = 0; i < last; i++)
|
---|
380 | if(list[i].equals(str))
|
---|
381 | break;
|
---|
382 | if(i == last)
|
---|
383 | list[last++] = str;
|
---|
384 | return last;
|
---|
385 | }
|
---|
386 |
|
---|
387 | public void connected(String server) {
|
---|
388 | nameLbl.setText("...connected");
|
---|
389 | }
|
---|
390 | public void startFile(String file, int size) {
|
---|
391 | double kSize = (double)size / 1024;
|
---|
392 | sizeLbl.setText(round(kSize) + " kB");
|
---|
393 | nameLbl.setText(file);
|
---|
394 | progress.setMax(size, true);
|
---|
395 | if(startTime == 0)
|
---|
396 | startTime = System.currentTimeMillis();
|
---|
397 | curFileSize = size;
|
---|
398 | fileTransSize = 0;
|
---|
399 | fileCnt++;
|
---|
400 | }
|
---|
401 | public void startDir(String file) {
|
---|
402 | if(startTime == 0)
|
---|
403 | startTime = System.currentTimeMillis();
|
---|
404 | if(file.length() > curDir.length())
|
---|
405 | file = file.substring(curDir.length());
|
---|
406 | if(toRemote) {
|
---|
407 | srcLbl.setText(cutName("localhost:" + file, 32));
|
---|
408 | } else {
|
---|
409 | dstLbl.setText(cutName("localhost:" + file, 32));
|
---|
410 | }
|
---|
411 | }
|
---|
412 | public void endFile() {
|
---|
413 | progress.setValue(curFileSize, true);
|
---|
414 | }
|
---|
415 | public void endDir() {
|
---|
416 | }
|
---|
417 | public void progress(int size) {
|
---|
418 | totTransSize += size;
|
---|
419 | fileTransSize += size;
|
---|
420 | if((curFileSize > 0) && ((((totTransSize - lastSize) * 100) / curFileSize) >= 1)) {
|
---|
421 | progress.setValue(fileTransSize, !background);
|
---|
422 | long now = System.currentTimeMillis();
|
---|
423 | long totSec = ((now - startTime) / 1000);
|
---|
424 | double rate = (totSec != 0 ? (((double)totTransSize / 1024) / totSec) : 0.0);
|
---|
425 | totSec = (now - lastTime);
|
---|
426 | if(totSec != 0) {
|
---|
427 | double rate2 = ((double)(totTransSize - lastSize) / 1024) / totSec;
|
---|
428 | rate = (rate + rate2) / 2.0;
|
---|
429 | }
|
---|
430 | speedLbl.setText("" + round(rate) + " kB/sec");
|
---|
431 | lastSize = totTransSize;
|
---|
432 | lastTime = now;
|
---|
433 | }
|
---|
434 | }
|
---|
435 | double round(double val) {
|
---|
436 | val = val * 10.0;
|
---|
437 | val = Math.floor(val);
|
---|
438 | val = val / 10.0;
|
---|
439 | return val;
|
---|
440 | }
|
---|
441 |
|
---|
442 | String cutName(String name, int len) {
|
---|
443 | if(name.length() > len) {
|
---|
444 | len -= 3;
|
---|
445 | String pre = name.substring(0, len / 2);
|
---|
446 | String suf = name.substring(name.length() - (len / 2));
|
---|
447 | name = pre + "..." + suf;
|
---|
448 | }
|
---|
449 | return name;
|
---|
450 | }
|
---|
451 |
|
---|
452 | }
|
---|