From b4af8a5a2ae40939e9b3fe2d8a83d1806d96808a Mon Sep 17 00:00:00 2001 From: mindless Date: Sun, 22 Nov 2009 22:54:13 +0000 Subject: [PATCH] Fix createSymlink so it overwrites existing link properly if the existing link points to a non-existent file/dir (in this case File.exists() returns false, so it was not removed before creating new symlink). This change also avoids exec of "rm -rf" when simple File.delete() succeeds. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23972 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/Util.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 46a5936362..53e00d8ae3 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -318,7 +318,7 @@ public class Util { fileInCanonicalParent = new File( parentDir.getCanonicalPath(), name ); } return !fileInCanonicalParent.getCanonicalFile().equals( fileInCanonicalParent.getAbsoluteFile() ); - } + } /** * Creates a new temporary directory. @@ -960,8 +960,10 @@ public class Util { if(!isWindows() && !NO_SYMLINK) { try { // if a file or a directory exists here, delete it first. + // try simple delete first (whether exists() or not, as it may be symlink pointing + // to non-existent target), but fallback to "rm -rf" to delete non-empty dir. File symlinkFile = new File(baseDir, symlinkPath); - if (symlinkFile.exists()) + if (!symlinkFile.delete() && symlinkFile.exists()) // ignore a failure. new LocalProc(new String[]{"rm","-rf", symlinkPath},new String[0],listener.getLogger(), baseDir).join();