Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/classes/sun/nio/fs/UnixUserPrincipals.java
41137 views
1
/*
2
* Copyright (c) 2008, 2021, 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
public 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
@Override
71
public String getName() {
72
return name;
73
}
74
75
@Override
76
public String toString() {
77
return name;
78
}
79
80
@Override
81
public boolean equals(Object obj) {
82
if (obj == this)
83
return true;
84
if (!(obj instanceof User))
85
return false;
86
User other = (User)obj;
87
if ((this.id != other.id) ||
88
(this.isGroup != other.isGroup)) {
89
return false;
90
}
91
// specials
92
if (this.id == -1 && other.id == -1)
93
return this.name.equals(other.name);
94
95
return true;
96
}
97
98
@Override
99
public int hashCode() {
100
return (id != -1) ? id : name.hashCode();
101
}
102
}
103
104
static class Group extends User implements GroupPrincipal {
105
Group(int id, String name) {
106
super(id, true, name);
107
}
108
}
109
110
// return UserPrincipal representing given uid
111
public static User fromUid(int uid) {
112
String name;
113
try {
114
name = Util.toString(getpwuid(uid));
115
} catch (UnixException x) {
116
name = Integer.toString(uid);
117
}
118
return new User(uid, name);
119
}
120
121
// return GroupPrincipal representing given gid
122
public static Group fromGid(int gid) {
123
String name;
124
try {
125
name = Util.toString(getgrgid(gid));
126
} catch (UnixException x) {
127
name = Integer.toString(gid);
128
}
129
return new Group(gid, name);
130
}
131
132
// lookup user or group name
133
private static int lookupName(String name, boolean isGroup)
134
throws IOException
135
{
136
@SuppressWarnings("removal")
137
SecurityManager sm = System.getSecurityManager();
138
if (sm != null) {
139
sm.checkPermission(new RuntimePermission("lookupUserInformation"));
140
}
141
int id;
142
try {
143
id = (isGroup) ? getgrnam(name) : getpwnam(name);
144
} catch (UnixException x) {
145
throw new IOException(name + ": " + x.errorString());
146
}
147
if (id == -1) {
148
// lookup failed, allow input to be uid or gid
149
try {
150
id = Integer.parseInt(name);
151
} catch (NumberFormatException ignore) {
152
throw new UserPrincipalNotFoundException(name);
153
}
154
}
155
return id;
156
157
}
158
159
// lookup user name
160
static UserPrincipal lookupUser(String name) throws IOException {
161
if (name.equals(SPECIAL_OWNER.getName()))
162
return SPECIAL_OWNER;
163
if (name.equals(SPECIAL_GROUP.getName()))
164
return SPECIAL_GROUP;
165
if (name.equals(SPECIAL_EVERYONE.getName()))
166
return SPECIAL_EVERYONE;
167
int uid = lookupName(name, false);
168
return new User(uid, name);
169
}
170
171
// lookup group name
172
static GroupPrincipal lookupGroup(String group)
173
throws IOException
174
{
175
int gid = lookupName(group, true);
176
return new Group(gid, group);
177
}
178
}
179
180