893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport static sun.nio.fs.UnixNativeDispatcher.*;
893N/A
893N/A/**
893N/A * Unix implementation of java.nio.file.attribute.UserPrincipal
893N/A */
893N/A
893N/Aclass UnixUserPrincipals {
893N/A private static User createSpecial(String name) { return new User(-1, name); }
893N/A
893N/A static final User SPECIAL_OWNER = createSpecial("OWNER@");
893N/A static final User SPECIAL_GROUP = createSpecial("GROUP@");
893N/A static final User SPECIAL_EVERYONE = createSpecial("EVERYONE@");
893N/A
893N/A static class User implements UserPrincipal {
893N/A private final int id; // uid or gid
893N/A private final boolean isGroup;
893N/A private final String name;
893N/A
893N/A private User(int id, boolean isGroup, String name) {
893N/A this.id = id;
893N/A this.isGroup = isGroup;
893N/A this.name = name;
893N/A }
893N/A
893N/A User(int id, String name) {
893N/A this(id, false, name);
893N/A }
893N/A
893N/A int uid() {
893N/A if (isGroup)
893N/A throw new AssertionError();
893N/A return id;
893N/A }
893N/A
893N/A int gid() {
893N/A if (isGroup)
893N/A return id;
893N/A throw new AssertionError();
893N/A }
893N/A
893N/A boolean isSpecial() {
893N/A return id == -1;
893N/A }
893N/A
893N/A @Override
893N/A public String getName() {
893N/A return name;
893N/A }
893N/A
893N/A @Override
893N/A public String toString() {
893N/A return name;
893N/A }
893N/A
893N/A @Override
893N/A public boolean equals(Object obj) {
893N/A if (obj == this)
893N/A return true;
893N/A if (!(obj instanceof User))
893N/A return false;
893N/A User other = (User)obj;
893N/A if ((this.id != other.id) ||
893N/A (this.isGroup != other.isGroup)) {
893N/A return false;
893N/A }
893N/A // specials
893N/A if (this.id == -1 && other.id == -1)
893N/A return this.name.equals(other.name);
893N/A
893N/A return true;
893N/A }
893N/A
893N/A @Override
893N/A public int hashCode() {
893N/A return (id != -1) ? id : name.hashCode();
893N/A }
893N/A }
893N/A
893N/A static class Group extends User implements GroupPrincipal {
893N/A Group(int id, String name) {
893N/A super(id, true, name);
893N/A }
893N/A }
893N/A
893N/A // return UserPrincipal representing given uid
893N/A static User fromUid(int uid) {
893N/A String name = null;
893N/A try {
893N/A name = new String(getpwuid(uid));
893N/A } catch (UnixException x) {
893N/A name = Integer.toString(uid);
893N/A }
893N/A return new User(uid, name);
893N/A }
893N/A
893N/A // return GroupPrincipal representing given gid
893N/A static Group fromGid(int gid) {
893N/A String name = null;
893N/A try {
893N/A name = new String(getgrgid(gid));
893N/A } catch (UnixException x) {
893N/A name = Integer.toString(gid);
893N/A }
893N/A return new Group(gid, name);
893N/A }
893N/A
893N/A // lookup user or group name
893N/A private static int lookupName(String name, boolean isGroup)
893N/A throws IOException
893N/A {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A sm.checkPermission(new RuntimePermission("lookupUserInformation"));
893N/A }
893N/A int id = -1;
893N/A try {
893N/A id = (isGroup) ? getgrnam(name) : getpwnam(name);
893N/A } catch (UnixException x) {
893N/A throw new IOException(name + ": " + x.errorString());
893N/A }
1319N/A if (id == -1) {
1319N/A // lookup failed, allow input to be uid or gid
1319N/A try {
1319N/A id = Integer.parseInt(name);
1319N/A } catch (NumberFormatException ignore) {
1319N/A throw new UserPrincipalNotFoundException(name);
1319N/A }
1319N/A }
893N/A return id;
893N/A
893N/A }
893N/A
893N/A // lookup user name
893N/A static UserPrincipal lookupUser(String name) throws IOException {
893N/A if (name.equals(SPECIAL_OWNER.getName()))
893N/A return SPECIAL_OWNER;
893N/A if (name.equals(SPECIAL_GROUP.getName()))
893N/A return SPECIAL_GROUP;
893N/A if (name.equals(SPECIAL_EVERYONE.getName()))
893N/A return SPECIAL_EVERYONE;
893N/A int uid = lookupName(name, false);
893N/A return new User(uid, name);
893N/A }
893N/A
893N/A // lookup group name
893N/A static GroupPrincipal lookupGroup(String group)
893N/A throws IOException
893N/A {
893N/A int gid = lookupName(group, true);
893N/A return new Group(gid, group);
893N/A }
893N/A}