From 037967860a2ce1164e314b0e8e631a0c82ce6c2b Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 30 Mar 2011 17:39:54 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50984 When using the Manager application ensure that undeployment is reported as failed if a file cannot be deleted. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1087026 13f79535-47bb-0310-9956-ffa450edef68 --- .../catalina/manager/LocalStrings.properties | 2 + .../apache/catalina/manager/ManagerServlet.java | 70 +++++++++++++++------- webapps/docs/changelog.xml | 4 ++ 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/java/org/apache/catalina/manager/LocalStrings.properties b/java/org/apache/catalina/manager/LocalStrings.properties index 9ed856a4b..8e1f50aae 100644 --- a/java/org/apache/catalina/manager/LocalStrings.properties +++ b/java/org/apache/catalina/manager/LocalStrings.properties @@ -69,6 +69,7 @@ htmlManagerServlet.title=Tomcat Web Application Manager managerServlet.alreadyContext=FAIL - Application already exists at path {0} managerServlet.alreadyDocBase=FAIL - Directory {0} is already in use managerServlet.configured=OK - Deployed application from context file {0} +managerServlet.deleteFail=FAIL - Unable to delete [{0}]. The continued presence of this file may cause problems. managerServlet.deployed=OK - Deployed application at context path {0} managerServlet.deployFailed=FAIL - Failed to deploy application at context path {0} managerServlet.deployedButNotStarted=FAIL - Deployed application at context path {0} but context failed to start @@ -78,6 +79,7 @@ managerServlet.invalidPath=FAIL - Invalid context path {0} was specified managerServlet.invalidWar=FAIL - Invalid application URL {0} was specified managerServlet.listed=OK - Listed applications for virtual host {0} managerServlet.listitem={0}:{1}:{2}:{3} +managerServlet.mkdirFail=FAIL - Unable to create directory [{0}] managerServlet.noAppBase=FAIL - Cannot identify application base for context path {0} managerServlet.noCommand=FAIL - No command was specified managerServlet.noContext=FAIL - No context exists for path {0} diff --git a/java/org/apache/catalina/manager/ManagerServlet.java b/java/org/apache/catalina/manager/ManagerServlet.java index ab277305c..bbfc3e614 100644 --- a/java/org/apache/catalina/manager/ManagerServlet.java +++ b/java/org/apache/catalina/manager/ManagerServlet.java @@ -627,7 +627,11 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { File deployedPath = deployed; if (tag != null) { deployedPath = new File(versioned, tag); - deployedPath.mkdirs(); + if (!deployedPath.isDirectory() && !deployedPath.mkdirs()) { + writer.println(smClient.getString("managerServlet.mkdirFail", + deployedPath)); + return; + } } // Upload the web application archive to a local WAR file @@ -642,7 +646,7 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { addServiced(name); try { // Upload WAR - uploadWar(request, localWar); + uploadWar(writer, request, localWar, smClient); // Copy WAR and XML to the host app base if needed if (tag != null) { deployedPath = deployed; @@ -817,7 +821,11 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { addServiced(name); try { if (config != null) { - configBase.mkdirs(); + if (!configBase.isDirectory() && !configBase.mkdirs()) { + writer.println(smClient.getString( + "managerServlet.mkdirFail",configBase)); + return; + } copy(new File(config), new File(configBase, baseName + ".xml")); } @@ -1333,12 +1341,18 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { File war = new File(getAppBase(), baseName + ".war"); File dir = new File(getAppBase(), baseName); File xml = new File(configBase, baseName + ".xml"); - if (war.exists()) { - war.delete(); - } else if (dir.exists()) { - undeployDir(dir); - } else { - xml.delete(); + if (war.exists() && !war.delete()) { + writer.println(smClient.getString( + "managerServlet.deleteFail", war)); + return; + } else if (dir.exists() && !undeployDir(dir)) { + writer.println(smClient.getString( + "managerServlet.deleteFail", dir)); + return; + } else if (xml.exists() && !xml.delete()) { + writer.println(smClient.getString( + "managerServlet.deleteFail", xml)); + return; } // Perform new deployment check(name); @@ -1446,11 +1460,11 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { /** * Delete the specified directory, including all of its contents and - * subdirectories recursively. + * subdirectories recursively. The code assumes that the directory exists. * - * @param dir File object representing the directory to be deleted + * @param dir File object representing the directory to be deleted. */ - protected void undeployDir(File dir) { + protected boolean undeployDir(File dir) { String files[] = dir.list(); if (files == null) { @@ -1459,13 +1473,16 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { for (int i = 0; i < files.length; i++) { File file = new File(dir, files[i]); if (file.isDirectory()) { - undeployDir(file); + if (!undeployDir(file)) { + return false; + } } else { - file.delete(); + if (!file.delete()) { + return false; + } } } - dir.delete(); - + return dir.delete(); } @@ -1473,15 +1490,21 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { * Upload the WAR file included in this request, and store it at the * specified file location. * - * @param request The servlet request we are processing - * @param war The file into which we should store the uploaded WAR + * @param writer Writer to render to + * @param request The servlet request we are processing + * @param war The file into which we should store the uploaded WAR + * @param smClient The StringManager used to construct i18n messages based + * on the Locale of the client * * @exception IOException if an I/O error occurs during processing */ - protected void uploadWar(HttpServletRequest request, File war) - throws IOException { + protected void uploadWar(PrintWriter writer, HttpServletRequest request, + File war, StringManager smClient) throws IOException { - war.delete(); + if (war.exists() && !war.delete()) { + String msg = smClient.getString("managerServlet.deleteFail", war); + throw new IOException(msg); + } ServletInputStream istream = null; BufferedOutputStream ostream = null; try { @@ -1502,7 +1525,10 @@ public class ManagerServlet extends HttpServlet implements ContainerServlet { istream.close(); istream = null; } catch (IOException e) { - war.delete(); + if (war.exists() && !war.delete()) { + writer.println( + smClient.getString("managerServlet.deleteFail", war)); + } throw e; } finally { if (ostream != null) { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 62d949933..cdcb07804 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -181,6 +181,10 @@ Prevent the custom error pages for the Manager and Host Manager applications from being accessed directly. (markt) + + 50984: When using the Manager application ensure that + undeployment fails if a file cannot be deleted. (markt) + -- 2.11.0