Browse Source

setpermissions task using NIO rather than platform tools

master
Stefan Bodewig 9 years ago
parent
commit
cee2935c79
9 changed files with 291 additions and 0 deletions
  1. +3
    -0
      WHATSNEW
  2. +3
    -0
      manual/Tasks/attrib.html
  3. +3
    -0
      manual/Tasks/chmod.html
  4. +96
    -0
      manual/Tasks/setpermissions.html
  5. +1
    -0
      manual/tasklist.html
  6. +5
    -0
      manual/tasksoverview.html
  7. +112
    -0
      src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
  8. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  9. +67
    -0
      src/tests/antunit/taskdefs/setpermissions-test.xml

+ 3
- 0
WHATSNEW View File

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



+ 3
- 0
manual/Tasks/attrib.html View File

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


+ 3
- 0
manual/Tasks/chmod.html View File

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


+ 96
- 0
manual/Tasks/setpermissions.html View File

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

+ 1
- 0
manual/tasklist.html View File

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


+ 5
- 0
manual/tasksoverview.html View File

@@ -575,6 +575,11 @@ documentation.</p>
files.</p></td>
</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>


+ 112
- 0
src/main/org/apache/tools/ant/taskdefs/SetPermissions.java View File

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

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

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


+ 67
- 0
src/tests/antunit/taskdefs/setpermissions-test.xml View File

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

Loading…
Cancel
Save