From d53b476ec5a3ba3c141a909e44a0713d87537fee Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 18 Apr 2008 21:44:09 +0000 Subject: [PATCH] fixed a problem where Hudson fails to wipe out files in a directory if the directory is not writable. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@8724 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/Util.java | 56 ++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 4f5c8139be..168cb83378 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -153,29 +153,51 @@ public class Util { return; // perhaps this file is read-only? - // try chmod. this becomes no-op if this is not Unix. - try { - Chmod chmod = new Chmod(); - chmod.setProject(new org.apache.tools.ant.Project()); - chmod.setFile(f); - chmod.setPerm("u+w"); - chmod.execute(); - } catch (BuildException e) { - LOGGER.log(Level.INFO,"Failed to chmod "+f,e); - } - - // also try JDK6-way of doing it. - try { - f.setWritable(true); - } catch (NoSuchMethodError e) { - // not JDK6 - } + makeWritable(f); + /* + on Unix both the file and the directory that contains it has to be writable + for a file deletion to be successful. (Confirmed on Solaris 9) + + $ ls -la + total 6 + dr-xr-sr-x 2 hudson hudson 512 Apr 18 14:41 . + dr-xr-sr-x 3 hudson hudson 512 Apr 17 19:36 .. + -r--r--r-- 1 hudson hudson 469 Apr 17 19:36 manager.xml + -rw-r--r-- 1 hudson hudson 0 Apr 18 14:41 x + $ rm x + rm: x not removed: Permission denied + */ + + makeWritable(f.getParentFile()); if(!f.delete() && f.exists()) throw new IOException("Unable to delete " + f.getPath()); } } + /** + * Makes the given file writable. + */ + private static void makeWritable(File f) { + // try chmod. this becomes no-op if this is not Unix. + try { + Chmod chmod = new Chmod(); + chmod.setProject(new Project()); + chmod.setFile(f); + chmod.setPerm("u+w"); + chmod.execute(); + } catch (BuildException e) { + LOGGER.log(Level.INFO,"Failed to chmod "+f,e); + } + + // also try JDK6-way of doing it. + try { + f.setWritable(true); + } catch (NoSuchMethodError e) { + // not JDK6 + } + } + public static void deleteRecursive(File dir) throws IOException { if(!isSymlink(dir)) deleteContentsRecursive(dir); -- GitLab