Search in sources :

Example 6 with SFTPv3FileHandle

use of com.trilead.ssh2.SFTPv3FileHandle 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 7 with SFTPv3FileHandle

use of com.trilead.ssh2.SFTPv3FileHandle 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 8 with SFTPv3FileHandle

use of com.trilead.ssh2.SFTPv3FileHandle 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 9 with SFTPv3FileHandle

use of com.trilead.ssh2.SFTPv3FileHandle in project pentaho-kettle by pentaho.

the class JobEntrySSH2GET method copyFile.

/**
 * @param sourceLocation
 * @param targetLocation
 * @param sftpClient
 * @return
 */
private void copyFile(String sourceLocation, String targetLocation, SFTPv3Client sftpClient) {
    SFTPv3FileHandle sftpFileHandle = null;
    FileOutputStream fos = null;
    File transferFile = null;
    long remoteFileSize = -1;
    boolean filecopied = true;
    try {
        transferFile = new File(targetLocation);
        if ((onlyGettingNewFiles == false) || (onlyGettingNewFiles == true) && !FileExists(transferFile.getAbsolutePath())) {
            new File(transferFile.getParent()).mkdirs();
            remoteFileSize = this.getFileSize(sftpClient, sourceLocation);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ReceivingFile", sourceLocation, transferFile.getAbsolutePath(), "" + remoteFileSize));
            }
            sftpFileHandle = sftpClient.openFileRO(sourceLocation);
            fos = null;
            long offset = 0;
            fos = new FileOutputStream(transferFile);
            byte[] buffer = new byte[2048];
            while (true) {
                int len = sftpClient.read(sftpFileHandle, offset, buffer, 0, buffer.length);
                if (len <= 0) {
                    break;
                }
                fos.write(buffer, 0, len);
                offset += len;
            }
            fos.flush();
            fos.close();
            fos = null;
            nbfilestoget++;
            if (remoteFileSize > 0 && remoteFileSize != transferFile.length()) {
                filecopied = false;
                nbrerror++;
                logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.Error.RemoteFileLocalDifferent", "" + remoteFileSize, transferFile.length() + "", "" + offset));
            } else {
                nbgot++;
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RemoteFileLocalCopied", sourceLocation, transferFile + ""));
                }
            }
        }
        // Let's now delete or move file if needed...
        if (filecopied && !afterFtpPut.equals("do_nothing")) {
            deleteOrMoveFiles(sftpClient, sourceLocation, environmentSubstitute(destinationfolder));
        }
    } catch (Exception e) {
        nbrerror++;
        logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.Error.WritingFile", transferFile.getAbsolutePath(), e.getMessage()));
    } finally {
        try {
            if (sftpFileHandle != null) {
                sftpClient.closeFile(sftpFileHandle);
                sftpFileHandle = null;
            }
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (Exception ex) {
                // Ignore errors
                }
            }
        } catch (Exception e) {
        // Ignore errors
        }
    }
}
Also used : SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) FileOutputStream(java.io.FileOutputStream) File(java.io.File) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException)

Example 10 with SFTPv3FileHandle

use of com.trilead.ssh2.SFTPv3FileHandle in project pentaho-kettle by pentaho.

the class JobEntrySSH2PUT method putFile.

private boolean putFile(FileObject localFile, String remotefilename, SFTPv3Client sftpClient) {
    long filesize = -1;
    InputStream in = null;
    BufferedInputStream inBuf = null;
    SFTPv3FileHandle sftpFileHandle = null;
    boolean retval = false;
    try {
        // Put file in the folder
        sftpFileHandle = sftpClient.createFileTruncate(remotefilename);
        // Associate a file input stream for the current local file
        in = KettleVFS.getInputStream(localFile);
        inBuf = new BufferedInputStream(in);
        byte[] buf = new byte[2048];
        long offset = 0;
        long length = localFile.getContent().getSize();
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.SendingFile", localFile.toString(), "" + length, remotefilename));
        }
        // Write to remote file
        while (true) {
            int len = in.read(buf, 0, buf.length);
            if (len <= 0) {
                break;
            }
            sftpClient.write(sftpFileHandle, offset, buf, 0, len);
            offset += len;
        }
        // Get File size
        filesize = getFileSize(sftpClient, remotefilename);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.FileOnRemoteHost", remotefilename, "" + filesize));
        }
        retval = true;
    } catch (Exception e) {
        // We failed to put files
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ErrorCopyingFile", localFile.toString()) + ":" + e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
                in = null;
            } catch (Exception ex) {
            // Ignore errors
            }
        }
        if (inBuf != null) {
            try {
                inBuf.close();
                inBuf = null;
            } catch (Exception ex) {
            // Ignore errors
            }
        }
        if (sftpFileHandle != null) {
            try {
                sftpClient.closeFile(sftpFileHandle);
                sftpFileHandle = null;
            } catch (Exception ex) {
            // Ignore errors
            }
        }
    }
    return retval;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException)

Aggregations

SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)6 TypesWriter (com.trilead.ssh2.packets.TypesWriter)5 TypesReader (com.trilead.ssh2.packets.TypesReader)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)2 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1