Search in sources :

Example 16 with TypesWriter

use of com.trilead.ssh2.packets.TypesWriter in project intellij-community by JetBrains.

the class SFTPv3Client method init.

private void init() throws IOException {
    /* Send SSH_FXP_INIT (version 3) */
    final int client_version = 3;
    if (debug != null)
        debug.println("Sending SSH_FXP_INIT (" + client_version + ")...");
    TypesWriter tw = new TypesWriter();
    tw.writeUINT32(client_version);
    sendMessage(Packet.SSH_FXP_INIT, 0, tw.getBytes());
    if (debug != null)
        debug.println("Waiting for SSH_FXP_VERSION...");
    TypesReader tr = new TypesReader(receiveMessage(34000));
    /* Should be enough for any reasonable server */
    int type = tr.readByte();
    if (type != Packet.SSH_FXP_VERSION) {
        throw new IOException("The server did not send a SSH_FXP_VERSION packet (got " + type + ")");
    }
    protocol_version = tr.readUINT32();
    if (debug != null)
        debug.println("SSH_FXP_VERSION: protocol_version = " + protocol_version);
    if (protocol_version != 3)
        throw new IOException("Server version " + protocol_version + " is currently not supported");
    while (tr.remain() != 0) {
        String name = tr.readString();
        byte[] value = tr.readByteString();
        server_extensions.put(name, value);
        if (debug != null)
            debug.println("SSH_FXP_VERSION: extension: " + name + " = '" + expandString(value, 0, value.length) + "'");
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 17 with TypesWriter

use of com.trilead.ssh2.packets.TypesWriter in project intellij-community by JetBrains.

the class SFTPv3Client method fstat.

/**
	 * Retrieve the file attributes of an open file.
	 * 
	 * @param handle a SFTPv3FileHandle handle.
	 * @return a SFTPv3FileAttributes object.
	 * @throws IOException
	 */
public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException {
    checkHandleValidAndOpen(handle);
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
    if (debug != null) {
        debug.println("Sending SSH_FXP_FSTAT...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_ATTRS) {
        return readAttrs(tr);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 18 with TypesWriter

use of com.trilead.ssh2.packets.TypesWriter in project intellij-community by JetBrains.

the class SFTPv3Client method openFile.

private SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttributes attr) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(fileName, charsetName);
    tw.writeUINT32(flags);
    tw.writeBytes(createAttrs(attr));
    if (debug != null) {
        debug.println("Sending SSH_FXP_OPEN...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_HANDLE) {
        if (debug != null) {
            debug.println("Got SSH_FXP_HANDLE.");
            debug.flush();
        }
        return new SFTPv3FileHandle(this, tr.readByteString());
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    String errorMessage = tr.readString();
    throw new SFTPException(errorMessage, errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 19 with TypesWriter

use of com.trilead.ssh2.packets.TypesWriter in project intellij-community by JetBrains.

the class SFTPv3Client method closeHandle.

private final void closeHandle(byte[] handle) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(handle, 0, handle.length);
    sendMessage(Packet.SSH_FXP_CLOSE, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 20 with TypesWriter

use of com.trilead.ssh2.packets.TypesWriter in project intellij-community by JetBrains.

the class SFTPv3Client method createSymlink.

/**
	 * Create a symbolic link on the server. Creates a link "src" that points
	 * to "target".
	 * 
	 * @param src See the {@link SFTPv3Client comment} for the class for more details.
	 * @param target See the {@link SFTPv3Client comment} for the class for more details.
	 * @throws IOException
	 */
public void createSymlink(String src, String target) throws IOException {
    int req_id = generateNextRequestID();
    /* Either I am too stupid to understand the SFTP draft
		 * or the OpenSSH guys changed the semantics of src and target.
		 */
    TypesWriter tw = new TypesWriter();
    tw.writeString(target, charsetName);
    tw.writeString(src, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_SYMLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_SYMLINK, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Aggregations

TypesWriter (com.trilead.ssh2.packets.TypesWriter)23 TypesReader (com.trilead.ssh2.packets.TypesReader)10 Vector (java.util.Vector)1