Search in sources :

Example 6 with ForbiddenSymlinkException

use of org.opengrok.indexer.util.ForbiddenSymlinkException in project OpenGrok by OpenGrok.

the class MercurialHistoryParser method processStream.

/**
 * Process the output from the {@code hg log} command and collect
 * {@link HistoryEntry} elements.
 *
 * @param input The output from the process
 * @throws java.io.IOException If an error occurs while reading the stream
 */
@Override
public void processStream(InputStream input) throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    entries = new ArrayList<>();
    String s;
    HistoryEntry entry = null;
    while ((s = in.readLine()) != null) {
        if (s.startsWith(MercurialRepository.CHANGESET)) {
            entry = new HistoryEntry();
            entries.add(entry);
            entry.setActive(true);
            entry.setRevision(s.substring(MercurialRepository.CHANGESET.length()).trim());
        } else if (s.startsWith(MercurialRepository.USER) && entry != null) {
            entry.setAuthor(s.substring(MercurialRepository.USER.length()).trim());
        } else if (s.startsWith(MercurialRepository.DATE) && entry != null) {
            Date date;
            try {
                date = repository.parse(s.substring(MercurialRepository.DATE.length()).trim());
            } catch (ParseException pe) {
                // 
                throw new IOException("Could not parse date: " + s, pe);
            }
            entry.setDate(date);
        } else if (s.startsWith(MercurialRepository.FILES) && entry != null) {
            String[] strings = s.split(" ");
            for (int ii = 1; ii < strings.length; ++ii) {
                if (strings[ii].length() > 0) {
                    File f = new File(mydir, strings[ii]);
                    try {
                        String path = env.getPathRelativeToSourceRoot(f);
                        entry.addFile(path.intern());
                    } catch (ForbiddenSymlinkException e) {
                        LOGGER.log(Level.FINER, e.getMessage());
                    // ignore
                    } catch (FileNotFoundException e) {
                    // NOPMD
                    // If the file is not located under the source root,
                    // ignore it (bug #11664).
                    } catch (InvalidPathException e) {
                        LOGGER.log(Level.WARNING, e.getMessage());
                    }
                }
            }
        } else if (repository.isHandleRenamedFiles() && s.startsWith(MercurialRepository.FILE_COPIES) && entry != null && isDir) {
            /*
                 * 'file_copies:' should be present only for directories but
                 * we use isDir to be on the safe side.
                 */
            s = s.replaceFirst(MercurialRepository.FILE_COPIES, "");
            String[] splitArray = s.split("\\)");
            for (String part : splitArray) {
                /*
                      * This will fail for file names containing ' ('.
                      */
                String[] move = part.split(" \\(");
                File f = new File(mydir + move[0]);
                if (!move[0].isEmpty() && f.exists()) {
                    renamedFiles.add(repository.getDirectoryNameRelative() + File.separator + move[0]);
                }
            }
        } else if (s.startsWith(DESC_PREFIX) && entry != null) {
            entry.setMessage(decodeDescription(s));
        } else if (s.equals(MercurialRepository.END_OF_ENTRY) && entry != null) {
            entry = null;
        } else if (s.length() > 0) {
            LOGGER.log(Level.WARNING, "Invalid/unexpected output {0} from hg log for repo {1}", new Object[] { s, repository.getDirectoryName() });
        }
    }
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) InputStreamReader(java.io.InputStreamReader) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Date(java.util.Date) InvalidPathException(java.nio.file.InvalidPathException) BufferedReader(java.io.BufferedReader) ParseException(java.text.ParseException) File(java.io.File)

Example 7 with ForbiddenSymlinkException

use of org.opengrok.indexer.util.ForbiddenSymlinkException in project OpenGrok by OpenGrok.

the class BazaarHistoryParser method processStream.

/**
 * Process the output from the log command and insert the HistoryEntries
 * into the history field.
 *
 * @param input The output from the process
 * @throws java.io.IOException If an error occurs while reading the stream
 */
