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() });
}
}
}
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);
}
}
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);
}
}
}
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);
}
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;
}
Aggregations