Search in sources :

Example 1 with PathAccepter

use of org.opengrok.indexer.configuration.PathAccepter in project OpenGrok by OpenGrok.

the class DirectoryListing method getSimplifiedPath.

/**
 * Traverse directory until subdirectory with more than one item
 * (other than directory) or end of path is reached.
 * @param dir directory to traverse
 * @return string representing path with empty directories or the name of the directory
 */
private static String getSimplifiedPath(File dir) {
    String[] files = dir.list();
    // Permissions can prevent getting list of items in the directory.
    if (files == null) {
        return dir.getName();
    }
    if (files.length == 1) {
        File entry = new File(dir, files[0]);
        PathAccepter pathAccepter = RuntimeEnvironment.getInstance().getPathAccepter();
        if (pathAccepter.accept(entry) && entry.isDirectory()) {
            return (dir.getName() + "/" + getSimplifiedPath(entry));
        }
    }
    return dir.getName();
}
Also used : PathAccepter(org.opengrok.indexer.configuration.PathAccepter) File(java.io.File)

Example 2 with PathAccepter

use of org.opengrok.indexer.configuration.PathAccepter in project OpenGrok by OpenGrok.

the class HistoryGuru method addRepositories.

/**
 * recursively search for repositories with a depth limit, add those found
 * to the internally used map.
 *
 * @param files list of files to check if they contain a repository
 * @param allowedNesting number of levels of nested repos to allow
 * @param depth current depth - using global scanningDepth - one can limit
 * this to improve scanning performance
 * @param isNested a value indicating if a parent {@link Repository} was
 * already found above the {@code files}
 * @return collection of added repositories
 */
