forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnixFileSystem.java
More file actions
392 lines (338 loc) · 12.8 KB
/
Copy pathUnixFileSystem.java
File metadata and controls
392 lines (338 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.util.Properties;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;
class UnixFileSystem extends FileSystem {
private final char slash;
private final char colon;
private final String javaHome;
private final String userDir;
public UnixFileSystem() {
Properties props = GetPropertyAction.privilegedGetProperties();
slash = props.getProperty("file.separator").charAt(0);
colon = props.getProperty("path.separator").charAt(0);
javaHome = StaticProperty.javaHome();
userDir = StaticProperty.userDir();
cache = useCanonCaches ? new ExpiringCache() : null;
javaHomePrefixCache = useCanonPrefixCache ? new ExpiringCache() : null;
}
/* -- Normalization and construction -- */
@Override
public char getSeparator() {
return slash;
}
@Override
public char getPathSeparator() {
return colon;
}
/* A normal Unix pathname contains no duplicate slashes and does not end
with a slash. It may be the empty string. */
/**
* Normalize the given pathname, starting at the given
* offset; everything before off is already normal, and there's at least
* one duplicate or trailing slash to be removed
*/
private String normalize(String pathname, int off) {
int n = pathname.length();
while ((n > off) && (pathname.charAt(n - 1) == '/')) n--;
if (n == 0) return "/";
if (n == off) return pathname.substring(0, off);
StringBuilder sb = new StringBuilder(n);
if (off > 0) sb.append(pathname, 0, off);
char prevChar = 0;
for (int i = off; i < n; i++) {
char c = pathname.charAt(i);
if ((prevChar == '/') && (c == '/')) continue;
sb.append(c);
prevChar = c;
}
return sb.toString();
}
/* Check that the given pathname is normal. If not, invoke the real
normalizer on the part of the pathname that requires normalization.
This way we iterate through the whole pathname string only once. */
@Override
public String normalize(String pathname) {
int doubleSlash = pathname.indexOf("//");
if (doubleSlash >= 0) {
return normalize(pathname, doubleSlash);
}
if (pathname.endsWith("/")) {
return normalize(pathname, pathname.length() - 1);
}
return pathname;
}
@Override
public int prefixLength(String pathname) {
return pathname.startsWith("/") ? 1 : 0;
}
@Override
public String resolve(String parent, String child) {
if (child.isEmpty()) return parent;
if (child.charAt(0) == '/') {
if (parent.equals("/")) return child;
return parent + child;
}
if (parent.equals("/")) return parent + child;
return parent + '/' + child;
}
@Override
public String getDefaultParent() {
return "/";
}
@Override
public String fromURIPath(String path) {
String p = path;
if (p.endsWith("/") && (p.length() > 1)) {
// "/foo/" --> "/foo", but "/" --> "/"
p = p.substring(0, p.length() - 1);
}
return p;
}
/* -- Path operations -- */
@Override
public boolean isAbsolute(File f) {
return (f.getPrefixLength() != 0);
}
@Override
public String resolve(File f) {
if (isAbsolute(f)) return f.getPath();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess("user.dir");
}
return resolve(userDir, f.getPath());
}
// Caches for canonicalization results to improve startup performance.
// The first cache handles repeated canonicalizations of the same path
// name. The prefix cache handles repeated canonicalizations within the
// same directory, and must not create results differing from the true
// canonicalization algorithm in canonicalize_md.c. For this reason the
// prefix cache is conservative and is not used for complex path names.
private final ExpiringCache cache;
// On Unix symlinks can jump anywhere in the file system, so we only
// treat prefixes in java.home as trusted and cacheable in the
// canonicalization algorithm
private final ExpiringCache javaHomePrefixCache;
@Override
public String canonicalize(String path) throws IOException {
if (!useCanonCaches) {
return canonicalize0(path);
} else {
String res = cache.get(path);
if (res == null) {
String dir = null;
String resDir;
if (useCanonPrefixCache) {
// Note that this can cause symlinks that should
// be resolved to a destination directory to be
// resolved to the directory they're contained in
dir = parentOrNull(path);
if (dir != null) {
resDir = javaHomePrefixCache.get(dir);
if (resDir != null) {
// Hit only in prefix cache; full path is canonical
String filename = path.substring(1 + dir.length());
res = resDir + slash + filename;
cache.put(dir + slash + filename, res);
}
}
}
if (res == null) {
res = canonicalize0(path);
cache.put(path, res);
if (useCanonPrefixCache &&
dir != null && dir.startsWith(javaHome)) {
resDir = parentOrNull(res);
// Note that we don't allow a resolved symlink
// to elsewhere in java.home to pollute the
// prefix cache (java.home prefix cache could
// just as easily be a set at this point)
if (resDir != null && resDir.equals(dir)) {
File f = new File(res);
if (f.exists() && !f.isDirectory()) {
javaHomePrefixCache.put(dir, resDir);
}
}
}
}
}
return res;
}
}
private native String canonicalize0(String path) throws IOException;
// Best-effort attempt to get parent of this path; used for
// optimization of filename canonicalization. This must return null for
// any cases where the code in canonicalize_md.c would throw an
// exception or otherwise deal with non-simple pathnames like handling
// of "." and "..". It may conservatively return null in other
// situations as well. Returning null will cause the underlying
// (expensive) canonicalization routine to be called.
static String parentOrNull(String path) {
if (path == null) return null;
char sep = File.separatorChar;
int last = path.length() - 1;
int idx = last;
int adjacentDots = 0;
int nonDotCount = 0;
while (idx > 0) {
char c = path.charAt(idx);
if (c == '.') {
if (++adjacentDots >= 2) {
// Punt on pathnames containing . and ..
return null;
}
} else if (c == sep) {
if (adjacentDots == 1 && nonDotCount == 0) {
// Punt on pathnames containing . and ..
return null;
}
if (idx == 0 ||
idx >= last - 1 ||
path.charAt(idx - 1) == sep) {
// Punt on pathnames containing adjacent slashes
// toward the end
return null;
}
return path.substring(0, idx);
} else {
++nonDotCount;
adjacentDots = 0;
}
--idx;
}
return null;
}
/* -- Attribute accessors -- */
public native int getBooleanAttributes0(File f);
@Override
public int getBooleanAttributes(File f) {
int rv = getBooleanAttributes0(f);
return rv | isHidden(f);
}
@Override
public boolean hasBooleanAttributes(File f, int attributes) {
int rv = getBooleanAttributes0(f);
if ((attributes & BA_HIDDEN) != 0) {
rv |= isHidden(f);
}
return (rv & attributes) == attributes;
}
private static int isHidden(File f) {
return f.getName().startsWith(".") ? BA_HIDDEN : 0;
}
@Override
public native boolean checkAccess(File f, int access);
@Override
public native long getLastModifiedTime(File f);
@Override
public native long getLength(File f);
@Override
public native boolean setPermission(File f, int access, boolean enable, boolean owneronly);
/* -- File operations -- */
@Override
public native boolean createFileExclusively(String path)
throws IOException;
@Override
public boolean delete(File f) {
// Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this
// (i.e., only remove/update affected entries) but probably
// not worth it since these entries expire after 30 seconds
// anyway.
if (useCanonCaches) {
cache.clear();
}
if (useCanonPrefixCache) {
javaHomePrefixCache.clear();
}
return delete0(f);
}
private native boolean delete0(File f);
@Override
public native String[] list(File f);
@Override
public native boolean createDirectory(File f);
@Override
public boolean rename(File f1, File f2) {
// Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this
// (i.e., only remove/update affected entries) but probably
// not worth it since these entries expire after 30 seconds
// anyway.
if (useCanonCaches) {
cache.clear();
}
if (useCanonPrefixCache) {
javaHomePrefixCache.clear();
}
return rename0(f1, f2);
}
private native boolean rename0(File f1, File f2);
@Override
public native boolean setLastModifiedTime(File f, long time);
@Override
public native boolean setReadOnly(File f);
/* -- Filesystem interface -- */
@Override
public File[] listRoots() {
try {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead("/");
}
return new File[] { new File("/") };
} catch (SecurityException x) {
return new File[0];
}
}
/* -- Disk usage -- */
@Override
public native long getSpace(File f, int t);
/* -- Basic infrastructure -- */
private native long getNameMax0(String path);
@Override
public int getNameMax(String path) {
long nameMax = getNameMax0(path);
if (nameMax > Integer.MAX_VALUE) {
nameMax = Integer.MAX_VALUE;
}
return (int)nameMax;
}
@Override
public int compare(File f1, File f2) {
return f1.getPath().compareTo(f2.getPath());
}
@Override
public int hashCode(File f) {
return f.getPath().hashCode() ^ 1234321;
}
private static native void initIDs();
static {
initIDs();
}
}