< 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


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nio.zipfs;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.net.URI;
  33 import java.nio.channels.FileChannel;
  34 import java.nio.channels.SeekableByteChannel;
  35 import java.nio.file.*;
  36 import java.nio.file.DirectoryStream.Filter;
  37 import java.nio.file.attribute.BasicFileAttributeView;
  38 import java.nio.file.attribute.BasicFileAttributes;
  39 import java.nio.file.attribute.FileAttribute;
  40 import java.nio.file.attribute.FileAttributeView;
  41 import java.nio.file.attribute.FileTime;
  42 import java.util.Arrays;
  43 import java.util.Iterator;
  44 import java.util.Map;
  45 import java.util.NoSuchElementException;
  46 import java.util.Objects;
  47 import java.util.Set;
  48 
  49 import static java.nio.charset.StandardCharsets.UTF_8;
  50 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  51 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  52 import static java.nio.file.StandardOpenOption.CREATE;
  53 import static java.nio.file.StandardOpenOption.READ;
  54 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  55 import static java.nio.file.StandardOpenOption.WRITE;
  56 
  57 /**
  58  * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal
  59  */
  60 final class ZipPath implements Path {
  61 


 694                 }
 695             }
 696 
 697             @Override
 698             public void remove() {
 699                 throw new ReadOnlyFileSystemException();
 700             }
 701         };
 702     }
 703 
 704     /////////////////////////////////////////////////////////////////////
 705 
 706     @SuppressWarnings("unchecked") // Cast to V
 707     <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
 708         if (type == null)
 709             throw new NullPointerException();
 710         if (type == BasicFileAttributeView.class)
 711             return (V)new ZipFileAttributeView(this, false);
 712         if (type == ZipFileAttributeView.class)
 713             return (V)new ZipFileAttributeView(this, true);






 714         throw new UnsupportedOperationException("view <" + type + "> is not supported");
 715     }
 716 
 717     private ZipFileAttributeView getFileAttributeView(String type) {
 718         if (type == null)
 719             throw new NullPointerException();
 720         if ("basic".equals(type))
 721             return new ZipFileAttributeView(this, false);
 722         if ("zip".equals(type))
 723             return new ZipFileAttributeView(this, true);






 724         throw new UnsupportedOperationException("view <" + type + "> is not supported");
 725     }
 726 
 727     void createDirectory(FileAttribute<?>... attrs)
 728         throws IOException
 729     {
 730         zfs.createDirectory(getResolvedPath(), attrs);
 731     }
 732 
 733     InputStream newInputStream(OpenOption... options) throws IOException
 734     {
 735         if (options.length > 0) {
 736             for (OpenOption opt : options) {
 737                 if (opt != READ)
 738                     throw new UnsupportedOperationException("'" + opt + "' not allowed");
 739             }
 740         }
 741         return zfs.newInputStream(getResolvedPath());
 742     }
 743 


 747         return new ZipDirectoryStream(this, filter);
 748     }
 749 
 750     void delete() throws IOException {
 751         zfs.deleteFile(getResolvedPath(), true);
 752     }
 753 
 754     private void deleteIfExists() throws IOException {
 755         zfs.deleteFile(getResolvedPath(), false);
 756     }
 757 
 758     ZipFileAttributes readAttributes() throws IOException {
 759         ZipFileAttributes zfas = zfs.getFileAttributes(getResolvedPath());
 760         if (zfas == null)
 761             throw new NoSuchFileException(toString());
 762         return zfas;
 763     }
 764 
 765     @SuppressWarnings("unchecked") // Cast to A
 766     <A extends BasicFileAttributes> A readAttributes(Class<A> type) throws IOException {

 767         if (type == BasicFileAttributes.class || type == ZipFileAttributes.class) {
 768             return (A)readAttributes();
 769         }
 770 





 771         throw new UnsupportedOperationException("Attributes of type " +
 772             type.getName() + " not supported");
 773     }
 774 
 775     void setAttribute(String attribute, Object value, LinkOption... options)
 776         throws IOException
 777     {
 778         String type;
 779         String attr;
 780         int colonPos = attribute.indexOf(':');
 781         if (colonPos == -1) {
 782             type = "basic";
 783             attr = attribute;
 784         } else {
 785             type = attribute.substring(0, colonPos++);
 786             attr = attribute.substring(colonPos);
 787         }
 788         getFileAttributeView(type).setAttribute(attr, value);
 789     }
 790 
 791     void setTimes(FileTime mtime, FileTime atime, FileTime ctime)
 792         throws IOException
 793     {
 794         zfs.setTimes(getResolvedPath(), mtime, atime, ctime);
 795     }
 796 
 797     Map<String, Object> readAttributes(String attributes, LinkOption... options)




 798         throws IOException



 799 






 800     {
 801         String view;
 802         String attrs;
 803         int colonPos = attributes.indexOf(':');
 804         if (colonPos == -1) {
 805             view = "basic";
 806             attrs = attributes;
 807         } else {
 808             view = attributes.substring(0, colonPos++);
 809             attrs = attributes.substring(colonPos);
 810         }
 811         return getFileAttributeView(view).readAttributes(attrs);
 812     }
 813 
 814     FileStore getFileStore() throws IOException {
 815         // each ZipFileSystem only has one root (as requested for now)
 816         if (exists())
 817             return zfs.getFileStore(this);
 818         throw new NoSuchFileException(zfs.getString(path));
 819     }


 931             exists = target.exists();
 932         }
 933         if (exists)
 934             throw new FileAlreadyExistsException(target.toString());
 935 
 936         if (zfas.isDirectory()) {
 937             // create directory or file
 938             target.createDirectory();
 939         } else {
 940             try (InputStream is = zfs.newInputStream(getResolvedPath());
 941                  OutputStream os = target.newOutputStream())
 942             {
 943                 byte[] buf = new byte[8192];
 944                 int n;
 945                 while ((n = is.read(buf)) != -1) {
 946                     os.write(buf, 0, n);
 947                 }
 948             }
 949         }
 950         if (copyAttrs) {
 951             BasicFileAttributeView view =
 952                 target.getFileAttributeView(BasicFileAttributeView.class);
 953             try {
 954                 view.setTimes(zfas.lastModifiedTime(),
 955                               zfas.lastAccessTime(),
 956                               zfas.creationTime());


 957             } catch (IOException x) {
 958                 // rollback?
 959                 try {
 960                     target.delete();
 961                 } catch (IOException ignore) { }
 962                 throw x;
 963             }
 964         }
 965     }
 966 
 967     private static int decode(char c) {
 968         if ((c >= '0') && (c <= '9'))
 969             return c - '0';
 970         if ((c >= 'a') && (c <= 'f'))
 971             return c - 'a' + 10;
 972         if ((c >= 'A') && (c <= 'F'))
 973             return c - 'A' + 10;
 974         assert false;
 975         return -1;
 976     }




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nio.zipfs;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.net.URI;
  33 import java.nio.channels.FileChannel;
  34 import java.nio.channels.SeekableByteChannel;
  35 import java.nio.file.*;
  36 import java.nio.file.DirectoryStream.Filter;
  37 import java.nio.file.attribute.*;




  38 import java.util.Arrays;
  39 import java.util.Iterator;
  40 import java.util.Map;
  41 import java.util.NoSuchElementException;
  42 import java.util.Objects;
  43 import java.util.Set;
  44 
  45 import static java.nio.charset.StandardCharsets.UTF_8;
  46 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  47 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  48 import static java.nio.file.StandardOpenOption.CREATE;
  49 import static java.nio.file.StandardOpenOption.READ;
  50 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  51 import static java.nio.file.StandardOpenOption.WRITE;
  52 
  53 /**
  54  * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal
  55  */
  56 final class ZipPath implements Path {
  57 


 690                 }
 691             }
 692 
 693             @Override
 694             public void remove() {
 695                 throw new ReadOnlyFileSystemException();
 696             }
 697         };
 698     }
 699 
 700     /////////////////////////////////////////////////////////////////////
 701 
 702     @SuppressWarnings("unchecked") // Cast to V
 703     <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
 704         if (type == null)
 705             throw new NullPointerException();
 706         if (type == BasicFileAttributeView.class)
 707             return (V)new ZipFileAttributeView(this, false);
 708         if (type == ZipFileAttributeView.class)
 709             return (V)new ZipFileAttributeView(this, true);
 710         if (zfs.supportPosix) {
 711             if (type == PosixFileAttributeView.class)
 712                 return (V)new ZipPosixFileAttributeView(this, false);
 713             if (type == FileOwnerAttributeView.class)
 714                 return (V)new ZipPosixFileAttributeView(this,true);
 715         }
 716         throw new UnsupportedOperationException("view <" + type + "> is not supported");
 717     }
 718 
 719     private ZipFileAttributeView getFileAttributeView(String type) {
 720         if (type == null)
 721             throw new NullPointerException();
 722         if ("basic".equals(type))
 723             return new ZipFileAttributeView(this, false);
 724         if ("zip".equals(type))
 725             return new ZipFileAttributeView(this, true);
 726         if (zfs.supportPosix) {
 727             if ("posix".equals(type))
 728                 return new ZipPosixFileAttributeView(this, false);
 729             if ("owner".equals(type))
 730                 return new ZipPosixFileAttributeView(this, true);
 731         }
 732         throw new UnsupportedOperationException("view <" + type + "> is not supported");
 733     }
 734 
 735     void createDirectory(FileAttribute<?>... attrs)
 736         throws IOException
 737     {
 738         zfs.createDirectory(getResolvedPath(), attrs);
 739     }
 740 
 741     InputStream newInputStream(OpenOption... options) throws IOException
 742     {
 743         if (options.length > 0) {
 744             for (OpenOption opt : options) {
 745                 if (opt != READ)
 746                     throw new UnsupportedOperationException("'" + opt + "' not allowed");
 747             }
 748         }
 749         return zfs.newInputStream(getResolvedPath());
 750     }
 751 


 755         return new ZipDirectoryStream(this, filter);
 756     }
 757 
 758     void delete() throws IOException {
 759         zfs.deleteFile(getResolvedPath(), true);
 760     }
 761 
 762     private void deleteIfExists() throws IOException {
 763         zfs.deleteFile(getResolvedPath(), false);
 764     }
 765 
 766     ZipFileAttributes readAttributes() throws IOException {
 767         ZipFileAttributes zfas = zfs.getFileAttributes(getResolvedPath());
 768         if (zfas == null)
 769             throw new NoSuchFileException(toString());
 770         return zfas;
 771     }
 772 
 773     @SuppressWarnings("unchecked") // Cast to A
 774     <A extends BasicFileAttributes> A readAttributes(Class<A> type) throws IOException {
 775         // unconditionally support BasicFileAttributes and ZipFileAttributes
 776         if (type == BasicFileAttributes.class || type == ZipFileAttributes.class) {
 777             return (A)readAttributes();
 778         }
 779 
 780         // support PosixFileAttributes when activated
 781         if (type == PosixFileAttributes.class && zfs.supportPosix) {
 782             return (A)readAttributes();
 783         }
 784 
 785         throw new UnsupportedOperationException("Attributes of type " +
 786             type.getName() + " not supported");
 787     }
 788 
 789     void setAttribute(String attribute, Object value, LinkOption... options)
 790         throws IOException
 791     {
 792         String type;
 793         String attr;
 794         int colonPos = attribute.indexOf(':');
 795         if (colonPos == -1) {
 796             type = "basic";
 797             attr = attribute;
 798         } else {
 799             type = attribute.substring(0, colonPos++);
 800             attr = attribute.substring(colonPos);
 801         }
 802         getFileAttributeView(type).setAttribute(attr, value);
 803     }
 804 
 805     void setTimes(FileTime mtime, FileTime atime, FileTime ctime)
 806         throws IOException
 807     {
 808         zfs.setTimes(getResolvedPath(), mtime, atime, ctime);
 809     }
 810 
 811     void setOwner(UserPrincipal owner) throws IOException {
 812         zfs.setOwner(getResolvedPath(), owner);
 813     }
 814 
 815     void setPermissions(Set<PosixFilePermission> perms)
 816         throws IOException
 817     {
 818         zfs.setPermissions(getResolvedPath(), perms);
 819     }
 820 
 821     void setGroup(GroupPrincipal group) throws IOException {
 822         zfs.setGroup(getResolvedPath(), group);
 823     }
 824 
 825     Map<String, Object> readAttributes(String attributes, LinkOption... options)
 826         throws IOException
 827     {
 828         String view;
 829         String attrs;
 830         int colonPos = attributes.indexOf(':');
 831         if (colonPos == -1) {
 832             view = "basic";
 833             attrs = attributes;
 834         } else {
 835             view = attributes.substring(0, colonPos++);
 836             attrs = attributes.substring(colonPos);
 837         }
 838         return getFileAttributeView(view).readAttributes(attrs);
 839     }
 840 
 841     FileStore getFileStore() throws IOException {
 842         // each ZipFileSystem only has one root (as requested for now)
 843         if (exists())
 844             return zfs.getFileStore(this);
 845         throw new NoSuchFileException(zfs.getString(path));
 846     }


 958             exists = target.exists();
 959         }
 960         if (exists)
 961             throw new FileAlreadyExistsException(target.toString());
 962 
 963         if (zfas.isDirectory()) {
 964             // create directory or file
 965             target.createDirectory();
 966         } else {
 967             try (InputStream is = zfs.newInputStream(getResolvedPath());
 968                  OutputStream os = target.newOutputStream())
 969             {
 970                 byte[] buf = new byte[8192];
 971                 int n;
 972                 while ((n = is.read(buf)) != -1) {
 973                     os.write(buf, 0, n);
 974                 }
 975             }
 976         }
 977         if (copyAttrs) {
 978             ZipFileAttributeView view =
 979                 target.getFileAttributeView(ZipFileAttributeView.class);
 980             try {
 981                 view.setTimes(zfas.lastModifiedTime(),
 982                               zfas.lastAccessTime(),
 983                               zfas.creationTime());
 984                 // copy permissions
 985                 view.setPermissions(zfas.storedPermissions().orElse(null));
 986             } catch (IOException x) {
 987                 // rollback?
 988                 try {
 989                     target.delete();
 990                 } catch (IOException ignore) { }
 991                 throw x;
 992             }
 993         }
 994     }
 995 
 996     private static int decode(char c) {
 997         if ((c >= '0') && (c <= '9'))
 998             return c - '0';
 999         if ((c >= 'a') && (c <= 'f'))
1000             return c - 'a' + 10;
1001         if ((c >= 'A') && (c <= 'F'))
1002             return c - 'A' + 10;
1003         assert false;
1004         return -1;
1005     }


< prev index next >