From 81ae3f744741f4e58135f7add16c1f732685d6f5 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Wed, 19 Sep 2007 12:29:39 +0000 Subject: [PATCH] bugzilla ID 38199, symlink fails on second call bugzilla ID 43426, cant create a symlink over a file solution is the same: opt for ln -sf after trying to do it internally. We could perhaps drop all ant deletion operations, for a much simpler operation. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@577281 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 7 +++ .../ant/taskdefs/optional/unix/Symlink.java | 25 +++++---- .../taskdefs/optional/unix/symlink-test.xml | 55 +++++++++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 src/tests/antunit/taskdefs/optional/unix/symlink-test.xml diff --git a/WHATSNEW b/WHATSNEW index b0b477744..d3934a643 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -41,6 +41,13 @@ Changes that could break older environments: Fixed bugs: ----------- + * task couldn't overwrite existing symlinks that pointed to nonexistent files + Bugzilla report 38199 + + * task couldn't overwrite files that were in the way of the symlink. + Bugzilla report 43426 + + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java b/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java index 0a91930f0..18374037b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java @@ -404,7 +404,7 @@ public class Symlink extends DispatchTask { * fail. */ public static void deleteSymlink(File linkfil) - throws IOException, FileNotFoundException { + throws IOException{ if (!linkfil.exists()) { throw new FileNotFoundException("No such symlink: " + linkfil); } @@ -466,6 +466,7 @@ public class Symlink extends DispatchTask { * * @param msg The message to log, or include in the * BuildException. + * @throws BuildException with the message if failonerror=true */ private void handleError(String msg) { if (failonerror) { @@ -481,20 +482,24 @@ public class Symlink extends DispatchTask { * * @param res The path of the resource we are linking to. * @param lnk The name of the link we wish to make. + * @throws BuildException when things go wrong */ private void doLink(String res, String lnk) throws BuildException { File linkfil = new File(lnk); - if (overwrite && linkfil.exists()) { - try { - deleteSymlink(linkfil); - } catch (FileNotFoundException fnfe) { - handleError("Symlink disappeared before it was deleted: " + lnk); - } catch (IOException ioe) { - handleError("Unable to overwrite preexisting link: " + lnk); + String options = "-s"; + if (overwrite) { + options += "f"; + if (linkfil.exists()) { + try { + deleteSymlink(linkfil); + } catch (FileNotFoundException fnfe) { + log("Symlink disappeared before it was deleted: " + lnk); + } catch (IOException ioe) { + log("Unable to overwrite preexisting link or file: " + lnk,ioe, Project.MSG_INFO); + } } } - String[] cmd = new String[] {"ln", "-s", res, lnk}; - log(Commandline.toString(cmd)); + String[] cmd = new String[] {"ln", options, res, lnk}; Execute.runCommand(this, cmd); } diff --git a/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml b/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml new file mode 100644 index 000000000..d2408301e --- /dev/null +++ b/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file