Browse Source

SONAR-17571 Ensure file existence prior to file size assessment

tags/9.8.0.63668
Klaudio Sinani 1 year ago
parent
commit
b2904b7b51

+ 6
- 3
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java View File

@@ -120,9 +120,8 @@ public class FileIndexer {
return;
}

long maxFileSize = properties.fileSizeLimit();
if (Files.size(realAbsoluteFile) > maxFileSize * 1024L * 1024L) {
LOG.warn("File '{}' is bigger than {}MB and as consequence is removed from the analysis scope.", realAbsoluteFile.toAbsolutePath(), maxFileSize);
if (Files.exists(realAbsoluteFile) && isFileSizeBiggerThanLimit(realAbsoluteFile)) {
LOG.warn("File '{}' is bigger than {}MB and as consequence is removed from the analysis scope.", realAbsoluteFile.toAbsolutePath(), properties.fileSizeLimit());
return;
}

@@ -282,4 +281,8 @@ public class FileIndexer {
private static String pluralizeFiles(int count) {
return count == 1 ? "file" : "files";
}

private boolean isFileSizeBiggerThanLimit(Path filePath) throws IOException {
return Files.size(filePath) > properties.fileSizeLimit() * 1024L * 1024L;
}
}

+ 19
- 0
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java View File

@@ -27,6 +27,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
@@ -438,6 +439,24 @@ public class FileSystemMediumTest {
.contains(format("File '%s' is bigger than 1MB and as consequence is removed from the analysis scope.", fileGreaterThanLimit.getAbsolutePath()));
}

@Test
public void analysisFailsIfFileDoesNotExist() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();

File target = writeFile(srcDir, "target.xoo", 1024 * 1024 + 1);
Path link = Paths.get(srcDir.getPath(), "target_link.xoo");
Files.createSymbolicLink(link, target.toPath());
Files.delete(target.toPath());

AnalysisBuilder analysis = tester.newAnalysis()
.properties(builder.build());

assertThatThrownBy(analysis::execute)
.isExactlyInstanceOf(IllegalStateException.class)
.hasMessageEndingWith(format("Unable to read file %s", link));
}

@Test
public void test_inclusions_on_multi_modules() throws IOException {
File baseDir = temp.getRoot();

Loading…
Cancel
Save