Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GlobalTempFolderProvider.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.lang3.StringUtils;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.sonar.api.CoreProperties;
  32. import org.sonar.api.impl.utils.DefaultTempFolder;
  33. import org.sonar.api.utils.TempFolder;
  34. import org.springframework.context.annotation.Bean;
  35. import static org.sonar.core.util.FileUtils.deleteQuietly;
  36. public class GlobalTempFolderProvider {
  37. private static final Logger LOG = LoggerFactory.getLogger(GlobalTempFolderProvider.class);
  38. private static final long CLEAN_MAX_AGE = TimeUnit.DAYS.toMillis(21);
  39. static final String TMP_NAME_PREFIX = ".sonartmp_";
  40. @Bean("GlobalTempFolder")
  41. public TempFolder provide(ScannerProperties scannerProps, SonarUserHome userHome) {
  42. var workingPathName = StringUtils.defaultIfBlank(scannerProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE);
  43. var workingPath = Paths.get(workingPathName);
  44. if (!workingPath.isAbsolute()) {
  45. var home = userHome.getPath();
  46. workingPath = home.resolve(workingPath).normalize();
  47. }
  48. try {
  49. cleanTempFolders(workingPath);
  50. } catch (IOException e) {
  51. LOG.error(String.format("failed to clean global working directory: %s", workingPath), e);
  52. }
  53. var tempDir = createTempFolder(workingPath);
  54. return new DefaultTempFolder(tempDir.toFile(), true);
  55. }
  56. private static Path createTempFolder(Path workingPath) {
  57. try {
  58. Path realPath = workingPath;
  59. if (Files.isSymbolicLink(realPath)) {
  60. realPath = realPath.toRealPath();
  61. }
  62. Files.createDirectories(realPath);
  63. } catch (IOException e) {
  64. throw new IllegalStateException("Failed to create working path: " + workingPath, e);
  65. }
  66. try {
  67. return Files.createTempDirectory(workingPath, TMP_NAME_PREFIX);
  68. } catch (IOException e) {
  69. throw new IllegalStateException("Failed to create temporary folder in " + workingPath, e);
  70. }
  71. }
  72. private static void cleanTempFolders(Path path) throws IOException {
  73. if (Files.exists(path)) {
  74. try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) {
  75. for (Path p : stream) {
  76. deleteQuietly(p.toFile());
  77. }
  78. }
  79. }
  80. }
  81. private static class CleanFilter implements DirectoryStream.Filter<Path> {
  82. @Override
  83. public boolean accept(Path path) {
  84. if (!Files.exists(path)) {
  85. return false;
  86. }
  87. if (!path.getFileName().toString().startsWith(TMP_NAME_PREFIX)) {
  88. return false;
  89. }
  90. long threshold = System.currentTimeMillis() - CLEAN_MAX_AGE;
  91. // we could also check the timestamp in the name, instead
  92. BasicFileAttributes attrs;
  93. try {
  94. attrs = Files.readAttributes(path, BasicFileAttributes.class);
  95. } catch (IOException ioe) {
  96. LOG.error(String.format("Couldn't read file attributes for %s : ", path), ioe);
  97. return false;
  98. }
  99. long creationTime = attrs.creationTime().toMillis();
  100. return creationTime < threshold;
  101. }
  102. }
  103. }