use of com.jcraft.jsch.SftpException in project DataX by alibaba.
the class SftpHelperImpl method mkDirSingleHierarchy.
public boolean mkDirSingleHierarchy(String directoryPath) throws SftpException {
boolean isDirExist = false;
try {
SftpATTRS sftpATTRS = this.channelSftp.lstat(directoryPath);
isDirExist = sftpATTRS.isDir();
} catch (SftpException e) {
if (!isDirExist) {
LOG.info(String.format("正在逐级创建目录 [%s]", directoryPath));
this.channelSftp.mkdir(directoryPath);
return true;
}
}
if (!isDirExist) {
LOG.info(String.format("正在逐级创建目录 [%s]", directoryPath));
this.channelSftp.mkdir(directoryPath);
}
return true;
}
use of com.jcraft.jsch.SftpException in project DataX by alibaba.
the class SftpHelperImpl method getRemoteFileContent.
@Override
public String getRemoteFileContent(String filePath) {
try {
this.completePendingCommand();
this.printWorkingDirectory();
String parentDir = filePath.substring(0, StringUtils.lastIndexOf(filePath, IOUtils.DIR_SEPARATOR));
this.channelSftp.cd(parentDir);
this.printWorkingDirectory();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(22);
this.channelSftp.get(filePath, outputStream);
String result = outputStream.toString();
IOUtils.closeQuietly(outputStream);
return result;
} catch (SftpException e) {
String message = String.format("写出文件[%s] 时出错,请确认文件%s有权限写出, errorMessage:%s", filePath, filePath, e.getMessage());
LOG.error(message);
throw DataXException.asDataXException(FtpWriterErrorCode.OPEN_FILE_ERROR, message);
}
}
use of com.jcraft.jsch.SftpException in project voltdb by VoltDB.
the class SFTPSession method ensureDirectoriesExistFor.
/**
* Akin to mkdir -p for all directories containing the given collection of
* remote files.
*
* @param files a collection of files specified as absolute paths
*
* @throws {@link SFTPException} when an error occurs during SFTP operations
* performed by this method
*/
public void ensureDirectoriesExistFor(final Collection<File> files) {
Preconditions.checkArgument(files != null, "null file collection");
Preconditions.checkState(m_channel != null, "stale session");
verifyAllAreAbsolutePaths(files);
/*
* directory entries are sorted first by their level (/l1 < /l1/l2 < /l1/l2/l3)
* and then their name. This loop adds all the directories that are required
* to ensure that given list of destination files can be copied over successfully
*/
TreeSet<DirectoryEntry> directories = new TreeSet<DirectoryEntry>();
for (File f : files) {
addDirectoryAncestors(f.getParentFile(), directories);
}
/*
* for each entry it tests whether or not it already exists, and if it
* does not, it creates it (akin to mkdir -p)
*/
for (DirectoryEntry entry : directories) {
if (!directoryExists(entry.getDirectory())) {
try {
m_channel.mkdir(entry.getDirectory().getPath());
if (m_log.isDebugEnabled()) {
m_log.debug("SFTP: mkdir " + entry.getDirectory().getPath());
}
} catch (SftpException sfex) {
throw new SFTPException("create directory " + entry, sfex);
}
}
}
directories.clear();
}
use of com.jcraft.jsch.SftpException in project voltdb by VoltDB.
the class SFTPSession method deletePreviouslyInstalledArtifacts.
/**
* if found, it deletes artifacts held in the directories that
* contain the given list of absolute file paths
*
* @param files a collection of files specified as absolute paths
*
* @throws SFTPException when an error occurs during SFTP operations performed
* by this method
*/
public void deletePreviouslyInstalledArtifacts(final Collection<File> files) {
Preconditions.checkArgument(files != null, "null file collection");
Preconditions.checkState(m_channel != null, "stale session");
verifyAllAreAbsolutePaths(files);
// dedup directories containing files
TreeSet<File> directories = new TreeSet<File>();
for (File f : files) {
directories.add(f.getParentFile());
}
// look for file artifacts that end with .so, .jar, and .jnilib
for (File d : directories) {
final ArrayList<String> toBeDeleted = new ArrayList<String>();
LsEntrySelector selector = new LsEntrySelector() {
@Override
public int select(LsEntry entry) {
Matcher mtc = ARTIFACT_REGEXP.matcher(entry.getFilename());
SftpATTRS attr = entry.getAttrs();
if (mtc.find() && !attr.isDir() && !attr.isLink()) {
toBeDeleted.add(entry.getFilename());
}
return CONTINUE;
}
};
try {
m_channel.ls(d.getPath(), selector);
if (m_log.isDebugEnabled()) {
m_log.debug("SFTP: ls " + d.getPath());
}
} catch (SftpException sfex) {
throw new SFTPException("list directory " + d, sfex);
}
// delete found artifacts
for (String f : toBeDeleted) {
File artifact = new File(d, f);
try {
m_channel.rm(artifact.getPath());
if (m_log.isDebugEnabled()) {
m_log.debug("SFTP: rm " + artifact.getPath());
}
} catch (SftpException sfex) {
throw new SFTPException("remove artifact " + artifact, sfex);
}
}
}
}
use of com.jcraft.jsch.SftpException in project voltdb by VoltDB.
the class ExportOnServerVerifier method verifySetup.
boolean verifySetup(String[] args) throws Exception {
String[] remoteHosts = args[0].split(",");
final String homeDir = System.getProperty("user.home");
final String sshDir = homeDir + File.separator + ".ssh";
final String sshConfigPath = sshDir + File.separator + "config";
//Oh yes...
loadAllPrivateKeys(new File(sshDir));
OpenSshConfig sshConfig = null;
if (new File(sshConfigPath).exists()) {
sshConfig = new OpenSshConfig(new File(sshConfigPath));
}
final String defaultKnownHosts = sshDir + "/known_hosts";
if (new File(defaultKnownHosts).exists()) {
m_jsch.setKnownHosts(defaultKnownHosts);
}
for (String hostString : remoteHosts) {
String[] split = hostString.split(":");
String host = split[0];
RemoteHost rh = new RemoteHost();
rh.path = split[1];
String user = System.getProperty("user.name");
int port = 22;
File identityFile = null;
String configHost = host;
if (sshConfig != null) {
OpenSshConfig.Host hostConfig = sshConfig.lookup(host);
if (hostConfig.getUser() != null) {
user = hostConfig.getUser();
}
if (hostConfig.getPort() != -1) {
port = hostConfig.getPort();
}
if (hostConfig.getIdentityFile() != null) {
identityFile = hostConfig.getIdentityFile();
}
if (hostConfig.getHostName() != null) {
configHost = hostConfig.getHostName();
}
}
Session session = null;
if (identityFile != null) {
JSch jsch = new JSch();
jsch.addIdentity(identityFile.getAbsolutePath());
session = jsch.getSession(user, configHost, port);
} else {
session = m_jsch.getSession(user, configHost, port);
}
rh.session = session;
session.setConfig("StrictHostKeyChecking", "no");
session.setDaemonThread(true);
session.connect();
final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
rh.channel = channel;
channel.connect();
touchActiveTracker(rh);
m_hosts.add(rh);
}
m_partitions = Integer.parseInt(args[1]);
for (int i = 0; i < m_partitions; i++) {
m_rowTxnIds.put(i, new TreeMap<Long, Long>());
m_maxPartTxId.put(i, Long.MIN_VALUE);
m_checkedUpTo.put(i, 0);
m_readUpTo.put(i, new AtomicLong(0));
}
m_clientPath = new File(args[2]);
if (!m_clientPath.exists() || !m_clientPath.isDirectory()) {
if (!m_clientPath.mkdir()) {
throw new IOException("Issue with transaction ID path");
}
}
for (RemoteHost rh : m_hosts) {
boolean existsOrIsDir = true;
try {
SftpATTRS stat = rh.channel.stat(rh.path);
if (!stat.isDir()) {
existsOrIsDir = false;
}
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
existsOrIsDir = false;
} else {
Throwables.propagate(e);
}
}
if (!existsOrIsDir) {
rh.channel.mkdir(rh.path);
}
}
boolean skinny = false;
if (args.length > 3 && args[3] != null && !args[3].trim().isEmpty()) {
skinny = Boolean.parseBoolean(args[3].trim().toLowerCase());
}
return skinny;
}
Aggregations