Search in sources :

Example 6 with SFTPException

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

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

Example 7 with SFTPException

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

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

Example 8 with SFTPException

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

the class SFTPv3Client method openDirectory.

private final byte[] openDirectory(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_OPENDIR...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_OPENDIR, 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();
        }
        byte[] handle = tr.readByteString();
        return handle;
    }
    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 9 with SFTPException

use of com.trilead.ssh2.SFTPException in project agileway by fangjinuo.

the class Ssh2SftpSession method open.

@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
    try {
        SFTPv3FileHandle handle = null;
        if (!Sftps.exists(this, filepath)) {
            if (attrs == null) {
                attrs = new FileAttrs();
            }
            FileMode fileMode = attrs.getFileMode();
            if (fileMode == null) {
                fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
                attrs.setFileMode(fileMode);
            }
            if (OpenMode.isCreatable(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            } else {
                throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
            }
        } else {
            if (OpenMode.isTruncated(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            }
        }
        if (handle != null) {
            sftpClient.closeFile(handle);
            handle = null;
        }
        if (OpenMode.isWritable(openMode)) {
            handle = sftpClient.openFileRW(filepath);
        } else {
            handle = sftpClient.openFileRO(filepath);
        }
        Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
        sftpFile.setFileHandle(handle);
        return sftpFile;
    } catch (Throwable ex) {
        throw new SftpException(ex);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException)

Example 10 with SFTPException

use of com.trilead.ssh2.SFTPException in project agileway by fangjinuo.

the class Ssh2SftpSession method setStat.

@Override
public void setStat(String path, FileAttrs attrs) throws SftpException {
    try {
        SFTPv3FileAttributes attributes = Ssh2Sftps.toSsh2FileAttributes(attrs);
        sftpClient.setstat(path, attributes);
    } catch (Throwable ex) {
        throw Ssh2Sftps.wrapSftpException(ex);
    }
}
Also used : SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes)

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