Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/pathNames/General.java
38812 views
1
/*
2
* Copyright (c) 1998, 2013, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
@summary Common definitions for general exhaustive pathname tests
26
@author Mark Reinhold
27
*/
28
29
import java.io.*;
30
import java.util.*;
31
import java.nio.file.*;
32
33
34
public class General {
35
36
public static boolean debug = false;
37
38
private static boolean win32 = (File.separatorChar == '\\');
39
40
private static int gensymCounter = 0;
41
42
protected static final String userDir = System.getProperty("user.dir");
43
44
protected static String baseDir = null;
45
protected static String relative = null;
46
47
/* Generate a filename unique to this run */
48
private static String gensym() {
49
return "x." + ++gensymCounter;
50
}
51
52
/**
53
* Create files and folders in the test working directory.
54
* The purpose is to make sure the test will not go out of
55
* its user dir when walking the file tree.
56
*
57
* @param depth The number of directory levels to be created under
58
* the user directory. It should be the maximum value
59
* of the depths passed to checkNames method (including
60
* direct or indirect calling) in a whole test.
61
*/
62
protected static void initTestData(int depth) throws IOException {
63
File parent = new File(userDir);
64
for (int i = 0; i < depth; i++) {
65
File tmp = new File(parent, gensym());
66
tmp.createNewFile();
67
tmp = new File(parent, gensym());
68
if (tmp.mkdir())
69
parent = tmp;
70
else
71
throw new IOException("Fail to create directory, " + tmp);
72
}
73
baseDir = parent.getAbsolutePath();
74
relative = baseDir.substring(userDir.length() + 1);
75
}
76
77
/**
78
* Find a file in the given subdirectory, or descend into further
79
* subdirectories, if any, if no file is found here. Return null if no
80
* file can be found anywhere beneath the given subdirectory.
81
* @param dir Directory at which we started
82
* @param subdir Subdirectory that we're exploring
83
* @param dl Listing of subdirectory
84
*/
85
private static String findSomeFile(String dir, String subdir, String[] dl) {
86
for (int i = 0; i < dl.length; i++) {
87
File f = new File(subdir, dl[i]);
88
File df = new File(dir, f.getPath());
89
if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
90
return f.getPath();
91
}
92
}
93
for (int i = 0; i < dl.length; i++) {
94
File f = (subdir.length() == 0) ? new File(dl[i])
95
: new File(subdir, dl[i]);
96
File df = new File(dir, f.getPath());
97
if (Files.isDirectory(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
98
String[] dl2 = df.list();
99
if (dl2 != null) {
100
String ff = findSomeFile(dir, f.getPath(), dl2);
101
if (ff != null) return ff;
102
}
103
}
104
}
105
return null;
106
}
107
108
109
/**
110
* Construct a string that names a file in the given directory. If create
111
* is true, then create a file if none is found, and throw an exception if
112
* that is not possible; otherwise, return null if no file can be found.
113
*/
114
private static String findSomeFile(String dir, boolean create) {
115
File d = new File(dir);
116
String[] dl = d.list();
117
if (dl == null) {
118
throw new RuntimeException("Can't list " + dir);
119
}
120
for (int i = 0; i < dl.length; i++) {
121
File f = new File(dir, dl[i]);
122
if (Files.isRegularFile(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
123
return dl[i];
124
}
125
}
126
String f = findSomeFile(dir, "", dl);
127
if (f != null) {
128
return f;
129
}
130
if (create) {
131
File nf = new File(d, gensym());
132
OutputStream os;
133
try {
134
os = new FileOutputStream(nf);
135
os.close();
136
} catch (IOException x) {
137
throw new RuntimeException("Can't create a file in " + dir);
138
}
139
return nf.getName();
140
}
141
return null;
142
}
143
144
145
/**
146
* Construct a string that names a subdirectory of the given directory.
147
* If create is true, then create a subdirectory if none is found, and
148
* throw an exception if that is not possible; otherwise, return null if
149
* no subdirectory can be found.
150
*/
151
private static String findSomeDir(String dir, boolean create) {
152
File d = new File(dir);
153
String[] dl = d.list();
154
if (dl == null) {
155
throw new RuntimeException("Can't list " + dir);
156
}
157
for (int i = 0; i < dl.length; i++) {
158
File f = new File(d, dl[i]);
159
if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
160
String[] dl2 = f.list();
161
if (dl2 == null || dl2.length >= 250) {
162
/* Heuristic to avoid scanning huge directories */
163
continue;
164
}
165
return dl[i];
166
}
167
}
168
if (create) {
169
File sd = new File(d, gensym());
170
if (sd.mkdir()) return sd.getName();
171
}
172
return null;
173
}
174
175
176
/** Construct a string that does not name a file in the given directory */
177
private static String findNon(String dir) {
178
File d = new File(dir);
179
String[] x = new String[] { "foo", "bar", "baz" };
180
for (int i = 0; i < x.length; i++) {
181
File f = new File(d, x[i]);
182
if (!f.exists()) {
183
return x[i];
184
}
185
}
186
for (int i = 0; i < 1024; i++) {
187
String n = "xx" + Integer.toString(i);
188
File f = new File(d, n);
189
if (!f.exists()) {
190
return n;
191
}
192
}
193
throw new RuntimeException("Can't find a non-existent file in " + dir);
194
}
195
196
197
/** Ensure that the named file does not exist */
198
public static void ensureNon(String fn) {
199
if ((new File(fn)).exists()) {
200
throw new RuntimeException("Test path " + fn + " exists");
201
}
202
}
203
204
205
/** Tell whether the given character is a "slash" on this platform */
206
private static boolean isSlash(char x) {
207
if (x == File.separatorChar) return true;
208
if (win32 && (x == '/')) return true;
209
return false;
210
}
211
212
213
/**
214
* Trim trailing slashes from the given string, but leave singleton slashes
215
* alone (they denote root directories)
216
*/
217
private static String trimTrailingSlashes(String s) {
218
int n = s.length();
219
if (n == 0) return s;
220
n--;
221
while ((n > 0) && isSlash(s.charAt(n))) {
222
if ((n >= 1) && s.charAt(n - 1) == ':') break;
223
n--;
224
}
225
return s.substring(0, n + 1);
226
}
227
228
229
/** Concatenate two paths, trimming slashes as needed */
230
private static String pathConcat(String a, String b) {
231
if (a.length() == 0) return b;
232
if (b.length() == 0) return a;
233
if (isSlash(a.charAt(a.length() - 1))
234
|| isSlash(b.charAt(0))
235
|| (win32 && (a.charAt(a.length() - 1) == ':'))) {
236
return a + b;
237
} else {
238
return a + File.separatorChar + b;
239
}
240
}
241
242
243
244
/** Hash table of input pathnames, used to detect duplicates */
245
private static Hashtable<String, String> checked = new Hashtable<>();
246
247
/**
248
* Check the given pathname. Its canonical pathname should be the given
249
* answer. If the path names a file that exists and is readable, then
250
* FileInputStream and RandomAccessFile should both be able to open it.
251
*/
252
public static void check(String answer, String path) throws IOException {
253
String ans = trimTrailingSlashes(answer);
254
if (path.length() == 0) return;
255
if (checked.get(path) != null) {
256
System.err.println("DUP " + path);
257
return;
258
}
259
checked.put(path, path);
260
261
String cpath;
262
try {
263
File f = new File(path);
264
cpath = f.getCanonicalPath();
265
if (f.exists() && f.isFile() && f.canRead()) {
266
InputStream in = new FileInputStream(path);
267
in.close();
268
RandomAccessFile raf = new RandomAccessFile(path, "r");
269
raf.close();
270
}
271
} catch (IOException x) {
272
System.err.println(ans + " <-- " + path + " ==> " + x);
273
if (debug) return;
274
else throw x;
275
}
276
if (cpath.equals(ans)) {
277
System.err.println(ans + " <== " + path);
278
} else {
279
System.err.println(ans + " <-- " + path + " ==> " + cpath + " MISMATCH");
280
if (!debug) {
281
throw new RuntimeException("Mismatch: " + path + " ==> " + cpath +
282
", should be " + ans);
283
}
284
}
285
}
286
287
288
289
/*
290
* The following three mutually-recursive methods generate and check a tree
291
* of filenames of arbitrary depth. Each method has (at least) these
292
* arguments:
293
*
294
* int depth Remaining tree depth
295
* boolean create Controls whether test files and directories
296
* will be created as needed
297
* String ans Expected answer for the check method (above)
298
* String ask Input pathname to be passed to the check method
299
*/
300
301
302
/** Check a single slash case, plus its children */
303
private static void checkSlash(int depth, boolean create,
304
String ans, String ask, String slash)
305
throws Exception
306
{
307
check(ans, ask + slash);
308
checkNames(depth, create,
309
ans.endsWith(File.separator) ? ans : ans + File.separator,
310
ask + slash);
311
}
312
313
314
/** Check slash cases for the given ask string */
315
public static void checkSlashes(int depth, boolean create,
316
String ans, String ask)
317
throws Exception
318
{
319
check(ans, ask);
320
if (depth == 0) return;
321
322
checkSlash(depth, create, ans, ask, "/");
323
checkSlash(depth, create, ans, ask, "//");
324
checkSlash(depth, create, ans, ask, "///");
325
if (win32) {
326
checkSlash(depth, create, ans, ask, "\\");
327
checkSlash(depth, create, ans, ask, "\\\\");
328
checkSlash(depth, create, ans, ask, "\\/");
329
checkSlash(depth, create, ans, ask, "/\\");
330
checkSlash(depth, create, ans, ask, "\\\\\\");
331
}
332
}
333
334
335
/** Check name cases for the given ask string */
336
public static void checkNames(int depth, boolean create,
337
String ans, String ask)
338
throws Exception
339
{
340
int d = depth - 1;
341
File f = new File(ans);
342
String n;
343
344
/* Normal name */
345
if (f.exists()) {
346
if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) {
347
if ((n = findSomeFile(ans, create)) != null)
348
checkSlashes(d, create, ans + n, ask + n);
349
if ((n = findSomeDir(ans, create)) != null)
350
checkSlashes(d, create, ans + n, ask + n);
351
}
352
n = findNon(ans);
353
checkSlashes(d, create, ans + n, ask + n);
354
} else {
355
n = "foo" + depth;
356
checkSlashes(d, create, ans + n, ask + n);
357
}
358
359
/* "." */
360
checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
361
362
/* ".." */
363
if ((n = f.getParent()) != null) {
364
String n2;
365
if (win32
366
&& ((n2 = f.getParentFile().getParent()) != null)
367
&& n2.equals("\\\\")) {
368
/* Win32 resolves \\foo\bar\.. to \\foo\bar */
369
checkSlashes(d, create, ans, ask + "..");
370
} else {
371
checkSlashes(d, create, n, ask + "..");
372
}
373
}
374
else {
375
if (win32)
376
checkSlashes(d, create, ans, ask + "..");
377
else {
378
// Fix for 4237875. We must ensure that we are sufficiently
379
// deep in the path hierarchy to test parents this high up
380
File thisPath = new File(ask);
381
File nextPath = new File(ask + "..");
382
if (!thisPath.getCanonicalPath().equals(nextPath.getCanonicalPath()))
383
checkSlashes(d, create, ans + "..", ask + "..");
384
}
385
}
386
}
387
}
388
389