Search in sources :

Example 1 with ForbiddenSymlinkException

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

the class IndexDatabase method getDefinitions.

/**
 * Get the latest definitions for a file from the index.
 *
 * @param file the file whose definitions to find
 * @return definitions for the file, or {@code null} if they 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
 * @throws ClassNotFoundException if the class for the stored definitions
 * instance cannot be found
 */
public static Definitions getDefinitions(File file) throws IOException, ParseException, ClassNotFoundException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path;
    try {
        path = env.getPathRelativeToSourceRoot(file);
    } catch (ForbiddenSymlinkException e) {
        LOGGER.log(Level.FINER, e.getMessage());
        return null;
    }
    // sanitize windows path delimiters
    // in order not to conflict with Lucene escape character
    path = path.replace("\\", "/");
    IndexReader ireader = getIndexReader(path);
    if (ireader == null) {
        // No index, no definitions...
        return null;
    }
    try {
        Query q = new QueryBuilder().setPath(path).build();
        IndexSearcher searcher = new IndexSearcher(ireader);
        TopDocs top = searcher.search(q, 1);
        if (top.totalHits == 0) {
            // No hits, no definitions...
            return null;
        }
        Document doc = searcher.doc(top.scoreDocs[0].doc);
        String foundPath = doc.get(QueryBuilder.PATH);
        // Only use the definitions if we found an exact match.
        if (path.equals(foundPath)) {
            IndexableField tags = doc.getField(QueryBuilder.TAGS);
            if (tags != null) {
                return Definitions.deserialize(tags.binaryValue().bytes);
            }
        }
    } finally {
        ireader.close();
    }
    // Didn't find any definitions.
    return null;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Document(org.apache.lucene.document.Document)

Example 2 with ForbiddenSymlinkException

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

the class IndexDatabase method addFile.

/**
 * Add a file to the Lucene index (and generate a xref file)
 *
 * @param file The file to add
 * @param path The path to the file (from source root)
 * @param ctags a defined instance to use (only if its binary is not null)
 * @throws java.io.IOException if an error occurs
 * @throws InterruptedException if a timeout occurs
 */
private void addFile(File file, String path, Ctags ctags) throws IOException, InterruptedException {
    FileAnalyzer fa;
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        fa = AnalyzerGuru.getAnalyzer(in, path);
    }
    for (IndexChangedListener listener : listeners) {
        listener.fileAdd(path, fa.getClass().getSimpleName());
    }
    ctags.setTabSize(project != null ? project.getTabSize() : 0);
    if (ctags.getBinary() != null)
        fa.setCtags(ctags);
    fa.setProject(Project.getProject(path));
    fa.setScopesEnabled(RuntimeEnvironment.getInstance().isScopesEnabled());
    fa.setFoldingEnabled(RuntimeEnvironment.getInstance().isFoldingEnabled());
    Document doc = new Document();
    try (Writer xrefOut = getXrefWriter(fa, path)) {
        analyzerGuru.populateDocument(doc, file, path, fa, xrefOut);
    } catch (InterruptedException e) {
        LOGGER.log(Level.WARNING, "File ''{0}'' interrupted--{1}", new Object[] { path, e.getMessage() });
        cleanupResources(doc);
        throw e;
    } catch (ForbiddenSymlinkException e) {
        LOGGER.log(Level.FINER, e.getMessage());
        cleanupResources(doc);
        return;
    } catch (Exception e) {
        LOGGER.log(Level.INFO, "Skipped file ''{0}'' because the analyzer didn''t " + "understand it.", path);
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "Exception from analyzer " + fa.getClass().getName(), e);
        }
        cleanupResources(doc);
        return;
    } finally {
        fa.setCtags(null);
    }
    try {
        writer.addDocument(doc);
    } catch (Throwable t) {
        cleanupResources(doc);
        throw t;
    }
    setDirty();
    for (IndexChangedListener listener : listeners) {
        listener.fileAdded(path, fa.getClass().getSimpleName());
    }
}
Also used : FileAnalyzer(org.opensolaris.opengrok.analysis.FileAnalyzer) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(org.apache.lucene.document.Document) FileInputStream(java.io.FileInputStream) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) HistoryException(org.opensolaris.opengrok.history.HistoryException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.lucene.queryparser.classic.ParseException) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ForbiddenSymlinkException

use of org.opensolaris.opengrok.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 {
    DateFormat df = repository.getDateFormat();
    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 = df.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
                    }
                }
                break;
            default:
                LOGGER.log(Level.WARNING, "Unknown parser state: {0}", state);
                break;
        }
    }
    if (entry != null && state > 2) {
        entries.add(entry);
    }
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) InputStreamReader(java.io.InputStreamReader) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) IOException(java.io.IOException) Date(java.util.Date) DateFormat(java.text.DateFormat) BufferedReader(java.io.BufferedReader) ParseException(java.text.ParseException) File(java.io.File)

Example 4 with ForbiddenSymlinkException

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

the class FileHistoryCache method clearFile.

@Override
public void clearFile(String path) {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    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 : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) File(java.io.File)

Example 5 with ForbiddenSymlinkException

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

the class MercurialHistoryParser 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();
    DateFormat df = repository.getDateFormat();
    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    entries = new ArrayList<HistoryEntry>();
    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 = new Date();
            try {
                date = df.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).
                    }
                }
            }
        } else if (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.contains(move[0])) {
                    renamedFiles.add(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.opensolaris.opengrok.configuration.RuntimeEnvironment) InputStreamReader(java.io.InputStreamReader) ForbiddenSymlinkException(org.opensolaris.opengrok.util.ForbiddenSymlinkException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Date(java.util.Date) DateFormat(java.text.DateFormat) BufferedReader(java.io.BufferedReader) ParseException(java.text.ParseException) File(java.io.File)

Aggregations

ForbiddenSymlinkException (org.opensolaris.opengrok.util.ForbiddenSymlinkException)16 File (java.io.File)14 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)11 IOException (java.io.IOException)10 FileNotFoundException (java.io.FileNotFoundException)4 DateFormat (java.text.DateFormat)4 ParseException (java.text.ParseException)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Document (org.apache.lucene.document.Document)2 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedWriter (java.io.BufferedWriter)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1