Author: bodewig Date: Thu Sep 4 07:57:04 2008 New Revision: 692081 URL: http://svn.apache.org/viewvc?rev=692081&view=rev Log: symlink delete now works if the link points to a parent directory as well. PR 45743.
Modified: ant/core/trunk/WHATSNEW ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java ant/core/trunk/src/tests/antunit/core/dirscanner-symlinks-test.xml ant/core/trunk/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml Modified: ant/core/trunk/WHATSNEW URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=692081&r1=692080&r2=692081&view=diff ============================================================================== --- ant/core/trunk/WHATSNEW (original) +++ ant/core/trunk/WHATSNEW Thu Sep 4 07:57:04 2008 @@ -196,6 +196,10 @@ contained line feeds some excess output ended up in Ant's log. Bugzilla Report 45411. + * <symlink action="delete"> failed to delete a link that pointed to + a parent directory. + Bugzilla Report 45743. + Other changes: -------------- Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java?rev=692081&r1=692080&r2=692081&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java Thu Sep 4 07:57:04 2008 @@ -386,13 +386,19 @@ * <p>This is a utility method that removes a unix symlink without removing * the resource that the symlink points to. If it is accidentally invoked * on a real file, the real file will not be harmed, but an exception - * will be thrown when the deletion is attempted. This method works by + * will be thrown when the deletion is attempted.</p> + * + * <p>Normaly this method works by * getting the canonical path of the link, using the canonical path to * rename the resource (breaking the link) and then deleting the link. * The resource is then returned to its original name inside a finally * block to ensure that the resource is unharmed even in the event of * an exception.</p> * + * <p>There may be cases where the algorithm described above doesn't work, + * in that case the method tries to use the native "rm" command on + * the symlink instead.</p> + * * <p>Since Ant 1.8.0 this method will try to delete the File object if * it reports it wouldn't exist (as symlinks pointing nowhere usually do). * Prior version would throw a FileNotFoundException in that case.</p> @@ -410,12 +416,22 @@ linkfil.delete(); return; } + // find the resource of the existing link: File canfil = linkfil.getCanonicalFile(); // rename the resource, thus breaking the link: File temp = FILE_UTILS.createTempFile("symlink", ".tmp", - canfil.getParentFile(), false, false); + canfil.getParentFile(), false, + false); + + if (FILE_UTILS.isLeadingPath(canfil, linkfil)) { + // link points to a parent directory, renaming the parent + // will rename the file + linkfil = new File(temp, + FILE_UTILS.removeLeadingPath(canfil, linkfil)); + } + try { try { FILE_UTILS.rename(canfil, temp); Modified: ant/core/trunk/src/tests/antunit/core/dirscanner-symlinks-test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/core/dirscanner-symlinks-test.xml?rev=692081&r1=692080&r2=692081&view=diff ============================================================================== --- ant/core/trunk/src/tests/antunit/core/dirscanner-symlinks-test.xml (original) +++ ant/core/trunk/src/tests/antunit/core/dirscanner-symlinks-test.xml Thu Sep 4 07:57:04 2008 @@ -81,9 +81,7 @@ <copy todir="${output}"> <fileset dir="${base}" followsymlinks="true"/> </copy> - <exec executable="rm"> - <arg file="${base}/A"/> - </exec> + <symlink action="delete" link="${base}/A"/> <au:assertFileExists file="${output}/A/B/file.txt"/> </target> @@ -95,9 +93,7 @@ <include name="A/B/*"/> </fileset> </copy> - <exec executable="rm"> - <arg file="${base}/A"/> - </exec> + <symlink action="delete" link="${base}/A"/> <au:assertFileExists file="${output}/A/B/file.txt"/> </target> @@ -110,9 +106,7 @@ <include name="A/base/A/B/*"/> </fileset> </copy> - <exec executable="rm"> - <arg file="${base}/A"/> - </exec> + <symlink action="delete" link="${base}/A"/> <au:assertFileExists file="${output}/A/base/A/B/file.txt"/> </target> @@ -122,9 +116,7 @@ <copy todir="${output}"> <fileset dir="${base}" followsymlinks="false"/> </copy> - <exec executable="rm"> - <arg file="${base}/A"/> - </exec> + <symlink action="delete" link="${base}/A"/> <au:assertFileDoesntExist file="${output}/A/B/file.txt"/> </target> @@ -134,9 +126,7 @@ <copy todir="${output}"> <fileset dir="${base}" followsymlinks="true"/> </copy> - <exec executable="rm"> - <arg file="${base}"/> - </exec> + <symlink action="delete" link="${base}"/> <assertDirIsEmpty/> </target> @@ -146,9 +136,7 @@ <copy todir="${output}"> <fileset dir="${base}" followsymlinks="false"/> </copy> - <exec executable="rm"> - <arg file="${base}"/> - </exec> + <symlink action="delete" link="${base}"/> <assertDirIsEmpty/> </target> Modified: ant/core/trunk/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml?rev=692081&r1=692080&r2=692081&view=diff ============================================================================== --- ant/core/trunk/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml (original) +++ ant/core/trunk/src/tests/antunit/taskdefs/optional/unix/symlink-test.xml Thu Sep 4 07:57:04 2008 @@ -69,4 +69,10 @@ <au:assertFileDoesntExist file="${output}/link"/> </target> + <target name="testDeleteLinkToParent" depends="init" if="unix"> + <symlink link="${output}/link" resource="${output}"/> + <symlink link="${output}/link" action="delete"/> + <au:assertFileDoesntExist file="${output}/link"/> + </target> + </project>