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