annotate app/src/main/java/ch/ethz/ssh2/SCPClient.java @ 461:8ac203bbd3c2

Added tag stable-1.9.3-4 for changeset 4776062555f3
author Carl Byington <carl@five-ten-sg.com>
date Sun, 28 Apr 2019 17:10:22 -0700
parents d29cce60f393
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 package ch.ethz.ssh2;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2
292
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
3 import java.io.BufferedInputStream;
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
4 import java.io.BufferedOutputStream;
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
5 import java.io.File;
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
6 import java.io.FileInputStream;
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
7 import java.io.FileOutputStream;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.io.IOException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 import java.io.InputStream;
292
855cdc3b2ced start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 290
diff changeset
10 import java.io.OutputStream;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 import java.nio.charset.Charset;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 import java.nio.charset.UnsupportedCharsetException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * A very basic <code>SCPClient</code> that can be used to copy files from/to
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 * the SSH-2 server. On the server side, the "scp" program must be in the PATH.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 * <p/>
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 * This scp client is thread safe - you can download (and upload) different sets
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 * of files concurrently without any troubles. The <code>SCPClient</code> is
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 * actually mapping every request to a distinct {@link ch.ethz.ssh2.Session}.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 * @author Christian Plattner, plattner@inf.ethz.ch
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 * @version $Id: SCPClient.java 85 2014-04-07 14:05:09Z dkocher@sudo.ch $
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
26 public class SCPClient {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
27 Connection conn;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
29 String charsetName = null;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
31 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
32 * Set the charset used to convert between Java Unicode Strings and byte encodings
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
33 * used by the server for paths and file names.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
34 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
35 * @param charset the name of the charset to be used or <code>null</code> to use the platform's
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
36 * default encoding.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
37 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
38 * @see #getCharset()
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
39 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
40 public void setCharset(String charset) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
41 if (charset == null) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
42 charsetName = charset;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
43 return;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
44 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
46 try {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
47 Charset.forName(charset);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
48 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
49 catch (UnsupportedCharsetException e) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
50 throw new IOException("This charset is not supported", e);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
51 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
52
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
53 charsetName = charset;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
54 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
56 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
57 * The currently used charset for filename encoding/decoding.
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
58 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
59 * @return The name of the charset (<code>null</code> if the platform's default charset is being used)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
60 * @see #setCharset(String)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
61 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
62 public String getCharset() {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
63 return charsetName;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
64 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
66 public class LenNamePair {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
67 public long length;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
68 String filename;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
69 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
71 public SCPClient(Connection conn) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
72 if (conn == null)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
73 throw new IllegalArgumentException("Cannot accept null argument!");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
75 this.conn = conn;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
76 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
77
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
78 protected void readResponse(InputStream is) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
79 int c = is.read();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
81 if (c == 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
82 return;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
84 if (c == -1)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
85 throw new IOException("Remote scp terminated unexpectedly.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
87 if ((c != 1) && (c != 2))
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
88 throw new IOException("Remote scp sent illegal error code.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
90 if (c == 2)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
91 throw new IOException("Remote scp terminated with error.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
93 String err = receiveLine(is);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
94 throw new IOException("Remote scp terminated with error (" + err + ").");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
95 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
97 protected String receiveLine(InputStream is) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
98 StringBuilder sb = new StringBuilder(30);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
100 while (true) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
101 /* This is a random limit - if your path names are longer, then adjust it */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
102 if (sb.length() > 8192)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
103 throw new IOException("Remote scp sent a too long line");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
105 int c = is.read();
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
107 if (c < 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
108 throw new IOException("Remote scp terminated unexpectedly.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
110 if (c == '\n')
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
111 break;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
113 sb.append((char) c);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
114 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
116 return sb.toString();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
117 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
119 protected LenNamePair parseCLine(String line) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
120 /* Minimum line: "xxxx y z" ---> 8 chars */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
121 if (line.length() < 8)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
122 throw new IOException("Malformed C line sent by remote SCP binary, line too short.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
124 if ((line.charAt(4) != ' ') || (line.charAt(5) == ' '))
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
125 throw new IOException("Malformed C line sent by remote SCP binary.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
127 int length_name_sep = line.indexOf(' ', 5);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
129 if (length_name_sep == -1)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
130 throw new IOException("Malformed C line sent by remote SCP binary.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
132 String length_substring = line.substring(5, length_name_sep);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
133 String name_substring = line.substring(length_name_sep + 1);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
135 if ((length_substring.length() <= 0) || (name_substring.length() <= 0))
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
136 throw new IOException("Malformed C line sent by remote SCP binary.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
138 if ((6 + length_substring.length() + name_substring.length()) != line.length())
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
139 throw new IOException("Malformed C line sent by remote SCP binary.");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
140
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
141 final long len;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
143 try {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
144 len = Long.parseLong(length_substring);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
145 }
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
146 catch (NumberFormatException e) {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
147 throw new IOException("Malformed C line sent by remote SCP binary, cannot parse file length.");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
148 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
150 if (len < 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
151 throw new IOException("Malformed C line sent by remote SCP binary, illegal file length.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
152
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
153 LenNamePair lnp = new LenNamePair();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
154 lnp.length = len;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
155 lnp.filename = name_substring;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
156 return lnp;
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
157 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
158
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
159 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
160 * The session for opened for this SCP transfer must be closed using
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
161 * SCPOutputStream#close
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
162 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
163 * @param remoteFile
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
164 * @param length The size of the file to send
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
165 * @param remoteTargetDirectory
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
166 * @param mode
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
167 * @return
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
168 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
169 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
170 public SCPOutputStream put(final String remoteFile, long length, String remoteTargetDirectory, String mode)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
171 throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
172 Session sess = null;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
173
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
174 if (null == remoteFile)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
175 throw new IllegalArgumentException("Null argument.");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
176
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
177 if (null == remoteTargetDirectory)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
178 remoteTargetDirectory = "";
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
179
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
180 if (null == mode)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
181 mode = "0600";
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
182
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
183 if (mode.length() != 4)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
184 throw new IllegalArgumentException("Invalid mode.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
185
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
186 for (int i = 0; i < mode.length(); i++)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
187 if (Character.isDigit(mode.charAt(i)) == false)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
188 throw new IllegalArgumentException("Invalid mode.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
189
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
190 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : ".";
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
191 String cmd = "scp -t -d \"" + remoteTargetDirectory + "\"";
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
192 sess = conn.openSession();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
193 sess.execCommand(cmd, charsetName);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
194 return new SCPOutputStream(this, sess, remoteFile, length, mode);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
195 }
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
196
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
197 /**
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
198 * The session for opened for this SCP transfer must be closed using
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
199 * SCPInputStream#close
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
200 *
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
201 * @param remoteFile
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
202 * @return
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
203 * @throws IOException
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
204 */
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
205 public SCPInputStream get(final String remoteFile) throws IOException {
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
206 Session sess = null;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
207
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
208 if (null == remoteFile)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
209 throw new IllegalArgumentException("Null argument.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
210
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
211 if (remoteFile.length() == 0)
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
212 throw new IllegalArgumentException("Cannot accept empty filename.");
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
213
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
214 String cmd = "scp -f";
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
215 cmd += (" \"" + remoteFile + "\"");
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
216 sess = conn.openSession();
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
217 sess.execCommand(cmd, charsetName);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
218 return new SCPInputStream(this, sess);
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 292
diff changeset
219 }
290
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
220 private void sendBytes(Session sess, byte[] data, String fileName, String mode) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
221 OutputStream os = sess.getStdin();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
222 InputStream is = new BufferedInputStream(sess.getStdout(), 512);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
223 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
224 String cline = "C" + mode + " " + data.length + " " + fileName + "\n";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
225 os.write(cline.getBytes("ISO-8859-1"));
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
226 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
227 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
228 os.write(data, 0, data.length);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
229 os.write(0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
230 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
231 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
232 os.write("E\n".getBytes("ISO-8859-1"));
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
233 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
234 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
235
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
236 private void sendFiles(Session sess, String[] files, String[] remoteFiles, String mode) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
237 byte[] buffer = new byte[8192];
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
238 OutputStream os = new BufferedOutputStream(sess.getStdin(), 40000);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
239 InputStream is = new BufferedInputStream(sess.getStdout(), 512);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
240 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
241
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
242 for (int i = 0; i < files.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
243 File f = new File(files[i]);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
244 long remain = f.length();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
245 String remoteName;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
246
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
247 if ((remoteFiles != null) && (remoteFiles.length > i) && (remoteFiles[i] != null))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
248 remoteName = remoteFiles[i];
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
249 else
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
250 remoteName = f.getName();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
251
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
252 String cline = "C" + mode + " " + remain + " " + remoteName + "\n";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
253 os.write(cline.getBytes("ISO-8859-1"));
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
254 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
255 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
256 FileInputStream fis = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
257
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
258 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
259 fis = new FileInputStream(f);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
260
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
261 while (remain > 0) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
262 int trans;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
263
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
264 if (remain > buffer.length)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
265 trans = buffer.length;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
266 else
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
267 trans = (int) remain;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
268
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
269 if (fis.read(buffer, 0, trans) != trans)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
270 throw new IOException("Cannot read enough from local file " + files[i]);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
271
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
272 os.write(buffer, 0, trans);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
273 remain -= trans;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
274 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
275 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
276 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
277 if (fis != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
278 fis.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
279 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
280
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
281 os.write(0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
282 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
283 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
284 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
285
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
286 os.write("E\n".getBytes("ISO-8859-1"));
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
287 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
288 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
289
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
290 private void receiveFiles(Session sess, OutputStream[] targets) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
291 byte[] buffer = new byte[8192];
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
292 OutputStream os = new BufferedOutputStream(sess.getStdin(), 512);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
293 InputStream is = new BufferedInputStream(sess.getStdout(), 40000);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
294 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
295 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
296
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
297 for (int i = 0; i < targets.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
298 LenNamePair lnp = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
299
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
300 while (true) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
301 int c = is.read();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
302
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
303 if (c < 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
304 throw new IOException("Remote scp terminated unexpectedly.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
305
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
306 String line = receiveLine(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
307
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
308 if (c == 'T') {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
309 /* Ignore modification times */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
310 continue;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
311 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
312
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
313 if ((c == 1) || (c == 2))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
314 throw new IOException("Remote SCP error: " + line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
315
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
316 if (c == 'C') {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
317 lnp = parseCLine(line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
318 break;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
319 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
320
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
321 throw new IOException("Remote SCP error: " + ((char) c) + line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
322 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
323
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
324 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
325 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
326 long remain = lnp.length;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
327
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
328 while (remain > 0) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
329 int trans;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
330
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
331 if (remain > buffer.length)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
332 trans = buffer.length;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
333 else
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
334 trans = (int) remain;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
335
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
336 int this_time_received = is.read(buffer, 0, trans);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
337
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
338 if (this_time_received < 0) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
339 throw new IOException("Remote scp terminated connection unexpectedly");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
340 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
341
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
342 targets[i].write(buffer, 0, this_time_received);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
343 remain -= this_time_received;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
344 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
345
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
346 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
347 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
348 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
349 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
350 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
351
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
352 private void receiveFiles(Session sess, String[] files, String target) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
353 byte[] buffer = new byte[8192];
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
354 OutputStream os = new BufferedOutputStream(sess.getStdin(), 512);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
355 InputStream is = new BufferedInputStream(sess.getStdout(), 40000);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
356 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
357 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
358
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
359 for (int i = 0; i < files.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
360 LenNamePair lnp = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
361
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
362 while (true) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
363 int c = is.read();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
364
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
365 if (c < 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
366 throw new IOException("Remote scp terminated unexpectedly.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
367
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
368 String line = receiveLine(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
369
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
370 if (c == 'T') {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
371 /* Ignore modification times */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
372 continue;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
373 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
374
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
375 if ((c == 1) || (c == 2))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
376 throw new IOException("Remote SCP error: " + line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
377
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
378 if (c == 'C') {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
379 lnp = parseCLine(line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
380 break;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
381 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
382
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
383 throw new IOException("Remote SCP error: " + ((char) c) + line);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
384 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
385
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
386 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
387 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
388 File f = new File(target + File.separatorChar + lnp.filename);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
389 FileOutputStream fop = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
390
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
391 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
392 fop = new FileOutputStream(f);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
393 long remain = lnp.length;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
394
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
395 while (remain > 0) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
396 int trans;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
397
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
398 if (remain > buffer.length)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
399 trans = buffer.length;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
400 else
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
401 trans = (int) remain;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
402
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
403 int this_time_received = is.read(buffer, 0, trans);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
404
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
405 if (this_time_received < 0) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
406 throw new IOException("Remote scp terminated connection unexpectedly");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
407 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
408
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
409 fop.write(buffer, 0, this_time_received);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
410 remain -= this_time_received;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
411 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
412 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
413 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
414 if (fop != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
415 fop.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
416 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
417
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
418 readResponse(is);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
419 os.write(0x0);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
420 os.flush();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
421 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
422 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
423
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
424 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
425 * Copy a local file to a remote directory, uses mode 0600 when creating the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
426 * file on the remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
427 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
428 * @param localFile
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
429 * Path and name of local file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
430 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
431 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
432 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
433 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
434 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
435 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
436 public void put(String localFile, String remoteTargetDirectory) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
437 put(new String[] { localFile }, remoteTargetDirectory, "0600");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
438 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
439
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
440 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
441 * Copy a set of local files to a remote directory, uses mode 0600 when
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
442 * creating files on the remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
443 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
444 * @param localFiles
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
445 * Paths and names of local file names.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
446 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
447 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
448 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
449 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
450 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
451 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
452
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
453 public void put(String[] localFiles, String remoteTargetDirectory) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
454 put(localFiles, remoteTargetDirectory, "0600");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
455 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
456
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
457 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
458 * Copy a local file to a remote directory, uses the specified mode when
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
459 * creating the file on the remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
460 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
461 * @param localFile
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
462 * Path and name of local file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
463 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
464 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
465 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
466 * @param mode
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
467 * a four digit string (e.g., 0644, see "man chmod", "man open")
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
468 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
469 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
470 public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
471 put(new String[] { localFile }, remoteTargetDirectory, mode);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
472 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
473
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
474 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
475 * Copy a local file to a remote directory, uses the specified mode and
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
476 * remote filename when creating the file on the remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
477 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
478 * @param localFile
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
479 * Path and name of local file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
480 * @param remoteFileName
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
481 * The name of the file which will be created in the remote
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
482 * target directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
483 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
484 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
485 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
486 * @param mode
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
487 * a four digit string (e.g., 0644, see "man chmod", "man open")
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
488 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
489 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
490 public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
491 throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
492 put(new String[] { localFile }, new String[] { remoteFileName }, remoteTargetDirectory, mode);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
493 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
494
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
495 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
496 * Create a remote file and copy the contents of the passed byte array into
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
497 * it. Uses mode 0600 for creating the remote file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
498 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
499 * @param data
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
500 * the data to be copied into the remote file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
501 * @param remoteFileName
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
502 * The name of the file which will be created in the remote
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
503 * target directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
504 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
505 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
506 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
507 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
508 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
509
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
510 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
511 put(data, remoteFileName, remoteTargetDirectory, "0600");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
512 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
513
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
514 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
515 * Create a remote file and copy the contents of the passed byte array into
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
516 * it. The method use the specified mode when creating the file on the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
517 * remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
518 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
519 * @param data
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
520 * the data to be copied into the remote file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
521 * @param remoteFileName
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
522 * The name of the file which will be created in the remote
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
523 * target directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
524 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
525 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
526 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
527 * @param mode
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
528 * a four digit string (e.g., 0644, see "man chmod", "man open")
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
529 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
530 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
531 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
532 Session sess = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
533
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
534 if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
535 throw new IllegalArgumentException("Null argument.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
536
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
537 if (mode.length() != 4)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
538 throw new IllegalArgumentException("Invalid mode.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
539
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
540 for (int i = 0; i < mode.length(); i++)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
541 if (Character.isDigit(mode.charAt(i)) == false)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
542 throw new IllegalArgumentException("Invalid mode.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
543
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
544 remoteTargetDirectory = remoteTargetDirectory.trim();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
545 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : ".";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
546 String cmd = "scp -t -d " + remoteTargetDirectory;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
547
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
548 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
549 sess = conn.openSession();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
550 sess.execCommand(cmd);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
551 sendBytes(sess, data, remoteFileName, mode);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
552 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
553 catch (IOException e) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
554 throw(IOException) new IOException("Error during SCP transfer.").initCause(e);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
555 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
556 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
557 if (sess != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
558 sess.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
559 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
560 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
561
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
562 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
563 * Copy a set of local files to a remote directory, uses the specified mode
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
564 * when creating the files on the remote side.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
565 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
566 * @param localFiles
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
567 * Paths and names of the local files.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
568 * @param remoteTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
569 * Remote target directory. Use an empty string to specify the
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
570 * default directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
571 * @param mode
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
572 * a four digit string (e.g., 0644, see "man chmod", "man open")
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
573 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
574 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
575 public void put(String[] localFiles, String remoteTargetDirectory, String mode) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
576 put(localFiles, null, remoteTargetDirectory, mode);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
577 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
578
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
579 public void put(String[] localFiles, String[] remoteFiles, String remoteTargetDirectory, String mode)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
580 throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
581 Session sess = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
582
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
583 /*
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
584 * remoteFiles may be null, indicating that the local filenames shall be
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
585 * used
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
586 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
587
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
588 if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
589 throw new IllegalArgumentException("Null argument.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
590
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
591 if (mode.length() != 4)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
592 throw new IllegalArgumentException("Invalid mode.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
593
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
594 for (int i = 0; i < mode.length(); i++)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
595 if (Character.isDigit(mode.charAt(i)) == false)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
596 throw new IllegalArgumentException("Invalid mode.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
597
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
598 if (localFiles.length == 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
599 return;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
600
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
601 remoteTargetDirectory = remoteTargetDirectory.trim();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
602 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : ".";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
603 String cmd = "scp -t -d " + remoteTargetDirectory;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
604
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
605 for (int i = 0; i < localFiles.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
606 if (localFiles[i] == null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
607 throw new IllegalArgumentException("Cannot accept null filename.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
608 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
609
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
610 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
611 sess = conn.openSession();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
612 sess.execCommand(cmd);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
613 sendFiles(sess, localFiles, remoteFiles, mode);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
614 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
615 catch (IOException e) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
616 throw(IOException) new IOException("Error during SCP transfer.").initCause(e);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
617 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
618 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
619 if (sess != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
620 sess.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
621 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
622 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
623
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
624 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
625 * Download a file from the remote server to a local directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
626 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
627 * @param remoteFile
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
628 * Path and name of the remote file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
629 * @param localTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
630 * Local directory to put the downloaded file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
631 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
632 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
633 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
634 public void get(String remoteFile, String localTargetDirectory) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
635 get(new String[] { remoteFile }, localTargetDirectory);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
636 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
637
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
638 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
639 * Download a file from the remote server and pipe its contents into an
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
640 * <code>OutputStream</code>. Please note that, to enable flexible usage
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
641 * of this method, the <code>OutputStream</code> will not be closed nor
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
642 * flushed.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
643 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
644 * @param remoteFile
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
645 * Path and name of the remote file.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
646 * @param target
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
647 * OutputStream where the contents of the file will be sent to.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
648 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
649 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
650 public void get(String remoteFile, OutputStream target) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
651 get(new String[] { remoteFile }, new OutputStream[] { target });
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
652 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
653
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
654 private void get(String remoteFiles[], OutputStream[] targets) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
655 Session sess = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
656
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
657 if ((remoteFiles == null) || (targets == null))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
658 throw new IllegalArgumentException("Null argument.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
659
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
660 if (remoteFiles.length != targets.length)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
661 throw new IllegalArgumentException("Length of arguments does not match.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
662
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
663 if (remoteFiles.length == 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
664 return;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
665
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
666 String cmd = "scp -f";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
667
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
668 for (int i = 0; i < remoteFiles.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
669 if (remoteFiles[i] == null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
670 throw new IllegalArgumentException("Cannot accept null filename.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
671
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
672 String tmp = remoteFiles[i].trim();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
673
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
674 if (tmp.length() == 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
675 throw new IllegalArgumentException("Cannot accept empty filename.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
676
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
677 cmd += (" " + tmp);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
678 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
679
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
680 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
681 sess = conn.openSession();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
682 sess.execCommand(cmd);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
683 receiveFiles(sess, targets);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
684 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
685 catch (IOException e) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
686 throw(IOException) new IOException("Error during SCP transfer.").initCause(e);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
687 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
688 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
689 if (sess != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
690 sess.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
691 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
692 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
693
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
694 /**
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
695 * Download a set of files from the remote server to a local directory.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
696 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
697 * @param remoteFiles
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
698 * Paths and names of the remote files.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
699 * @param localTargetDirectory
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
700 * Local directory to put the downloaded files.
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
701 *
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
702 * @throws IOException
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
703 */
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
704 public void get(String remoteFiles[], String localTargetDirectory) throws IOException {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
705 Session sess = null;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
706
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
707 if ((remoteFiles == null) || (localTargetDirectory == null))
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
708 throw new IllegalArgumentException("Null argument.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
709
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
710 if (remoteFiles.length == 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
711 return;
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
712
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
713 String cmd = "scp -f";
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
714
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
715 for (int i = 0; i < remoteFiles.length; i++) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
716 if (remoteFiles[i] == null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
717 throw new IllegalArgumentException("Cannot accept null filename.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
718
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
719 String tmp = remoteFiles[i].trim();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
720
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
721 if (tmp.length() == 0)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
722 throw new IllegalArgumentException("Cannot accept empty filename.");
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
723
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
724 cmd += (" " + tmp);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
725 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
726
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
727 try {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
728 sess = conn.openSession();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
729 sess.execCommand(cmd);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
730 receiveFiles(sess, remoteFiles, localTargetDirectory);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
731 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
732 catch (IOException e) {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
733 throw(IOException) new IOException("Error during SCP transfer.").initCause(e);
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
734 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
735 finally {
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
736 if (sess != null)
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
737 sess.close();
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
738 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
739 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
740 }
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
741
9ae4ed7bb523 start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
742