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/UnixFileSystem.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.nio.file.spi.*;
31
import java.io.IOException;
32
import java.util.*;
33
import java.util.regex.Pattern;
34
import sun.security.action.GetPropertyAction;
35
36
/**
37
* Base implementation of FileSystem for Unix-like implementations.
38
*/
39
40
abstract class UnixFileSystem
41
extends FileSystem
42
{
43
private final UnixFileSystemProvider provider;
44
private final byte[] defaultDirectory;
45
private final boolean needToResolveAgainstDefaultDirectory;
46
private final UnixPath rootDirectory;
47
48
// package-private
49
UnixFileSystem(UnixFileSystemProvider provider, String dir) {
50
this.provider = provider;
51
this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
52
if (this.defaultDirectory[0] != '/') {
53
throw new RuntimeException("default directory must be absolute");
54
}
55
56
// if process-wide chdir is allowed or default directory is not the
57
// process working directory then paths must be resolved against the
58
// default directory.
59
String propValue = GetPropertyAction
60
.privilegedGetProperty("sun.nio.fs.chdirAllowed", "false");
61
boolean chdirAllowed = propValue.isEmpty() ? true : Boolean.parseBoolean(propValue);
62
if (chdirAllowed) {
63
this.needToResolveAgainstDefaultDirectory = true;
64
} else {
65
byte[] cwd = UnixNativeDispatcher.getcwd();
66
boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
67
if (defaultIsCwd) {
68
for (int i=0; i<cwd.length; i++) {
69
if (cwd[i] != defaultDirectory[i]) {
70
defaultIsCwd = false;
71
break;
72
}
73
}
74
}
75
this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
76
}
77
78
// the root directory
79
this.rootDirectory = new UnixPath(this, "/");
80
}
81
82
// package-private
83
byte[] defaultDirectory() {
84
return defaultDirectory;
85
}
86
87
boolean needToResolveAgainstDefaultDirectory() {
88
return needToResolveAgainstDefaultDirectory;
89
}
90
91
UnixPath rootDirectory() {
92
return rootDirectory;
93
}
94
95
static List<String> standardFileAttributeViews() {
96
return Arrays.asList("basic", "posix", "unix", "owner");
97
}
98
99
@Override
100
public final FileSystemProvider provider() {
101
return provider;
102
}
103
104
@Override
105
public final String getSeparator() {
106
return "/";
107
}
108
109
@Override
110
public final boolean isOpen() {
111
return true;
112
}
113
114
@Override
115
public final boolean isReadOnly() {
116
return false;
117
}
118
119
@Override
120
public final void close() throws IOException {
121
throw new UnsupportedOperationException();
122
}
123
124
/**
125
* Copies non-POSIX attributes from the source to target file.
126
*
127
* Copying a file preserving attributes, or moving a file, will preserve
128
* the file owner/group/permissions/timestamps but it does not preserve
129
* other non-POSIX attributes. This method is invoked by the
130
* copy or move operation to preserve these attributes. It should copy
131
* extended attributes, ACLs, or other attributes.
132
*
133
* @param sfd
134
* Open file descriptor to source file
135
* @param tfd
136
* Open file descriptor to target file
137
*/
138
void copyNonPosixAttributes(int sfd, int tfd) {
139
// no-op by default
140
}
141
142
/**
143
* Unix systems only have a single root directory (/)
144
*/
145
@Override
146
public final Iterable<Path> getRootDirectories() {
147
final List<Path> allowedList =
148
Collections.unmodifiableList(Arrays.asList((Path)rootDirectory));
149
return new Iterable<>() {
150
public Iterator<Path> iterator() {
151
try {
152
@SuppressWarnings("removal")
153
SecurityManager sm = System.getSecurityManager();
154
if (sm != null)
155
sm.checkRead(rootDirectory.toString());
156
return allowedList.iterator();
157
} catch (SecurityException x) {
158
List<Path> disallowed = Collections.emptyList();
159
return disallowed.iterator();
160
}
161
}
162
};
163
}
164
165
/**
166
* Returns object to iterate over entries in mounttab or equivalent
167
*/
168
abstract Iterable<UnixMountEntry> getMountEntries();
169
170
/**
171
* Returns a FileStore to represent the file system for the given mount
172
* mount.
173
*/
174
abstract FileStore getFileStore(UnixMountEntry entry) throws IOException;
175
176
/**
177
* Iterator returned by getFileStores method.
178
*/
179
private class FileStoreIterator implements Iterator<FileStore> {
180
private final Iterator<UnixMountEntry> entries;
181
private FileStore next;
182
183
FileStoreIterator() {
184
this.entries = getMountEntries().iterator();
185
}
186
187
private FileStore readNext() {
188
assert Thread.holdsLock(this);
189
for (;;) {
190
if (!entries.hasNext())
191
return null;
192
UnixMountEntry entry = entries.next();
193
194
// skip entries with the "ignore" option
195
if (entry.isIgnored())
196
continue;
197
198
// check permission to read mount point
199
@SuppressWarnings("removal")
200
SecurityManager sm = System.getSecurityManager();
201
if (sm != null) {
202
try {
203
sm.checkRead(Util.toString(entry.dir()));
204
} catch (SecurityException x) {
205
continue;
206
}
207
}
208
try {
209
return getFileStore(entry);
210
} catch (IOException ignore) {
211
// ignore as per spec
212
}
213
}
214
}
215
216
@Override
217
public synchronized boolean hasNext() {
218
if (next != null)
219
return true;
220
next = readNext();
221
return next != null;
222
}
223
224
@Override
225
public synchronized FileStore next() {
226
if (next == null)
227
next = readNext();
228
if (next == null) {
229
throw new NoSuchElementException();
230
} else {
231
FileStore result = next;
232
next = null;
233
return result;
234
}
235
}
236
237
@Override
238
public void remove() {
239
throw new UnsupportedOperationException();
240
}
241
}
242
243
@Override
244
public final Iterable<FileStore> getFileStores() {
245
@SuppressWarnings("removal")
246
SecurityManager sm = System.getSecurityManager();
247
if (sm != null) {
248
try {
249
sm.checkPermission(new RuntimePermission("getFileStoreAttributes"));
250
} catch (SecurityException se) {
251
return Collections.emptyList();
252
}
253
}
254
return new Iterable<>() {
255
public Iterator<FileStore> iterator() {
256
return new FileStoreIterator();
257
}
258
};
259
}
260
261
@Override
262
public final Path getPath(String first, String... more) {
263
Objects.requireNonNull(first);
264
String path;
265
if (more.length == 0) {
266
path = first;
267
} else {
268
StringBuilder sb = new StringBuilder();
269
sb.append(first);
270
for (String segment: more) {
271
if (!segment.isEmpty()) {
272
if (sb.length() > 0)
273
sb.append('/');
274
sb.append(segment);
275
}
276
}
277
path = sb.toString();
278
}
279
return new UnixPath(this, path);
280
}
281
282
@Override
283
public PathMatcher getPathMatcher(String syntaxAndInput) {
284
int pos = syntaxAndInput.indexOf(':');
285
if (pos <= 0 || pos == syntaxAndInput.length())
286
throw new IllegalArgumentException();
287
String syntax = syntaxAndInput.substring(0, pos);
288
String input = syntaxAndInput.substring(pos+1);
289
290
String expr;
291
if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
292
expr = Globs.toUnixRegexPattern(input);
293
} else {
294
if (syntax.equalsIgnoreCase(REGEX_SYNTAX)) {
295
expr = input;
296
} else {
297
throw new UnsupportedOperationException("Syntax '" + syntax +
298
"' not recognized");
299
}
300
}
301
302
// return matcher
303
final Pattern pattern = compilePathMatchPattern(expr);
304
305
return new PathMatcher() {
306
@Override
307
public boolean matches(Path path) {
308
return pattern.matcher(path.toString()).matches();
309
}
310
};
311
}
312
313
private static final String GLOB_SYNTAX = "glob";
314
private static final String REGEX_SYNTAX = "regex";
315
316
@Override
317
public final UserPrincipalLookupService getUserPrincipalLookupService() {
318
return LookupService.instance;
319
}
320
321
private static class LookupService {
322
static final UserPrincipalLookupService instance =
323
new UserPrincipalLookupService() {
324
@Override
325
public UserPrincipal lookupPrincipalByName(String name)
326
throws IOException
327
{
328
return UnixUserPrincipals.lookupUser(name);
329
}
330
331
@Override
332
public GroupPrincipal lookupPrincipalByGroupName(String group)
333
throws IOException
334
{
335
return UnixUserPrincipals.lookupGroup(group);
336
}
337
};
338
}
339
340
// Override if the platform has different path match requirement, such as
341
// case insensitive or Unicode canonical equal on MacOSX
342
Pattern compilePathMatchPattern(String expr) {
343
return Pattern.compile(expr);
344
}
345
346
// Override if the platform uses different Unicode normalization form
347
// for native file path. For example on MacOSX, the native path is stored
348
// in Unicode NFD form.
349
String normalizeNativePath(String path) {
350
return path;
351
}
352
353
// Override if the native file path use non-NFC form. For example on MacOSX,
354
// the native path is stored in Unicode NFD form, the path need to be
355
// normalized back to NFC before passed back to Java level.
356
String normalizeJavaPath(String path) {
357
return path;
358
}
359
}
360
361