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.

DefaultTempFolder.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.api.impl.utils;
  21. import java.nio.file.FileVisitResult;
  22. import java.nio.file.SimpleFileVisitor;
  23. import java.nio.file.attribute.BasicFileAttributes;
  24. import org.apache.commons.io.FileUtils;
  25. import org.sonar.api.Startable;
  26. import org.sonar.api.utils.TempFolder;
  27. import javax.annotation.Nullable;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import org.sonar.api.utils.log.Logger;
  33. import org.sonar.api.utils.log.Loggers;
  34. public class DefaultTempFolder implements TempFolder, Startable {
  35. private static final Logger LOG = Loggers.get(DefaultTempFolder.class);
  36. private final File tempDir;
  37. private final boolean deleteOnExit;
  38. public DefaultTempFolder(File tempDir) {
  39. this(tempDir, false);
  40. }
  41. public DefaultTempFolder(File tempDir, boolean deleteOnExit) {
  42. this.tempDir = tempDir;
  43. this.deleteOnExit = deleteOnExit;
  44. }
  45. @Override
  46. public File newDir() {
  47. return createTempDir(tempDir.toPath()).toFile();
  48. }
  49. private static Path createTempDir(Path baseDir) {
  50. try {
  51. return Files.createTempDirectory(baseDir, null);
  52. } catch (IOException e) {
  53. throw new IllegalStateException("Failed to create temp directory", e);
  54. }
  55. }
  56. @Override
  57. public File newDir(String name) {
  58. File dir = new File(tempDir, name);
  59. try {
  60. FileUtils.forceMkdir(dir);
  61. } catch (IOException e) {
  62. throw new IllegalStateException("Failed to create temp directory - " + dir, e);
  63. }
  64. return dir;
  65. }
  66. @Override
  67. public File newFile() {
  68. return newFile(null, null);
  69. }
  70. @Override
  71. public File newFile(@Nullable String prefix, @Nullable String suffix) {
  72. return createTempFile(tempDir.toPath(), prefix, suffix).toFile();
  73. }
  74. private static Path createTempFile(Path baseDir, String prefix, String suffix) {
  75. try {
  76. return Files.createTempFile(baseDir, prefix, suffix);
  77. } catch (IOException e) {
  78. throw new IllegalStateException("Failed to create temp file", e);
  79. }
  80. }
  81. public void clean() {
  82. try {
  83. if (tempDir.exists()) {
  84. Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
  85. }
  86. } catch (IOException e) {
  87. LOG.error("Failed to delete temp folder", e);
  88. }
  89. }
  90. @Override
  91. public void start() {
  92. // nothing to do
  93. }
  94. @Override
  95. public void stop() {
  96. if (deleteOnExit) {
  97. clean();
  98. }
  99. }
  100. private static final class DeleteRecursivelyFileVisitor extends SimpleFileVisitor<Path> {
  101. public static final DeleteRecursivelyFileVisitor INSTANCE = new DeleteRecursivelyFileVisitor();
  102. @Override
  103. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  104. Files.deleteIfExists(file);
  105. return FileVisitResult.CONTINUE;
  106. }
  107. @Override
  108. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  109. Files.deleteIfExists(dir);
  110. return FileVisitResult.CONTINUE;
  111. }
  112. }
  113. }