Search in sources :

Example 21 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method expectStatusOKMessage.

private void expectStatusOKMessage(int id) throws IOException {
    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 != id)
        throw new IOException("The server sent an invalid id field.");
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    if (errorCode == ErrorCodes.SSH_FX_OK)
        return;
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException)

Example 22 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

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) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 23 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method canonicalPath.

/**
 * Have the server canonicalize any given path name to an absolute path.
 * This is useful for converting path names containing ".." components or
 * relative pathnames without a leading slash into absolute paths.
 *
 * @param path See the {@link SFTPv3Client comment} for the class for more details.
 * @return An absolute path.
 * @throws IOException
 */
public String canonicalPath(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_REALPATH...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_REALPATH, 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_NAME) {
        int count = tr.readUINT32();
        if (count != 1)
            throw new IOException("The server sent an invalid SSH_FXP_NAME packet.");
        return tr.readString(charsetName);
    }
    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) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 24 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method write.

/**
 * Write bytes to a file. If <code>len</code> &gt; 32768, then the write operation will
 * be split into multiple writes.
 *
 * @param handle a SFTPv3FileHandle handle.
 * @param fileOffset offset (in bytes) in the file.
 * @param src the source byte array.
 * @param srcoff offset in the source byte array.
 * @param len how many bytes to write.
 * @throws IOException
 */
public void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException {
    checkHandleValidAndOpen(handle);
    while (len > 0) {
        int writeRequestLen = len;
        if (writeRequestLen > 32768)
            writeRequestLen = 32768;
        int req_id = generateNextRequestID();
        TypesWriter tw = new TypesWriter();
        tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
        tw.writeUINT64(fileOffset);
        tw.writeString(src, srcoff, writeRequestLen);
        if (debug != null) {
            debug.println("Sending SSH_FXP_WRITE...");
            debug.flush();
        }
        sendMessage(Packet.SSH_FXP_WRITE, req_id, tw.getBytes());
        fileOffset += writeRequestLen;
        srcoff += writeRequestLen;
        len -= writeRequestLen;
        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_STATUS)
            throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
        int errorCode = tr.readUINT32();
        if (errorCode == ErrorCodes.SSH_FX_OK)
            continue;
        String errorMessage = tr.readString();
        throw new SFTPException(errorMessage, errorCode);
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 25 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method scanDirectory.

private final Vector scanDirectory(byte[] handle) throws IOException {
    Vector files = new Vector();
    while (true) {
        int req_id = generateNextRequestID();
        TypesWriter tw = new TypesWriter();
        tw.writeString(handle, 0, handle.length);
        if (debug != null) {
            debug.println("Sending SSH_FXP_READDIR...");
            debug.flush();
        }
        sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes());
        /* Some servers send here a packet with size > 34000 */
        /* To whom it may concern: please learn to read the specs. */
        byte[] resp = receiveMessage(65536);
        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_NAME) {
            int count = tr.readUINT32();
            if (debug != null)
                debug.println("Parsing " + count + " name entries...");
            while (count > 0) {
                SFTPv3DirectoryEntry dirEnt = new SFTPv3DirectoryEntry();
                dirEnt.filename = tr.readString(charsetName);
                dirEnt.longEntry = tr.readString(charsetName);
                dirEnt.attributes = readAttrs(tr);
                files.addElement(dirEnt);
                if (debug != null)
                    debug.println("File: '" + dirEnt.filename + "'");
                count--;
            }
            continue;
        }
        if (t != Packet.SSH_FXP_STATUS)
            throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
        int errorCode = tr.readUINT32();
        if (errorCode == ErrorCodes.SSH_FX_EOF)
            return files;
        throw new SFTPException(tr.readString(), errorCode);
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException) Vector(java.util.Vector) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Aggregations

TypesReader (com.trilead.ssh2.packets.TypesReader)20 TypesWriter (com.trilead.ssh2.packets.TypesWriter)18 IOException (java.io.IOException)11 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)3 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)3 SFTPException (com.trilead.ssh2.SFTPException)3 ResponseStatusCode (com.jn.agileway.ssh.client.sftp.ResponseStatusCode)2 Connection (com.trilead.ssh2.Connection)2 SFTPv3Client (com.trilead.ssh2.SFTPv3Client)2 Vector (java.util.Vector)2 SFTPException (ch.ethz.ssh2.SFTPException)1 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)1 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)1 SFTPv3FileAttributes (com.trilead.ssh2.SFTPv3FileAttributes)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1 FileNotFoundException (java.io.FileNotFoundException)1