private Collection<RepositoryInfo> addRepositories(File[] files, int allowedNesting, int depth, boolean isNested) {
    List<RepositoryInfo> repoList = new ArrayList<>();
    PathAccepter pathAccepter = env.getPathAccepter();
    for (File file : files) {
        if (!file.isDirectory()) {
            continue;
        }
        String path;
        try {
            path = file.getCanonicalPath();
            Repository repository = null;
            try {
                repository = RepositoryFactory.getRepository(file, CommandTimeoutType.INDEXER, isNested);
            } catch (InstantiationException | NoSuchMethodException | InvocationTargetException e) {
                LOGGER.log(Level.WARNING, "Could not create repository for '" + file + "', could not instantiate the repository.", e);
            } catch (IllegalAccessException iae) {
                LOGGER.log(Level.WARNING, "Could not create repository for '" + file + "', missing access rights.", iae);
                continue;
            } catch (ForbiddenSymlinkException e) {
                LOGGER.log(Level.WARNING, "Could not create repository for ''{0}'': {1}", new Object[] { file, e.getMessage() });
                continue;
            }
            if (repository == null) {
                if (depth > env.getScanningDepth()) {
                    // we reached our search max depth, skip looking through the children
                    continue;
                }
                // Not a repository, search its sub-dirs.
                if (pathAccepter.accept(file)) {
                    File[] subFiles = file.listFiles();
                    if (subFiles == null) {
                        LOGGER.log(Level.WARNING, "Failed to get sub directories for ''{0}'', " + "check access permissions.", file.getAbsolutePath());
                    } else {
                        // Recursive call to scan next depth
                        repoList.addAll(addRepositories(subFiles, allowedNesting, depth + 1, isNested));
                    }
                }
            } else {
                LOGGER.log(Level.CONFIG, "Adding <{0}> repository: <{1}>", new Object[] { repository.getClass().getName(), path });
                repoList.add(new RepositoryInfo(repository));
                putRepository(repository);
                if (allowedNesting > 0 && repository.supportsSubRepositories()) {
                    File[] subFiles = file.listFiles();
                    if (subFiles == null) {
                        LOGGER.log(Level.WARNING, "Failed to get sub directories for ''{0}'', check access permissions.", file.getAbsolutePath());
                    } else if (depth <= env.getScanningDepth()) {
                        // Search down to a limit -- if not: too much
                        // stat'ing for huge Mercurial repositories
                        repoList.addAll(addRepositories(subFiles, allowedNesting - 1, depth + 1, true));
                    }
                }
            }
        } catch (IOException exp) {
            LOGGER.log(Level.WARNING, "Failed to get canonical path for {0}: {1}", new Object[] { file.getAbsolutePath(), exp.getMessage() });
            LOGGER.log(Level.WARNING, "Repository will be ignored...", exp);
        }
    }
    return repoList;
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PathAccepter(org.opengrok.indexer.configuration.PathAccepter) File(java.io.File)

Example 3 with PathAccepter

use of org.opengrok.indexer.configuration.PathAccepter in project OpenGrok by OpenGrok.

the class DirectoryListing method extraListTo.

/**
 * Write a HTML-ized listing of the given directory to the given destination.
 *
 * @param contextPath path used for link prefixes
 * @param dir the directory to list
 * @param out write destination
 * @param path virtual path of the directory (usually the path name of
 *  <var>dir</var> with the source root directory stripped off).
 * @param entries basenames of potential children of the directory to list,
 *  but filtered by {@link PathAccepter}.
 * @return a possible empty list of README files included in the written
 *  listing.
 * @throws IOException when cannot write to the {@code out} parameter
 * @throws HistoryException when failed to get last modified time for files in directory
 */
public List<String> extraListTo(String contextPath, File dir, Writer out, String path, List<DirectoryEntry> entries) throws IOException, HistoryException {
    // TODO this belongs to a jsp, not here
    ArrayList<String> readMes = new ArrayList<>();
    int offset = -1;
    EftarFileReader.FNode parentFNode = null;
    if (desc != null) {
        try {
            parentFNode = desc.getNode(path);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, String.format("cannot get Eftar node for path ''%s''", path), e);
        }
        if (parentFNode != null) {
            offset = parentFNode.getChildOffset();
        }
    }
    out.write("<table id=\"dirlist\" class=\"tablesorter tablesorter-default\">\n");
    out.write("<thead>\n");
    out.write("<tr>\n");
    out.write("<th class=\"sorter-false\"></th>\n");
    out.write("<th>Name</th>\n");
    out.write("<th class=\"sorter-false\"></th>\n");
    out.write("<th class=\"sort-dates\">Date</th>\n");
    out.write("<th class=\"sort-groksizes\">Size</th>\n");
    out.write("<th>#Lines</th>\n");
    out.write("<th>LOC</th>\n");
    if (offset > 0) {
        out.write("<th><samp>Description</samp></th>\n");
    }
    out.write("</tr>\n</thead>\n<tbody>\n");
    PathAccepter pathAccepter = RuntimeEnvironment.getInstance().getPathAccepter();
    Format dateFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
    // Print the '..' entry even for empty directories.
    if (path.length() != 0) {
        out.write("<tr><td><p class=\"'r'\"/></td><td>");
        out.write("<b><a href=\"..\">..</a></b></td><td></td>");
        printDateSize(out, dir.getParentFile(), null, dateFormatter);
        out.write("</tr>\n");
    }
    Map<String, Date> modTimes = HistoryGuru.getInstance().getLastModifiedTimes(dir);
    if (entries != null) {
        for (DirectoryEntry entry : entries) {
            File child = entry.getFile();
            if (!pathAccepter.accept(child)) {
                continue;
            }
            String filename = child.getName();
            String filenameLower = filename.toLowerCase(Locale.ROOT);
            if (filenameLower.startsWith("readme") || filenameLower.endsWith("readme")) {
                readMes.add(filename);
            }
            boolean isDir = child.isDirectory();
            out.write("<tr><td>");
            out.write("<p class=\"");
            out.write(isDir ? 'r' : 'p');
            out.write("\"/>");
            out.write("</td><td><a href=\"");
            if (isDir) {
                String longpath = getSimplifiedPath(child);
                out.write(Util.uriEncodePath(longpath));
                out.write("/\"><b>");
                int idx;
                if ((idx = longpath.lastIndexOf('/')) > 0) {
                    out.write("<span class=\"simplified-path\">");
                    out.write(longpath.substring(0, idx + 1));
                    out.write("</span>");
                    out.write(longpath.substring(idx + 1));
                } else {
                    out.write(longpath);
                }
                out.write("</b></a>/");
            } else {
                out.write(Util.uriEncodePath(filename));
                out.write("\">");
                out.write(filename);
                out.write("</a>");
            }
            out.write("</td>");
            Util.writeHAD(out, contextPath, path + filename, isDir);
            printDateSize(out, child, modTimes.get(filename), dateFormatter);
            printNumlines(out, entry, isDir);
            printLoc(out, entry, isDir);
            if (offset > 0) {
                String briefDesc = desc.getChildTag(parentFNode, filename);
                if (briefDesc == null) {
                    out.write("<td/>");
                } else {
                    out.write("<td>");
                    out.write(briefDesc);
                    out.write("</td>");
                }
            }
            out.write("</tr>\n");
        }
    }
    out.write("</tbody>\n</table>");
    return readMes;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DirectoryEntry(org.opengrok.indexer.search.DirectoryEntry) EftarFileReader(org.opengrok.indexer.web.EftarFileReader) Date(java.util.Date) PathAccepter(org.opengrok.indexer.configuration.PathAccepter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Aggregations

File (java.io.File)3 PathAccepter (org.opengrok.indexer.configuration.PathAccepter)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 DirectoryEntry (org.opengrok.indexer.search.DirectoryEntry)1 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)1 EftarFileReader (org.opengrok.indexer.web.EftarFileReader)1