< prev index next >

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java

Print this page
rev 54963 : 8213031: (zipfs) Add support for POSIX file permissions

@@ -32,15 +32,11 @@
 import java.net.URI;
 import java.nio.channels.FileChannel;
 import java.nio.channels.SeekableByteChannel;
 import java.nio.file.*;
 import java.nio.file.DirectoryStream.Filter;
-import java.nio.file.attribute.BasicFileAttributeView;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.nio.file.attribute.FileAttribute;
-import java.nio.file.attribute.FileAttributeView;
-import java.nio.file.attribute.FileTime;
+import java.nio.file.attribute.*;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Objects;

@@ -709,20 +705,32 @@
             throw new NullPointerException();
         if (type == BasicFileAttributeView.class)
             return (V)new ZipFileAttributeView(this, false);
         if (type == ZipFileAttributeView.class)
             return (V)new ZipFileAttributeView(this, true);
+        if (zfs.supportPosix) {
+            if (type == PosixFileAttributeView.class)
+                return (V)new ZipPosixFileAttributeView(this, false);
+            if (type == FileOwnerAttributeView.class)
+                return (V)new ZipPosixFileAttributeView(this,true);
+        }
         throw new UnsupportedOperationException("view <" + type + "> is not supported");
     }
 
     private ZipFileAttributeView getFileAttributeView(String type) {
         if (type == null)
             throw new NullPointerException();
         if ("basic".equals(type))
             return new ZipFileAttributeView(this, false);
         if ("zip".equals(type))
             return new ZipFileAttributeView(this, true);
+        if (zfs.supportPosix) {
+            if ("posix".equals(type))
+                return new ZipPosixFileAttributeView(this, false);
+            if ("owner".equals(type))
+                return new ZipPosixFileAttributeView(this, true);
+        }
         throw new UnsupportedOperationException("view <" + type + "> is not supported");
     }
 
     void createDirectory(FileAttribute<?>... attrs)
         throws IOException

@@ -762,14 +770,20 @@
         return zfas;
     }
 
     @SuppressWarnings("unchecked") // Cast to A
     <A extends BasicFileAttributes> A readAttributes(Class<A> type) throws IOException {
+        // unconditionally support BasicFileAttributes and ZipFileAttributes
         if (type == BasicFileAttributes.class || type == ZipFileAttributes.class) {
             return (A)readAttributes();
         }
 
+        // support PosixFileAttributes when activated
+        if (type == PosixFileAttributes.class && zfs.supportPosix) {
+            return (A)readAttributes();
+        }
+
         throw new UnsupportedOperationException("Attributes of type " +
             type.getName() + " not supported");
     }
 
     void setAttribute(String attribute, Object value, LinkOption... options)

@@ -792,13 +806,26 @@
         throws IOException
     {
         zfs.setTimes(getResolvedPath(), mtime, atime, ctime);
     }
 
-    Map<String, Object> readAttributes(String attributes, LinkOption... options)
+    void setOwner(UserPrincipal owner) throws IOException {
+        zfs.setOwner(getResolvedPath(), owner);
+    }
+
+    void setPermissions(Set<PosixFilePermission> perms)
         throws IOException
+    {
+        zfs.setPermissions(getResolvedPath(), perms);
+    }
 
+    void setGroup(GroupPrincipal group) throws IOException {
+        zfs.setGroup(getResolvedPath(), group);
+    }
+
+    Map<String, Object> readAttributes(String attributes, LinkOption... options)
+        throws IOException
     {
         String view;
         String attrs;
         int colonPos = attributes.indexOf(':');
         if (colonPos == -1) {

@@ -946,16 +973,18 @@
                     os.write(buf, 0, n);
                 }
             }
         }
         if (copyAttrs) {
-            BasicFileAttributeView view =
-                target.getFileAttributeView(BasicFileAttributeView.class);
+            ZipFileAttributeView view =
+                target.getFileAttributeView(ZipFileAttributeView.class);
             try {
                 view.setTimes(zfas.lastModifiedTime(),
                               zfas.lastAccessTime(),
                               zfas.creationTime());
+                // copy permissions
+                view.setPermissions(zfas.storedPermissions().orElse(null));
             } catch (IOException x) {
                 // rollback?
                 try {
                     target.delete();
                 } catch (IOException ignore) { }
< prev index next >