Search in sources :

Example 1 with ForbiddenSymlinkException

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

the class FileHistoryCache method clearFile.

@Override
public void clearFile(String path) {
    File historyFile;
    try {
        historyFile = getCachedFile(new File(env.getSourceRootPath() + path));
    } catch (ForbiddenSymlinkException ex) {
        LOGGER.log(Level.FINER, ex.getMessage());
        return;
    } catch (HistoryException ex) {
        LOGGER.log(Level.WARNING, "cannot get history file for file " + path, ex);
        return;
    }
    File parent = historyFile.getParentFile();
    if (!historyFile.delete() && historyFile.exists()) {
        LOGGER.log(Level.WARNING, "Failed to remove obsolete history cache-file: {0}", historyFile.getAbsolutePath());
    }
    if (parent.delete()) {
        LOGGER.log(Level.FINE, "Removed empty history cache dir:{0}", parent.getAbsolutePath());
    }
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) File(java.io.File)

Example 2 with ForbiddenSymlinkException

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

the class FileHistoryCache method get.

@Override
public History get(File file, Repository repository, boolean withFiles, boolean fallback) throws HistoryException, ForbiddenSymlinkException {
    File cacheFile = getCachedFile(file);
    if (isUpToDate(file, cacheFile)) {
        try {
            if (fileHistoryCacheHits != null) {
                fileHistoryCacheHits.increment();
            }
            return readCache(cacheFile);
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, String.format("Error when reading cache file '%s'", cacheFile), e);
        }
    }
    if (fileHistoryCacheMisses != null) {
        fileHistoryCacheMisses.increment();
    }
    /*
         * Some mirrors of repositories which are capable of fetching history
         * for directories may contain lots of files untracked by given SCM.
         * For these it would be waste of time to get their history
         * since the history of all files in this repository should have been
         * fetched in the first phase of indexing.
         */
    if (isHistoryIndexDone() && repository.isHistoryEnabled() && repository.hasHistoryForDirectories() && !fallback) {
        return null;
    }
    if (!pathAccepter.accept(file)) {
        return null;
    }
    final History history;
    try {
        history = repository.getHistory(file);
    } catch (UnsupportedOperationException e) {
        // An example is a non-SCCS file somewhere in an SCCS-controlled workspace.
        return null;
    }
    return history;
}
Also used : File(java.io.File) NoSuchFileException(java.nio.file.NoSuchFileException) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) IOException(java.io.IOException)

Example 3 with ForbiddenSymlinkException

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

the class GitRepository method getHistory.

public History getHistory(File file, String sinceRevision, String tillRevision, Integer numCommits) throws HistoryException {
    if (numCommits != null && numCommits <= 0) {
        return null;
    }
    final List<HistoryEntry> entries = new ArrayList<>();
    final Set<String> renamedFiles = new HashSet<>();
    boolean isDirectory = file.isDirectory();
    try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(getDirectoryName());
        RevWalk walk = new RevWalk(repository)) {
        if (sinceRevision != null) {
            walk.markUninteresting(walk.lookupCommit(repository.resolve(sinceRevision)));
        }
        if (tillRevision != null) {
            walk.markStart(walk.lookupCommit(repository.resolve(tillRevision)));
        } else {
            walk.markStart(walk.parseCommit(repository.resolve(Constants.HEAD)));
        }
        String relativePath = RuntimeEnvironment.getInstance().getPathRelativeToSourceRoot(file);
        if (!getDirectoryNameRelative().equals(relativePath)) {
            if (isHandleRenamedFiles()) {
                Config config = repository.getConfig();
                config.setBoolean("diff", null, "renames", true);
                org.eclipse.jgit.diff.DiffConfig dc = config.get(org.eclipse.jgit.diff.DiffConfig.KEY);
                FollowFilter followFilter = FollowFilter.create(getGitFilePath(getRepoRelativePath(file)), dc);
                walk.setTreeFilter(followFilter);
            } else {
                walk.setTreeFilter(AndTreeFilter.create(PathFilter.create(getGitFilePath(getRepoRelativePath(file))), TreeFilter.ANY_DIFF));
            }
        }
        int num = 0;
        for (RevCommit commit : walk) {
            if (commit.getParentCount() > 1 && !isMergeCommitsEnabled()) {
                continue;
            }
            HistoryEntry historyEntry = new HistoryEntry(commit.getId().abbreviate(GIT_ABBREV_LEN).name(), commit.getAuthorIdent().getWhen(), commit.getAuthorIdent().getName() + " <" + commit.getAuthorIdent().getEmailAddress() + ">", commit.getFullMessage(), true);
            if (isDirectory) {
                SortedSet<String> files = new TreeSet<>();
                getFilesForCommit(renamedFiles, files, commit, repository);
                historyEntry.setFiles(files);
            }
            entries.add(historyEntry);
            if (numCommits != null && ++num >= numCommits) {
                break;
            }
        }
    } catch (IOException | ForbiddenSymlinkException e) {
        throw new HistoryException(String.format("failed to get history for ''%s''", file), e);
    }
    History result = new History(entries, renamedFiles);
    // because we know it :-)
    if (RuntimeEnvironment.getInstance().isTagsEnabled()) {
        assignTagsInHistory(result);
    }
    return result;
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) FollowFilter(org.eclipse.jgit.revwalk.FollowFilter) Config(org.eclipse.jgit.lib.Config) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeSet(java.util.TreeSet) Repository(org.eclipse.jgit.lib.Repository) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with ForbiddenSymlinkException

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

