ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject ant git commit: setpermissions task using NIO rather than platform tools
Date Sun, 17 Apr 2016 15:30:40 GMT
Repository: ant
Updated Branches:
  refs/heads/master 97a0b26c7 -> cee2935c7


setpermissions task using NIO rather than platform tools


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/cee2935c
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/cee2935c
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/cee2935c

Branch: refs/heads/master
Commit: cee2935c795303db59a6e956d32d8f731dbe65e2
Parents: 97a0b26
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sun Apr 17 17:30:07 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sun Apr 17 17:30:07 2016 +0200

----------------------------------------------------------------------
 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(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/WHATSNEW
----------------------------------------------------------------------
diff --git a/WHATSNEW b/WHATSNEW
index 2caaec4..125b042 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -23,6 +23,9 @@ Other changes:
 
  * New file selectors <executable>, <symlink> and <ownedBy>.
 
+ * New task <setpermissions> that provides the ability to set POSIX
+   compatible permssions via NIO's PosixFilePermission
+
 Changes from Ant 1.9.6 TO Ant 1.9.7
 ===================================
 

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/manual/Tasks/attrib.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/attrib.html b/manual/Tasks/attrib.html
index a26f0c5..ab09f65 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.</p-->
   the Windows command, you can use the task's os attribute and set its
   value to your current os.</p>
 
+<p>See the <a href="setpermissions.html">setpermissions</a> task for a
+  platform independent alternative.</p>
+
 <h3>Parameters</h3>
 <table border="1" cellpadding="2" cellspacing="0">
   <tr>

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/manual/Tasks/chmod.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/chmod.html b/manual/Tasks/chmod.html
index 74e71d0..59ecc5a 100644
--- a/manual/Tasks/chmod.html
+++ b/manual/Tasks/chmod.html
@@ -63,6 +63,9 @@ could use as initial value for these experiments.</p>
   the Unix command, you can use the task's os attribute and set its
   value to your current os.</p>
 
+<p>See the <a href="setpermissions.html">setpermissions</a> task for a
+  platform independent alternative.</p>
+
 <h3>Parameters</h3>
 <table border="1" cellpadding="2" cellspacing="0">
   <tr>

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/manual/Tasks/setpermissions.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/setpermissions.html b/manual/Tasks/setpermissions.html
new file mode 100644
index 0000000..6d0189e
--- /dev/null
+++ b/manual/Tasks/setpermissions.html
@@ -0,0 +1,96 @@
+<!--
+   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.
+-->
+<html>
+
+<head>
+<meta http-equiv="Content-Language" content="en-us">
+<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
+<title>SetPermissions Task</title>
+</head>
+
+<body>
+
+<h2><a name="setpermissions">SetPermissions</a></h2>
+<p><em>Since Ant 1.10.0.</em></p>
+<h3>Description</h3>
+<p>Changes the file permissions using Java's NIO support for
+  permissions.</p>
+<p>This task provides a subset of the platform specific abilities of
+  <a href="chmod.html">chmod</a> and <a href="attrib.html">attrib</a>
+  in a platform independent way.</p>
+<p>If no permissions are specified either via the mode or the
+  permissions attribute, then all permissions will be removed from the
+  nested resources.</p>
+<p>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.</p>
+<p>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.</p>
+
+<h3>Parameters</h3>
+<table border="1" cellpadding="2" cellspacing="0">
+  <tr>
+    <td valign="top"><b>Attribute</b></td>
+    <td valign="top"><b>Description</b></td>
+    <td align="center" valign="top"><b>Required</b></td>
+  </tr>
+  <tr>
+    <td valign="top">permissions</td>
+    <td valign="top">The permissions to set as comma separated list of
+    names
+    of <a href="http://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFilePermission.html">PosixFilePermission</a>
+    values.</td>
+    <td valign="top" align="center">No</td>
+  </tr>
+  <tr>
+    <td valign="top">mode</td>
+    <td valign="top">The permissions to set as tradional Unix
+      three-digit octal number.</td>
+    <td valign="top" align="center">No</td>
+  </tr>
+  <tr>
+    <td valign="top">failonerror</td>
+    <td valign="top">Whether to stop the build if setting permissions
+      fails.</td>
+    <td valign="top" align="center">No, defaults to true</td>
+  </tr>
+</table>
+<h3>Parameters specified as nested elements</h3>
+
+<h4>any resource collection</h4>
+<p><a href="../Types/resources.html#collection">Resource
+Collection</a>s are used to select groups of resources.</p>
+<h3>Examples</h3>
+  <blockquote><pre>
+&lt;setpermissions mode=&quot;755&quot;&gt;
+  &lt;file file=&quot;${dist}/start.sh&quot;/&gt;
+&lt;/setpermissions&gt;
+  </pre></blockquote>
+<p>makes the &quot;start.sh&quot; file readable and executable for
+  anyone and in addition writable by the owner.</p>
+  <blockquote><pre>
+&lt;setpermissions permissions=&quot;OWNER_READ,OWNER_WRITE,OWNER_EXECUTE,OTHERS_READ,OTHERS_EXECUTE,GROUP_READ,GROUP_EXECUTE&quot;&gt;
+  &lt;file file=&quot;${dist}/start.sh&quot;/&gt;
+&lt;/setpermissions&gt;
+  </pre></blockquote>
+<p>makes the &quot;start.sh&quot; file readable and executable for
+  anyone and in addition writable by the owner.</p>
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/manual/tasklist.html
----------------------------------------------------------------------
diff --git a/manual/tasklist.html b/manual/tasklist.html
index 003ba75..b57c851 100644
--- a/manual/tasklist.html
+++ b/manual/tasklist.html
@@ -147,6 +147,7 @@
 <li><a href="Tasks/rexec.html">RExec</a></li>
 <li><a href="Tasks/rmic.html">Rmic</a></li>
 <li><a href="Tasks/rpm.html">Rpm</a></li>
+<li><a href="Tasks/setpermissions.html">SetPermissions</a></li>
 <li><a href="Tasks/schemavalidate.html">SchemaValidate</a></li>
 <li><a href="Tasks/scp.html">Scp</a></li>
 <li><a href="Tasks/script.html">Script</a></li>

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/manual/tasksoverview.html
----------------------------------------------------------------------
diff --git a/manual/tasksoverview.html b/manual/tasksoverview.html
index 34b24fb..d04fb4f 100644
--- a/manual/tasksoverview.html
+++ b/manual/tasksoverview.html
@@ -576,6 +576,11 @@ documentation.</p>
   </tr>
 
   <tr valign="top">
+    <td nowrap><a href="Tasks/setpermissions.html">SetPermissions</a></td>
+    <td><p>Changes the permissions of a collection of resources.</p></td>
+  </tr>
+
+  <tr valign="top">
     <td nowrap><a href="Tasks/sync.html">Sync</a></td>
     <td><p>Synchronize two directory trees.</p></td>
   </tr>

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
----------------------------------------------------------------------
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 0000000..b417060
--- /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.
+ *
+ * <p>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.</p>
+ *
+ * @since Ant 1.10.0
+ */
+public class SetPermissions extends Task {
+    private final Set<PosixFilePermission> 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 <code>String</code> 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);
+                }
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/src/main/org/apache/tools/ant/taskdefs/defaults.properties
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
index e1022fb..06507a3 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

