Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java
41139 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.util.*;
32
import java.util.regex.Pattern;
33
import java.io.IOException;
34
35
class WindowsFileSystem
36
extends FileSystem
37
{
38
private final WindowsFileSystemProvider provider;
39
40
// default directory (is absolute), and default root
41
private final String defaultDirectory;
42
private final String defaultRoot;
43
44
// package-private
45
WindowsFileSystem(WindowsFileSystemProvider provider,
46
String dir)
47
{
48
this.provider = provider;
49
50
// parse default directory and check it is absolute
51
WindowsPathParser.Result result = WindowsPathParser.parse(dir);
52
53
if ((result.type() != WindowsPathType.ABSOLUTE) &&
54
(result.type() != WindowsPathType.UNC))
55
throw new AssertionError("Default directory is not an absolute path");
56
this.defaultDirectory = result.path();
57
this.defaultRoot = result.root();
58
}
59
60
// package-private
61
String defaultDirectory() {
62
return defaultDirectory;
63
}
64
65
String defaultRoot() {
66
return defaultRoot;
67
}
68
69
@Override
70
public FileSystemProvider provider() {
71
return provider;
72
}
73
74
@Override
75
public String getSeparator() {
76
return "\\";
77
}
78
79
@Override
80
public boolean isOpen() {
81
return true;
82
}
83
84
@Override
85
public boolean isReadOnly() {
86
return false;
87
}
88
89
@Override
90
public void close() throws IOException {
91
throw new UnsupportedOperationException();
92
}
93
94
@Override
95
public Iterable<Path> getRootDirectories() {
96
int drives = 0;
97
try {
98
drives = WindowsNativeDispatcher.GetLogicalDrives();
99
} catch (WindowsException x) {
100
// shouldn't happen
101
throw new AssertionError(x.getMessage());
102
}
103
104
// iterate over roots, ignoring those that the security manager denies
105
ArrayList<Path> result = new ArrayList<>();
106
@SuppressWarnings("removal")
107
SecurityManager sm = System.getSecurityManager();
108
for (int i = 0; i <= 25; i++) { // 0->A, 1->B, 2->C...
109
if ((drives & (1 << i)) != 0) {
110
StringBuilder sb = new StringBuilder(3);
111
sb.append((char)('A' + i));
112
sb.append(":\\");
113
String root = sb.toString();
114
if (sm != null) {
115
try {
116
sm.checkRead(root);
117
} catch (SecurityException x) {
118
continue;
119
}
120
}
121
result.add(WindowsPath.createFromNormalizedPath(this, root));
122
}
123
}
124
return Collections.unmodifiableList(result);
125
}
126
127
/**
128
* Iterator returned by getFileStores method.
129
*/
130
private class FileStoreIterator implements Iterator<FileStore> {
131
private final Iterator<Path> roots;
132
private FileStore next;
133
134
FileStoreIterator() {
135
this.roots = getRootDirectories().iterator();
136
}
137
138
private FileStore readNext() {
139
assert Thread.holdsLock(this);
140
for (;;) {
141
if (!roots.hasNext())
142
return null;
143
WindowsPath root = (WindowsPath)roots.next();
144
// ignore if security manager denies access
145
try {
146
root.checkRead();
147
} catch (SecurityException x) {
148
continue;
149
}
150
try {
151
FileStore fs = WindowsFileStore.create(root.toString(), true);
152
if (fs != null)
153
return fs;
154
} catch (IOException ioe) {
155
// skip it
156
}
157
}
158
}
159
160
@Override
161
public synchronized boolean hasNext() {
162
if (next != null)
163
return true;
164
next = readNext();
165
return next != null;
166
}
167
168
@Override
169
public synchronized FileStore next() {
170
if (next == null)
171
next = readNext();
172
if (next == null) {
173
throw new NoSuchElementException();
174
} else {
175
FileStore result = next;
176
next = null;
177
return result;
178
}
179
}
180
181
@Override
182
public void remove() {
183
throw new UnsupportedOperationException();
184
}
185
}
186
187
@Override
188
public Iterable<FileStore> getFileStores() {
189
@SuppressWarnings("removal")
190
SecurityManager sm = System.getSecurityManager();
191
if (sm != null) {
192
try {
193
sm.checkPermission(new RuntimePermission("getFileStoreAttributes"));
194
} catch (SecurityException se) {
195
return Collections.emptyList();
196
}
197
}
198
return new Iterable<FileStore>() {
199
public Iterator<FileStore> iterator() {
200
return new FileStoreIterator();
201
}
202
};
203
}
204
205
// supported views
206
private static final Set<String> supportedFileAttributeViews = Collections
207
.unmodifiableSet(new HashSet<String>(Arrays.asList("basic", "dos", "acl", "owner", "user")));
208
209
@Override
210
public Set<String> supportedFileAttributeViews() {
211
return supportedFileAttributeViews;
212
}
213
214
@Override
215
public final Path getPath(String first, String... more) {
216
Objects.requireNonNull(first);
217
String path;
218
if (more.length == 0) {
219
path = first;
220
} else {
221
StringBuilder sb = new StringBuilder();
222
sb.append(first);
223
for (String segment: more) {
224
if (!segment.isEmpty()) {
225
if (sb.length() > 0)
226
sb.append('\\');
227
sb.append(segment);
228
}
229
}
230
path = sb.toString();
231
}
232
return WindowsPath.parse(this, path);
233
}
234
235
@Override
236
public UserPrincipalLookupService getUserPrincipalLookupService() {
237
return LookupService.instance;
238
}
239
240
private static class LookupService {
241
static final UserPrincipalLookupService instance =
242
new UserPrincipalLookupService() {
243
@Override
244
public UserPrincipal lookupPrincipalByName(String name)
245
throws IOException
246
{
247
return WindowsUserPrincipals.lookup(name);
248
}
249
@Override
250
public GroupPrincipal lookupPrincipalByGroupName(String group)
251
throws IOException
252
{
253
UserPrincipal user = WindowsUserPrincipals.lookup(group);
254
if (!(user instanceof GroupPrincipal))
255
throw new UserPrincipalNotFoundException(group);
256
return (GroupPrincipal)user;
257
}
258
};
259
}
260
261
@Override
262
public PathMatcher getPathMatcher(String syntaxAndInput) {
263
int pos = syntaxAndInput.indexOf(':');
264
if (pos <= 0 || pos == syntaxAndInput.length())
265
throw new IllegalArgumentException();
266
String syntax = syntaxAndInput.substring(0, pos);
267
String input = syntaxAndInput.substring(pos+1);
268
269
String expr;
270
if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
271
expr = Globs.toWindowsRegexPattern(input);
272
} else {
273
if (syntax.equalsIgnoreCase(REGEX_SYNTAX)) {
274
expr = input;
275
} else {
276
throw new UnsupportedOperationException("Syntax '" + syntax +
277
"' not recognized");
278
}
279
}
280
281
// match in unicode_case_insensitive
282
final Pattern pattern = Pattern.compile(expr,
283
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
284
285
// return matcher
286
return new PathMatcher() {
287
@Override
288
public boolean matches(Path path) {
289
return pattern.matcher(path.toString()).matches();
290
}
291
};
292
}
293
private static final String GLOB_SYNTAX = "glob";
294
private static final String REGEX_SYNTAX = "regex";
295
296
@Override
297
public WatchService newWatchService()
298
throws IOException
299
{
300
return new WindowsWatchService(this);
301
}
302
}
303
304