diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-03-14 15:09:55 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-03-15 14:36:52 +0100 |
commit | 7b58f2b2b631ff3514c223dd0822c71ad8b3ee3b (patch) | |
tree | ce49453cfd84b24b51b41a9481189825dac3d762 | |
parent | 74e0acf70cb85d2c501e7cd5ea58dfd082ab72cd (diff) | |
download | sonarqube-7b58f2b2b631ff3514c223dd0822c71ad8b3ee3b.tar.gz sonarqube-7b58f2b2b631ff3514c223dd0822c71ad8b3ee3b.zip |
SONAR-6719 Wrong casing when indexing individual files lead to issue during scm blame
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java | 31 | ||||
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java | 7 |
2 files changed, 22 insertions, 16 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java index ad825731593..0ed036192b2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java @@ -20,7 +20,6 @@ package org.sonar.batch.scan.filesystem; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.BatchSide; @@ -42,6 +41,7 @@ import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; @@ -122,28 +122,29 @@ public class FileIndexer { } private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, InputFileBuilder inputFileBuilder, List<File> sources, InputFile.Type type) { - for (File dirOrFile : sources) { - if (dirOrFile.isDirectory()) { - try { + try { + for (File dirOrFile : sources) { + if (dirOrFile.isDirectory()) { indexDirectory(inputFileBuilder, fileSystem, progress, dirOrFile, type); - } catch (IOException e) { - throw new IllegalStateException("Failed to index files", e); + } else { + indexFile(inputFileBuilder, fileSystem, progress, dirOrFile.toPath(), type); } - } else { - indexFile(inputFileBuilder, fileSystem, progress, dirOrFile, type); } + } catch (IOException e) { + throw new IllegalStateException("Failed to index files", e); } } private void indexDirectory(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fileSystem, final Progress status, final File dirToIndex, final InputFile.Type type) throws IOException { - final Path absDir = dirToIndex.toPath().normalize(); - Files.walkFileTree(absDir, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, + Files.walkFileTree(dirToIndex.toPath().normalize(), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new IndexFileVisitor(inputFileBuilder, fileSystem, status, type)); } - private void indexFile(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress progress, File sourceFile, InputFile.Type type) { - DefaultInputFile inputFile = inputFileBuilder.create(sourceFile); + private void indexFile(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress progress, Path sourceFile, InputFile.Type type) throws IOException { + // get case of real file without resolving link + Path realFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS); + DefaultInputFile inputFile = inputFileBuilder.create(realFile.toFile()); if (inputFile != null) { // Set basedir on input file prior to adding it to the FS since exclusions filters may require the absolute path inputFile.setModuleBaseDir(fileSystem.baseDirPath()); @@ -204,8 +205,8 @@ public class FileIndexer { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path fileName = dir.getFileName(); - - if (fileName != null && StringUtils.isNotEmpty(fileName.toString()) && fileName.toString().charAt(0) == '.') { + + if (fileName != null && fileName.toString().length() > 1 && fileName.toString().charAt(0) == '.') { return FileVisitResult.SKIP_SUBTREE; } if (Files.isHidden(dir)) { @@ -217,7 +218,7 @@ public class FileIndexer { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!Files.isHidden(file) && !ProjectLock.LOCK_FILE_NAME.equals(file.getFileName().toString())) { - indexFile(inputFileBuilder, fileSystem, status, file.toFile(), type); + indexFile(inputFileBuilder, fileSystem, status, file, type); } return FileVisitResult.CONTINUE; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java index bcfe3e453fa..50d6d899588 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java @@ -35,6 +35,7 @@ import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.file.LinkOption; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; @@ -73,7 +74,11 @@ public class DefaultFileSystem implements FileSystem { protected DefaultFileSystem(@Nullable File baseDir, Cache cache) { // Basedir can be null with views - this.baseDir = baseDir != null ? baseDir.toPath().toAbsolutePath().normalize() : new File(".").toPath(); + try { + this.baseDir = baseDir != null ? baseDir.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS) : new File(".").toPath().toAbsolutePath().normalize(); + } catch (IOException e) { + throw new IllegalStateException(e); + } this.cache = cache; this.predicates = new DefaultFilePredicates(this.baseDir); } |