symlink and executable file selectors
Project: http://git-wip-us.apache.org/repos/asf/ant/repo Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/c5e90bea Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/c5e90bea Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/c5e90bea Branch: refs/heads/master Commit: c5e90beadb2583e589051154b37f6e092a88e50d Parents: dcf2413 Author: Stefan Bodewig <[email protected]> Authored: Sat Apr 9 17:04:02 2016 +0200 Committer: Stefan Bodewig <[email protected]> Committed: Sat Apr 9 17:04:02 2016 +0200 ---------------------------------------------------------------------- WHATSNEW | 2 + manual/Types/selectors.html | 26 +++++- .../apache/tools/ant/types/AbstractFileSet.java | 10 +++ .../selectors/AbstractSelectorContainer.java | 8 ++ .../types/selectors/BaseSelectorContainer.java | 8 ++ .../ant/types/selectors/ExecutableSelector.java | 51 ++++++++++++ .../ant/types/selectors/SymlinkSelector.java | 50 ++++++++++++ .../antunit/types/selectors/executable-test.xml | 85 +++++++++++++++++++ .../antunit/types/selectors/symlink-test.xml | 86 ++++++++++++++++++++ 9 files changed, 325 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/WHATSNEW ---------------------------------------------------------------------- diff --git a/WHATSNEW b/WHATSNEW index 4372dca..1836653 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -16,6 +16,8 @@ Fixed bugs: Other changes: -------------- + * New file selectors <executable> and <symlink> + Changes from Ant 1.9.6 TO Ant 1.9.7 =================================== http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/manual/Types/selectors.html ---------------------------------------------------------------------- diff --git a/manual/Types/selectors.html b/manual/Types/selectors.html index 560b416..ab0a293 100644 --- a/manual/Types/selectors.html +++ b/manual/Types/selectors.html @@ -92,6 +92,10 @@ Select files if they are readable.</li> <li><a href="#writable"><code><writable></code></a> - Select files if they are writable.</li> + <li><a href="#executable"><code><executable></code></a> - + Select files if they are executable.</li> + <li><a href="#symlink"><code><symlink></code></a> - + Select files if they are symlink.</li> </ul> <h4><a name="containsselect">Contains Selector</a></h4> @@ -1009,7 +1013,7 @@ but the Java VM cannot detect this state, this selector will still select the file.</p> - <h4><a name="writable">Writable Selector</a></h4> + <h4><a name="symlink">Writable Selector</a></h4> <p>The <code><writable></code> selector selects only files that are writable. Ant only invokes @@ -1017,6 +1021,26 @@ but the Java VM cannot detect this state, this selector will still select the file.</p> + <h4><a name="executable">Executable Selector</a></h4> + + <p>The <code><executable></code> selector selects only files + that are executable. Ant only invokes + <code>java.nio.file.Files#isExecutable</code> so if a file is not executable + but the Java VM cannot detect this state, this selector will + still select the file.</p> + + <p><em>Since Ant 1.10.0</em></p> + + <h4><a name="symlink">Symlink Selector</a></h4> + + <p>The <code><symlink></code> selector selects only files + that are symbolic links. Ant only invokes + <code>java.nio.file.Files#isSymbolicLink</code> so if a file + is a symbolic link but the Java VM cannot detect this state, + this selector will not select the file.</p> + + <p><em>Since Ant 1.10.0</em></p> + <h4><a name="scriptselector">Script Selector</a></h4> <p> http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/main/org/apache/tools/ant/types/AbstractFileSet.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/AbstractFileSet.java b/src/main/org/apache/tools/ant/types/AbstractFileSet.java index 8f274a0..bf15607 100644 --- a/src/main/org/apache/tools/ant/types/AbstractFileSet.java +++ b/src/main/org/apache/tools/ant/types/AbstractFileSet.java @@ -36,6 +36,7 @@ import org.apache.tools.ant.types.selectors.DependSelector; import org.apache.tools.ant.types.selectors.DepthSelector; import org.apache.tools.ant.types.selectors.DifferentSelector; import org.apache.tools.ant.types.selectors.ExtendSelector; +import org.apache.tools.ant.types.selectors.ExecutableSelector; import org.apache.tools.ant.types.selectors.FileSelector; import org.apache.tools.ant.types.selectors.FilenameSelector; import org.apache.tools.ant.types.selectors.MajoritySelector; @@ -48,6 +49,7 @@ import org.apache.tools.ant.types.selectors.SelectSelector; import org.apache.tools.ant.types.selectors.SelectorContainer; import org.apache.tools.ant.types.selectors.SelectorScanner; import org.apache.tools.ant.types.selectors.SizeSelector; +import org.apache.tools.ant.types.selectors.SymlinkSelector; import org.apache.tools.ant.types.selectors.TypeSelector; import org.apache.tools.ant.types.selectors.WritableSelector; import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector; @@ -797,6 +799,14 @@ public abstract class AbstractFileSet extends DataType appendSelector(w); } + public void addExecutable(ExecutableSelector e) { + appendSelector(e); + } + + public void addSymlink(SymlinkSelector e) { + appendSelector(e); + } + /** * Add an arbitrary selector. * @param selector the <code>FileSelector</code> to add. http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java index b80816d..c2f41a7 100644 --- a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java @@ -312,6 +312,14 @@ public abstract class AbstractSelectorContainer extends DataType appendSelector(w); } + public void addExecutable(ExecutableSelector e) { + appendSelector(e); + } + + public void addSymlink(SymlinkSelector e) { + appendSelector(e); + } + /** * add an arbitrary selector * @param selector the selector to add http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java b/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java index 1edf085..e7aa612 100644 --- a/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java @@ -315,6 +315,14 @@ public abstract class BaseSelectorContainer extends BaseSelector appendSelector(w); } + public void addExecutable(ExecutableSelector e) { + appendSelector(e); + } + + public void addSymlink(SymlinkSelector e) { + appendSelector(e); + } + /** * add an arbitrary selector * @param selector the selector to add http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java b/src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java new file mode 100644 index 0000000..891b795 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types.selectors; + +import java.nio.file.Files; +import java.io.File; + +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileProvider; +import org.apache.tools.ant.types.resources.selectors.ResourceSelector; + +/** + * A selector that selects executable files. + * + * <p>Executable is defined in terms of {@link + * java.nio.file.Files#isExecutable}, this means the selector will + * accept any file that exists and is executable by the + * application.</p> + * + * @since Ant 1.10.0 + */ +public class ExecutableSelector implements FileSelector, ResourceSelector { + + public boolean isSelected(File basedir, String filename, File file) { + return file != null && Files.isExecutable(file.toPath()); + } + + public boolean isSelected(Resource r) { + FileProvider fp = r.as(FileProvider.class); + if (fp != null) { + return isSelected(null, null, fp.getFile()); + } + return false; + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java b/src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java new file mode 100644 index 0000000..28c3bcb --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types.selectors; + +import java.nio.file.Files; +import java.io.File; + +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileProvider; +import org.apache.tools.ant.types.resources.selectors.ResourceSelector; + +/** + * A selector that selects symbolic links. + * + * <p>Executable is defined in terms of {@link + * java.nio.file.Files#isSymbolicLink}, this means the selector will + * accept any file that exists and is a symbolic link.</p> + * + * @since Ant 1.10.0 + */ +public class SymlinkSelector implements FileSelector, ResourceSelector { + + public boolean isSelected(File basedir, String filename, File file) { + return file != null && Files.isSymbolicLink(file.toPath()); + } + + public boolean isSelected(Resource r) { + FileProvider fp = r.as(FileProvider.class); + if (fp != null) { + return isSelected(null, null, fp.getFile()); + } + return false; + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/tests/antunit/types/selectors/executable-test.xml ---------------------------------------------------------------------- diff --git a/src/tests/antunit/types/selectors/executable-test.xml b/src/tests/antunit/types/selectors/executable-test.xml new file mode 100644 index 0000000..ed1e1ce --- /dev/null +++ b/src/tests/antunit/types/selectors/executable-test.xml @@ -0,0 +1,85 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit"> + + <import file="../../antunit-base.xml" /> + + <property name="file" value="testfile"/> + + <condition property="unix"> + <os family="unix"/> + </condition> + + <target name="createTestdir"> + <mkdir dir="${output}"/> + <touch file="${output}/${file}"/> + </target> + + <target name="testExecutable" depends="makeFileExecutable" if="unix"> + <au:assertTrue> + <resourcecount when="equal" count="1"> + <fileset dir="${output}"> + <executable/> + </fileset> + </resourcecount> + </au:assertTrue> + <au:assertTrue> + <resourcecount when="equal" count="0"> + <fileset dir="${output}" excludes="${file}"> + <executable/> + </fileset> + </resourcecount> + </au:assertTrue> + </target> + + <target name="makeFileExecutable" + depends="createTestdir,makeFileExecutable-Unix,makeFileExecutable-Windows"/> + <target name="makeFileExecutable-Unix" if="unix"> + <chmod file="${output}/${file}" perm="755"/> + </target> + <target name="makeFileExecutable-Windows" unless="unix"> + <!-- no idea how to do this --> + </target> + + <target name="testNotexecutable" depends="createTestdir"> + <au:assertTrue> + <resourcecount when="equal" count="0"> + <fileset dir="${output}"> + <executable/> + </fileset> + </resourcecount> + </au:assertTrue> + </target> + + <target name="testAsFalseConditions" depends="createTestdir"> + <au:assertFalse> + <isfileselected file="${output}/${file}"> + <executable/> + </isfileselected> + </au:assertFalse> + </target> + + <target name="testAsTrueConditions" depends="makeFileExecutable" if="unix"> + <au:assertTrue> + <isfileselected file="${output}/${file}"> + <executable/> + </isfileselected> + </au:assertTrue> + </target> + +</project> http://git-wip-us.apache.org/repos/asf/ant/blob/c5e90bea/src/tests/antunit/types/selectors/symlink-test.xml ---------------------------------------------------------------------- diff --git a/src/tests/antunit/types/selectors/symlink-test.xml b/src/tests/antunit/types/selectors/symlink-test.xml new file mode 100644 index 0000000..0c47326 --- /dev/null +++ b/src/tests/antunit/types/selectors/symlink-test.xml @@ -0,0 +1,86 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit"> + + <import file="../../antunit-base.xml" /> + + <property name="file" value="testfile"/> + <property name="link" value="testlink"/> + + <condition property="unix"> + <os family="unix"/> + </condition> + + <target name="createTestdir"> + <mkdir dir="${output}"/> + <touch file="${output}/${file}"/> + </target> + + <target name="testSymlink" depends="makeSymlink" if="unix"> + <au:assertTrue> + <resourcecount when="equal" count="1"> + <fileset dir="${output}"> + <symlink/> + </fileset> + </resourcecount> + </au:assertTrue> + <au:assertTrue> + <resourcecount when="equal" count="0"> + <fileset dir="${output}" excludes="${link}"> + <symlink/> + </fileset> + </resourcecount> + </au:assertTrue> + </target> + + <target name="makeSymlink" + depends="createTestdir,makeSymlink-Unix,makeSymlink-Windows"/> + <target name="makeSymlink-Unix" if="unix"> + <symlink link="${output}/${link}" resource="${output}/${file}"/> + </target> + <target name="makeSymlink-Windows" unless="unix"> + <!-- no idea how to do this --> + </target> + + <target name="testNoSymlink" depends="createTestdir"> + <au:assertTrue> + <resourcecount when="equal" count="0"> + <fileset dir="${output}"> + <symlink/> + </fileset> + </resourcecount> + </au:assertTrue> + </target> + + <target name="testAsFalseConditions" depends="createTestdir"> + <au:assertFalse> + <isfileselected file="${output}/${link}"> + <symlink/> + </isfileselected> + </au:assertFalse> + </target> + + <target name="testAsTrueConditions" depends="makeSymlink" if="unix"> + <au:assertTrue> + <isfileselected file="${output}/${link}"> + <symlink/> + </isfileselected> + </au:assertTrue> + </target> + +</project>
