Search in sources :

Example 11 with TypesWriter

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

the class SFTPv3Client method createAttrs.

private byte[] createAttrs(SFTPv3FileAttributes attr) {
    TypesWriter tw = new TypesWriter();
    int attrFlags = 0;
    if (attr == null) {
        tw.writeUINT32(0);
    } else {
        if (attr.size != null)
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
        if ((attr.uid != null) && (attr.gid != null))
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID;
        if (attr.permissions != null)
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
        if ((attr.atime != null) && (attr.mtime != null))
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME;
        tw.writeUINT32(attrFlags);
        if (attr.size != null)
            tw.writeUINT64(attr.size.longValue());
        if ((attr.uid != null) && (attr.gid != null)) {
            tw.writeUINT32(attr.uid.intValue());
            tw.writeUINT32(attr.gid.intValue());
        }
        if (attr.permissions != null)
            tw.writeUINT32(attr.permissions.intValue());
        if ((attr.atime != null) && (attr.mtime != null)) {
            tw.writeUINT32(attr.atime.intValue());
            tw.writeUINT32(attr.mtime.intValue());
        }
    }
    return tw.getBytes();
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 12 with TypesWriter

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

the class SFTPv3Client method statBoth.

private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
        debug.flush();
    }
    sendMessage(statMethod, 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 13 with TypesWriter

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

the class SFTPv3Client method setstat.

/**
	 *  Modify the attributes of a file. Used for operations such as changing
	 *  the ownership, permissions or access times, as well as for truncating a file.
	 * 
	 * @param path See the {@link SFTPv3Client comment} for the class for more details.
	 * @param attr A SFTPv3FileAttributes object. Specifies the modifications to be
	 *             made to the attributes of the file. Empty fields will be ignored.
	 * @throws IOException
	 */
public void setstat(String path, SFTPv3FileAttributes attr) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    tw.writeBytes(createAttrs(attr));
    if (debug != null) {
        debug.println("Sending SSH_FXP_SETSTAT...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_SETSTAT, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 14 with TypesWriter

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

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

Example 15 with TypesWriter

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

the class SFTPv3Client method readLink.

/**
	 * Read the target of a symbolic link.
	 * 
	 * @param path See the {@link SFTPv3Client comment} for the class for more details.
	 * @return The target of the link.
	 * @throws IOException
	 */
public String readLink(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_READLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_READLINK, 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) 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