You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2016/05/16 09:47:42 UTC

[2/3] ant git commit: implement DOS-friendly fallback in setpermissions

implement DOS-friendly fallback in setpermissions


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

Branch: refs/heads/master
Commit: 04b9e2bd1771a2eed96b6bbb762b4dbadb746039
Parents: 6ab698c
Author: Stefan Bodewig <bo...@apache.org>
Authored: Mon May 16 11:41:07 2016 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Mon May 16 11:44:58 2016 +0200

----------------------------------------------------------------------
 manual/Tasks/setpermissions.html                |  13 +++
 .../tools/ant/taskdefs/SetPermissions.java      | 102 ++++++++++++++++++-
 .../antunit/taskdefs/setpermissions-test.xml    |   5 +-
 3 files changed, 114 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/04b9e2bd/manual/Tasks/setpermissions.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/setpermissions.html b/manual/Tasks/setpermissions.html
index 6d0189e..22a6fb8 100644
--- a/manual/Tasks/setpermissions.html
+++ b/manual/Tasks/setpermissions.html
@@ -66,6 +66,19 @@
     <td valign="top" align="center">No</td>
   </tr>
   <tr>
+    <td valign="top">nonPosixMode</td>
+    <td valign="top">What to do if changing the permissions of a file
+      is not possible because the file-system doesn't support POSIX
+      file permissions. Possible options are <code>fail</code> (fail
+      the build), <code>pass</code> (just log an
+      error), <code>tryDosOrFail</code> (at least try to set the
+      read-only flag on DOS file systems, fail if that isn't possible
+      either) and <code>tryDosOrPass</code> (at least try to set the
+      read-only flag on DOS file systems, just log an error if that
+      isn't possible either).</td>
+    <td valign="top" align="center">No, defaults to <code>fail</code></td>
+  </tr>
+  <tr>
     <td valign="top">failonerror</td>
     <td valign="top">Whether to stop the build if setting permissions
       fails.</td>

http://git-wip-us.apache.org/repos/asf/ant/blob/04b9e2bd/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
index f06c4af..e78914b 100644
--- a/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
+++ b/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
@@ -19,6 +19,9 @@
 package org.apache.tools.ant.taskdefs;
 
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.DosFileAttributeView;
 import java.nio.file.attribute.PosixFilePermission;
 import java.util.Arrays;
 import java.util.EnumSet;
@@ -38,7 +41,13 @@ import org.apache.tools.ant.util.StringUtils;
  *
  * <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>
+ * in less platform dependent way.</p>
+ *
+ * <p>It requires a file system that supports PosixFilePermissions for
+ * its full potential. It can optionally fall back to
+ * DosFilePermissions (only changing the readonly state) on file
+ * systems that don't support POSIX permissions. See {@link
+ * SetPermissions.NonPosixMode}</p>
  *
  * @since Ant 1.10.0
  */
