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/UnixFileAttributeViews.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.*;
29
import java.nio.file.attribute.*;
30
import java.util.*;
31
import java.util.concurrent.TimeUnit;
32
import java.io.IOException;
33
34
import static sun.nio.fs.UnixNativeDispatcher.*;
35
36
class UnixFileAttributeViews {
37
38
static class Basic extends AbstractBasicFileAttributeView {
39
protected final UnixPath file;
40
protected final boolean followLinks;
41
42
Basic(UnixPath file, boolean followLinks) {
43
this.file = file;
44
this.followLinks = followLinks;
45
}
46
47
@Override
48
public BasicFileAttributes readAttributes() throws IOException {
49
file.checkRead();
50
try {
51
UnixFileAttributes attrs =
52
UnixFileAttributes.get(file, followLinks);
53
return attrs.asBasicFileAttributes();
54
} catch (UnixException x) {
55
x.rethrowAsIOException(file);
56
return null; // keep compiler happy
57
}
58
}
59
60
@Override
61
public void setTimes(FileTime lastModifiedTime,
62
FileTime lastAccessTime,
63
FileTime createTime) throws IOException
64
{
65
// null => don't change
66
if (lastModifiedTime == null && lastAccessTime == null) {
67
// no effect
68
return;
69
}
70
71
// permission check
72
file.checkWrite();
73
74
boolean haveFd = false;
75
boolean useFutimes = false;
76
boolean useFutimens = false;
77
boolean useLutimes = false;
78
int fd = -1;
79
try {
80
if (!followLinks) {
81
useLutimes = lutimesSupported() &&
82
UnixFileAttributes.get(file, false).isSymbolicLink();
83
}
84
if (!useLutimes) {
85
fd = file.openForAttributeAccess(followLinks);
86
if (fd != -1) {
87
haveFd = true;
88
if (!(useFutimens = futimensSupported())) {
89
useFutimes = futimesSupported();
90
}
91
}
92
}
93
} catch (UnixException x) {
94
if (!(x.errno() == UnixConstants.ENXIO ||
95
(x.errno() == UnixConstants.ELOOP && useLutimes))) {
96
x.rethrowAsIOException(file);
97
}
98
}
99
100
try {
101
// assert followLinks || !UnixFileAttributes.get(fd).isSymbolicLink();
102
103
// if not changing both attributes then need existing attributes
104
if (lastModifiedTime == null || lastAccessTime == null) {
105
try {
106
UnixFileAttributes attrs = haveFd ?
107
UnixFileAttributes.get(fd) :
108
UnixFileAttributes.get(file, followLinks);
109
if (lastModifiedTime == null)
110
lastModifiedTime = attrs.lastModifiedTime();
111
if (lastAccessTime == null)
112
lastAccessTime = attrs.lastAccessTime();
113
} catch (UnixException x) {
114
x.rethrowAsIOException(file);
115
}
116
}
117
118
// update times
119
TimeUnit timeUnit = useFutimens ?
120
TimeUnit.NANOSECONDS : TimeUnit.MICROSECONDS;
121
long modValue = lastModifiedTime.to(timeUnit);
122
long accessValue= lastAccessTime.to(timeUnit);
123
124
boolean retry = false;
125
try {
126
if (useFutimens) {
127
futimens(fd, accessValue, modValue);
128
} else if (useFutimes) {
129
futimes(fd, accessValue, modValue);
130
} else if (useLutimes) {
131
lutimes(file, accessValue, modValue);
132
} else {
133
utimes(file, accessValue, modValue);
134
}
135
} catch (UnixException x) {
136
// if futimes/utimes fails with EINVAL and one/both of the times is
137
// negative then we adjust the value to the epoch and retry.
138
if (x.errno() == UnixConstants.EINVAL &&
139
(modValue < 0L || accessValue < 0L)) {
140
retry = true;
141
} else {
142
x.rethrowAsIOException(file);
143
}
144
}
145
if (retry) {
146
if (modValue < 0L) modValue = 0L;
147
if (accessValue < 0L) accessValue= 0L;
148
try {
149
if (useFutimens) {
150
futimens(fd, accessValue, modValue);
151
} else if (useFutimes) {
152
futimes(fd, accessValue, modValue);
153
} else if (useLutimes) {
154
lutimes(file, accessValue, modValue);
155
} else {
156
utimes(file, accessValue, modValue);
157
}
158
} catch (UnixException x) {
159
x.rethrowAsIOException(file);
160
}
161
}
162
} finally {
163
close(fd);
164
}
165
}
166
}
167
168
private static class Posix extends Basic implements PosixFileAttributeView {
169
private static final String PERMISSIONS_NAME = "permissions";
170
private static final String OWNER_NAME = "owner";
171
private static final String GROUP_NAME = "group";
172
173
// the names of the posix attributes (includes basic)
174
static final Set<String> posixAttributeNames =
175
Util.newSet(basicAttributeNames, PERMISSIONS_NAME, OWNER_NAME, GROUP_NAME);
176
177
Posix(UnixPath file, boolean followLinks) {
178
super(file, followLinks);
179
}
180
181
final void checkReadExtended() {
182
@SuppressWarnings("removal")
183
SecurityManager sm = System.getSecurityManager();
184
if (sm != null) {
185
file.checkRead();
186
sm.checkPermission(new RuntimePermission("accessUserInformation"));
187
}
188
}
189
190
final void checkWriteExtended() {
191
@SuppressWarnings("removal")
192
SecurityManager sm = System.getSecurityManager();
193
if (sm != null) {
194
file.checkWrite();
195
sm.checkPermission(new RuntimePermission("accessUserInformation"));
196
}
197
}
198
199
@Override
200
public String name() {
201
return "posix";
202
}
203
204
@Override
205
@SuppressWarnings("unchecked")
206
public void setAttribute(String attribute, Object value)
207
throws IOException
208
{
209
if (attribute.equals(PERMISSIONS_NAME)) {
210
setPermissions((Set<PosixFilePermission>)value);
211
return;
212
}
213
if (attribute.equals(OWNER_NAME)) {
214
setOwner((UserPrincipal)value);
215
return;
216
}
217
if (attribute.equals(GROUP_NAME)) {
218
setGroup((GroupPrincipal)value);
219
return;
220
}
221
super.setAttribute(attribute, value);
222
}
223
224
/**
225
* Invoked by readAttributes or sub-classes to add all matching posix
226
* attributes to the builder
227
*/
228
final void addRequestedPosixAttributes(PosixFileAttributes attrs,
229
AttributesBuilder builder)
230
{
231
addRequestedBasicAttributes(attrs, builder);
232
if (builder.match(PERMISSIONS_NAME))
233
builder.add(PERMISSIONS_NAME, attrs.permissions());
234
if (builder.match(OWNER_NAME))
235
builder.add(OWNER_NAME, attrs.owner());
236
if (builder.match(GROUP_NAME))
237
builder.add(GROUP_NAME, attrs.group());
238
}
239
240
@Override
241
public Map<String,Object> readAttributes(String[] requested)
242
throws IOException
243
{
244
AttributesBuilder builder =
245
AttributesBuilder.create(posixAttributeNames, requested);
246
PosixFileAttributes attrs = readAttributes();
247
addRequestedPosixAttributes(attrs, builder);
248
return builder.unmodifiableMap();
249
}
250
251
@Override
252
public UnixFileAttributes readAttributes() throws IOException {
253
checkReadExtended();
254
try {
255
return UnixFileAttributes.get(file, followLinks);
256
} catch (UnixException x) {
257
x.rethrowAsIOException(file);
258
return null; // keep compiler happy
259
}
260
}
261
262
// chmod
263
final void setMode(int mode) throws IOException {
264
checkWriteExtended();
265
try {
266
if (followLinks) {
267
chmod(file, mode);
268
} else {
269
int fd = file.openForAttributeAccess(false);
270
try {
271
fchmod(fd, mode);
272
} finally {
273
close(fd);
274
}
275
}
276
} catch (UnixException x) {
277
x.rethrowAsIOException(file);
278
}
279
}
280
281
// chown
282
final void setOwners(int uid, int gid) throws IOException {
283
checkWriteExtended();
284
try {
285
if (followLinks) {
286
chown(file, uid, gid);
287
} else {
288
lchown(file, uid, gid);
289
}
290
} catch (UnixException x) {
291
x.rethrowAsIOException(file);
292
}
293
}
294
295
@Override
296
public void setPermissions(Set<PosixFilePermission> perms)
297
throws IOException
298
{
299
setMode(UnixFileModeAttribute.toUnixMode(perms));
300
}
301
302
@Override
303
public void setOwner(UserPrincipal owner)
304
throws IOException
305
{
306
if (owner == null)
307
throw new NullPointerException("'owner' is null");
308
if (!(owner instanceof UnixUserPrincipals.User))
309
throw new ProviderMismatchException();
310
if (owner instanceof UnixUserPrincipals.Group)
311
throw new IOException("'owner' parameter can't be a group");
312
int uid = ((UnixUserPrincipals.User)owner).uid();
313
setOwners(uid, -1);
314
}
315
316
@Override
317
public UserPrincipal getOwner() throws IOException {
318
return readAttributes().owner();
319
}
320
321
@Override
322
public void setGroup(GroupPrincipal group)
323
throws IOException
324
{
325
if (group == null)
326
throw new NullPointerException("'owner' is null");
327
if (!(group instanceof UnixUserPrincipals.Group))
328
throw new ProviderMismatchException();
329
int gid = ((UnixUserPrincipals.Group)group).gid();
330
setOwners(-1, gid);
331
}
332
}
333
334
private static class Unix extends Posix {
335
private static final String MODE_NAME = "mode";
336
private static final String INO_NAME = "ino";
337
private static final String DEV_NAME = "dev";
338
private static final String RDEV_NAME = "rdev";
339
private static final String NLINK_NAME = "nlink";
340
private static final String UID_NAME = "uid";
341
private static final String GID_NAME = "gid";
342
private static final String CTIME_NAME = "ctime";
343
344
// the names of the unix attributes (including posix)
345
static final Set<String> unixAttributeNames =
346
Util.newSet(posixAttributeNames,
347
MODE_NAME, INO_NAME, DEV_NAME, RDEV_NAME,
348
NLINK_NAME, UID_NAME, GID_NAME, CTIME_NAME);
349
350
Unix(UnixPath file, boolean followLinks) {
351
super(file, followLinks);
352
}
353
354
@Override
355
public String name() {
356
return "unix";
357
}
358
359
@Override
360
public void setAttribute(String attribute, Object value)
361
throws IOException
362
{
363
if (attribute.equals(MODE_NAME)) {
364
setMode((Integer)value);
365
return;
366
}
367
if (attribute.equals(UID_NAME)) {
368
setOwners((Integer)value, -1);
369
return;
370
}
371
if (attribute.equals(GID_NAME)) {
372
setOwners(-1, (Integer)value);
373
return;
374
}
375
super.setAttribute(attribute, value);
376
}
377
378
@Override
379
public Map<String,Object> readAttributes(String[] requested)
380
throws IOException
381
{
382
AttributesBuilder builder =
383
AttributesBuilder.create(unixAttributeNames, requested);
384
UnixFileAttributes attrs = readAttributes();
385
addRequestedPosixAttributes(attrs, builder);
386
if (builder.match(MODE_NAME))
387
builder.add(MODE_NAME, attrs.mode());
388
if (builder.match(INO_NAME))
389
builder.add(INO_NAME, attrs.ino());
390
if (builder.match(DEV_NAME))
391
builder.add(DEV_NAME, attrs.dev());
392
if (builder.match(RDEV_NAME))
393
builder.add(RDEV_NAME, attrs.rdev());
394
if (builder.match(NLINK_NAME))
395
builder.add(NLINK_NAME, attrs.nlink());
396
if (builder.match(UID_NAME))
397
builder.add(UID_NAME, attrs.uid());
398
if (builder.match(GID_NAME))
399
builder.add(GID_NAME, attrs.gid());
400
if (builder.match(CTIME_NAME))
401
builder.add(CTIME_NAME, attrs.ctime());
402
return builder.unmodifiableMap();
403
}
404
}
405
406
static Basic createBasicView(UnixPath file, boolean followLinks) {
407
return new Basic(file, followLinks);
408
}
409
410
static Posix createPosixView(UnixPath file, boolean followLinks) {
411
return new Posix(file, followLinks);
412
}
413
414
static Unix createUnixView(UnixPath file, boolean followLinks) {
415
return new Unix(file, followLinks);
416
}
417
418
static FileOwnerAttributeViewImpl createOwnerView(UnixPath file, boolean followLinks) {
419
return new FileOwnerAttributeViewImpl(createPosixView(file, followLinks));
420
}
421
}
422
423