Search in sources :

Example 1 with SftpATTRS

use of com.jcraft.jsch.SftpATTRS in project DataX by alibaba.

the class SftpHelperImpl method mkDirRecursive.

@Override
public void mkDirRecursive(String directoryPath) {
    boolean isDirExist = false;
    try {
        this.printWorkingDirectory();
        SftpATTRS sftpATTRS = this.channelSftp.lstat(directoryPath);
        isDirExist = sftpATTRS.isDir();
    } catch (SftpException e) {
        if (e.getMessage().toLowerCase().equals("no such file")) {
            LOG.warn(String.format("您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s", directoryPath, e.getMessage()), e);
            isDirExist = false;
        }
    }
    if (!isDirExist) {
        StringBuilder dirPath = new StringBuilder();
        dirPath.append(IOUtils.DIR_SEPARATOR_UNIX);
        String[] dirSplit = StringUtils.split(directoryPath, IOUtils.DIR_SEPARATOR_UNIX);
        try {
            // ftp server不支持递归创建目录,只能一级一级创建
            for (String dirName : dirSplit) {
                dirPath.append(dirName);
                mkDirSingleHierarchy(dirPath.toString());
                dirPath.append(IOUtils.DIR_SEPARATOR_UNIX);
            }
        } catch (SftpException e) {
            String message = String.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s", directoryPath, e.getMessage());
            LOG.error(message, e);
            throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
    }
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 2 with SftpATTRS

use of com.jcraft.jsch.SftpATTRS in project DataX by alibaba.

the class SftpHelperImpl method mkdir.

@Override
public void mkdir(String directoryPath) {
    boolean isDirExist = false;
    try {
        this.printWorkingDirectory();
        SftpATTRS sftpATTRS = this.channelSftp.lstat(directoryPath);
        isDirExist = sftpATTRS.isDir();
    } catch (SftpException e) {
        if (e.getMessage().toLowerCase().equals("no such file")) {
            LOG.warn(String.format("您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s", directoryPath, e.getMessage()), e);
            isDirExist = false;
        }
    }
    if (!isDirExist) {
        try {
            // warn 检查mkdir -p
            this.channelSftp.mkdir(directoryPath);
        } catch (SftpException e) {
            String message = String.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s", directoryPath, e.getMessage());
            LOG.error(message, e);
            throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
    }
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 3 with SftpATTRS

use of com.jcraft.jsch.SftpATTRS 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;
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 4 with SftpATTRS

use of com.jcraft.jsch.SftpATTRS 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);
            }
        }
    }
}
Also used : LsEntrySelector(com.jcraft.jsch.ChannelSftp.LsEntrySelector) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 5 with SftpATTRS

use of com.jcraft.jsch.SftpATTRS 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;
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) ChannelSftp(com.jcraft.jsch.ChannelSftp) AtomicLong(java.util.concurrent.atomic.AtomicLong) OpenSshConfig(org.spearce_voltpatches.jgit.transport.OpenSshConfig) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

SftpATTRS (com.jcraft.jsch.SftpATTRS)8 SftpException (com.jcraft.jsch.SftpException)8 File (java.io.File)3 IOException (java.io.IOException)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 FileNotFoundException (java.io.FileNotFoundException)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 Path (org.apache.hadoop.fs.Path)2 FsPermission (org.apache.hadoop.fs.permission.FsPermission)2 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 LsEntrySelector (com.jcraft.jsch.ChannelSftp.LsEntrySelector)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Matcher (java.util.regex.Matcher)1 OpenSshConfig (org.spearce_voltpatches.jgit.transport.OpenSshConfig)1