Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/SFTPv3Client.java @ 308:42b15aaa7ac7 ganymed
merge
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 30 Jul 2014 14:21:50 -0700 |
parents | 071eccdff8ea |
children |
comparison
equal
deleted
inserted
replaced
306:90e47d99ea54 | 308:42b15aaa7ac7 |
---|---|
99 public SFTPv3Client(Connection conn) throws IOException { | 99 public SFTPv3Client(Connection conn) throws IOException { |
100 this(conn, new PacketListener() { | 100 this(conn, new PacketListener() { |
101 public void read(String packet) { | 101 public void read(String packet) { |
102 log.debug("Read packet " + packet); | 102 log.debug("Read packet " + packet); |
103 } | 103 } |
104 | |
105 public void write(String packet) { | 104 public void write(String packet) { |
106 log.debug("Write packet " + packet); | 105 log.debug("Write packet " + packet); |
107 } | 106 } |
108 }); | 107 }); |
109 } | 108 } |
119 this.listener = listener; | 118 this.listener = listener; |
120 } | 119 } |
121 | 120 |
122 public SFTPv3FileAttributes fstat(SFTPFileHandle handle) throws IOException { | 121 public SFTPv3FileAttributes fstat(SFTPFileHandle handle) throws IOException { |
123 int req_id = generateNextRequestID(); | 122 int req_id = generateNextRequestID(); |
124 | |
125 TypesWriter tw = new TypesWriter(); | 123 TypesWriter tw = new TypesWriter(); |
126 tw.writeString(handle.getHandle(), 0, handle.getHandle().length); | 124 tw.writeString(handle.getHandle(), 0, handle.getHandle().length); |
127 | |
128 sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes()); | 125 sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes()); |
129 | |
130 byte[] resp = receiveMessage(34000); | 126 byte[] resp = receiveMessage(34000); |
131 | |
132 TypesReader tr = new TypesReader(resp); | 127 TypesReader tr = new TypesReader(resp); |
133 | |
134 int t = tr.readByte(); | 128 int t = tr.readByte(); |
135 listener.read(Packet.forName(t)); | 129 listener.read(Packet.forName(t)); |
136 | |
137 int rep_id = tr.readUINT32(); | 130 int rep_id = tr.readUINT32(); |
138 if(rep_id != req_id) { | 131 |
132 if (rep_id != req_id) { | |
139 throw new RequestMismatchException(); | 133 throw new RequestMismatchException(); |
140 } | 134 } |
141 | 135 |
142 if(t == Packet.SSH_FXP_ATTRS) { | 136 if (t == Packet.SSH_FXP_ATTRS) { |
143 return new SFTPv3FileAttributes(tr); | 137 return new SFTPv3FileAttributes(tr); |
144 } | 138 } |
145 | 139 |
146 if(t != Packet.SSH_FXP_STATUS) { | 140 if (t != Packet.SSH_FXP_STATUS) { |
147 throw new PacketTypeException(t); | 141 throw new PacketTypeException(t); |
148 } | 142 } |
149 | 143 |
150 int errorCode = tr.readUINT32(); | 144 int errorCode = tr.readUINT32(); |
151 String errorMessage = tr.readString(); | 145 String errorMessage = tr.readString(); |
153 throw new SFTPException(errorMessage, errorCode); | 147 throw new SFTPException(errorMessage, errorCode); |
154 } | 148 } |
155 | 149 |
156 private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException { | 150 private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException { |
157 int req_id = generateNextRequestID(); | 151 int req_id = generateNextRequestID(); |
158 | |
159 TypesWriter tw = new TypesWriter(); | 152 TypesWriter tw = new TypesWriter(); |
160 tw.writeString(path, this.getCharset()); | 153 tw.writeString(path, this.getCharset()); |
161 | |
162 sendMessage(statMethod, req_id, tw.getBytes()); | 154 sendMessage(statMethod, req_id, tw.getBytes()); |
163 | |
164 byte[] resp = receiveMessage(34000); | 155 byte[] resp = receiveMessage(34000); |
165 | |
166 TypesReader tr = new TypesReader(resp); | 156 TypesReader tr = new TypesReader(resp); |
167 | |
168 int t = tr.readByte(); | 157 int t = tr.readByte(); |
169 listener.read(Packet.forName(t)); | 158 listener.read(Packet.forName(t)); |
170 | |
171 int rep_id = tr.readUINT32(); | 159 int rep_id = tr.readUINT32(); |
172 if(rep_id != req_id) { | 160 |
161 if (rep_id != req_id) { | |
173 throw new RequestMismatchException(); | 162 throw new RequestMismatchException(); |
174 } | 163 } |
175 | 164 |
176 if(t == Packet.SSH_FXP_ATTRS) { | 165 if (t == Packet.SSH_FXP_ATTRS) { |
177 return new SFTPv3FileAttributes(tr); | 166 return new SFTPv3FileAttributes(tr); |
178 } | 167 } |
179 | 168 |
180 if(t != Packet.SSH_FXP_STATUS) { | 169 if (t != Packet.SSH_FXP_STATUS) { |
181 throw new PacketTypeException(t); | 170 throw new PacketTypeException(t); |
182 } | 171 } |
183 | 172 |
184 int errorCode = tr.readUINT32(); | 173 int errorCode = tr.readUINT32(); |
185 String errorMessage = tr.readString(); | 174 String errorMessage = tr.readString(); |
197 | 186 |
198 | 187 |
199 private List<SFTPv3DirectoryEntry> scanDirectory(byte[] handle) throws IOException { | 188 private List<SFTPv3DirectoryEntry> scanDirectory(byte[] handle) throws IOException { |
200 List<SFTPv3DirectoryEntry> files = new ArrayList<SFTPv3DirectoryEntry>(); | 189 List<SFTPv3DirectoryEntry> files = new ArrayList<SFTPv3DirectoryEntry>(); |
201 | 190 |
202 while(true) { | 191 while (true) { |
203 int req_id = generateNextRequestID(); | 192 int req_id = generateNextRequestID(); |
204 | |
205 TypesWriter tw = new TypesWriter(); | 193 TypesWriter tw = new TypesWriter(); |
206 tw.writeString(handle, 0, handle.length); | 194 tw.writeString(handle, 0, handle.length); |
207 | |
208 sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes()); | 195 sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes()); |
209 | |
210 byte[] resp = receiveMessage(34000); | 196 byte[] resp = receiveMessage(34000); |
211 | |
212 TypesReader tr = new TypesReader(resp); | 197 TypesReader tr = new TypesReader(resp); |
213 | |
214 int t = tr.readByte(); | 198 int t = tr.readByte(); |
215 listener.read(Packet.forName(t)); | 199 listener.read(Packet.forName(t)); |
216 | |
217 int rep_id = tr.readUINT32(); | 200 int rep_id = tr.readUINT32(); |
218 if(rep_id != req_id) { | 201 |
202 if (rep_id != req_id) { | |
219 throw new RequestMismatchException(); | 203 throw new RequestMismatchException(); |
220 } | 204 } |
221 | 205 |
222 if(t == Packet.SSH_FXP_NAME) { | 206 if (t == Packet.SSH_FXP_NAME) { |
223 int count = tr.readUINT32(); | 207 int count = tr.readUINT32(); |
224 if(log.isDebugEnabled()) { | 208 |
209 if (log.isDebugEnabled()) { | |
225 log.debug(String.format("Parsing %d name entries", count)); | 210 log.debug(String.format("Parsing %d name entries", count)); |
226 } | 211 } |
227 while(count > 0) { | 212 |
213 while (count > 0) { | |
228 SFTPv3DirectoryEntry file = new SFTPv3DirectoryEntry(); | 214 SFTPv3DirectoryEntry file = new SFTPv3DirectoryEntry(); |
229 file.filename = tr.readString(this.getCharset()); | 215 file.filename = tr.readString(this.getCharset()); |
230 file.longEntry = tr.readString(this.getCharset()); | 216 file.longEntry = tr.readString(this.getCharset()); |
231 listener.read(file.longEntry); | 217 listener.read(file.longEntry); |
232 file.attributes = new SFTPv3FileAttributes(tr); | 218 file.attributes = new SFTPv3FileAttributes(tr); |
233 if(log.isDebugEnabled()) { | 219 |
220 if (log.isDebugEnabled()) { | |
234 log.debug(String.format("Adding file %s", file)); | 221 log.debug(String.format("Adding file %s", file)); |
235 } | 222 } |
223 | |
236 files.add(file); | 224 files.add(file); |
237 count--; | 225 count--; |
238 } | 226 } |
227 | |
239 continue; | 228 continue; |
240 } | 229 } |
241 | 230 |
242 if(t != Packet.SSH_FXP_STATUS) { | 231 if (t != Packet.SSH_FXP_STATUS) { |
243 throw new PacketTypeException(t); | 232 throw new PacketTypeException(t); |
244 } | 233 } |
245 | 234 |
246 int errorCode = tr.readUINT32(); | 235 int errorCode = tr.readUINT32(); |
247 | 236 |
248 if(errorCode == ErrorCodes.SSH_FX_EOF) { | 237 if (errorCode == ErrorCodes.SSH_FX_EOF) { |
249 return files; | 238 return files; |
250 } | 239 } |
240 | |
251 String errorMessage = tr.readString(); | 241 String errorMessage = tr.readString(); |
252 listener.read(errorMessage); | 242 listener.read(errorMessage); |
253 throw new SFTPException(errorMessage, errorCode); | 243 throw new SFTPException(errorMessage, errorCode); |
254 } | 244 } |
255 } | 245 } |
256 | 246 |
257 public final SFTPv3FileHandle openDirectory(String path) throws IOException { | 247 public final SFTPv3FileHandle openDirectory(String path) throws IOException { |
258 int req_id = generateNextRequestID(); | 248 int req_id = generateNextRequestID(); |
259 | |
260 TypesWriter tw = new TypesWriter(); | 249 TypesWriter tw = new TypesWriter(); |
261 tw.writeString(path, this.getCharset()); | 250 tw.writeString(path, this.getCharset()); |
262 | |
263 sendMessage(Packet.SSH_FXP_OPENDIR, req_id, tw.getBytes()); | 251 sendMessage(Packet.SSH_FXP_OPENDIR, req_id, tw.getBytes()); |
264 | |
265 byte[] resp = receiveMessage(34000); | 252 byte[] resp = receiveMessage(34000); |
266 | |
267 TypesReader tr = new TypesReader(resp); | 253 TypesReader tr = new TypesReader(resp); |
268 | |
269 int t = tr.readByte(); | 254 int t = tr.readByte(); |
270 listener.read(Packet.forName(t)); | 255 listener.read(Packet.forName(t)); |
271 | |
272 int rep_id = tr.readUINT32(); | 256 int rep_id = tr.readUINT32(); |
273 if(rep_id != req_id) { | 257 |
258 if (rep_id != req_id) { | |
274 throw new RequestMismatchException(); | 259 throw new RequestMismatchException(); |
275 } | 260 } |
276 | 261 |
277 if(t == Packet.SSH_FXP_HANDLE) { | 262 if (t == Packet.SSH_FXP_HANDLE) { |
278 return new SFTPv3FileHandle(this, tr.readByteString()); | 263 return new SFTPv3FileHandle(this, tr.readByteString()); |
279 } | 264 } |
280 | 265 |
281 if(t != Packet.SSH_FXP_STATUS) { | 266 if (t != Packet.SSH_FXP_STATUS) { |
282 throw new PacketTypeException(t); | 267 throw new PacketTypeException(t); |
283 } | 268 } |
284 | 269 |
285 int errorCode = tr.readUINT32(); | 270 int errorCode = tr.readUINT32(); |
286 String errorMessage = tr.readString(); | 271 String errorMessage = tr.readString(); |
403 } | 388 } |
404 | 389 |
405 @Override | 390 @Override |
406 public SFTPv3FileHandle openFile(String filename, int flags, SFTPFileAttributes attr) throws IOException { | 391 public SFTPv3FileHandle openFile(String filename, int flags, SFTPFileAttributes attr) throws IOException { |
407 int req_id = generateNextRequestID(); | 392 int req_id = generateNextRequestID(); |
408 | |
409 TypesWriter tw = new TypesWriter(); | 393 TypesWriter tw = new TypesWriter(); |
410 tw.writeString(filename, this.getCharset()); | 394 tw.writeString(filename, this.getCharset()); |
411 tw.writeUINT32(flags); | 395 tw.writeUINT32(flags); |
412 | |
413 tw.writeBytes(attr.toBytes()); | 396 tw.writeBytes(attr.toBytes()); |
414 | |
415 sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes()); | 397 sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes()); |
416 | |
417 byte[] resp = receiveMessage(34000); | 398 byte[] resp = receiveMessage(34000); |
418 | |
419 TypesReader tr = new TypesReader(resp); | 399 TypesReader tr = new TypesReader(resp); |
420 | |
421 int t = tr.readByte(); | 400 int t = tr.readByte(); |
422 listener.read(Packet.forName(t)); | 401 listener.read(Packet.forName(t)); |
423 | |
424 int rep_id = tr.readUINT32(); | 402 int rep_id = tr.readUINT32(); |
425 if(rep_id != req_id) { | 403 |
404 if (rep_id != req_id) { | |
426 throw new RequestMismatchException(); | 405 throw new RequestMismatchException(); |
427 } | 406 } |
428 | 407 |
429 if(t == Packet.SSH_FXP_HANDLE) { | 408 if (t == Packet.SSH_FXP_HANDLE) { |
430 return new SFTPv3FileHandle(this, tr.readByteString()); | 409 return new SFTPv3FileHandle(this, tr.readByteString()); |
431 } | 410 } |
432 | 411 |
433 if(t != Packet.SSH_FXP_STATUS) { | 412 if (t != Packet.SSH_FXP_STATUS) { |
434 throw new PacketTypeException(t); | 413 throw new PacketTypeException(t); |
435 } | 414 } |
436 | 415 |
437 int errorCode = tr.readUINT32(); | 416 int errorCode = tr.readUINT32(); |
438 String errorMessage = tr.readString(); | 417 String errorMessage = tr.readString(); |
441 } | 420 } |
442 | 421 |
443 @Override | 422 @Override |
444 public void createSymlink(String src, String target) throws IOException { | 423 public void createSymlink(String src, String target) throws IOException { |
445 int req_id = generateNextRequestID(); | 424 int req_id = generateNextRequestID(); |
446 | |
447 // Changed semantics of src and target. The bug is known on SFTP servers shipped with all | 425 // Changed semantics of src and target. The bug is known on SFTP servers shipped with all |
448 // versions of OpenSSH (Bug #861). | 426 // versions of OpenSSH (Bug #861). |
449 TypesWriter tw = new TypesWriter(); | 427 TypesWriter tw = new TypesWriter(); |
450 tw.writeString(target, this.getCharset()); | 428 tw.writeString(target, this.getCharset()); |
451 tw.writeString(src, this.getCharset()); | 429 tw.writeString(src, this.getCharset()); |
452 | |
453 sendMessage(Packet.SSH_FXP_SYMLINK, req_id, tw.getBytes()); | 430 sendMessage(Packet.SSH_FXP_SYMLINK, req_id, tw.getBytes()); |
454 | |
455 expectStatusOKMessage(req_id); | 431 expectStatusOKMessage(req_id); |
456 } | 432 } |
457 } | 433 } |