|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package sun.nio.fs; |
|
27 |
|
28 import java.nio.file.attribute.*; |
|
29 import java.io.IOException; |
|
30 import static sun.nio.fs.UnixNativeDispatcher.*; |
|
31 |
|
32 /** |
|
33 * Unix implementation of java.nio.file.attribute.UserPrincipal |
|
34 */ |
|
35 |
|
36 class UnixUserPrincipals { |
|
37 private static User createSpecial(String name) { return new User(-1, name); } |
|
38 |
|
39 static final User SPECIAL_OWNER = createSpecial("OWNER@"); |
|
40 static final User SPECIAL_GROUP = createSpecial("GROUP@"); |
|
41 static final User SPECIAL_EVERYONE = createSpecial("EVERYONE@"); |
|
42 |
|
43 static class User implements UserPrincipal { |
|
44 private final int id; // uid or gid |
|
45 private final boolean isGroup; |
|
46 private final String name; |
|
47 |
|
48 private User(int id, boolean isGroup, String name) { |
|
49 this.id = id; |
|
50 this.isGroup = isGroup; |
|
51 this.name = name; |
|
52 } |
|
53 |
|
54 User(int id, String name) { |
|
55 this(id, false, name); |
|
56 } |
|
57 |
|
58 int uid() { |
|
59 if (isGroup) |
|
60 throw new AssertionError(); |
|
61 return id; |
|
62 } |
|
63 |
|
64 int gid() { |
|
65 if (isGroup) |
|
66 return id; |
|
67 throw new AssertionError(); |
|
68 } |
|
69 |
|
70 boolean isSpecial() { |
|
71 return id == -1; |
|
72 } |
|
73 |
|
74 @Override |
|
75 public String getName() { |
|
76 return name; |
|
77 } |
|
78 |
|
79 @Override |
|
80 public String toString() { |
|
81 return name; |
|
82 } |
|
83 |
|
84 @Override |
|
85 public boolean equals(Object obj) { |
|
86 if (obj == this) |
|
87 return true; |
|
88 if (!(obj instanceof User)) |
|
89 return false; |
|
90 User other = (User)obj; |
|
91 if ((this.id != other.id) || |
|
92 (this.isGroup != other.isGroup)) { |
|
93 return false; |
|
94 } |
|
95 // specials |
|
96 if (this.id == -1 && other.id == -1) |
|
97 return this.name.equals(other.name); |
|
98 |
|
99 return true; |
|
100 } |
|
101 |
|
102 @Override |
|
103 public int hashCode() { |
|
104 return (id != -1) ? id : name.hashCode(); |
|
105 } |
|
106 } |
|
107 |
|
108 static class Group extends User implements GroupPrincipal { |
|
109 Group(int id, String name) { |
|
110 super(id, true, name); |
|
111 } |
|
112 } |
|
113 |
|
114 // return UserPrincipal representing given uid |
|
115 static User fromUid(int uid) { |
|
116 String name = null; |
|
117 try { |
|
118 name = new String(getpwuid(uid)); |
|
119 } catch (UnixException x) { |
|
120 name = Integer.toString(uid); |
|
121 } |
|
122 return new User(uid, name); |
|
123 } |
|
124 |
|
125 // return GroupPrincipal representing given gid |
|
126 static Group fromGid(int gid) { |
|
127 String name = null; |
|
128 try { |
|
129 name = new String(getgrgid(gid)); |
|
130 } catch (UnixException x) { |
|
131 name = Integer.toString(gid); |
|
132 } |
|
133 return new Group(gid, name); |
|
134 } |
|
135 |
|
136 // lookup user or group name |
|
137 private static int lookupName(String name, boolean isGroup) |
|
138 throws IOException |
|
139 { |
|
140 SecurityManager sm = System.getSecurityManager(); |
|
141 if (sm != null) { |
|
142 sm.checkPermission(new RuntimePermission("lookupUserInformation")); |
|
143 } |
|
144 int id = -1; |
|
145 try { |
|
146 id = (isGroup) ? getgrnam(name) : getpwnam(name); |
|
147 } catch (UnixException x) { |
|
148 throw new IOException(name + ": " + x.errorString()); |
|
149 } |
|
150 if (id == -1) |
|
151 throw new UserPrincipalNotFoundException(name); |
|
152 return id; |
|
153 |
|
154 } |
|
155 |
|
156 // lookup user name |
|
157 static UserPrincipal lookupUser(String name) throws IOException { |
|
158 if (name.equals(SPECIAL_OWNER.getName())) |
|
159 return SPECIAL_OWNER; |
|
160 if (name.equals(SPECIAL_GROUP.getName())) |
|
161 return SPECIAL_GROUP; |
|
162 if (name.equals(SPECIAL_EVERYONE.getName())) |
|
163 return SPECIAL_EVERYONE; |
|
164 int uid = lookupName(name, false); |
|
165 return new User(uid, name); |
|
166 } |
|
167 |
|
168 // lookup group name |
|
169 static GroupPrincipal lookupGroup(String group) |
|
170 throws IOException |
|
171 { |
|
172 int gid = lookupName(group, true); |
|
173 return new Group(gid, group); |
|
174 } |
|
175 } |