Skip to content

Commit b231548

Browse files
author
Vladimir Kotal
committed
sanitize path
fixes #2315
1 parent 77933fd commit b231548

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ public void populateDocument(Document doc, File file, String path,
523523
FileAnalyzer fa, Writer xrefOut) throws IOException,
524524
InterruptedException, ForbiddenSymlinkException {
525525

526+
// Sanitize Windows path delimiters in order not to conflict with Lucene escape character
527+
// and also so the path appears as correctly formed URI in the search results.
528+
path = path.replace("\\", "/");
529+
526530
String date = DateTools.timeToString(file.lastModified(),
527531
DateTools.Resolution.MILLISECOND);
528532
doc.add(new Field(QueryBuilder.U, Util.path2uid(path, date),

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ public void setDirectoryName(File dir) {
157157
String originalPath = dir.getPath();
158158
try {
159159
path = PathUtils.getRelativeToCanonical(originalPath, rootPath);
160-
// N.b. OpenGrok has a weird convention that
161-
// `directoryNameRelative' must start with a '/', as it is
162-
// elsewhere directly appended to env.getSourceRootPath() and
163-
// also stored as such.
160+
// OpenGrok has a weird convention that directoryNameRelative must start with a path separator,
161+
// as it is elsewhere directly appended to env.getSourceRootPath() and also stored as such.
164162
if (!path.equals(originalPath)) {
165163
path = File.separator + path;
166164
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexDatabase.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,9 +1521,8 @@ public static Definitions getDefinitions(File file)
15211521
LOGGER.log(Level.FINER, e.getMessage());
15221522
return null;
15231523
}
1524-
//sanitize windows path delimiters
1525-
//in order not to conflict with Lucene escape character
1526-
path=path.replace("\\", "/");
1524+
// Sanitize Windows path delimiters in order not to conflict with Lucene escape character.
1525+
path = path.replace("\\", "/");
15271526

15281527
IndexReader ireader = getIndexReader(path);
15291528

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexDatabaseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
package org.opengrok.indexer.index;
2525

2626
import java.io.File;
27+
import java.io.IOException;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
2930
import java.util.TreeSet;
31+
32+
import org.apache.lucene.document.Document;
33+
import org.apache.lucene.search.ScoreDoc;
3034
import org.junit.AfterClass;
3135
import org.junit.Assert;
3236
import org.junit.BeforeClass;
@@ -44,6 +48,8 @@
4448
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4549
import org.opengrok.indexer.history.HistoryGuru;
4650
import org.opengrok.indexer.history.RepositoryFactory;
51+
import org.opengrok.indexer.search.QueryBuilder;
52+
import org.opengrok.indexer.search.SearchEngine;
4753
import org.opengrok.indexer.util.TestRepository;
4854

4955
/**
@@ -180,4 +186,24 @@ public void testCleanupAfterIndexRemoval() throws Exception {
180186
checkDataExistence(projectName + File.separator + fileName, false);
181187
Assert.assertEquals(origNumFiles - 1, idb.getNumFiles());
182188
}
189+
190+
/**
191+
* This is a test of {@code populateDocument} so it should be rather in {@code AnalyzerGuruTest}
192+
* however it lacks the pre-requisite indexing phase.
193+
* @throws IOException
194+
*/
195+
@Test
196+
public void testIndexPath() throws IOException {
197+
SearchEngine instance = new SearchEngine();
198+
// Use as broad search as possible.
199+
instance.setFile("c");
200+
instance.search();
201+
ScoreDoc[] scoredocs = instance.scoreDocs();
202+
assertTrue("need some search hits to perform the check",scoredocs.length > 0);
203+
for (ScoreDoc sd : scoredocs) {
204+
Document doc = instance.doc(sd.doc);
205+
assertFalse("PATH field should not contain backslash characters",
206+
doc.getField(QueryBuilder.PATH).stringValue().contains("\\"));
207+
}
208+
}
183209
}

0 commit comments

Comments
 (0)