@Override public SFTPException chain(Throwable t) { if (t instanceof SFTPException) return (SFTPException) t; else return new SFTPException(t); }
private void readIntoBuffer(byte[] buf, int off, int len) throws IOException { int count = 0; int read = 0; while (count < len && ((read = in.read(buf, off + count, len - count)) != -1)) count += read; if (read == -1) throw new SFTPException("EOF while reading packet"); }
protected String error(StatusCode sc) throws SFTPException { try { throw new SFTPException(sc, protocolVersion < 3 ? sc.toString() : readString()); } catch (BufferException be) { throw new SFTPException(be); } }
public synchronized void cd(String dirname) throws IOException { final String targetCwd = cwdify(dirname); if (statExistence(targetCwd) == null) { throw new SFTPException(targetCwd + ": does not exist"); } cwd = targetCwd; log.debug("CWD = {}", cwd); }
public PacketType readType() throws SFTPException { try { return PacketType.fromByte(readByte()); } catch (BufferException be) { throw new SFTPException(be); } }
public Response(Buffer<Response> pk, int protocolVersion) throws SFTPException { super(pk); this.protocolVersion = protocolVersion; this.type = readType(); try { this.reqID = readUInt32(); } catch (BufferException be) { throw new SFTPException(be); } }
public StatusCode readStatusCode() throws SFTPException { try { return StatusCode.fromInt(readUInt32AsInt()); } catch (BufferException be) { throw new SFTPException(be); } }
throw new SFTPException("Unexpected response size (0), bailing out");
public void handle() throws SFTPException { Response resp = new Response(packet, engine.getOperativeProtocolVersion()); Promise<Response, SFTPException> promise = promises.remove(resp.getRequestID()); log.debug("Received {} packet", resp.getType()); if (promise == null) throw new SFTPException("Received [" + resp.readType() + "] response for request-id " + resp.getRequestID() + ", no such request was made"); else promise.deliver(resp); }
private static byte[] readSingleNameAsBytes(Response res) throws IOException { res.ensurePacketTypeIs(PacketType.NAME); if (res.readUInt32AsInt() == 1) return res.readStringAsBytes(); else throw new SFTPException("Unexpected data in " + res.getType() + " packet"); }
public SFTPEngine init() throws IOException { transmit(new SFTPPacket<Request>(PacketType.INIT).putUInt32(MAX_SUPPORTED_VERSION)); final SFTPPacket<Response> response = reader.readPacket(); final PacketType type = response.readType(); if (type != PacketType.VERSION) throw new SFTPException("Expected INIT packet, received: " + type); operativeVersion = response.readUInt32AsInt(); log.debug("Server version {}", operativeVersion); if (MAX_SUPPORTED_VERSION < operativeVersion) throw new SFTPException("Server reported incompatible protocol version: " + operativeVersion); while (response.available() > 0) serverExtensions.put(response.readString(), response.readString()); // Start reader thread reader.start(); return this; }
protected int checkReadResponse(Response res, byte[] to, int offset) throws Buffer.BufferException, SFTPException { switch (res.getType()) { case DATA: int recvLen = res.readUInt32AsInt(); System.arraycopy(res.array(), res.rpos(), to, offset, recvLen); return recvLen; case STATUS: res.ensureStatusIs(StatusCode.EOF); return -1; default: throw new SFTPException("Unexpected packet: " + res.getType()); } }
private boolean retrieveUnconfirmedRead(boolean blocking) throws IOException { if (unconfirmedReads.size() <= 0) { return false; } if (!blocking && !unconfirmedReads.peek().isDelivered()) { return false; } unconfirmedReadOffsets.remove(); final Response res = unconfirmedReads.remove().retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS); switch (res.getType()) { case DATA: int recvLen = res.readUInt32AsInt(); responseOffset += recvLen; pending = new ByteArrayInputStream(res.array(), res.rpos(), recvLen); break; case STATUS: res.ensureStatusIs(Response.StatusCode.EOF); eof = true; break; default: throw new SFTPException("Unexpected packet: " + res.getType()); } return true; }
public Response ensurePacketTypeIs(PacketType pt) throws SFTPException { if (getType() != pt) if (getType() == PacketType.STATUS) error(readStatusCode()); else throw new SFTPException("Unexpected packet " + getType()); return this; }
public void mkdirs(String path) throws IOException { final Deque<String> dirsToMake = new LinkedList<String>(); for (PathComponents current = engine.getPathHelper().getComponents(path); ; current = engine.getPathHelper().getComponents(current.getParent())) { final FileAttributes attrs = statExistence(current.getPath()); if (attrs == null) { dirsToMake.push(current.getPath()); } else if (attrs.getType() != FileMode.Type.DIRECTORY) { throw new SFTPException(current.getPath() + " exists but is not a directory"); } else { break; } } while (!dirsToMake.isEmpty()) { mkdir(dirsToMake.pop()); } }
public String readLink(String path) throws IOException { if (operativeVersion < 3) throw new SFTPException("READLINK is not supported in SFTPv" + operativeVersion); return readSingleName( doRequest( newRequest(PacketType.READLINK).putString(path, sub.getRemoteCharset()) ), sub.getRemoteCharset()); }
public void rename(String oldPath, String newPath) throws IOException { if (operativeVersion < 1) throw new SFTPException("RENAME is not supported in SFTPv" + operativeVersion); doRequest( newRequest(PacketType.RENAME).putString(oldPath, sub.getRemoteCharset()).putString(newPath, sub.getRemoteCharset()) ).ensureStatusPacketIsOK(); }
public void symlink(String linkpath, String targetpath) throws IOException { if (operativeVersion < 3) throw new SFTPException("SYMLINK is not supported in SFTPv" + operativeVersion); doRequest( newRequest(PacketType.SYMLINK).putString(linkpath, sub.getRemoteCharset()).putString(targetpath, sub.getRemoteCharset()) ).ensureStatusPacketIsOK(); }
throw new SFTPException("Unexpected packet: " + res.getType());
public FileAttributes readFileAttributes() throws SFTPException { final FileAttributes.Builder builder = new FileAttributes.Builder(); try { final int mask = readUInt32AsInt(); if (FileAttributes.Flag.SIZE.isSet(mask)) builder.withSize(readUInt64()); if (FileAttributes.Flag.UIDGID.isSet(mask)) builder.withUIDGID(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.MODE.isSet(mask)) builder.withPermissions(readUInt32AsInt()); if (FileAttributes.Flag.ACMODTIME.isSet(mask)) builder.withAtimeMtime(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.EXTENDED.isSet(mask)) { final int extCount = readUInt32AsInt(); for (int i = 0; i < extCount; i++) builder.withExtended(readString(), readString()); } } catch (BufferException be) { throw new SFTPException(be); } return builder.build(); }