Search in sources :

Example 81 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project ant by apache.

the class ScpFromMessage method setLastModified.

private void setLastModified(final File localFile) throws JSchException {
    SftpATTRS fileAttributes = null;
    final ChannelSftp channel = openSftpChannel();
    channel.connect();
    try {
        fileAttributes = channel.lstat(remoteDir(remoteFile) + localFile.getName());
    } catch (final SftpException e) {
        throw new JSchException("failed to stat remote file", e);
    }
    FileUtils.getFileUtils().setFileLastModified(localFile, ((long) fileAttributes.getMTime()) * 1000);
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 82 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project ant by apache.

the class ScpFromMessageBySftp method execute.

/**
 * Carry out the transfer.
 * @throws IOException on i/o errors
 * @throws JSchException on errors detected by scp
 */
@Override
public void execute() throws IOException, JSchException {
    final ChannelSftp channel = openSftpChannel();
    try {
        channel.connect();
        try {
            final SftpATTRS attrs = channel.stat(remoteFile);
            if (attrs.isDir() && !remoteFile.endsWith("/")) {
                remoteFile = remoteFile + "/";
            }
        } catch (final SftpException ee) {
        // Ignored
        }
        getDir(channel, remoteFile, localFile);
    } catch (final SftpException e) {
        throw new JSchException("Could not get '" + remoteFile + "' to '" + localFile + "' - " + e.toString(), e);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
    log("done\n");
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 83 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project ant by apache.

the class ScpFromMessageBySftp method getDir.

private void getDir(final ChannelSftp channel, final String remoteFile, final File localFile) throws IOException, SftpException {
    String pwd = remoteFile;
    if (remoteFile.lastIndexOf('/') != -1) {
        if (remoteFile.length() > 1) {
            pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
        }
    }
    channel.cd(pwd);
    if (!localFile.exists()) {
        localFile.mkdirs();
    }
    @SuppressWarnings("unchecked") final List<ChannelSftp.LsEntry> files = channel.ls(remoteFile);
    for (ChannelSftp.LsEntry le : files) {
        final String name = le.getFilename();
        if (le.getAttrs().isDir()) {
            if (".".equals(name) || "..".equals(name)) {
                continue;
            }
            getDir(channel, channel.pwd() + "/" + name + "/", new File(localFile, le.getFilename()));
        } else {
            getFile(channel, le, localFile);
        }
    }
    channel.cd("..");
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) File(java.io.File)

Example 84 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.

the class SftpFsHelper method getFileMTime.

@Override
public long getFileMTime(String filePath) throws FileBasedHelperException {
    ChannelSftp channelSftp = null;
    try {
        channelSftp = getSftpChannel();
        int modificationTime = channelSftp.lstat(filePath).getMTime();
        return modificationTime;
    } catch (SftpException e) {
        throw new FileBasedHelperException(String.format("Failed to get modified timestamp for file at path %s due to error %s", filePath, e.getMessage()), e);
    } finally {
        if (channelSftp != null) {
            channelSftp.disconnect();
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) FileBasedHelperException(org.apache.gobblin.source.extractor.filebased.FileBasedHelperException) SftpException(com.jcraft.jsch.SftpException)

Example 85 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project incubator-gobblin by apache.

the class SftpLightWeightFileSystem method rename.

@Override
public boolean rename(Path oldPath, Path newPath) throws IOException {
    ChannelSftp channelSftp = null;
    try {
        channelSftp = this.fsHelper.getSftpChannel();
        channelSftp.rename(HadoopUtils.toUriPath(oldPath), HadoopUtils.toUriPath(newPath));
    } catch (SftpException e) {
        throw new IOException(e);
    } finally {
        safeDisconnect(channelSftp);
    }
    return true;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)99 SftpException (com.jcraft.jsch.SftpException)61 IOException (java.io.IOException)36 JSchException (com.jcraft.jsch.JSchException)28 Session (com.jcraft.jsch.Session)25 JSch (com.jcraft.jsch.JSch)20 Channel (com.jcraft.jsch.Channel)17 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)17 File (java.io.File)16 Test (org.junit.Test)14 InputStream (java.io.InputStream)12 SftpATTRS (com.jcraft.jsch.SftpATTRS)11 FileNotFoundException (java.io.FileNotFoundException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 FileInputStream (java.io.FileInputStream)7 OutputStream (java.io.OutputStream)7 Path (org.apache.hadoop.fs.Path)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7