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/UnixFileAttributes.java
41137 views
1
/*
2
* Copyright (c) 2008, 2019, 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.util.concurrent.TimeUnit;
30
import java.util.Set;
31
import java.util.HashSet;
32
33
/**
34
* Unix implementation of PosixFileAttributes.
35
*/
36
37
class UnixFileAttributes
38
implements PosixFileAttributes
39
{
40
private int st_mode;
41
private long st_ino;
42
private long st_dev;
43
private long st_rdev;
44
private int st_nlink;
45
private int st_uid;
46
private int st_gid;
47
private long st_size;
48
private long st_atime_sec;
49
private long st_atime_nsec;
50
private long st_mtime_sec;
51
private long st_mtime_nsec;
52
private long st_ctime_sec;
53
private long st_ctime_nsec;
54
private long st_birthtime_sec;
55
56
// created lazily
57
private volatile UserPrincipal owner;
58
private volatile GroupPrincipal group;
59
private volatile UnixFileKey key;
60
61
private UnixFileAttributes() {
62
}
63
64
// get the UnixFileAttributes for a given file
65
static UnixFileAttributes get(UnixPath path, boolean followLinks)
66
throws UnixException
67
{
68
UnixFileAttributes attrs = new UnixFileAttributes();
69
if (followLinks) {
70
UnixNativeDispatcher.stat(path, attrs);
71
} else {
72
UnixNativeDispatcher.lstat(path, attrs);
73
}
74
return attrs;
75
}
76
77
// get the UnixFileAttributes for an open file
78
static UnixFileAttributes get(int fd) throws UnixException {
79
UnixFileAttributes attrs = new UnixFileAttributes();
80
UnixNativeDispatcher.fstat(fd, attrs);
81
return attrs;
82
}
83
84
// get the UnixFileAttributes for a given file, relative to open directory
85
static UnixFileAttributes get(int dfd, UnixPath path, boolean followLinks)
86
throws UnixException
87
{
88
UnixFileAttributes attrs = new UnixFileAttributes();
89
int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW;
90
UnixNativeDispatcher.fstatat(dfd, path.asByteArray(), flag, attrs);
91
return attrs;
92
}
93
94
// package-private
95
boolean isSameFile(UnixFileAttributes attrs) {
96
return ((st_ino == attrs.st_ino) && (st_dev == attrs.st_dev));
97
}
98
99
// package-private
100
int mode() { return st_mode; }
101
long ino() { return st_ino; }
102
long dev() { return st_dev; }
103
long rdev() { return st_rdev; }
104
int nlink() { return st_nlink; }
105
int uid() { return st_uid; }
106
int gid() { return st_gid; }
107
108
private static FileTime toFileTime(long sec, long nsec) {
109
if (nsec == 0) {
110
return FileTime.from(sec, TimeUnit.SECONDS);
111
} else {
112
try {
113
long nanos = Math.addExact(nsec,
114
Math.multiplyExact(sec, 1_000_000_000L));
115
return FileTime.from(nanos, TimeUnit.NANOSECONDS);
116
} catch (ArithmeticException ignore) {
117
// truncate to microseconds if nanoseconds overflow
118
long micro = sec*1_000_000L + nsec/1_000L;
119
return FileTime.from(micro, TimeUnit.MICROSECONDS);
120
}
121
}
122
}
123
124
FileTime ctime() {
125
return toFileTime(st_ctime_sec, st_ctime_nsec);
126
}
127
128
boolean isDevice() {
129
int type = st_mode & UnixConstants.S_IFMT;
130
return (type == UnixConstants.S_IFCHR ||
131
type == UnixConstants.S_IFBLK ||
132
type == UnixConstants.S_IFIFO);
133
}
134
135
@Override
136
public FileTime lastModifiedTime() {
137
return toFileTime(st_mtime_sec, st_mtime_nsec);
138
}
139
140
@Override
141
public FileTime lastAccessTime() {
142
return toFileTime(st_atime_sec, st_atime_nsec);
143
}
144
145
@Override
146
public FileTime creationTime() {
147
if (UnixNativeDispatcher.birthtimeSupported()) {
148
return FileTime.from(st_birthtime_sec, TimeUnit.SECONDS);
149
} else {
150
// return last modified when birth time not supported
151
return lastModifiedTime();
152
}
153
}
154
155
@Override
156
public boolean isRegularFile() {
157
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFREG);
158
}
159
160
@Override
161
public boolean isDirectory() {
162
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFDIR);
163
}
164
165
@Override
166
public boolean isSymbolicLink() {
167
return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFLNK);
168
}
169
170
@Override
171
public boolean isOther() {
172
int type = st_mode & UnixConstants.S_IFMT;
173
return (type != UnixConstants.S_IFREG &&
174
type != UnixConstants.S_IFDIR &&
175
type != UnixConstants.S_IFLNK);
176
}
177
178
@Override
179
public long size() {
180
return st_size;
181
}
182
183
@Override
184
public UnixFileKey fileKey() {
185
if (key == null) {
186
synchronized (this) {
187
if (key == null) {
188
key = new UnixFileKey(st_dev, st_ino);
189
}
190
}
191
}
192
return key;
193
}
194
195
@Override
196
public UserPrincipal owner() {
197
if (owner == null) {
198
synchronized (this) {
199
if (owner == null) {
200
owner = UnixUserPrincipals.fromUid(st_uid);
201
}
202
}
203
}
204
return owner;
205
}
206
207
@Override
208
public GroupPrincipal group() {
209
if (group == null) {
210
synchronized (this) {
211
if (group == null) {
212
group = UnixUserPrincipals.fromGid(st_gid);
213
}
214
}
215
}
216
return group;
217
}
218
219
@Override
220
public Set<PosixFilePermission> permissions() {
221
int bits = (st_mode & UnixConstants.S_IAMB);
222
HashSet<PosixFilePermission> perms = new HashSet<>();
223
224
if ((bits & UnixConstants.S_IRUSR) > 0)
225
perms.add(PosixFilePermission.OWNER_READ);
226
if ((bits & UnixConstants.S_IWUSR) > 0)
227
perms.add(PosixFilePermission.OWNER_WRITE);
228
if ((bits & UnixConstants.S_IXUSR) > 0)
229
perms.add(PosixFilePermission.OWNER_EXECUTE);
230
231
if ((bits & UnixConstants.S_IRGRP) > 0)
232
perms.add(PosixFilePermission.GROUP_READ);
233
if ((bits & UnixConstants.S_IWGRP) > 0)
234
perms.add(PosixFilePermission.GROUP_WRITE);
235
if ((bits & UnixConstants.S_IXGRP) > 0)
236
perms.add(PosixFilePermission.GROUP_EXECUTE);
237
238
if ((bits & UnixConstants.S_IROTH) > 0)
239
perms.add(PosixFilePermission.OTHERS_READ);
240
if ((bits & UnixConstants.S_IWOTH) > 0)
241
perms.add(PosixFilePermission.OTHERS_WRITE);
242
if ((bits & UnixConstants.S_IXOTH) > 0)
243
perms.add(PosixFilePermission.OTHERS_EXECUTE);
244
245
return perms;
246
}
247
248
// wrap this object with BasicFileAttributes object to prevent leaking of
249
// user information
250
BasicFileAttributes asBasicFileAttributes() {
251
return UnixAsBasicFileAttributes.wrap(this);
252
}
253
254
// unwrap BasicFileAttributes to get the underlying UnixFileAttributes
255
// object. Returns null is not wrapped.
256
static UnixFileAttributes toUnixFileAttributes(BasicFileAttributes attrs) {
257
if (attrs instanceof UnixFileAttributes)
258
return (UnixFileAttributes)attrs;
259
if (attrs instanceof UnixAsBasicFileAttributes) {
260
return ((UnixAsBasicFileAttributes)attrs).unwrap();
261
}
262
return null;
263
}
264
265
// wrap a UnixFileAttributes object as a BasicFileAttributes
266
private static class UnixAsBasicFileAttributes implements BasicFileAttributes {
267
private final UnixFileAttributes attrs;
268
269
private UnixAsBasicFileAttributes(UnixFileAttributes attrs) {
270
this.attrs = attrs;
271
}
272
273
static UnixAsBasicFileAttributes wrap(UnixFileAttributes attrs) {
274
return new UnixAsBasicFileAttributes(attrs);
275
}
276
277
UnixFileAttributes unwrap() {
278
return attrs;
279
}
280
281
@Override
282
public FileTime lastModifiedTime() {
283
return attrs.lastModifiedTime();
284
}
285
@Override
286
public FileTime lastAccessTime() {
287
return attrs.lastAccessTime();
288
}
289
@Override
290
public FileTime creationTime() {
291
return attrs.creationTime();
292
}
293
@Override
294
public boolean isRegularFile() {
295
return attrs.isRegularFile();
296
}
297
@Override
298
public boolean isDirectory() {
299
return attrs.isDirectory();
300
}
301
@Override
302
public boolean isSymbolicLink() {
303
return attrs.isSymbolicLink();
304
}
305
@Override
306
public boolean isOther() {
307
return attrs.isOther();
308
}
309
@Override
310
public long size() {
311
return attrs.size();
312
}
313
@Override
314
public Object fileKey() {
315
return attrs.fileKey();
316
}
317
}
318
}
319
320