diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 4f5c8139be3017869af483c2d76ed6d6e9b6ae8f..168cb83378453813ae9f95032cff05c14b4de53c 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);