< prev index next >

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

Print this page
rev 53081 : 8213031: (zipfs) Add support for POSIX file permissions
Reviewed-by: simonis


  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.concurrent.TimeUnit;
  37 import java.util.regex.PatternSyntaxException;
  38 
  39 /**
  40  * @author Xueming Shen
  41  */
  42 class ZipUtils {





























































































































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




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


< prev index next >