From cee2935c795303db59a6e956d32d8f731dbe65e2 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sun, 17 Apr 2016 17:30:07 +0200 Subject: [PATCH] setpermissions task using NIO rather than platform tools --- WHATSNEW | 3 + manual/Tasks/attrib.html | 3 + manual/Tasks/chmod.html | 3 + manual/Tasks/setpermissions.html | 96 +++++++++++++++ manual/tasklist.html | 1 + manual/tasksoverview.html | 5 + .../tools/ant/taskdefs/SetPermissions.java | 112 ++++++++++++++++++ .../tools/ant/taskdefs/defaults.properties | 1 + .../antunit/taskdefs/setpermissions-test.xml | 67 +++++++++++ 9 files changed, 291 insertions(+) create mode 100644 manual/Tasks/setpermissions.html create mode 100644 src/main/org/apache/tools/ant/taskdefs/SetPermissions.java create mode 100644 src/tests/antunit/taskdefs/setpermissions-test.xml diff --git a/WHATSNEW b/WHATSNEW index 2caaec44c..125b042c0 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -23,6 +23,9 @@ Other changes: * New file selectors , and . + * New task that provides the ability to set POSIX + compatible permssions via NIO's PosixFilePermission + Changes from Ant 1.9.6 TO Ant 1.9.7 =================================== diff --git a/manual/Tasks/attrib.html b/manual/Tasks/attrib.html index a26f0c555..ab09f6540 100644 --- a/manual/Tasks/attrib.html +++ b/manual/Tasks/attrib.html @@ -57,6 +57,9 @@ directory tree), so you'll have to experiment a little. the Windows command, you can use the task's os attribute and set its value to your current os.

+

See the setpermissions task for a + platform independent alternative.

+

Parameters

diff --git a/manual/Tasks/chmod.html b/manual/Tasks/chmod.html index 74e71d0a9..59ecc5a9f 100644 --- a/manual/Tasks/chmod.html +++ b/manual/Tasks/chmod.html @@ -63,6 +63,9 @@ could use as initial value for these experiments.

the Unix command, you can use the task's os attribute and set its value to your current os.

+

See the setpermissions task for a + platform independent alternative.

+

Parameters

diff --git a/manual/Tasks/setpermissions.html b/manual/Tasks/setpermissions.html new file mode 100644 index 000000000..6d0189ede --- /dev/null +++ b/manual/Tasks/setpermissions.html @@ -0,0 +1,96 @@ + + + + + + +SetPermissions Task + + + + +

SetPermissions

+

Since Ant 1.10.0.

+

Description

+

Changes the file permissions using Java's NIO support for + permissions.

+

This task provides a subset of the platform specific abilities of + chmod and attrib + in a platform independent way.

+

If no permissions are specified either via the mode or the + permissions attribute, then all permissions will be removed from the + nested resources.

+

The task accepts aribitrary resources as part of the nested + resource collections, but not all resources support setting + permissions. This task won't do anything for resources that don't + support setting permissions - for example URLs.

+

The permissions are applied to all resources contained within the + nested resources collections. You may want to ensure the collection + only returns files or directories if you want different sets of + permissions to apply to either type of resource.

+ +

Parameters

+
+ + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
permissionsThe permissions to set as comma separated list of + names + of PosixFilePermission + values.No
modeThe permissions to set as tradional Unix + three-digit octal number.No
failonerrorWhether to stop the build if setting permissions + fails.No, defaults to true
+

Parameters specified as nested elements

+ +

any resource collection

+

Resource +Collections are used to select groups of resources.

+

Examples

+
+<setpermissions mode="755">
+  <file file="${dist}/start.sh"/>
+</setpermissions>
+  
+

makes the "start.sh" file readable and executable for + anyone and in addition writable by the owner.

+
+<setpermissions permissions="OWNER_READ,OWNER_WRITE,OWNER_EXECUTE,OTHERS_READ,OTHERS_EXECUTE,GROUP_READ,GROUP_EXECUTE">
+  <file file="${dist}/start.sh"/>
+</setpermissions>
+  
+

