< prev index next >

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

Print this page
rev 54189 : 8213031: (zipfs) Add support for POSIX file permissions
   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.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     /*


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


< prev index next >