Search in sources :

Example 1 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project hadoop by apache.

the class SFTPFileSystem method getFileStatus.

/**
   * 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 getFileStatus(ChannelSftp client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root directory
        // Length of root directory on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root directory not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
    }
    String pathName = parentPath.toUri().getPath();
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(pathName);
    } catch (SftpException e) {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    if (sftpFiles != null) {
        for (LsEntry sftpFile : sftpFiles) {
            if (sftpFile.getFilename().equals(file.getName())) {
                // file found in directory
                fileStat = getFileStatus(client, sftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
        }
    } else {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 2 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry 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 3 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project voltdb by VoltDB.

the class ExportOnServerVerifier method checkForMoreFilesRemote.

@SuppressWarnings("unchecked")
private void checkForMoreFilesRemote(Comparator<String> comparator) throws Exception {
    int onDoneRetries = 6;
    long start_time = System.currentTimeMillis();
    while (m_exportFiles.isEmpty()) {
        /*
             * Collect the list of remote files at each node
             * Sort the list from each node
             */
        int activeFound = 0;
        List<Pair<ChannelSftp, List<String>>> pathsFromAllNodes = new ArrayList<Pair<ChannelSftp, List<String>>>();
        for (RemoteHost rh : m_hosts) {
            Vector<LsEntry> files = rh.channel.ls(rh.path);
            List<String> paths = new ArrayList<String>();
            final int trackerModifyTime = rh.channel.stat(rh.path + "/" + TRACKER_FILENAME).getMTime();
            boolean activeInRemote = false;
            boolean filesInRemote = false;
            for (LsEntry entry : files) {
                activeInRemote = activeInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
                filesInRemote = filesInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
                if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..") && !entry.getAttrs().isDir()) {
                    final String entryFileName = rh.path + "/" + entry.getFilename();
                    final int entryModifyTime = entry.getAttrs().getMTime();
                    if (!entry.getFilename().contains("active")) {
                        Matcher mtc = EXPORT_FILENAME_REGEXP.matcher(entry.getFilename());
                        if (mtc.matches()) {
                            paths.add(entryFileName);
                            activeInRemote = activeInRemote || entryModifyTime > trackerModifyTime;
                            filesInRemote = true;
                        } else {
                            System.err.println("ERROR: " + entryFileName + " does not match expected export file name pattern");
                        }
                    } else if (entry.getFilename().trim().toLowerCase().startsWith("active-")) {
                        if ((trackerModifyTime - entryModifyTime) > 120) {
                            final String renamed = rh.path + "/" + entry.getFilename().substring("active-".length());
                            rh.channel.rename(entryFileName, renamed);
                            paths.add(renamed);
                        }
                    }
                }
            }
            touchActiveTracker(rh);
            rh.activeSeen = rh.activeSeen || activeInRemote;
            rh.fileSeen = rh.fileSeen || filesInRemote;
            if (activeInRemote)
                activeFound++;
            Collections.sort(paths, comparator);
            if (!paths.isEmpty())
                pathsFromAllNodes.add(Pair.of(rh.channel, paths));
        }
        if (!m_clientComplete.isEmpty()) {
            printExportFileSituation(pathsFromAllNodes, activeFound);
        }
        if (pathsFromAllNodes.isEmpty() && activeFound == 0 && allActiveSeen()) {
            if (--onDoneRetries <= 0)
                return;
            Thread.sleep(5000);
        }
        // add them to m_exportFiles as ordered by the comparator
        TreeMap<String, Pair<ChannelSftp, String>> hadPaths = new TreeMap<String, Pair<ChannelSftp, String>>(comparator);
        for (Pair<ChannelSftp, List<String>> p : pathsFromAllNodes) {
            final ChannelSftp c = p.getFirst();
            for (String path : p.getSecond()) {
                hadPaths.put(path, Pair.of(c, path));
            }
        }
        boolean hadOne = !hadPaths.isEmpty();
        Iterator<Map.Entry<String, Pair<ChannelSftp, String>>> itr = hadPaths.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry<String, Pair<ChannelSftp, String>> entry = itr.next();
            m_exportFiles.offer(entry.getValue());
            itr.remove();
        }
        long now = System.currentTimeMillis();
        if ((now - start_time) > FILE_TIMEOUT_MS) {
            throw new ValidationErr("Timed out waiting on new files.\n" + "This indicates a mismatch in the transaction streams between the client logs and the export data or the death of something important.", null, null);
        } else if (!hadOne) {
            Thread.sleep(1200);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ChannelSftp(com.jcraft.jsch.ChannelSftp) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) ArrayList(java.util.ArrayList) List(java.util.List) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Pair(org.voltcore.utils.Pair)

Example 4 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project ats-framework by Axway.

the class JschSftpClient method ls.

/**
     *
     * @param directoryPath directory path
     * @return {@link List} of {@link FileEntry} objects corresponding with the target directory
     *  file and folder entries.
     */
public List<FileEntry> ls(String directoryPath) {
    try {
        List<LsEntry> entries = new ArrayList<LsEntry>();
        channel.ls(directoryPath, new LsEntrySelector(entries));
        return getFileEntries(entries, directoryPath);
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) ArrayList(java.util.ArrayList) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 5 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project cdap by caskdata.

the class SFTPFileSystem method getFileStatus.

/**
   * 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 getFileStatus(ChannelSftp client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root directory
        // Length of root directory on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root directory not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
    }
    String pathName = parentPath.toUri().getPath();
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(pathName);
    } catch (SftpException e) {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    if (sftpFiles != null) {
        for (LsEntry sftpFile : sftpFiles) {
            if (sftpFile.getFilename().equals(file.getName())) {
                // file found in directory
                fileStat = getFileStatus(client, sftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
        }
    } else {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Aggregations

LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)12 SftpException (com.jcraft.jsch.SftpException)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)4 FileStatus (org.apache.hadoop.fs.FileStatus)4 Path (org.apache.hadoop.fs.Path)4 JschSftpClientException (com.axway.ats.core.ssh.exceptions.JschSftpClientException)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 FileNotFoundException (java.io.FileNotFoundException)2 Vector (java.util.Vector)2 Matcher (java.util.regex.Matcher)2 Channel (com.jcraft.jsch.Channel)1 LsEntrySelector (com.jcraft.jsch.ChannelSftp.LsEntrySelector)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 SftpATTRS (com.jcraft.jsch.SftpATTRS)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1