From 20a1224a63f0d1132ecc14e61eb13c07b8d91c31 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 27 Jun 2019 11:31:14 +0200 Subject: [PATCH] SONAR-11874 ignore inaccessible files when cleaning tmp dir --- .../application/ProcessLauncherImpl.java | 4 +++ .../java/org/sonar/process/FileUtils2.java | 28 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java b/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java index ecfe632fb51..128816433bc 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/ProcessLauncherImpl.java @@ -129,6 +129,10 @@ public class ProcessLauncherImpl implements ProcessLauncher { LOG.info("Deleting outdated search index data directory {}", outdatedDir.getAbsolutePath()); try { FileUtils2.deleteDirectory(outdatedDir); + + if (outdatedDir.exists()) { + LOG.info("Failed to delete outdated search index data directory {}", outdatedDir); + } } catch (IOException e) { LOG.info("Failed to delete outdated search index data directory {}", outdatedDir.getAbsolutePath(), e); } 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 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; } } -- 2.39.5