Search in sources :

Example 1 with IndexedSymlink

use of org.opengrok.indexer.index.IndexedSymlink in project OpenGrok by OpenGrok.

the class SearchHelper method getPrimeRelativePath.

/**
 * Determines if there is a prime equivalent to {@code relativePath}
 * according to indexed symlinks and translate (or not) accordingly.
 * @param project the project name or empty string if projects are not used
 * @param relativePath an OpenGrok-style (i.e. starting with a file
 *                     separator) relative path
 * @return a prime relative path or just {@code relativePath} if no prime
 * is matched
 */
public String getPrimeRelativePath(String project, String relativePath) throws IOException, ForbiddenSymlinkException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String sourceRoot = env.getSourceRootPath();
    if (sourceRoot == null) {
        throw new IllegalStateException("sourceRoot is not defined");
    }
    File absolute = new File(sourceRoot + relativePath);
    ensureSettingsHelper();
    settingsHelper.getSettings(project);
    Map<String, IndexedSymlink> indexedSymlinks = settingsHelper.getSymlinks(project);
    if (indexedSymlinks != null) {
        String canonical = absolute.getCanonicalFile().getPath();
        for (IndexedSymlink entry : indexedSymlinks.values()) {
            if (canonical.equals(entry.getCanonical())) {
                if (absolute.getPath().equals(entry.getAbsolute())) {
                    return relativePath;
                }
                Path newAbsolute = Paths.get(entry.getAbsolute());
                return env.getPathRelativeToSourceRoot(newAbsolute.toFile());
            } else if (canonical.startsWith(entry.getCanonicalSeparated())) {
                Path newAbsolute = Paths.get(entry.getAbsolute(), canonical.substring(entry.getCanonicalSeparated().length()));
                return env.getPathRelativeToSourceRoot(newAbsolute.toFile());
            }
        }
    }
    return relativePath;
}
Also used : Path(java.nio.file.Path) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) File(java.io.File) IndexedSymlink(org.opengrok.indexer.index.IndexedSymlink)

Aggregations

File (java.io.File)1 Path (java.nio.file.Path)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 IndexedSymlink (org.opengrok.indexer.index.IndexedSymlink)1