Skip to content

Commit 2738be5

Browse files
committed
Move PathUtils.getPathRelativeToSourceRoot to RuntimeEnvironment
Resolves odd circular dependency in that PathUtils.getPathRelativeToSourceRoot() is only called from the RuntimeEnvironment.getPathRelativeToSourceRoot() wrapper, but PathUtils.getPathRelativeToSourceRoot was needing to get a RuntimeEnvironment.Instance() for "allowed symlinks".
1 parent 875ab81 commit 2738be5

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,32 @@ public void setSourceRoot(String sourceRoot) {
419419
* @return Path relative to source root
420420
* @throws IOException If an IO error occurs
421421
* @throws FileNotFoundException if the file is not relative to source root
422+
* or if {@code sourceRoot} is not defined
422423
* @throws ForbiddenSymlinkException if symbolic-link checking encounters
423424
* an ineligible link
424425
*/
425426
public String getPathRelativeToSourceRoot(File file)
426427
throws IOException, ForbiddenSymlinkException {
427-
return PathUtils.getPathRelativeToSourceRoot(file);
428+
String sourceRoot = getSourceRootPath();
429+
if (sourceRoot == null) {
430+
throw new FileNotFoundException("sourceRoot is not defined");
431+
}
432+
433+
String maybeRelPath = PathUtils.getRelativeToCanonical(file.getPath(),
434+
sourceRoot, getAllowedSymlinks());
435+
File maybeRelFile = new File(maybeRelPath);
436+
if (!maybeRelFile.isAbsolute()) {
437+
/*
438+
* N.b. OpenGrok has a weird convention that source-root "relative"
439+
* paths must start with a '/' as they are elsewhere directly
440+
* appended to getSourceRootPath() and also stored as such.
441+
*/
442+
maybeRelPath = File.separator + maybeRelPath;
443+
return maybeRelPath;
444+
}
445+
446+
throw new FileNotFoundException("Failed to resolve [" + file.getPath()
447+
+ "] relative to source root [" + sourceRoot + "]");
428448
}
429449

430450
/**
@@ -1456,8 +1476,7 @@ private void generateProjectRepositoriesMap() throws IOException {
14561476
Project proj;
14571477
String repoPath;
14581478
try {
1459-
repoPath = PathUtils.getPathRelativeToSourceRoot(
1460-
new File(r.getDirectoryName()));
1479+
repoPath = getPathRelativeToSourceRoot(new File(r.getDirectoryName()));
14611480
} catch (ForbiddenSymlinkException e) {
14621481
LOGGER.log(Level.FINER, e.getMessage());
14631482
continue;

opengrok-indexer/src/main/java/org/opengrok/indexer/util/PathUtils.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package org.opengrok.indexer.util;
2525

2626
import java.io.File;
27-
import java.io.FileNotFoundException;
2827
import java.io.IOException;
2928
import java.nio.file.Files;
3029
import java.nio.file.InvalidPathException;
@@ -35,7 +34,6 @@
3534
import java.util.logging.Level;
3635
import java.util.logging.Logger;
3736

38-
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3937
import org.opengrok.indexer.logger.LoggerFactory;
4038

4139
/**
@@ -183,44 +181,6 @@ private static boolean isAllowedSymlink(String canonicalFile,
183181
return false;
184182
}
185183

186-
/**
187-
* Returns a path relative to source root. This would just be a simple
188-
* substring operation, except we need to support symlinks outside the
189-
* source root.
190-
*
191-
* @param file A file to resolve
192-
* @return Path relative to source root
193-
* @throws IOException if an IO error occurs
194-
* @throws FileNotFoundException if the file is not relative to source root
195-
* @throws ForbiddenSymlinkException if symbolic-link checking encounters
196-
* an ineligible link
197-
* @throws InvalidPathException if the path cannot be decoded
198-
*/
199-
public static String getPathRelativeToSourceRoot(File file)
200-
throws IOException, ForbiddenSymlinkException, FileNotFoundException,
201-
InvalidPathException {
202-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
203-
String sourceRoot = env.getSourceRootPath();
204-
if (sourceRoot == null) {
205-
throw new FileNotFoundException("Source Root Not Found");
206-
}
207-
208-
String maybeRelPath = PathUtils.getRelativeToCanonical(file.getPath(),
209-
sourceRoot, env.getAllowedSymlinks());
210-
File maybeRelFile = new File(maybeRelPath);
211-
if (!maybeRelFile.isAbsolute()) {
212-
// N.b. OpenGrok has a weird convention that
213-
// source-root "relative" paths must start with a '/' as they are
214-
// elsewhere directly appended to env.getSourceRootPath() and also
215-
// stored as such.
216-
maybeRelPath = File.separator + maybeRelPath;
217-
return maybeRelPath;
218-
}
219-
220-
throw new FileNotFoundException("Failed to resolve [" + file.getPath()
221-
+ "] relative to source root [" + sourceRoot + "]");
222-
}
223-
224184
/** Private to enforce static. */
225185
private PathUtils() {
226186
}

0 commit comments

Comments
 (0)