the class MonotoneHistoryParser method processStream.

/**
 * Process the output from the hg 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) {
        s = s.trim();
        // the minimum amount for maximum compatibility between monotone versions.
        if (s.startsWith("-----------------------------------------------------------------")) {
            if (entry != null && state > 2) {
                entries.add(entry);
            }
            entry = new HistoryEntry();
            entry.setActive(true);
            state = 0;
            continue;
        }
        switch(state) {
            case 0:
                if (s.startsWith("Revision:")) {
                    String rev = s.substring("Revision:".length()).trim();
                    entry.setRevision(rev);
                    ++state;
                }
                break;
            case 1:
                if (s.startsWith("Author:")) {
                    entry.setAuthor(s.substring("Author:".length()).trim());
                    ++state;
                }
                break;
            case 2:
                if (s.startsWith("Date:")) {
                    Date date = new Date();
                    try {
                        date = repository.parse(s.substring("date:".length()).trim());
                    } catch (ParseException pe) {
                        // 
                        throw new IOException("Could not parse date: " + s, pe);
                    }
                    entry.setDate(date);
                    ++state;
                }
                break;
            case 3:
                if (s.startsWith("Modified ") || s.startsWith("Added ") || s.startsWith("Deleted ")) {
                    ++state;
                } else if (s.equalsIgnoreCase("ChangeLog:")) {
                    state = 5;
                }
                break;
            case 4:
                if (s.startsWith("Modified ") || s.startsWith("Added ") || s.startsWith("Deleted ")) {
                    continue;
                } else if (s.equalsIgnoreCase("ChangeLog:")) {
                    state = 5;
                } else {
                    String[] files = s.split(" ");
                    for (String f : files) {
                        File file = new File(mydir, f);
                        try {
                            String path = env.getPathRelativeToSourceRoot(file);
                            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
                        } catch (InvalidPathException e) {
                            LOGGER.log(Level.WARNING, e.getMessage());
                        }
                    }
                }
                break;
            case 5:
                entry.appendMessage(s);
                break;
            default:
                LOGGER.warning("Unknown parser state: " + 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) 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 5 with ForbiddenSymlinkException

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

the class HistoryGuru method getHistory.

/**
 * Get the history for the specified file. The history cache is tried first, then the repository.
 *
 * @param file the file to get the history for
 * @param withFiles whether the returned history should contain a
 * list of files touched by each changeset (the file list may be skipped if false, but it doesn't have to)
 * @param ui called from the webapp
 * @return history for the file
 * @throws HistoryException on error when accessing the history
 */
public History getHistory(File file, boolean withFiles, boolean ui) throws HistoryException {
    final File dir = file.isDirectory() ? file : file.getParentFile();
    final Repository repository = getRepository(dir);
    if (!isRepoHistoryEligible(repository, file, ui)) {
        return null;
    }
    History history;
    try {
        history = getHistoryFromCache(file, repository, withFiles, true);
        if (history != null) {
            return history;
        }
    } catch (ForbiddenSymlinkException e) {
        LOGGER.log(Level.FINER, e.getMessage());
        return null;
    }
    return null;
}
Also used : ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) File(java.io.File)

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