bugzilla ID 43426, <symlink> 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-ffa450edef68master
@@ -41,6 +41,13 @@ Changes that could break older environments: | |||||
Fixed bugs: | Fixed bugs: | ||||
----------- | ----------- | ||||
* <symlink> task couldn't overwrite existing symlinks that pointed to nonexistent files | |||||
Bugzilla report 38199 | |||||
* <symlink> task couldn't overwrite files that were in the way of the symlink. | |||||
Bugzilla report 43426 | |||||
Other changes: | Other changes: | ||||
-------------- | -------------- | ||||
@@ -404,7 +404,7 @@ public class Symlink extends DispatchTask { | |||||
* fail. | * fail. | ||||
*/ | */ | ||||
public static void deleteSymlink(File linkfil) | public static void deleteSymlink(File linkfil) | ||||
throws IOException, FileNotFoundException { | |||||
throws IOException{ | |||||
if (!linkfil.exists()) { | if (!linkfil.exists()) { | ||||
throw new FileNotFoundException("No such symlink: " + linkfil); | 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 | * @param msg The message to log, or include in the | ||||
* <code>BuildException</code>. | * <code>BuildException</code>. | ||||
* @throws BuildException with the message if failonerror=true | |||||
*/ | */ | ||||
private void handleError(String msg) { | private void handleError(String msg) { | ||||
if (failonerror) { | if (failonerror) { | ||||
@@ -481,20 +482,24 @@ public class Symlink extends DispatchTask { | |||||
* | * | ||||
* @param res The path of the resource we are linking to. | * @param res The path of the resource we are linking to. | ||||
* @param lnk The name of the link we wish to make. | * @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 { | private void doLink(String res, String lnk) throws BuildException { | ||||
File linkfil = new File(lnk); | 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); | Execute.runCommand(this, cmd); | ||||
} | } | ||||
@@ -0,0 +1,55 @@ | |||||
<?xml version="1.0" encoding="utf-8"?> | |||||
<project name="symlink-test" | |||||
default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | |||||
<import file="../../../antunit-base.xml"/> | |||||
<property name="build.dir" location="build" /> | |||||
<target name="setUp"> | |||||
<condition property="isUnix"> | |||||
<os family="unix" /> | |||||
</condition> | |||||
</target> | |||||
<target name="os"> | |||||
<mkdir dir="${build.dir}" /> | |||||
<condition property="unix"> | |||||
<os family="unix" /> | |||||
</condition> | |||||
<property name="file_ref" | |||||
location="${build.dir}/file"/> | |||||
<property name="hanging_ref" | |||||
location="${build.dir}/hanging_ref"/> | |||||
</target> | |||||
<target name="init" depends="os" if="unix"> | |||||
<touch file="${file_ref}" /> | |||||
</target> | |||||
<target name="tearDown"> | |||||
<delete dir="${build.dir}" /> | |||||
</target> | |||||
<target name="testCreateDouble" depends="init" if="unix"> | |||||
<symlink overwrite="true" link="${build.dir}/link" | |||||
resource="${file_ref}"/> | |||||
<symlink overwrite="true" link="${build.dir}/link" | |||||
resource="${file_ref}"/> | |||||
</target> | |||||
<target name="testCreateDoubleHanging" depends="init" if="unix"> | |||||
<symlink overwrite="true" link="${build.dir}/link2" | |||||
resource="${hanging_ref}"/> | |||||
<symlink overwrite="true" link="${build.dir}/link2" | |||||
resource="${hanging_ref}"/> | |||||
</target> | |||||
<target name="testCreateOverFile" depends="init" if="unix"> | |||||
<touch file="${build.dir}/link3" /> | |||||
<symlink overwrite="true" link="${build.dir}/link3" | |||||
resource="${file_ref}"/> | |||||
</target> | |||||
</project> |