Search in sources :

Example 1 with SftpException

use of com.jcraft.jsch.SftpException in project che by eclipse.

the class JschSshClient method copyFile.

private void copyFile(String sourcePath, String absoluteTargetPath, ChannelSftp channelSftp) throws MachineException {
    try {
        channelSftp.put(sourcePath, absoluteTargetPath);
        // apply permissions
        File file = new File(sourcePath);
        // read
        int permissions = 256;
        // execute
        if (file.canExecute()) {
            permissions += 64;
        }
        // write
        if (file.canWrite()) {
            permissions += 128;
        }
        channelSftp.chmod(permissions, absoluteTargetPath);
    } catch (SftpException e) {
        throw new MachineException(format("Sftp copying of file %s failed. Error: %s", absoluteTargetPath, e.getLocalizedMessage()));
    }
}
Also used : SftpException(com.jcraft.jsch.SftpException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) File(java.io.File)

Example 2 with SftpException

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;
}
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)

Example 3 with SftpException

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();
}
Also used : TreeSet(java.util.TreeSet) SftpException(com.jcraft.jsch.SftpException) File(java.io.File)

Example 4 with SftpException

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);
            }
        }
    }
}
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 SftpException

use of com.jcraft.jsch.SftpException in project hadoop by apache.

the class SFTPFileSystem method open.

@Override
public FSDataInputStream open(Path f, int bufferSize) throws IOException {
    ChannelSftp channel = connect();
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    FileStatus fileStat = getFileStatus(channel, absolute);
    if (fileStat.isDirectory()) {
        disconnect(channel);
        throw new IOException(String.format(E_PATH_DIR, f));
    }
    InputStream is;
    try {
        // the path could be a symbolic link, so get the real path
        absolute = new Path("/", channel.realpath(absolute.toUri().getPath()));
        is = channel.get(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataInputStream fis = new FSDataInputStream(new SFTPInputStream(is, channel, statistics));
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Aggregations

SftpException (com.jcraft.jsch.SftpException)113 ChannelSftp (com.jcraft.jsch.ChannelSftp)67 IOException (java.io.IOException)49 JSchException (com.jcraft.jsch.JSchException)28 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)24 File (java.io.File)24 SftpATTRS (com.jcraft.jsch.SftpATTRS)16 Path (org.apache.hadoop.fs.Path)15 Session (com.jcraft.jsch.Session)13 OutputStream (java.io.OutputStream)11 JSch (com.jcraft.jsch.JSch)10 FileNotFoundException (java.io.FileNotFoundException)10 InputStream (java.io.InputStream)10 Channel (com.jcraft.jsch.Channel)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 FileStatus (org.apache.hadoop.fs.FileStatus)9 ArrayList (java.util.ArrayList)8 Vector (java.util.Vector)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Test (org.junit.Test)8