From 32bf39369dedbe35eb7309d9056596b12435cdef Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sun, 10 Apr 2016 06:52:10 +0200 Subject: [PATCH] ownedBy selector --- WHATSNEW | 2 +- manual/Types/selectors.html | 25 ++++++ .../tools/ant/types/AbstractFileSet.java | 14 ++++ .../selectors/AbstractSelectorContainer.java | 13 ++++ .../selectors/BaseSelectorContainer.java | 13 ++++ .../ant/types/selectors/OwnedBySelector.java | 78 +++++++++++++++++++ .../antunit/types/selectors/ownedby-test.xml | 65 ++++++++++++++++ 7 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/main/org/apache/tools/ant/types/selectors/OwnedBySelector.java create mode 100644 src/tests/antunit/types/selectors/ownedby-test.xml diff --git a/WHATSNEW b/WHATSNEW index 183665384..91123539f 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -16,7 +16,7 @@ Fixed bugs: Other changes: -------------- - * New file selectors and + * New file selectors , and . Changes from Ant 1.9.6 TO Ant 1.9.7 =================================== diff --git a/manual/Types/selectors.html b/manual/Types/selectors.html index 387d4d13b..153a2527a 100644 --- a/manual/Types/selectors.html +++ b/manual/Types/selectors.html @@ -96,6 +96,8 @@ Select files if they are executable.
  • <symlink> - Select files if they are symlink.
  • +
  • <ownedBy> - + Select files if they are owned by a given user.
  • Contains Selector

    @@ -1041,6 +1043,29 @@

    Since Ant 1.10.0

    +

    OwnedBy Selector

    + +

    The <ownedBy> selector selects only files + that are owned by the given user. Ant only invokes + java.nio.file.Files#getOwner so if a file system + doesn't support the operation this selector will not select + the file.

    + +

    Since Ant 1.10.0

    + + + + + + + + + + + + +
    AttributeDescriptionRequired
    ownerUsername of the expected owneryes
    +

    Script Selector

    diff --git a/src/main/org/apache/tools/ant/types/AbstractFileSet.java b/src/main/org/apache/tools/ant/types/AbstractFileSet.java index bf1560771..bc3d02ef9 100644 --- a/src/main/org/apache/tools/ant/types/AbstractFileSet.java +++ b/src/main/org/apache/tools/ant/types/AbstractFileSet.java @@ -43,6 +43,7 @@ import org.apache.tools.ant.types.selectors.MajoritySelector; import org.apache.tools.ant.types.selectors.NoneSelector; import org.apache.tools.ant.types.selectors.NotSelector; import org.apache.tools.ant.types.selectors.OrSelector; +import org.apache.tools.ant.types.selectors.OwnedBySelector; import org.apache.tools.ant.types.selectors.PresentSelector; import org.apache.tools.ant.types.selectors.ReadableSelector; import org.apache.tools.ant.types.selectors.SelectSelector; @@ -799,14 +800,27 @@ public abstract class AbstractFileSet extends DataType appendSelector(w); } + /** + * @since 1.10.0 + */ public void addExecutable(ExecutableSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ public void addSymlink(SymlinkSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ + public void addOwnedBy(OwnedBySelector o) { + appendSelector(o); + } + /** * Add an arbitrary selector. * @param selector the FileSelector to add. 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 c2f41a796..3039d481a 100644 --- a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java @@ -312,14 +312,27 @@ public abstract class AbstractSelectorContainer extends DataType appendSelector(w); } + /** + * @since 1.10.0 + */ public void addExecutable(ExecutableSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ public void addSymlink(SymlinkSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ + public void addOwnedBy(OwnedBySelector o) { + appendSelector(o); + } + /** * add an arbitrary selector * @param selector the selector to add 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 e7aa6121f..3fcccd7cb 100644 --- a/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java @@ -315,14 +315,27 @@ public abstract class BaseSelectorContainer extends BaseSelector appendSelector(w); } + /** + * @since 1.10.0 + */ public void addExecutable(ExecutableSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ public void addSymlink(SymlinkSelector e) { appendSelector(e); } + /** + * @since 1.10.0 + */ + public void addOwnedBy(OwnedBySelector o) { + appendSelector(o); + } + /** * add an arbitrary selector * @param selector the selector to add diff --git a/src/main/org/apache/tools/ant/types/selectors/OwnedBySelector.java b/src/main/org/apache/tools/ant/types/selectors/OwnedBySelector.java new file mode 100644 index 000000000..7f344980a --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/OwnedBySelector.java @@ -0,0 +1,78 @@ +/* + * 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.nio.file.attribute.UserPrincipal; +import java.io.File; +import java.io.IOException; + +import org.apache.tools.ant.BuildException; +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 files based on their owner. + * + *

    Owner is defined in terms of {@link + * java.nio.file.Files#getOwner}, this means the selector will accept + * any file that exists and is owned by the given user. If the {@code + * getOwner} method throws an {@code UnsupportedOperattionException} + * the file in question is not included.

    + * + * @since Ant 1.10.0 + */ +public class OwnedBySelector implements FileSelector, ResourceSelector { + + private String owner; + + /** + * Sets the User-Name to look for. + * @param the user name + */ + public void setOwner(String owner) { + this.owner = owner; + } + + @Override + public boolean isSelected(File basedir, String filename, File file) { + if (owner == null) { + throw new BuildException("the owner attribute is required"); + } + if (file != null) { + try { + UserPrincipal user = Files.getOwner(file.toPath()); + return user != null && owner.equals(user.getName()); + } catch (UnsupportedOperationException | IOException ex) { + // => not the expected owner + } + } + return false; + } + + @Override + public boolean isSelected(Resource r) { + FileProvider fp = r.as(FileProvider.class); + if (fp != null) { + return isSelected(null, null, fp.getFile()); + } + return false; + } +} diff --git a/src/tests/antunit/types/selectors/ownedby-test.xml b/src/tests/antunit/types/selectors/ownedby-test.xml new file mode 100644 index 000000000..7949c41f1 --- /dev/null +++ b/src/tests/antunit/types/selectors/ownedby-test.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +