FAQ
Repository: ant
Updated Branches:
   refs/heads/master fc0b2ab11 -> 24f3afe79


optional callbacks for non-POSIX file systems


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

Branch: refs/heads/master
Commit: 6ab698cf9ae4fd3fb1aaf33fdce7dccbcf857247
Parents: fc0b2ab
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Mon May 16 10:58:42 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Mon May 16 10:58:42 2016 +0200

----------------------------------------------------------------------
  .../tools/ant/taskdefs/SetPermissions.java | 2 +-
  .../apache/tools/ant/util/PermissionUtils.java | 44 ++++++++++++++++----
  .../tools/ant/util/PermissionUtilsTest.java | 12 +++---
  3 files changed, 44 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/6ab698cf/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 b1ccc5a..f06c4af 100644
--- a/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
+++ b/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
@@ -101,7 +101,7 @@ public class SetPermissions extends Task {
           for (Resource r : resources) {
            currentResource = r;
               try {
- PermissionUtils.setPermissions(r, permissions);
+ PermissionUtils.setPermissions(r, permissions, null);
               } catch (IOException ioe) {
                   maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", r, ioe.getMessage());
               }

http://git-wip-us.apache.org/repos/asf/ant/blob/6ab698cf/src/main/org/apache/tools/ant/util/PermissionUtils.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/util/PermissionUtils.java b/src/main/org/apache/tools/ant/util/PermissionUtils.java
index 6fbf163..ad363f0 100644
--- a/src/main/org/apache/tools/ant/util/PermissionUtils.java
+++ b/src/main/org/apache/tools/ant/util/PermissionUtils.java
@@ -21,9 +21,12 @@ import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.PosixFileAttributeView;
  import java.nio.file.attribute.PosixFilePermission;
  import java.util.EnumSet;
  import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;

  import org.apache.tools.ant.types.Resource;
  import org.apache.tools.ant.types.resources.ArchiveResource;
@@ -48,7 +51,7 @@ public class PermissionUtils {
       * @return the "mode"
       */
      public static int modeFromPermissions(Set<PosixFilePermission> permissions,
- FileType type) {
+ FileType type) {
          int mode;
          switch (type) {
          case SYMLINK:
@@ -101,12 +104,24 @@ public class PermissionUtils {
       *
       * @param resource the resource to set permissions for
       * @param permissions the permissions
+ * @param posixNotSupportedCallback optional callback that is
+ * invoked for a file provider resource if the file-system holding
+ * the file doesn't support PosixFilePermissions. The Path
+ * corresponding to the file is passed to the callback.
       */
- public static void setPermissions(Resource r, Set<PosixFilePermission> permissions)
+ public static void setPermissions(Resource r, Set<PosixFilePermission> permissions,
+ Consumer<Path> posixNotSupportedCallback)
          throws IOException {
          FileProvider f = r.as(FileProvider.class);
          if (f != null) {
- Files.setPosixFilePermissions(f.getFile().toPath(), permissions);
+ Path p = f.getFile().toPath();
+ PosixFileAttributeView view =
+ Files.getFileAttributeView(p, PosixFileAttributeView.class);
+ if (view != null) {
+ view.setPermissions(permissions);
+ } else if (posixNotSupportedCallback != null) {
+ posixNotSupportedCallback.accept(p);
+ }
          } else if (r instanceof ArchiveResource) {
              ((ArchiveResource) r).setMode(modeFromPermissions(permissions,
                                                                FileType.of(r)));
@@ -114,8 +129,10 @@ public class PermissionUtils {
      }

      /**
- * Sets permissions of a {@link Resource} - doesn't returns an
- * empty set for unsupported resource types.
+ * Sets permissions of a {@link Resource} - returns an empty set
+ * for unsupported resource types or file systems that don't
+ * support PosixFilePermissions and no fallback has been
+ * provided..
       *
       * <p>Supported types are:</p>
       * <ul>
@@ -124,12 +141,25 @@ public class PermissionUtils {
       * </ul>
       *
       * @param resource the resource to read permissions from
+ * @param posixNotSupportedFallback optional fallback function to provide
+ * permissions for file system that don't support
+ * PosixFilePermissions. The Path corresponding to the file is
+ * passed to the callback.
       * @return the permissions
       */
- public static Set<PosixFilePermission> getPermissions(Resource r) throws IOException {
+ public static Set<PosixFilePermission> getPermissions(Resource r,
+ Function<Path, Set<PosixFilePermission>> posixNotSupportedFallback)
+ throws IOException {
          FileProvider f = r.as(FileProvider.class);
          if (f != null) {
- return Files.getPosixFilePermissions(f.getFile().toPath());
+ Path p = f.getFile().toPath();
+ PosixFileAttributeView view =
+ Files.getFileAttributeView(p, PosixFileAttributeView.class);
+ if (view != null) {
+ return view.readAttributes().permissions();
+ } else if (posixNotSupportedFallback != null) {
+ return posixNotSupportedFallback.apply(p);
+ }
          } else if (r instanceof ArchiveResource) {
              return permissionsFromMode(((ArchiveResource) r).getMode());
          }

http://git-wip-us.apache.org/repos/asf/ant/blob/6ab698cf/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java
index eed265d..6ec486a 100644
--- a/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java
+++ b/src/tests/junit/org/apache/tools/ant/util/PermissionUtilsTest.java
@@ -105,8 +105,8 @@ public class PermissionUtilsTest {
                         PosixFilePermission.OWNER_WRITE,
                         PosixFilePermission.OWNER_EXECUTE,
                         PosixFilePermission.GROUP_READ);
- PermissionUtils.setPermissions(new FileResource(f), s);
- assertEquals(s, PermissionUtils.getPermissions(new FileResource(f)));
+ PermissionUtils.setPermissions(new FileResource(f), s, null);
+ assertEquals(s, PermissionUtils.getPermissions(new FileResource(f), null));
      }

      @Test
@@ -127,8 +127,8 @@ public class PermissionUtilsTest {
                         PosixFilePermission.OWNER_WRITE,
                         PosixFilePermission.OWNER_EXECUTE,
                         PosixFilePermission.GROUP_READ);
- PermissionUtils.setPermissions(r, s);
- assertEquals(s, PermissionUtils.getPermissions(r));
+ PermissionUtils.setPermissions(r, s, null);
+ assertEquals(s, PermissionUtils.getPermissions(r, null));
      }

      @Test
@@ -149,7 +149,7 @@ public class PermissionUtilsTest {
                         PosixFilePermission.OWNER_WRITE,
                         PosixFilePermission.OWNER_EXECUTE,
                         PosixFilePermission.GROUP_READ);
- PermissionUtils.setPermissions(r, s);
- assertEquals(s, PermissionUtils.getPermissions(r));
+ PermissionUtils.setPermissions(r, s, null);
+ assertEquals(s, PermissionUtils.getPermissions(r, null));
      }
  }

Search Discussions

  • Bodewig at May 16, 2016 at 9:47 am
    whitespace


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

    Branch: refs/heads/master
    Commit: 24f3afe7920e414a0ef83732fe4ea5aa58d17bce
    Parents: 04b9e2b
    Author: Stefan Bodewig <bodewig@apache.org>
    Authored: Mon May 16 11:46:04 2016 +0200
    Committer: Stefan Bodewig <bodewig@apache.org>
    Committed: Mon May 16 11:46:04 2016 +0200

    ----------------------------------------------------------------------
      .../tools/ant/taskdefs/SetPermissions.java | 36 ++++++++++----------
      1 file changed, 18 insertions(+), 18 deletions(-)
    ----------------------------------------------------------------------


    http://git-wip-us.apache.org/repos/asf/ant/blob/24f3afe7/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 e78914b..8716bee 100644
    --- a/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
    +++ b/src/main/org/apache/tools/ant/taskdefs/SetPermissions.java
    @@ -143,29 +143,29 @@ public class SetPermissions extends Task {
              }
              Resource currentResource = null;
              try {
    - for (Resource r : resources) {
    - currentResource = r;
    - try {
    - PermissionUtils.setPermissions(r, permissions, this::posixPermissionsNotSupported);
    - } catch (IOException ioe) {
    - maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", r, ioe.getMessage());
    - }
    - }
    + for (Resource r : resources) {
    + currentResource = r;
    + try {
    + PermissionUtils.setPermissions(r, permissions, this::posixPermissionsNotSupported);
    + } catch (IOException ioe) {
    + maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", r, ioe.getMessage());
    + }
    + }
              } catch (ClassCastException uoe) {
    - maybeThrowException(null, "some specified permissions are not of type PosixFilePermission: %s", StringUtils.join(permissions, ", "));
    + maybeThrowException(null, "some specified permissions are not of type PosixFilePermission: %s", StringUtils.join(permissions, ", "));
              } catch (SecurityException uoe) {
    - maybeThrowException(null, "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'", currentResource);
    + maybeThrowException(null, "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'", currentResource);
              }
          }

    - private void maybeThrowException(Exception ioe, String msgFormat, Object... msgArgs) {
    - String msg = String.format(msgFormat, msgArgs);
    - if (failonerror) {
    - throw new BuildException(msg, ioe);
    - } else {
    - log("Warning: " + msg, Project.MSG_ERR);
    - }
    - }
    + private void maybeThrowException(Exception ioe, String msgFormat, Object... msgArgs) {
    + String msg = String.format(msgFormat, msgArgs);
    + if (failonerror) {
    + throw new BuildException(msg, ioe);
    + } else {
    + log("Warning: " + msg, Project.MSG_ERR);
    + }
    + }

          private void posixPermissionsNotSupported(Path p) {
              String msg = String.format("the associated path '%s' does"
  • Bodewig at May 16, 2016 at 9:47 am
    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 <bodewig@apache.org>
    Authored: Mon May 16 11:41:07 2016 +0200
    Committer: Stefan Bodewig <bodewig@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>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnotifications @
categoriesant
postedMay 16, '16 at 9:47a
activeMay 16, '16 at 9:47a
posts3
users1
websiteant.apache.org

1 user in discussion

Bodewig: 3 posts

People

Translate

site design / logo © 2018 Grokbase