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);
}
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");
}
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("..");
}
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();
}
}
}
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;
}
Aggregations