Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java
32288 views
/*1* Copyright (c) 2008, 2013, 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 sun.nio.fs;2627import java.nio.file.attribute.*;28import java.io.IOException;29import static sun.nio.fs.UnixNativeDispatcher.*;3031/**32* Unix implementation of java.nio.file.attribute.UserPrincipal33*/3435class UnixUserPrincipals {36private static User createSpecial(String name) { return new User(-1, name); }3738static final User SPECIAL_OWNER = createSpecial("OWNER@");39static final User SPECIAL_GROUP = createSpecial("GROUP@");40static final User SPECIAL_EVERYONE = createSpecial("EVERYONE@");4142static class User implements UserPrincipal {43private final int id; // uid or gid44private final boolean isGroup;45private final String name;4647private User(int id, boolean isGroup, String name) {48this.id = id;49this.isGroup = isGroup;50this.name = name;51}5253User(int id, String name) {54this(id, false, name);55}5657int uid() {58if (isGroup)59throw new AssertionError();60return id;61}6263int gid() {64if (isGroup)65return id;66throw new AssertionError();67}6869boolean isSpecial() {70return id == -1;71}7273@Override74public String getName() {75return name;76}7778@Override79public String toString() {80return name;81}8283@Override84public boolean equals(Object obj) {85if (obj == this)86return true;87if (!(obj instanceof User))88return false;89User other = (User)obj;90if ((this.id != other.id) ||91(this.isGroup != other.isGroup)) {92return false;93}94// specials95if (this.id == -1 && other.id == -1)96return this.name.equals(other.name);9798return true;99}100101@Override102public int hashCode() {103return (id != -1) ? id : name.hashCode();104}105}106107static class Group extends User implements GroupPrincipal {108Group(int id, String name) {109super(id, true, name);110}111}112113// return UserPrincipal representing given uid114static User fromUid(int uid) {115String name = null;116try {117name = Util.toString(getpwuid(uid));118} catch (UnixException x) {119name = Integer.toString(uid);120}121return new User(uid, name);122}123124// return GroupPrincipal representing given gid125static Group fromGid(int gid) {126String name = null;127try {128name = Util.toString(getgrgid(gid));129} catch (UnixException x) {130name = Integer.toString(gid);131}132return new Group(gid, name);133}134135// lookup user or group name136private static int lookupName(String name, boolean isGroup)137throws IOException138{139SecurityManager sm = System.getSecurityManager();140if (sm != null) {141sm.checkPermission(new RuntimePermission("lookupUserInformation"));142}143int id = -1;144try {145id = (isGroup) ? getgrnam(name) : getpwnam(name);146} catch (UnixException x) {147throw new IOException(name + ": " + x.errorString());148}149if (id == -1) {150// lookup failed, allow input to be uid or gid151try {152id = Integer.parseInt(name);153} catch (NumberFormatException ignore) {154throw new UserPrincipalNotFoundException(name);155}156}157return id;158159}160161// lookup user name162static UserPrincipal lookupUser(String name) throws IOException {163if (name.equals(SPECIAL_OWNER.getName()))164return SPECIAL_OWNER;165if (name.equals(SPECIAL_GROUP.getName()))166return SPECIAL_GROUP;167if (name.equals(SPECIAL_EVERYONE.getName()))168return SPECIAL_EVERYONE;169int uid = lookupName(name, false);170return new User(uid, name);171}172173// lookup group name174static GroupPrincipal lookupGroup(String group)175throws IOException176{177int gid = lookupName(group, true);178return new Group(gid, group);179}180}181182183