Path: blob/master/src/java.base/share/classes/java/nio/file/attribute/PosixFilePermissions.java
41161 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file.attribute;2627import static java.nio.file.attribute.PosixFilePermission.*;28import java.util.*;2930/**31* This class consists exclusively of static methods that operate on sets of32* {@link PosixFilePermission} objects.33*34* @since 1.735*/3637public final class PosixFilePermissions {38private PosixFilePermissions() { }3940// Write string representation of permission bits to {@code sb}.41private static void writeBits(StringBuilder sb, boolean r, boolean w, boolean x) {42if (r) {43sb.append('r');44} else {45sb.append('-');46}47if (w) {48sb.append('w');49} else {50sb.append('-');51}52if (x) {53sb.append('x');54} else {55sb.append('-');56}57}5859/**60* Returns the {@code String} representation of a set of permissions. It61* is guaranteed that the returned {@code String} can be parsed by the62* {@link #fromString} method.63*64* <p> If the set contains {@code null} or elements that are not of type65* {@code PosixFilePermission} then these elements are ignored.66*67* @param perms68* the set of permissions69*70* @return the string representation of the permission set71*/72public static String toString(Set<PosixFilePermission> perms) {73StringBuilder sb = new StringBuilder(9);74writeBits(sb, perms.contains(OWNER_READ), perms.contains(OWNER_WRITE),75perms.contains(OWNER_EXECUTE));76writeBits(sb, perms.contains(GROUP_READ), perms.contains(GROUP_WRITE),77perms.contains(GROUP_EXECUTE));78writeBits(sb, perms.contains(OTHERS_READ), perms.contains(OTHERS_WRITE),79perms.contains(OTHERS_EXECUTE));80return sb.toString();81}8283private static boolean isSet(char c, char setValue) {84if (c == setValue)85return true;86if (c == '-')87return false;88throw new IllegalArgumentException("Invalid mode");89}90private static boolean isR(char c) { return isSet(c, 'r'); }91private static boolean isW(char c) { return isSet(c, 'w'); }92private static boolean isX(char c) { return isSet(c, 'x'); }9394/**95* Returns the set of permissions corresponding to a given {@code String}96* representation.97*98* <p> The {@code perms} parameter is a {@code String} representing the99* permissions. It has 9 characters that are interpreted as three sets of100* three. The first set refers to the owner's permissions; the next to the101* group permissions and the last to others. Within each set, the first102* character is {@code 'r'} to indicate permission to read, the second103* character is {@code 'w'} to indicate permission to write, and the third104* character is {@code 'x'} for execute permission. Where a permission is105* not set then the corresponding character is set to {@code '-'}.106*107* <p> <b>Usage Example:</b>108* Suppose we require the set of permissions that indicate the owner has read,109* write, and execute permissions, the group has read and execute permissions110* and others have none.111* <pre>112* Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");113* </pre>114*115* @param perms116* string representing a set of permissions117*118* @return the resulting set of permissions119*120* @throws IllegalArgumentException121* if the string cannot be converted to a set of permissions122*123* @see #toString(Set)124*/125public static Set<PosixFilePermission> fromString(String perms) {126if (perms.length() != 9)127throw new IllegalArgumentException("Invalid mode");128Set<PosixFilePermission> result = EnumSet.noneOf(PosixFilePermission.class);129if (isR(perms.charAt(0))) result.add(OWNER_READ);130if (isW(perms.charAt(1))) result.add(OWNER_WRITE);131if (isX(perms.charAt(2))) result.add(OWNER_EXECUTE);132if (isR(perms.charAt(3))) result.add(GROUP_READ);133if (isW(perms.charAt(4))) result.add(GROUP_WRITE);134if (isX(perms.charAt(5))) result.add(GROUP_EXECUTE);135if (isR(perms.charAt(6))) result.add(OTHERS_READ);136if (isW(perms.charAt(7))) result.add(OTHERS_WRITE);137if (isX(perms.charAt(8))) result.add(OTHERS_EXECUTE);138return result;139}140141/**142* Creates a {@link FileAttribute}, encapsulating a copy of the given file143* permissions, suitable for passing to the {@link java.nio.file.Files#createFile144* createFile} or {@link java.nio.file.Files#createDirectory createDirectory}145* methods.146*147* @param perms148* the set of permissions149*150* @return an attribute encapsulating the given file permissions with151* {@link FileAttribute#name name} {@code "posix:permissions"}152*153* @throws ClassCastException154* if the set contains elements that are not of type {@code155* PosixFilePermission}156*/157public static FileAttribute<Set<PosixFilePermission>>158asFileAttribute(Set<PosixFilePermission> perms)159{160// copy set and check for nulls (CCE will be thrown if an element is not161// a PosixFilePermission)162perms = new HashSet<>(perms);163for (PosixFilePermission p: perms) {164if (p == null)165throw new NullPointerException();166}167final Set<PosixFilePermission> value = perms;168return new FileAttribute<>() {169@Override170public String name() {171return "posix:permissions";172}173@Override174public Set<PosixFilePermission> value() {175return Collections.unmodifiableSet(value);176}177};178}179}180181182