Browse Source

symlink and executable file selectors

master
Stefan Bodewig 9 years ago
parent
commit
c5e90beadb
9 changed files with 325 additions and 1 deletions
  1. +2
    -0
      WHATSNEW
  2. +25
    -1
      manual/Types/selectors.html
  3. +10
    -0
      src/main/org/apache/tools/ant/types/AbstractFileSet.java
  4. +8
    -0
      src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java
  5. +8
    -0
      src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java
  6. +51
    -0
      src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java
  7. +50
    -0
      src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java
  8. +85
    -0
      src/tests/antunit/types/selectors/executable-test.xml
  9. +86
    -0
      src/tests/antunit/types/selectors/symlink-test.xml

+ 2
- 0
WHATSNEW View File

@@ -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
===================================



+ 25
- 1
manual/Types/selectors.html View File

@@ -92,6 +92,10 @@
Select files if they are readable.</li>
<li><a href="#writable"><code>&lt;writable&gt;</code></a> -
Select files if they are writable.</li>
<li><a href="#executable"><code>&lt;executable&gt;</code></a> -
Select files if they are executable.</li>
<li><a href="#symlink"><code>&lt;symlink&gt;</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>&lt;writable&gt;</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>&lt;executable&gt;</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>&lt;symlink&gt;</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>


+ 10
- 0
src/main/org/apache/tools/ant/types/AbstractFileSet.java View File

@@ -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.


+ 8
- 0
src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java View File

@@ -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


+ 8
- 0
src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java View File

@@ -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


+ 51
- 0
src/main/org/apache/tools/ant/types/selectors/ExecutableSelector.java View File

@@ -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;
}
}

+ 50
- 0
src/main/org/apache/tools/ant/types/selectors/SymlinkSelector.java View File

@@ -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;
}
}

+ 85
- 0
src/tests/antunit/types/selectors/executable-test.xml View File

@@ -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>

+ 86
- 0
src/tests/antunit/types/selectors/symlink-test.xml View File

@@ -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>

Loading…
Cancel
Save