From 54aaa3a398c6b0e480777244e5489cdbc564a1c5 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 27 Feb 2024 10:15:24 -0600 Subject: [PATCH] Improve DEBUG during WebInfConfiguration.unpack --- .../test/java/org/eclipse/jetty/io/IOTest.java | 16 ++++++++++++++++ .../src/main/java/org/eclipse/jetty/util/IO.java | 8 ++++++++ .../jetty/ee10/webapp/WebInfConfiguration.java | 13 +++++++++++-- .../jetty/ee9/webapp/WebInfConfiguration.java | 13 +++++++++++-- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java index f7ccf5735938..c194c83aad54 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java @@ -472,6 +472,22 @@ public void testDeleteNonExistentFile(WorkDir workDir) assertFalse(IO.delete(noFile.toFile())); } + @Test + public void testDeleteTreeDeep(WorkDir workDir) throws IOException + { + Path dir = workDir.getEmptyPathDir(); + FS.ensureEmpty(dir); + + Files.createDirectory(dir.resolve("foo")); + Files.createDirectory(dir.resolve("foo/bar")); + Files.createDirectory(dir.resolve("foo/zed")); + Files.createDirectory(dir.resolve("foo/zed/one")); + Files.writeString(dir.resolve("foo/bar/test.txt"), "Test"); + Files.writeString(dir.resolve("foo/zed/one/test.txt"), "Test"); + + assertTrue(IO.delete(dir)); + } + @Test public void testIsEmptyNull() { diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java index 8c0c1c798abc..670ae70da807 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java @@ -380,6 +380,12 @@ public static boolean delete(File file) return file.delete(); } + /** + * Delete the path, recursively. + * + * @param path the path to delete + * @return true if able to delete the path, false if unable to delete the path. + */ public static boolean delete(Path path) { if (path == null) @@ -406,6 +412,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } catch (IOException e) { + if (LOG.isDebugEnabled()) + LOG.debug("Unable to delete path: {}", path, e); return false; } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebInfConfiguration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebInfConfiguration.java index 253f7e296bc1..961e08023a7d 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebInfConfiguration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebInfConfiguration.java @@ -291,8 +291,17 @@ public void unpack(WebAppContext context) throws IOException if (originalWarResource.lastModified().isAfter(Files.getLastModifiedTime(extractedWebAppDir).toInstant()) || extractionLock.exists()) { extractionLock.createNewFile(); - IO.delete(extractedWebAppDir); - Files.createDirectory(extractedWebAppDir); + // Best effort delete + if (IO.delete(extractedWebAppDir)) + { + // Recreate the directory if it was deleted. + Files.createDirectory(extractedWebAppDir); + } + else + { + if (LOG.isInfoEnabled()) + LOG.info("Unable to delete path {}, reusing existing path", extractedWebAppDir); + } if (LOG.isDebugEnabled()) LOG.debug("Extract {} to {}", webApp, extractedWebAppDir); try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable()) diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebInfConfiguration.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebInfConfiguration.java index 77e37f4fee59..7b3689b424f2 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebInfConfiguration.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebInfConfiguration.java @@ -293,8 +293,17 @@ public void unpack(WebAppContext context) throws IOException if (originalWarResource.lastModified().isAfter(Files.getLastModifiedTime(extractedWebAppDir).toInstant()) || extractionLock.exists()) { extractionLock.createNewFile(); - IO.delete(extractedWebAppDir); - Files.createDirectory(extractedWebAppDir); + // Best effort delete + if (IO.delete(extractedWebAppDir)) + { + // Recreate the directory if it was deleted. + Files.createDirectory(extractedWebAppDir); + } + else + { + if (LOG.isInfoEnabled()) + LOG.info("Unable to delete path {}, reusing existing path", extractedWebAppDir); + } if (LOG.isDebugEnabled()) LOG.debug("Extract {} to {}", webApp, extractedWebAppDir); try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())