diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-07 18:11:58 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-08 10:51:13 +0200 |
commit | 45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196 (patch) | |
tree | 7e3f8ec2fdcc083324d28fd10ab1f3268e12d286 /sonar-scanner-engine/src/main/java/org | |
parent | a0f2105b46566f5bea0da5915e923fc20fd31eda (diff) | |
download | sonarqube-45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196.tar.gz sonarqube-45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196.zip |
Fix regression introduced during previous scanner hardening
Module workDir is deleted when cleaning root module work dir when they are nested.
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org')
3 files changed, 86 insertions, 59 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 4049d49014e..d4ff556e8ae 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -108,7 +108,7 @@ public class ProjectScanContainer extends ComponentContainer { addBatchExtensions(); ProjectLock lock = getComponentByType(ProjectLock.class); lock.tryLock(); - getComponentByType(WorkDirectoryCleaner.class).execute(); + getComponentByType(WorkDirectoriesInitializer.class).execute(); Settings settings = getComponentByType(Settings.class); if (settings != null && settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)) { add(PhasesSumUpTimeProfiler.class); @@ -122,7 +122,7 @@ public class ProjectScanContainer extends ComponentContainer { add( props, ProjectReactorBuilder.class, - WorkDirectoryCleaner.class, + WorkDirectoriesInitializer.class, new MutableProjectReactorProvider(), ProjectBuildersExecutor.class, ProjectLock.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java new file mode 100644 index 00000000000..3ee70e9baa3 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.scan; + +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Iterator; +import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.core.util.FileUtils; +import org.sonar.home.cache.DirectoryLock; + +/** + * Clean and create working directories of each module. + * Be careful that sub module work dir might be nested in parent working directory. + */ +public class WorkDirectoriesInitializer { + + private InputModuleHierarchy moduleHierarchy; + + public WorkDirectoriesInitializer(InputModuleHierarchy moduleHierarchy) { + this.moduleHierarchy = moduleHierarchy; + } + + public void execute() { + cleanAllWorkingDirs(moduleHierarchy.root()); + mkdirsAllWorkingDirs(moduleHierarchy.root()); + } + + private void cleanAllWorkingDirs(DefaultInputModule module) { + for (DefaultInputModule sub : moduleHierarchy.children(module)) { + cleanAllWorkingDirs(sub); + } + if (Files.exists(module.getWorkDir())) { + deleteAllRecursivelyExceptLockFile(module.getWorkDir()); + } + } + + private void mkdirsAllWorkingDirs(DefaultInputModule module) { + for (DefaultInputModule sub : moduleHierarchy.children(module)) { + mkdirsAllWorkingDirs(sub); + } + try { + Files.createDirectories(module.getWorkDir()); + } catch (Exception e) { + throw new IllegalStateException("Fail to create working dir: " + module.getWorkDir(), e); + } + } + + private static void deleteAllRecursivelyExceptLockFile(Path dirToDelete) { + try (DirectoryStream<Path> stream = list(dirToDelete)) { + + Iterator<Path> it = stream.iterator(); + while (it.hasNext()) { + FileUtils.deleteQuietly(it.next().toFile()); + } + } catch (IOException e) { + throw new IllegalStateException("Failed to clean working directory: " + dirToDelete.toString(), e); + } + } + + private static DirectoryStream<Path> list(Path dir) throws IOException { + return Files.newDirectoryStream(dir, entry -> !DirectoryLock.LOCK_FILE_NAME.equals(entry.getFileName().toString())); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoryCleaner.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoryCleaner.java deleted file mode 100644 index 83a9e8650f3..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoryCleaner.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.scanner.scan; - -import java.io.IOException; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Iterator; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; -import org.sonar.core.util.FileUtils; -import org.sonar.home.cache.DirectoryLock; - -public class WorkDirectoryCleaner { - private final Path workDir; - - public WorkDirectoryCleaner(InputModuleHierarchy moduleHierarchy) { - workDir = moduleHierarchy.root().getWorkDir(); - } - - public void execute() { - if (!workDir.toFile().exists()) { - return; - } - - try (DirectoryStream<Path> stream = list()) { - - Iterator<Path> it = stream.iterator(); - while (it.hasNext()) { - FileUtils.deleteQuietly(it.next().toFile()); - } - } catch (IOException e) { - throw new IllegalStateException("Failed to clean working directory: " + workDir.toString(), e); - } - } - - private DirectoryStream<Path> list() throws IOException { - return Files.newDirectoryStream(workDir, entry -> !DirectoryLock.LOCK_FILE_NAME.equals(entry.getFileName().toString())); - } -} |