Skip to content

Commit

Permalink
CxxIssuesReportSensor: use realPath as part of path normalization
Browse files Browse the repository at this point in the history
resolves #1651 (?)
  • Loading branch information
ivangalkin committed Jan 3, 2019
1 parent 263f1b5 commit 53c813c
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
package org.sonar.cxx.sensors.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewIssue;
Expand Down Expand Up @@ -139,8 +144,25 @@ public InputFile getInputFileIfInProject(SensorContext sensorContext, String pat
if (notFoundFiles.contains(path)) {
return null;
}
final InputFile inputFile = sensorContext.fileSystem().inputFile(sensorContext.
fileSystem().predicates().hasPath(path));

final Path absolutePath = sensorContext.fileSystem().baseDir().toPath().resolve(path);
FilePredicate searchPredicate;
try {
// normalize path by means of Path::toRealPath in order to please the
// case-insensitive file systems. Don't follow symbolic links, because we
// might end-up in the location outside of the module's base directory...
final Path realPath = absolutePath.toRealPath(LinkOption.NOFOLLOW_LINKS);
searchPredicate = sensorContext.fileSystem().predicates().hasAbsolutePath(realPath.toString());
} catch (IOException e) {
// ...if impossible - fallback to the most generic search predicate
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to get the real path: module {}, baseDir {}, path {}", sensorContext.module().key(),
sensorContext.fileSystem().baseDir(), path);
}
searchPredicate = sensorContext.fileSystem().predicates().hasPath(path);
}

final InputFile inputFile = sensorContext.fileSystem().inputFile(searchPredicate);
if (inputFile == null) {
LOG.warn("Cannot find the file '{}' in module '{}' base dir '{}', skipping violations.",
path, sensorContext.module().key(), sensorContext.fileSystem().baseDir());
Expand Down

0 comments on commit 53c813c

Please sign in to comment.