< prev index next >

src/java.base/share/classes/java/nio/file/attribute/PosixFilePermissions.java

Print this page
rev 51866 : 6194856: Zip Files lose ALL ownership and permissions of the files
   1 /*
   2  * Copyright (c) 2007, 2011, 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 java.nio.file.attribute;
  27 
  28 import static java.nio.file.attribute.PosixFilePermission.*;
  29 import java.util.*;






  30 
  31 /**
  32  * This class consists exclusively of static methods that operate on sets of
  33  * {@link PosixFilePermission} objects.
  34  *
  35  * @since 1.7
  36  */
  37 
  38 public final class PosixFilePermissions {
  39     private PosixFilePermissions() { }














































































































  40 
  41     // Write string representation of permission bits to {@code sb}.
  42     private static void writeBits(StringBuilder sb, boolean r, boolean w, boolean x) {
  43         if (r) {
  44             sb.append('r');
  45         } else {
  46             sb.append('-');
  47         }
  48         if (w) {
  49             sb.append('w');
  50         } else {
  51             sb.append('-');
  52         }
  53         if (x) {
  54             sb.append('x');
  55         } else {
  56             sb.append('-');
  57         }
  58     }
  59 


   1 /*
   2  * Copyright (c) 2007, 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 java.nio.file.attribute;
  27 
  28 import static java.nio.file.attribute.PosixFilePermission.*;
  29 
  30 import java.util.Collections;
  31 import java.util.EnumSet;
  32 import java.util.HashSet;
  33 import java.util.Set;
  34 import java.util.stream.Collectors;
  35 import java.util.stream.Stream;
  36 
  37 /**
  38  * This class consists exclusively of static methods that operate on sets of
  39  * {@link PosixFilePermission} objects.
  40  *
  41  * @since 1.7
  42  */

  43 public final class PosixFilePermissions {
  44     private PosixFilePermissions() { }
  45 
  46     /**
  47      * The bit flag used to specify read permission by the owner.
  48      */
  49     public static final int POSIX_USER_READ = 0400;
  50 
  51     /**
  52      * The bit flag used to specify write permission by the owner.
  53      */
  54     public static final int POSIX_USER_WRITE = 0200;
  55 
  56     /**
  57      * The bit flag used to specify execute permission by the owner.
  58      */
  59     public static final int POSIX_USER_EXECUTE = 0100;
  60 
  61     /**
  62      * The bit flag used to specify read permission by the group.
  63      */
  64     public static final int POSIX_GROUP_READ = 040;
  65 
  66     /**
  67      * The bit flag used to specify write permission by the group.
  68      */
  69     public static final int POSIX_GROUP_WRITE = 020;
  70 
  71     /**
  72      * The bit flag used to specify execute permission by the group.
  73      */
  74     public static final int POSIX_GROUP_EXECUTE = 010;
  75 
  76     /**
  77      * The bit flag used to specify read permission by others.
  78      */
  79     public static final int POSIX_OTHER_READ = 04;
  80 
  81     /**
  82      * The bit flag used to specify write permission by others.
  83      */
  84     public static final int POSIX_OTHER_WRITE = 02;
  85 
  86     /**
  87      * The bit flag used to specify execute permission by others.
  88      */
  89     public static final int POSIX_OTHER_EXECUTE = 01;
  90 
  91     /**
  92      * Convert a {@link PosixFilePermission} object into the appropriate bit
  93      * flag.
  94      *
  95      * @param perm The {@link PosixFilePermission} object.
  96      * @return The bit flag as int.
  97      */
  98     private static int permToFlag(PosixFilePermission perm) {
  99         switch(perm) {
 100         case OWNER_READ:
 101             return POSIX_USER_READ;
 102         case OWNER_WRITE:
 103             return POSIX_USER_WRITE;
 104         case OWNER_EXECUTE:
 105             return POSIX_USER_EXECUTE;
 106         case GROUP_READ:
 107             return POSIX_GROUP_READ;
 108         case GROUP_WRITE:
 109             return POSIX_GROUP_WRITE;
 110         case GROUP_EXECUTE:
 111             return POSIX_GROUP_EXECUTE;
 112         case OTHERS_READ:
 113             return POSIX_OTHER_READ;
 114         case OTHERS_WRITE:
 115             return POSIX_OTHER_WRITE;
 116         case OTHERS_EXECUTE:
 117             return POSIX_OTHER_EXECUTE;
 118         default:
 119             return 0;
 120         }
 121     }
 122 
 123     /**
 124      * Converts a set of {@link PosixFilePermission}s into an int value where
 125      * the according bits are set.
 126      *
 127      * @param perms A Set of {@link PosixFilePermission} objects.
 128      *
 129      * @return A bit mask representing the input Set.
 130      */
 131     public static int toFlags(Set<PosixFilePermission> perms) {
 132         return perms
 133             .stream()
 134             .mapToInt(PosixFilePermissions::permToFlag)
 135             .reduce(0, (p1, p2)-> p1 | p2);
 136     }
 137 
 138     /**
 139      * Converts a bit mask of Posix file permissions into a set of
 140      * {@link PosixFilePermission} objects.
 141      *
 142      * @param flags The bit mask containing the flags.
 143      *
 144      * @return A set of {@link PosixFilePermission} objects matching the input
 145      *         flags.
 146      */
 147     public static Set<PosixFilePermission> fromFlags(int flags) {
 148         if (flags == 0) {
 149             return Collections.<PosixFilePermission>emptySet();
 150         }
 151         return Stream.of(PosixFilePermission.values())
 152             .filter(perm -> 0 != (flags & permToFlag(perm)))
 153             .collect(Collectors.toSet());
 154     }
 155 
 156     // Write string representation of permission bits to {@code sb}.
 157     private static void writeBits(StringBuilder sb, boolean r, boolean w, boolean x) {
 158         if (r) {
 159             sb.append('r');
 160         } else {
 161             sb.append('-');
 162         }
 163         if (w) {
 164             sb.append('w');
 165         } else {
 166             sb.append('-');
 167         }
 168         if (x) {
 169             sb.append('x');
 170         } else {
 171             sb.append('-');
 172         }
 173     }
 174 


< prev index next >