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