Skip to content

Commit d6291e9

Browse files
committed
Throw and handle ForbiddenSymlinkException from a few more public APIs
This quiets the noisy exceptions for repos that heavily use symlinks -- e.g., Homebrew/brew.
1 parent d05ab34 commit d6291e9

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

src/org/opensolaris/opengrok/analysis/AnalyzerGuru.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import org.opensolaris.opengrok.history.HistoryReader;
107107
import org.opensolaris.opengrok.logger.LoggerFactory;
108108
import org.opensolaris.opengrok.search.QueryBuilder;
109+
import org.opensolaris.opengrok.util.ForbiddenSymlinkException;
109110
import org.opensolaris.opengrok.util.IOUtils;
110111
import org.opensolaris.opengrok.web.Util;
111112

@@ -394,10 +395,12 @@ public static FileAnalyzer getAnalyzer(InputStream in, String file) throws IOExc
394395
* @param xrefOut Where to write the xref (possibly {@code null})
395396
* @throws IOException If an exception occurs while collecting the data
396397
* @throws InterruptedException if a timeout occurs
398+
* @throws ForbiddenSymlinkException if symbolic-link checking encounters
399+
* an ineligible link
397400
*/
398401
public void populateDocument(Document doc, File file, String path,
399402
FileAnalyzer fa, Writer xrefOut) throws IOException,
400-
InterruptedException {
403+
InterruptedException, ForbiddenSymlinkException {
401404

402405
String date = DateTools.timeToString(file.lastModified(),
403406
DateTools.Resolution.MILLISECOND);

src/org/opensolaris/opengrok/history/FileHistoryCache.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public boolean supportsRepository(Repository repository) {
191191
* @param file the file to find the cache for
192192
* @return file that might contain cached history for <code>file</code>
193193
*/
194-
private static File getCachedFile(File file) throws HistoryException {
194+
private static File getCachedFile(File file) throws HistoryException,
195+
ForbiddenSymlinkException {
195196
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
196197

197198
StringBuilder sb = new StringBuilder();
@@ -210,7 +211,7 @@ private static File getCachedFile(File file) throws HistoryException {
210211
sb.append(DIRECTORY_FILE_PREFIX);
211212
}
212213
sb.append(".gz");
213-
} catch (IOException|ForbiddenSymlinkException e) {
214+
} catch (IOException e) {
214215
throw new HistoryException("Failed to get path relative to " +
215216
"source root for " + file, e);
216217
}
@@ -343,7 +344,13 @@ private History mergeOldAndNewHistory(File cacheFile, History histNew, Repositor
343344
private void storeFile(History histNew, File file, Repository repo,
344345
boolean mergeHistory) throws HistoryException {
345346

346-
File cacheFile = getCachedFile(file);
347+
File cacheFile;
348+
try {
349+
cacheFile = getCachedFile(file);
350+
} catch (ForbiddenSymlinkException e) {
351+
LOGGER.log(Level.FINER, e.getMessage());
352+
return;
353+
}
347354
History history = histNew;
348355

349356
File dir = cacheFile.getParentFile();
@@ -504,7 +511,13 @@ public void store(History history, Repository repository)
504511
// mkdirs() if there are multiple renamed files from single directory
505512
// handled in parallel.
506513
for (final String file : renamed_map.keySet()) {
507-
File cache = getCachedFile(new File(env.getSourceRootPath() + file));
514+
File cache;
515+
try {
516+
cache = getCachedFile(new File(env.getSourceRootPath() + file));
517+
} catch (ForbiddenSymlinkException ex) {
518+
LOGGER.log(Level.FINER, ex.getMessage());
519+
continue;
520+
}
508521
File dir = cache.getParentFile();
509522

510523
if (!dir.isDirectory() && !dir.mkdirs()) {
@@ -546,7 +559,7 @@ public void run() {
546559

547560
@Override
548561
public History get(File file, Repository repository, boolean withFiles)
549-
throws HistoryException {
562+
throws HistoryException, ForbiddenSymlinkException {
550563
File cache = getCachedFile(file);
551564
if (isUpToDate(file, cache)) {
552565
try {
@@ -640,7 +653,12 @@ public boolean hasCacheForDirectory(File directory, Repository repository)
640653

641654
@Override
642655
public boolean hasCacheForFile(File file) throws HistoryException {
643-
return getCachedFile(file).exists();
656+
try {
657+
return getCachedFile(file).exists();
658+
} catch (ForbiddenSymlinkException ex) {
659+
LOGGER.log(Level.FINER, ex.getMessage());
660+
return false;
661+
}
644662
}
645663

646664
public String getRepositoryHistDataDirname(Repository repository) {
@@ -767,6 +785,9 @@ public void clearFile(String path) {
767785
File historyFile;
768786
try {
769787
historyFile = getCachedFile(new File(env.getSourceRootPath() + path));
788+
} catch (ForbiddenSymlinkException ex) {
789+
LOGGER.log(Level.FINER, ex.getMessage());
790+
return;
770791
} catch (HistoryException ex) {
771792
LOGGER.log(Level.WARNING, "cannot get history file for file " + path, ex);
772793
return;

src/org/opensolaris/opengrok/history/HistoryCache.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.util.Date;
2727
import java.util.Map;
28+
import org.opensolaris.opengrok.util.ForbiddenSymlinkException;
2829

2930
interface HistoryCache {
3031
/**
@@ -58,9 +59,11 @@ interface HistoryCache {
5859
* the implementation is allowed to skip the file list, but it doesn't
5960
* have to.
6061
* @throws HistoryException if the history cannot be fetched
62+
* @throws ForbiddenSymlinkException if symbolic-link checking encounters
63+
* an ineligible link
6164
*/
6265
History get(File file, Repository repository, boolean withFiles)
63-
throws HistoryException;
66+
throws HistoryException, ForbiddenSymlinkException;
6467

6568
/**
6669
* Store the history for a repository.

src/org/opensolaris/opengrok/history/HistoryGuru.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
5151
import org.opensolaris.opengrok.index.IgnoredNames;
5252
import org.opensolaris.opengrok.logger.LoggerFactory;
53+
import org.opensolaris.opengrok.util.ForbiddenSymlinkException;
5354
import org.opensolaris.opengrok.util.PathUtils;
5455
import org.opensolaris.opengrok.util.Statistics;
5556

@@ -244,7 +245,6 @@ public History getHistory(File file, boolean withFiles, boolean ui)
244245
final File dir = file.isDirectory() ? file : file.getParentFile();
245246
final Repository repo = getRepository(dir);
246247

247-
History history = null;
248248
RemoteSCM rscm = RuntimeEnvironment.getInstance().getRemoteScmSupported();
249249
boolean doRemote = (ui && (rscm == RemoteSCM.UIONLY))
250250
|| (rscm == RemoteSCM.ON)
@@ -254,13 +254,17 @@ public History getHistory(File file, boolean withFiles, boolean ui)
254254
&& (!repo.isRemote() || doRemote)) {
255255

256256
if (useCache() && historyCache.supportsRepository(repo)) {
257-
history = historyCache.get(file, repo, withFiles);
258-
} else {
259-
history = repo.getHistory(file);
257+
try {
258+
return historyCache.get(file, repo, withFiles);
259+
} catch (ForbiddenSymlinkException ex) {
260+
LOGGER.log(Level.FINER, ex.getMessage());
261+
return null;
262+
}
260263
}
264+
return repo.getHistory(file);
261265
}
262266

263-
return history;
267+
return null;
264268
}
265269

266270
/**

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@ private void addFile(File file, String path, Ctags ctags)
696696
new Object[]{path, e.getMessage()});
697697
cleanupResources(doc);
698698
throw e;
699+
} catch (ForbiddenSymlinkException e) {
700+
LOGGER.log(Level.FINER, e.getMessage());
701+
cleanupResources(doc);
702+
return;
699703
} catch (Exception e) {
700704
LOGGER.log(Level.INFO,
701705
"Skipped file ''{0}'' because the analyzer didn''t "

0 commit comments

Comments
 (0)