@@ -47,6 +56,28 @@ public class SetPermissions extends Task {
         EnumSet.noneOf(PosixFilePermission.class);
     private Resources resources = null;
     private boolean failonerror = true;
+    private NonPosixMode nonPosixMode = NonPosixMode.fail;
+
+    /**
+     * Options for dealing with file systems that don't support POSIX
+     * permissions.
+     */
+    public enum NonPosixMode {
+        /** Fail the build. */
+        fail,
+        /** Log an error and go on. */
+        pass,
+        /**
+         * Try DosFilePermissions - setting the read-only flag - and
+         * fail the build if that fails as well.
+         */
+        tryDosOrFail,
+        /**
+         * Try DosFilePermissions - setting the read-only flag - and
+         * log an error and go on if that fails as well.
+         */
+        tryDosOrPass;
+    }
 
     /**
      * Adds permissions as a comma separated list.
@@ -75,6 +106,9 @@ public class SetPermissions extends Task {
     /**
      * Set whether to fail when errors are encountered. If false, note errors
      * to the output but keep going. Default is true.
+     * <p>Only applies to IO and SecurityExceptions, see {@link
+     * #setNonPosixMode} for ways to deal with file-systems that don't
+     * support PosixPermissions.</p>
      * @param failonerror true or false.
      */
     public void setFailOnError(final boolean failonerror) {
@@ -82,6 +116,17 @@ public class SetPermissions extends Task {
     }
 
     /**
+     * Set what to do if changing the permissions of a file is not
+     * possible because the file-system doesn't support POSIX file
+     * permissions.
+     * <p>The default is {@link NonPosixMode#fail}.</p>
+     * @param m what to do if changing the permissions of a file is not possible
+     */
+    public void setNonPosixMode(NonPosixMode m) {
+        this.nonPosixMode = m;
+    }
+
+    /**
      * Adds a collection of resources to set permissions on.
      * @param rc a resource collection
      */
@@ -101,13 +146,11 @@ public class SetPermissions extends Task {
 	        for (Resource r : resources) {
 	        	currentResource = r;
 	            try {
-	                PermissionUtils.setPermissions(r, permissions, null);
+	                PermissionUtils.setPermissions(r, permissions, this::posixPermissionsNotSupported);
 	            } catch (IOException ioe) {
 	                maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", r, ioe.getMessage());
 	            }
 	        }
-        } catch (UnsupportedOperationException uoe) {
-        	maybeThrowException(null, "the associated file system of resource '%s' does not support the PosixFileAttributeView", currentResource);
         } catch (ClassCastException uoe) {
         	maybeThrowException(null, "some specified permissions are not of type PosixFilePermission: %s", StringUtils.join(permissions, ", "));
         } catch (SecurityException uoe) {
@@ -123,4 +166,55 @@ public class SetPermissions extends Task {
 		    log("Warning: " + msg, Project.MSG_ERR);
 		}
 	}
+
+    private void posixPermissionsNotSupported(Path p) {
+        String msg = String.format("the associated path '%s' does"
+                                   + " not support the PosixFileAttributeView", p);
+        switch (nonPosixMode) {
+        case fail:
+            throw new BuildException(msg);
+        case pass:
+            log("Warning: " + msg, Project.MSG_ERR);
+            break;
+        case tryDosOrFail:
+            tryDos(p, true);
+            break;
+        case tryDosOrPass:
+            tryDos(p, false);
+            break;
+        }
+    }
+
+    private void tryDos(Path p, boolean failIfDosIsNotSupported) {
+        log("Falling back to DosFileAttributeView");
+        boolean readOnly = !isWritable();
+        DosFileAttributeView view = Files.getFileAttributeView(p, DosFileAttributeView.class);
+        if (view != null) {
+            try {
+                view.setReadOnly(readOnly);
+            } catch (IOException ioe) {
+                maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s",
+                                    p, ioe.getMessage());
+            } catch (SecurityException uoe) {
+        	maybeThrowException(null, "the SecurityManager denies role "
+                                    + "accessUserInformation or write access for "
+                                    + "SecurityManager.checkWrite for resource '%s'",
+                                    p);
+            }
+        } else {
+            String msg = String.format("the associated path '%s' does"
+                                       + " not support the DosFileAttributeView", p);
+            if (failIfDosIsNotSupported) {
+                throw new BuildException(msg);
+            } else {
+                log("Warning: " + msg, Project.MSG_ERR);
+            }
+        }
+    }
+
+    private boolean isWritable() {
+        return permissions.contains(PosixFilePermission.OWNER_WRITE)
+            || permissions.contains(PosixFilePermission.GROUP_WRITE)
+            || permissions.contains(PosixFilePermission.OTHERS_WRITE);
+    }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/04b9e2bd/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
index 61f771c..99b90e0 100644
--- a/src/tests/antunit/taskdefs/setpermissions-test.xml
+++ b/src/tests/antunit/taskdefs/setpermissions-test.xml
@@ -33,7 +33,8 @@
 
   <target name="testPermissionsSetViaPermissionsAttribute"
           depends="setUp">
-    <setpermissions permissions="OWNER_READ,GROUP_READ,OTHERS_READ">
+    <setpermissions permissions="OWNER_READ,GROUP_READ,OTHERS_READ"
+                    nonPosixMode="tryDosOrFail">
       <file file="${file}"/>
     </setpermissions>
     <au:assertFalse>
@@ -50,7 +51,7 @@
 
   <target name="testPermissionsSetViaModeAttribute"
           depends="setUp">
-    <setpermissions mode="444">
+    <setpermissions mode="444" nonPosixMode="tryDosOrFail">
       <file file="${file}"/>
     </setpermissions>
     <au:assertFalse>