@Override
public void processStream(InputStream input) throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    String s;
    HistoryEntry entry = null;
    int state = 0;
    while ((s = in.readLine()) != null) {
        if ("------------------------------------------------------------".equals(s)) {
            if (entry != null && state > 2) {
                entries.add(entry);
            }
            entry = new HistoryEntry();
            entry.setActive(true);
            state = 0;
            continue;
        }
        switch(state) {
            case 0:
                // First, go on until revno is found.
                if (s.startsWith("revno:")) {
                    String[] rev = s.substring("revno:".length()).trim().split(" ");
                    entry.setRevision(rev[0]);
                    ++state;
                }
                break;
            case 1:
                // Then, look for committer.
                if (s.startsWith("committer:")) {
                    entry.setAuthor(s.substring("committer:".length()).trim());
                    ++state;
                }
                break;
            case 2:
                // And then, look for timestamp.
                if (s.startsWith("timestamp:")) {
                    try {
                        Date date = repository.parse(s.substring("timestamp:".length()).trim());
                        entry.setDate(date);
                    } catch (ParseException e) {
                        // 
                        throw new IOException("Failed to parse history timestamp:" + s, e);
                    }
                    ++state;
                }
                break;
            case 3:
                // message.
                if (s.startsWith("modified:") || s.startsWith("added:") || s.startsWith("removed:")) {
                    ++state;
                } else if (s.startsWith("  ")) {
                    // Commit messages returned by bzr log -v are prefixed
                    // with two blanks.
                    entry.appendMessage(s.substring(2));
                }
                break;
            case 4:
                // files. (Except the labels.)
                if (!(s.startsWith("modified:") || s.startsWith("added:") || s.startsWith("removed:"))) {
                    // The list of files is prefixed with blanks.
                    s = s.trim();
                    int idx = s.indexOf(" => ");
                    if (idx != -1) {
                        s = s.substring(idx + 4);
                    }
                    File f = new File(myDir, s);
                    try {
                        String name = env.getPathRelativeToSourceRoot(f);
                        entry.addFile(name.intern());
                    } catch (ForbiddenSymlinkException e) {
                        LOGGER.log(Level.FINER, e.getMessage());
                    // ignored
                    } catch (InvalidPathException e) {
                        LOGGER.log(Level.WARNING, e.getMessage());
                    }
                }
                break;
            default:
                LOGGER.log(Level.WARNING, "Unknown parser state: {0}", state);
                break;
        }
    }
    if (entry != null && state > 2) {
        entries.add(entry);
    }
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) InputStreamReader(java.io.InputStreamReader) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) IOException(java.io.IOException) Date(java.util.Date) InvalidPathException(java.nio.file.InvalidPathException) BufferedReader(java.io.BufferedReader) ParseException(java.text.ParseException) File(java.io.File)

Example 8 with ForbiddenSymlinkException

use of org.opengrok.indexer.util.ForbiddenSymlinkException in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method generateProjectRepositoriesMap.

/**
 * Generate a TreeMap of projects with corresponding repository information.
 * <p>
 * Project with some repository information is considered as a repository
 * otherwise it is just a simple project.
 */
private void generateProjectRepositoriesMap() throws IOException {
    repository_map.clear();
    for (RepositoryInfo r : getRepositories()) {
        Project proj;
        String repoPath;
        try {
            repoPath = getPathRelativeToSourceRoot(new File(r.getDirectoryName()));
        } catch (ForbiddenSymlinkException e) {
            LOGGER.log(Level.FINER, e.getMessage());
            continue;
        }
        if ((proj = Project.getProject(repoPath)) != null) {
            List<RepositoryInfo> values = repository_map.computeIfAbsent(proj, k -> new ArrayList<>());
            // the map is held under the lock because the next call to
            // values.add(r) which should not be called from multiple threads at the same time
            values.add(r);
        }
    }
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) File(java.io.File)

Example 9 with ForbiddenSymlinkException

use of org.opengrok.indexer.util.ForbiddenSymlinkException in project OpenGrok by OpenGrok.

the class ProjectsController method deleteHistoryCacheWorkHorse.

