use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.
the class SftpFsHelper method getFileSize.
@Override
public long getFileSize(String filePath) throws FileBasedHelperException {
try {
ChannelSftp channelSftp = getSftpChannel();
long fileSize = channelSftp.lstat(filePath).getSize();
channelSftp.disconnect();
return fileSize;
} catch (SftpException e) {
throw new FileBasedHelperException(String.format("Failed to get size for file at path %s due to error %s", filePath, e.getMessage()), e);
}
}
use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.
the class SftpFsHelper method getExecChannel.
/**
* Create a new sftp channel to execute commands.
*
* @param command to execute on the remote machine
* @return a new execution channel
* @throws SftpException if a channel could not be opened
*/
public ChannelExec getExecChannel(String command) throws SftpException {
ChannelExec channelExec;
try {
channelExec = (ChannelExec) this.session.openChannel("exec");
channelExec.setCommand(command);
channelExec.connect();
return channelExec;
} catch (JSchException e) {
throw new SftpException(0, "Cannot open a channel to SFTP server", e);
}
}
use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.
the class SftpFsHelper method getSftpChannel.
/**
* Create new channel every time a command needs to be executed. This is required to support execution of multiple
* commands in parallel. All created channels are cleaned up when the session is closed.
*
* @return a new {@link ChannelSftp}
* @throws SftpException
*/
public ChannelSftp getSftpChannel() throws SftpException {
try {
ChannelSftp channelSftp = (ChannelSftp) this.session.openChannel("sftp");
// In millsec
int connTimeout = state.getPropAsInt(SFTP_CONNECTION_TIMEOUT_KEY, DEFAULT_SFTP_CONNECTION_TIMEOUT);
channelSftp.connect(connTimeout);
return channelSftp;
} catch (JSchException e) {
throw new SftpException(0, "Cannot open a channel to SFTP server", e);
}
}
use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.
the class SftpLightWeightFileSystem method mkdirs.
@Override
public boolean mkdirs(Path path, FsPermission permission) throws IOException {
ChannelSftp channel = null;
try {
channel = this.fsHelper.getSftpChannel();
channel.mkdir(HadoopUtils.toUriPath(path));
channel.chmod(permission.toShort(), HadoopUtils.toUriPath(path));
} catch (SftpException e) {
throw new IOException(e);
} finally {
safeDisconnect(channel);
}
return true;
}
use of com.jcraft.jsch.SftpException in project hadoop by apache.
the class SFTPFileSystem method listStatus.
/**
* Convenience method, so that we don't open a new connection when using this
* method from within another method. Otherwise every API invocation incurs
* the overhead of opening/closing a TCP connection.
*/
@SuppressWarnings("unchecked")
private FileStatus[] listStatus(ChannelSftp client, Path file) throws IOException {
Path workDir;
try {
workDir = new Path(client.pwd());
} catch (SftpException e) {
throw new IOException(e);
}
Path absolute = makeAbsolute(workDir, file);
FileStatus fileStat = getFileStatus(client, absolute);
if (!fileStat.isDirectory()) {
return new FileStatus[] { fileStat };
}
Vector<LsEntry> sftpFiles;
try {
sftpFiles = (Vector<LsEntry>) client.ls(absolute.toUri().getPath());
} catch (SftpException e) {
throw new IOException(e);
}
ArrayList<FileStatus> fileStats = new ArrayList<FileStatus>();
for (int i = 0; i < sftpFiles.size(); i++) {
LsEntry entry = sftpFiles.get(i);
String fname = entry.getFilename();
// skip current and parent directory, ie. "." and ".."
if (!".".equalsIgnoreCase(fname) && !"..".equalsIgnoreCase(fname)) {
fileStats.add(getFileStatus(client, entry, absolute));
}
}
return fileStats.toArray(new FileStatus[fileStats.size()]);
}
Aggregations