Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java
32288 views
1
/*
2
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* 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 = Util.toString(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 = Util.toString(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
// lookup failed, allow input to be uid or gid
152
try {
153
id = Integer.parseInt(name);
154
} catch (NumberFormatException ignore) {
155
throw new UserPrincipalNotFoundException(name);
156
}
157
}
158
return id;
159
160
}
161
162
// lookup user name
163
static UserPrincipal lookupUser(String name) throws IOException {
164
if (name.equals(SPECIAL_OWNER.getName()))
165
return SPECIAL_OWNER;
166
if (name.equals(SPECIAL_GROUP.getName()))
167
return SPECIAL_GROUP;
168
if (name.equals(SPECIAL_EVERYONE.getName()))
169
return SPECIAL_EVERYONE;
170
int uid = lookupName(name, false);
171
return new User(uid, name);
172
}
173
174
// lookup group name
175
static GroupPrincipal lookupGroup(String group)
176
throws IOException
177
{
178
int gid = lookupName(group, true);
179
return new Group(gid, group);
180
}
181
}
182
183