private void deleteHistoryCacheWorkHorse(String projectName, boolean clearHistoryGuru) {
    Project project = disableProject(projectName);
    LOGGER.log(Level.INFO, "deleting history cache for project {0}", projectName);
    List<RepositoryInfo> repos = env.getProjectRepositoriesMap().get(project);
    if (repos == null || repos.isEmpty()) {
        LOGGER.log(Level.INFO, "history cache for project {0} is not present", projectName);
        return;
    }
    // Delete history cache data.
    HistoryGuru guru = HistoryGuru.getInstance();
    guru.removeCache(repos.stream().map(x -> {
        try {
            return env.getPathRelativeToSourceRoot(new File((x).getDirectoryName()));
        } catch (ForbiddenSymlinkException e) {
            LOGGER.log(Level.FINER, e.getMessage());
            return "";
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "cannot remove files for repository {0}", x.getDirectoryName());
            // {@code removeCache()} will return nothing.
            return "";
        }
    }).filter(x -> !x.isEmpty()).collect(Collectors.toSet()), clearHistoryGuru);
}
Also used : Repository(org.opengrok.indexer.history.Repository) SuggesterService(org.opengrok.web.api.v1.suggester.provider.service.SuggesterService) Context(jakarta.ws.rs.core.Context) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) CompletableFuture(java.util.concurrent.CompletableFuture) GET(jakarta.ws.rs.GET) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) PUT(jakarta.ws.rs.PUT) WebApplicationException(jakarta.ws.rs.WebApplicationException) TreeSet(java.util.TreeSet) ApiTask(org.opengrok.web.api.ApiTask) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Path(jakarta.ws.rs.Path) ClassUtil(org.opengrok.indexer.util.ClassUtil) Response(jakarta.ws.rs.core.Response) RepositoryFactory.getRepository(org.opengrok.indexer.history.RepositoryFactory.getRepository) Map(java.util.Map) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) Laundromat(org.opengrok.indexer.web.Laundromat) Produces(jakarta.ws.rs.Produces) CommandTimeoutType(org.opengrok.indexer.configuration.CommandTimeoutType) Consumes(jakarta.ws.rs.Consumes) NotFoundException(jakarta.ws.rs.NotFoundException) POST(jakarta.ws.rs.POST) IOUtils(org.opengrok.indexer.util.IOUtils) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) IndexDatabase(org.opengrok.indexer.index.IndexDatabase) File(java.io.File) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Paths(java.nio.file.Paths) LoggerFactory(org.opengrok.indexer.logger.LoggerFactory) DELETE(jakarta.ws.rs.DELETE) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Inject(jakarta.inject.Inject) PathParam(jakarta.ws.rs.PathParam) Collections(java.util.Collections) Project(org.opengrok.indexer.configuration.Project) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) IOException(java.io.IOException) File(java.io.File)

Example 10 with ForbiddenSymlinkException

use of org.opengrok.indexer.util.ForbiddenSymlinkException in project OpenGrok by OpenGrok.

the class SearchHelper method searchSingle.

/**
 * Searches for a document for a single file from the index.
 * @param file the file whose definitions to find
 * @return {@link ScoreDoc#doc} or -1 if it could not be found
 * @throws IOException if an error happens when accessing the index
 * @throws ParseException if an error happens when building the Lucene query
 */
public int searchSingle(File file) throws IOException, ParseException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path;
    try {
        path = env.getPathRelativeToSourceRoot(file);
    } catch (ForbiddenSymlinkException e) {
        LOGGER.log(Level.FINER, e.getMessage());
        return -1;
    }
    // sanitize windows path delimiters
    // in order not to conflict with Lucene escape character
    path = path.replace("\\", "/");
    QueryBuilder singleBuilder = new QueryBuilder();
    if (builder != null) {
        singleBuilder.reset(builder);
    }
    query = singleBuilder.setPath(path).build();
    TopDocs top = searcher.search(query, 1);
    if (top.totalHits.value == 0) {
        return -1;
    }
    int docID = top.scoreDocs[0].doc;
    Document doc = searcher.doc(docID);
    String foundPath = doc.get(QueryBuilder.PATH);
    // Only use the result if PATH matches exactly.
    if (!path.equals(foundPath)) {
        return -1;
    }
    return docID;
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) Document(org.apache.lucene.document.Document)

Aggregations

ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)17 File (java.io.File)14 IOException (java.io.IOException)8 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)7 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 InvalidPathException (java.nio.file.InvalidPathException)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 FileNotFoundException (java.io.FileNotFoundException)2 Path (java.nio.file.Path)2 TreeSet (java.util.TreeSet)2 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)2 Statistics (org.opengrok.indexer.util.Statistics)2 Inject (jakarta.inject.Inject)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 Consumes (jakarta.ws.rs.Consumes)1 DELETE (jakarta.ws.rs.DELETE)1 GET (jakarta.ws.rs.GET)1