Search in sources :

Example 1 with LsEntrySelector

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

use of com.jcraft.jsch.ChannelSftp.LsEntrySelector in project nifi by apache.

the class SFTPTransfer method getListing.

private void getListing(final String path, final int depth, final int maxResults, final List<FileInfo> listing) throws IOException {
    if (maxResults < 1 || listing.size() >= maxResults) {
        return;
    }
    if (depth >= 100) {
        logger.warn(this + " had to stop recursively searching directories at a recursive depth of " + depth + " to avoid memory issues");
        return;
    }
    final boolean ignoreDottedFiles = ctx.getProperty(FileTransfer.IGNORE_DOTTED_FILES).asBoolean();
    final boolean recurse = ctx.getProperty(FileTransfer.RECURSIVE_SEARCH).asBoolean();
    final String fileFilterRegex = ctx.getProperty(FileTransfer.FILE_FILTER_REGEX).getValue();
    final Pattern pattern = (fileFilterRegex == null) ? null : Pattern.compile(fileFilterRegex);
    final String pathFilterRegex = ctx.getProperty(FileTransfer.PATH_FILTER_REGEX).getValue();
    final Pattern pathPattern = (!recurse || pathFilterRegex == null) ? null : Pattern.compile(pathFilterRegex);
    final String remotePath = ctx.getProperty(FileTransfer.REMOTE_PATH).evaluateAttributeExpressions().getValue();
    // check if this directory path matches the PATH_FILTER_REGEX
    boolean pathFilterMatches = true;
    if (pathPattern != null) {
        Path reldir = path == null ? Paths.get(".") : Paths.get(path);
        if (remotePath != null) {
            reldir = Paths.get(remotePath).relativize(reldir);
        }
        if (reldir != null && !reldir.toString().isEmpty()) {
            if (!pathPattern.matcher(reldir.toString().replace("\\", "/")).matches()) {
                pathFilterMatches = false;
            }
        }
    }
    final ChannelSftp sftp = getChannel(null);
    final boolean isPathMatch = pathFilterMatches;
    final List<LsEntry> subDirs = new ArrayList<>();
    try {
        final LsEntrySelector filter = new LsEntrySelector() {

            @Override
            public int select(final LsEntry entry) {
                final String entryFilename = entry.getFilename();
                // files regardless of ignoring dot files
                if (entryFilename.equals(".") || entryFilename.equals("..")) {
                    return LsEntrySelector.CONTINUE;
                }
                // ignoring them
                if (ignoreDottedFiles && entryFilename.startsWith(".")) {
                    return LsEntrySelector.CONTINUE;
                }
                // if is a directory and we're supposed to recurse
                if (recurse && entry.getAttrs().isDir()) {
                    subDirs.add(entry);
                    return LsEntrySelector.CONTINUE;
                }
                // FILE_FILTER_REGEX - then let's add it
                if (!entry.getAttrs().isDir() && !entry.getAttrs().isLink() && isPathMatch) {
                    if (pattern == null || pattern.matcher(entryFilename).matches()) {
                        listing.add(newFileInfo(entry, path));
                    }
                }
                if (listing.size() >= maxResults) {
                    return LsEntrySelector.BREAK;
                }
                return LsEntrySelector.CONTINUE;
            }
        };
        if (path == null || path.trim().isEmpty()) {
            sftp.ls(".", filter);
        } else {
            sftp.ls(path, filter);
        }
    } catch (final SftpException e) {
        final String pathDesc = path == null ? "current directory" : path;
        switch(e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException("Could not perform listing on " + pathDesc + " because could not find the file on the remote server");
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Could not perform listing on " + pathDesc + " due to insufficient permissions");
            default:
                throw new IOException("Failed to obtain file listing for " + pathDesc, e);
        }
    }
    for (final LsEntry entry : subDirs) {
        final String entryFilename = entry.getFilename();
        final File newFullPath = new File(path, entryFilename);
        final String newFullForwardPath = newFullPath.getPath().replace("\\", "/");
        try {
            getListing(newFullForwardPath, depth + 1, maxResults, listing);
        } catch (final IOException e) {
            logger.error("Unable to get listing from " + newFullForwardPath + "; skipping this subdirectory", e);
        }
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ChannelSftp(com.jcraft.jsch.ChannelSftp) LsEntrySelector(com.jcraft.jsch.ChannelSftp.LsEntrySelector) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) FlowFile(org.apache.nifi.flowfile.FlowFile) File(java.io.File)

Aggregations

LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 LsEntrySelector (com.jcraft.jsch.ChannelSftp.LsEntrySelector)2 SftpException (com.jcraft.jsch.SftpException)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)1 SftpATTRS (com.jcraft.jsch.SftpATTRS)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 TreeSet (java.util.TreeSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1