diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-06-27 11:31:14 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-06-28 20:21:12 +0200 |
commit | 20a1224a63f0d1132ecc14e61eb13c07b8d91c31 (patch) | |
tree | 63ee5a83b313314059306b70d7190d2872f7064c /server/sonar-process | |
parent | 75b26d711379894cfa0fbb075ccc73eeada03f2d (diff) | |
download | sonarqube-20a1224a63f0d1132ecc14e61eb13c07b8d91c31.tar.gz sonarqube-20a1224a63f0d1132ecc14e61eb13c07b8d91c31.zip |
SONAR-11874 ignore inaccessible files when cleaning tmp dir
Diffstat (limited to 'server/sonar-process')
-rw-r--r-- | server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java b/server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java index 258e0de2184..1434da5e77f 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java +++ b/server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java @@ -21,6 +21,8 @@ package org.sonar.process; import java.io.File; import java.io.IOException; +import java.nio.file.AccessDeniedException; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -29,6 +31,8 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.EnumSet; import javax.annotation.Nullable; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import static java.lang.String.format; import static java.util.Objects.requireNonNull; @@ -38,6 +42,7 @@ import static java.util.Objects.requireNonNull; * {@link org.apache.commons.io.FileUtils Common IO FileUtils} class. */ public final class FileUtils2 { + private static final Logger LOG = Loggers.get(FileUtils2.class); private static final String DIRECTORY_CAN_NOT_BE_NULL = "Directory can not be null"; private static final EnumSet<FileVisitOption> FOLLOW_LINKS = EnumSet.of(FileVisitOption.FOLLOW_LINKS); @@ -80,6 +85,10 @@ public final class FileUtils2 { try { if (file.isDirectory()) { deleteDirectory(file); + + if (file.exists()) { + LOG.warn("Unable to delete directory '{}'", file); + } } else { Files.delete(file.toPath()); } @@ -110,10 +119,6 @@ public final class FileUtils2 { throw new IOException(format("Directory '%s' is a file", directory)); } deleteDirectoryImpl(path); - - if (directory.exists()) { - throw new IOException(format("Unable to delete directory '%s'", directory)); - } } /** @@ -183,13 +188,24 @@ public final class FileUtils2 { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); + try { + Files.delete(file); + } catch (AccessDeniedException e) { + LOG.debug("Access delete to file '{}'. Ignoring and proceeding with recursive delete", file); + } return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); + try { + Files.delete(dir); + } catch (AccessDeniedException e) { + LOG.debug("Access denied to delete directory '{}'. Ignoring and proceeding with recursive delete", dir); + } catch (DirectoryNotEmptyException e) { + LOG.trace("Can not delete non empty directory '{}', presumably because it contained non accessible files/directories. " + + "Ignoring and proceeding with recursive delete", dir, e); + } return FileVisitResult.CONTINUE; } } |