http://git-wip-us.apache.org/repos/asf/ant/blob/cee2935c/src/tests/antunit/taskdefs/setpermissions-test.xml
----------------------------------------------------------------------
diff --git a/src/tests/antunit/taskdefs/setpermissions-test.xml b/src/tests/antunit/taskdefs/setpermissions-test.xml
new file mode 100644
index 0000000..61f771c
--- /dev/null
+++ b/src/tests/antunit/taskdefs/setpermissions-test.xml
@@ -0,0 +1,67 @@
+<?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" />
+
+  <target name="setUp">
+    <mkdir dir="${input}"/>
+    <property name="file" location="${input}/file"/>
+    <touch file="${file}"/>
+  </target>
+
+  <target name="testRequiresNestedResources" depends="setUp">
+    <au:expectfailure expectedMessage="At least one resource-collection is required">
+      <setpermissions mode="644" />
+    </au:expectfailure>
+  </target>
+
+  <target name="testPermissionsSetViaPermissionsAttribute"
+          depends="setUp">
+    <setpermissions permissions="OWNER_READ,GROUP_READ,OTHERS_READ">
+      <file file="${file}"/>
+    </setpermissions>
+    <au:assertFalse>
+      <isfileselected file="${file}">
+        <writable/>
+      </isfileselected>
+    </au:assertFalse>
+    <au:assertTrue>
+      <isfileselected file="${file}">
+        <readable/>
+      </isfileselected>
+    </au:assertTrue>
+  </target>
+
+  <target name="testPermissionsSetViaModeAttribute"
+          depends="setUp">
+    <setpermissions mode="444">
+      <file file="${file}"/>
+    </setpermissions>
+    <au:assertFalse>
+      <isfileselected file="${file}">
+        <writable/>
+      </isfileselected>
+    </au:assertFalse>
+    <au:assertTrue>
+      <isfileselected file="${file}">
+        <readable/>
+      </isfileselected>
+    </au:assertTrue>
+  </target>
+</project>


Mime
View raw message