ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [1/3] ant git commit: optional callbacks for non-POSIX file systems
Date Mon, 16 May 2016 09:47:41 GMT
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));
     }
 }


Mime
View raw message