makes the "start.sh" file readable and executable for + anyone and in addition writable by the owner.

+ + diff --git a/manual/tasklist.html b/manual/tasklist.html index 003ba7591..b57c85136 100644 --- a/manual/tasklist.html +++ b/manual/tasklist.html @@ -147,6 +147,7 @@
  • RExec
  • Rmic
  • Rpm
  • +
  • SetPermissions
  • SchemaValidate
  • Scp
  • Script
  • diff --git a/manual/tasksoverview.html b/manual/tasksoverview.html index 34b24fbd0..d04fb4fff 100644 --- a/manual/tasksoverview.html +++ b/manual/tasksoverview.html @@ -575,6 +575,11 @@ documentation.

    files.

    + + SetPermissions +

    Changes the permissions of a collection of resources.

    + + Sync

    Synchronize two directory trees.

    diff --git a/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java b/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java new file mode 100644 index 000000000..b417060c8 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java @@ -0,0 +1,112 @@ +/* + * 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.taskdefs; + +import java.io.IOException; +import java.nio.file.attribute.PosixFilePermission; +import java.util.Arrays; +import java.util.EnumSet; +import java.util.Set; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.Resources; +import org.apache.tools.ant.util.PermissionUtils; + +/** + * Sets {@link PosixFilePermission}s for resources. + * + *

    This task provides a subset of {@link Chmod}'s and {@link + * org.apache.tools.ant.taskdefs.optional.windows.Attrib}'s abilities + * in a platform independent way.

    + * + * @since Ant 1.10.0 + */ +public class SetPermissions extends Task { + private final Set permissions = + EnumSet.noneOf(PosixFilePermission.class); + private Resources resources = null; + private boolean failonerror = true; + + /** + * Adds permissions as a comma separated list. + * @param perms comma separated list of names of {@link PosixFilePermission}s. + */ + public void setPermissions(String perms) { + if (perms != null) { + Arrays.stream(perms.split(",")) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .map(s -> Enum.valueOf(PosixFilePermission.class, s)) + .forEach(permissions::add); + } + } + + /** + * A 3 digit octal string, specify the user, group and + * other modes in the standard Unix fashion; + * @param octalString a String value + */ + public void setMode(String octalString) { + int mode = Integer.parseInt(octalString, 8); + permissions.addAll(PermissionUtils.permissionsFromMode(mode)); + } + + /** + * Set whether to fail when errors are encountered. If false, note errors + * to the output but keep going. Default is true. + * @param failonerror true or false. + */ + public void setFailOnError(final boolean failonerror) { + this.failonerror = failonerror; + } + + /** + * Adds a collection of resources to set permissions on. + * @param rc a resource collection + */ + public void add(ResourceCollection rc) { + if (resources == null) { + resources = new Resources(); + } + resources.add(rc); + } + + public void execute() { + if (resources == null) { + throw new BuildException("At least one resource-collection is required"); + } + for (Resource r : resources) { + try { + PermissionUtils.setPermissions(r, permissions); + } catch (IOException ioe) { + String msg = "Failed to set permissions on " + r + " due to " + + ioe.getMessage(); + if (failonerror) { + throw new BuildException(msg, ioe); + } else { + log("Warning: " + msg, Project.MSG_ERR); + } + } + } + } +} diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index e1022fb83..06507a369 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -91,6 +91,7 @@ replace=org.apache.tools.ant.taskdefs.Replace resourcecount=org.apache.tools.ant.taskdefs.ResourceCount retry=org.apache.tools.ant.taskdefs.Retry rmic=org.apache.tools.ant.taskdefs.Rmic +setpermissions=org.apache.tools.ant.taskdefs.SetPermissions sequential=org.apache.tools.ant.taskdefs.Sequential signjar=org.apache.tools.ant.taskdefs.SignJar sleep=org.apache.tools.ant.taskdefs.Sleep diff --git a/src/tests/antunit/taskdefs/setpermissions-test.xml b/src/tests/antunit/taskdefs/setpermissions-test.xml new file mode 100644 index 000000000..61f771ccc --- /dev/null +++ b/src/tests/antunit/taskdefs/setpermissions-test.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +