You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlobalTempFolderProvider.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.bootstrap;
  21. import java.io.IOException;
  22. import java.nio.file.DirectoryStream;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.nio.file.attribute.BasicFileAttributes;
  27. import java.util.concurrent.TimeUnit;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.picocontainer.ComponentLifecycle;
  30. import org.picocontainer.PicoContainer;
  31. import org.picocontainer.injectors.ProviderAdapter;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.impl.utils.DefaultTempFolder;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.api.utils.TempFolder;
  36. import org.sonar.api.utils.log.Logger;
  37. import org.sonar.api.utils.log.Loggers;
  38. import static org.sonar.core.util.FileUtils.deleteQuietly;
  39. public class GlobalTempFolderProvider extends ProviderAdapter implements ComponentLifecycle<TempFolder> {
  40. private static final Logger LOG = Loggers.get(GlobalTempFolderProvider.class);
  41. private static final long CLEAN_MAX_AGE = TimeUnit.DAYS.toMillis(21);
  42. static final String TMP_NAME_PREFIX = ".sonartmp_";
  43. private boolean started = false;
  44. private System2 system;
  45. private DefaultTempFolder tempFolder;
  46. public GlobalTempFolderProvider() {
  47. this(new System2());
  48. }
  49. GlobalTempFolderProvider(System2 system) {
  50. this.system = system;
  51. }
  52. public TempFolder provide(RawScannerProperties scannerProps) {
  53. if (tempFolder == null) {
  54. String workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE);
  55. Path workingPath = Paths.get(workingPathName);
  56. if (!workingPath.isAbsolute()) {
  57. Path home = findSonarHome(scannerProps);
  58. workingPath = home.resolve(workingPath).normalize();
  59. }
  60. try {
  61. cleanTempFolders(workingPath);
  62. } catch (IOException e) {
  63. LOG.error(String.format("failed to clean global working directory: %s", workingPath), e);
  64. }
  65. Path tempDir = createTempFolder(workingPath);
  66. tempFolder = new DefaultTempFolder(tempDir.toFile(), true);
  67. }
  68. return tempFolder;
  69. }
  70. private static Path createTempFolder(Path workingPath) {
  71. try {
  72. Path realPath = workingPath;
  73. if (Files.isSymbolicLink(realPath)) {
  74. realPath = realPath.toRealPath();
  75. }
  76. Files.createDirectories(realPath);
  77. } catch (IOException e) {
  78. throw new IllegalStateException("Failed to create working path: " + workingPath, e);
  79. }
  80. try {
  81. return Files.createTempDirectory(workingPath, TMP_NAME_PREFIX);
  82. } catch (IOException e) {
  83. throw new IllegalStateException("Failed to create temporary folder in " + workingPath, e);
  84. }
  85. }
  86. private Path findSonarHome(RawScannerProperties props) {
  87. String home = props.property("sonar.userHome");
  88. if (home != null) {
  89. return Paths.get(home).toAbsolutePath();
  90. }
  91. home = system.envVariable("SONAR_USER_HOME");
  92. if (home != null) {
  93. return Paths.get(home).toAbsolutePath();
  94. }
  95. home = system.property("user.home");
  96. return Paths.get(home, ".sonar").toAbsolutePath();
  97. }
  98. private static void cleanTempFolders(Path path) throws IOException {
  99. if (path.toFile().exists()) {
  100. try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
  101. for (Path p : stream) {
  102. deleteQuietly(p.toFile());
  103. }
  104. }
  105. }
  106. }
  107. private static class CleanFilter implements DirectoryStream.Filter<Path> {
  108. @Override
  109. public boolean accept(Path path) throws IOException {
  110. if (!path.toFile().exists()) {
  111. return false;
  112. }
  113. if (!path.getFileName().toString().startsWith(TMP_NAME_PREFIX)) {
  114. return false;
  115. }
  116. long threshold = System.currentTimeMillis() - CLEAN_MAX_AGE;
  117. // we could also check the timestamp in the name, instead
  118. BasicFileAttributes attrs;
  119. try {
  120. attrs = Files.readAttributes(path, BasicFileAttributes.class);
  121. } catch (IOException ioe) {
  122. LOG.error(String.format("Couldn't read file attributes for %s : ", path), ioe);
  123. return false;
  124. }
  125. long creationTime = attrs.creationTime().toMillis();
  126. return creationTime < threshold;
  127. }
  128. }
  129. @Override
  130. public void start(PicoContainer container) {
  131. started = true;
  132. }
  133. @Override
  134. public void stop(PicoContainer container) {
  135. if (tempFolder != null) {
  136. tempFolder.stop();
  137. }
  138. }
  139. @Override
  140. public void dispose(PicoContainer container) {
  141. // nothing to do
  142. }
  143. @Override
  144. public boolean componentHasLifecycle() {
  145. return true;
  146. }
  147. @Override
  148. public boolean isStarted() {
  149. return started;
  150. }
  151. }