< prev index next >

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

Print this page
rev 52310 : 8213031: (zipfs) Add support for POSIX file permissions
   1 /*
   2  * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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.IOException;
  29 import java.io.OutputStream;

  30 import java.time.DateTimeException;
  31 import java.time.Instant;
  32 import java.time.LocalDateTime;
  33 import java.time.ZoneId;
  34 import java.util.Arrays;

  35 import java.util.Date;
  36 import java.util.regex.PatternSyntaxException;



  37 import java.util.concurrent.TimeUnit;



  38 
  39 /**
  40  *
  41  * @author Xueming Shen
  42  */
  43 
  44 class ZipUtils {








































































































































  45 
  46     /*
  47      * Writes a 16-bit short to the output stream in little-endian byte order.
  48      */
  49     public static void writeShort(OutputStream os, int v) throws IOException {
  50         os.write(v & 0xff);
  51         os.write((v >>> 8) & 0xff);
  52     }
  53 
  54     /*
  55      * Writes a 32-bit int to the output stream in little-endian byte order.
  56      */
  57     public static void writeInt(OutputStream os, long v) throws IOException {
  58         os.write((int)(v & 0xff));
  59         os.write((int)((v >>>  8) & 0xff));
  60         os.write((int)((v >>> 16) & 0xff));
  61         os.write((int)((v >>> 24) & 0xff));
  62     }
  63 
  64     /*


   1 /*
   2  * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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.IOException;
  29 import java.io.OutputStream;
  30 import java.nio.file.attribute.PosixFilePermission;
  31 import java.time.DateTimeException;
  32 import java.time.Instant;
  33 import java.time.LocalDateTime;
  34 import java.time.ZoneId;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.Date;
  38 import java.util.HashSet;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.WeakHashMap;
  42 import java.util.concurrent.TimeUnit;
  43 import java.util.regex.PatternSyntaxException;
  44 import java.util.stream.Collectors;
  45 import java.util.stream.Stream;
  46 
  47 /**

  48  * @author Xueming Shen
  49  */

  50 class ZipUtils {
  51 
  52     /**
  53      * The value indicating unix file attributes in CEN field "version made by".
  54      */
  55     static final int FILE_ATTRIBUTES_UNIX = 3;
  56 
  57     /**
  58      * Constant used to calculate "version made by".
  59      */
  60     static final int VERSION_BASE_UNIX = FILE_ATTRIBUTES_UNIX << 8;
  61 
  62     /**
  63      * The bit flag used to specify read permission by the owner.
  64      */
  65     static final int POSIX_USER_READ = 0400;
  66 
  67     /**
  68      * The bit flag used to specify write permission by the owner.
  69      */
  70     static final int POSIX_USER_WRITE = 0200;
  71 
  72     /**
  73      * The bit flag used to specify execute permission by the owner.
  74      */
  75     static final int POSIX_USER_EXECUTE = 0100;
  76 
  77     /**
  78      * The bit flag used to specify read permission by the group.
  79      */
  80     static final int POSIX_GROUP_READ = 040;
  81 
  82     /**
  83      * The bit flag used to specify write permission by the group.
  84      */
  85     static final int POSIX_GROUP_WRITE = 020;
  86 
  87     /**
  88      * The bit flag used to specify execute permission by the group.
  89      */
  90     static final int POSIX_GROUP_EXECUTE = 010;
  91 
  92     /**
  93      * The bit flag used to specify read permission by others.
  94      */
  95     static final int POSIX_OTHER_READ = 04;
  96 
  97     /**
  98      * The bit flag used to specify write permission by others.
  99      */
 100     static final int POSIX_OTHER_WRITE = 02;
 101 
 102     /**
 103      * The bit flag used to specify execute permission by others.
 104      */
 105     static final int POSIX_OTHER_EXECUTE = 01;
 106 
 107     /**
 108      * This weak hashmap contains pre-calculated permission sets.
 109      */
 110     private static final Map<Integer, Set<PosixFilePermission>> permCache = new WeakHashMap<>();
 111 
 112     /**
 113      * Convert a {@link PosixFilePermission} object into the appropriate bit
 114      * flag.
 115      *
 116      * @param perm The {@link PosixFilePermission} object.
 117      * @return The bit flag as int.
 118      */
 119     private static int permToFlag(PosixFilePermission perm) {
 120         switch(perm) {
 121         case OWNER_READ:
 122             return POSIX_USER_READ;
 123         case OWNER_WRITE:
 124             return POSIX_USER_WRITE;
 125         case OWNER_EXECUTE:
 126             return POSIX_USER_EXECUTE;
 127         case GROUP_READ:
 128             return POSIX_GROUP_READ;
 129         case GROUP_WRITE:
 130             return POSIX_GROUP_WRITE;
 131         case GROUP_EXECUTE:
 132             return POSIX_GROUP_EXECUTE;
 133         case OTHERS_READ:
 134             return POSIX_OTHER_READ;
 135         case OTHERS_WRITE:
 136             return POSIX_OTHER_WRITE;
 137         case OTHERS_EXECUTE:
 138             return POSIX_OTHER_EXECUTE;
 139         default:
 140             return 0;
 141         }
 142     }
 143 
 144     /**
 145      * Converts a set of {@link PosixFilePermission}s into an int value where
 146      * the according bits are set.
 147      *
 148      * @param perms A Set of {@link PosixFilePermission} objects.
 149      *
 150      * @return A bit mask representing the input Set.
 151      */
 152     static int permsToFlags(Set<PosixFilePermission> perms) {
 153         if (perms == null) {
 154             return -1;
 155         }
 156         int flags = 0;
 157         for (PosixFilePermission perm : perms) {
 158             flags |= permToFlag(perm);
 159         }
 160         return flags;
 161     }
 162 
 163     /**
 164      * Converts a bit mask of Posix file permissions into a set of
 165      * {@link PosixFilePermission} objects.
 166      *
 167      * @param flags The bit mask containing the flags.
 168      *
 169      * @return A set of {@link PosixFilePermission} objects matching the input
 170      *         flags.
 171      */
 172     static Set<PosixFilePermission> permsFromFlags(int flags) {
 173         if (flags == 0) {
 174             return Collections.<PosixFilePermission>emptySet();
 175         }
 176         Set<PosixFilePermission> perms = permCache.get(flags);
 177         if (perms == null) {
 178             perms = Stream.of(PosixFilePermission.values())
 179                 .filter(perm -> 0 != (flags & permToFlag(perm)))
 180                 .collect(Collectors.toSet());
 181             synchronized (permCache) {
 182                 permCache.put(flags, perms);
 183             }
 184         }
 185         return new HashSet<>(perms);
 186     }
 187 
 188     /*
 189      * Writes a 16-bit short to the output stream in little-endian byte order.
 190      */
 191     public static void writeShort(OutputStream os, int v) throws IOException {
 192         os.write(v & 0xff);
 193         os.write((v >>> 8) & 0xff);
 194     }
 195 
 196     /*
 197      * Writes a 32-bit int to the output stream in little-endian byte order.
 198      */
 199     public static void writeInt(OutputStream os, long v) throws IOException {
 200         os.write((int)(v & 0xff));
 201         os.write((int)((v >>>  8) & 0xff));
 202         os.write((int)((v >>> 16) & 0xff));
 203         os.write((int)((v >>> 24) & 0xff));
 204     }
 205 
 206     /*


< prev index next >