jdk/src/solaris/classes/java/io/UnixFileSystem.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 6300 700ec2a5d680
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
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, 2005, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class UnixFileSystem extends FileSystem {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final char slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private final char colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final String javaHome;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public UnixFileSystem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        slash = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            new GetPropertyAction("file.separator")).charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        colon = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            new GetPropertyAction("path.separator")).charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        javaHome = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            new GetPropertyAction("java.home"));
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
    /* -- Normalization and construction -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public char getSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public char getPathSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* A normal Unix pathname contains no duplicate slashes and does not end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
       with a slash.  It may be the empty string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* Normalize the given pathname, whose length is len, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
       offset; everything before this offset is already normal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private String normalize(String pathname, int len, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (len == 0) return pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (n == 0) return "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        StringBuffer sb = new StringBuffer(pathname.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (off > 0) sb.append(pathname.substring(0, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i = off; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            char c = pathname.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if ((prevChar == '/') && (c == '/')) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* Check that the given pathname is normal.  If not, invoke the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
       normalizer on the part of the pathname that requires normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
       This way we iterate through the whole pathname string only once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public String normalize(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int n = pathname.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            char c = pathname.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if ((prevChar == '/') && (c == '/'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return normalize(pathname, n, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (prevChar == '/') return normalize(pathname, n, n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public int prefixLength(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (pathname.length() == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return (pathname.charAt(0) == '/') ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public String resolve(String parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (child.equals("")) return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (child.charAt(0) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (parent.equals("/")) return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return parent + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (parent.equals("/")) return parent + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return parent + '/' + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public String getDefaultParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public String fromURIPath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        String p = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (p.endsWith("/") && (p.length() > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // "/foo/" --> "/foo", but "/" --> "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            p = p.substring(0, p.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public boolean isAbsolute(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return (f.getPrefixLength() != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public String resolve(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (isAbsolute(f)) return f.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return resolve(System.getProperty("user.dir"), f.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // Caches for canonicalization results to improve startup performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // The first cache handles repeated canonicalizations of the same path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // name. The prefix cache handles repeated canonicalizations within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // same directory, and must not create results differing from the true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // canonicalization algorithm in canonicalize_md.c. For this reason the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // prefix cache is conservative and is not used for complex path names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private ExpiringCache cache = new ExpiringCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // On Unix symlinks can jump anywhere in the file system, so we only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // treat prefixes in java.home as trusted and cacheable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // canonicalization algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private ExpiringCache javaHomePrefixCache = new ExpiringCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public String canonicalize(String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (!useCanonCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return canonicalize0(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            String res = cache.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                String dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                String resDir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (useCanonPrefixCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // Note that this can cause symlinks that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // be resolved to a destination directory to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // resolved to the directory they're contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    dir = parentOrNull(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    if (dir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        resDir = javaHomePrefixCache.get(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        if (resDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            // Hit only in prefix cache; full path is canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            String filename = path.substring(1 + dir.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            res = resDir + slash + filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            cache.put(dir + slash + filename, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    res = canonicalize0(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    cache.put(path, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    if (useCanonPrefixCache &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        dir != null && dir.startsWith(javaHome)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        resDir = parentOrNull(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        // Note that we don't allow a resolved symlink
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        // to elsewhere in java.home to pollute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        // prefix cache (java.home prefix cache could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        // just as easily be a set at this point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        if (resDir != null && resDir.equals(dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            File f = new File(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            if (f.exists() && !f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                javaHomePrefixCache.put(dir, resDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            assert canonicalize0(path).equals(res) || path.startsWith(javaHome);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private native String canonicalize0(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // Best-effort attempt to get parent of this path; used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // optimization of filename canonicalization. This must return null for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // any cases where the code in canonicalize_md.c would throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // exception or otherwise deal with non-simple pathnames like handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    // of "." and "..". It may conservatively return null in other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // situations as well. Returning null will cause the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // (expensive) canonicalization routine to be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static String parentOrNull(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (path == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        char sep = File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int last = path.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int idx = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int adjacentDots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int nonDotCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        while (idx > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            char c = path.charAt(idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (++adjacentDots >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    // Punt on pathnames containing . and ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } else if (c == sep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (adjacentDots == 1 && nonDotCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    // Punt on pathnames containing . and ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (idx == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    idx >= last - 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    path.charAt(idx - 1) == sep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    // Punt on pathnames containing adjacent slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    // toward the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return path.substring(0, idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                ++nonDotCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                adjacentDots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            --idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public native int getBooleanAttributes0(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int getBooleanAttributes(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int rv = getBooleanAttributes0(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        String name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        boolean hidden = (name.length() > 0) && (name.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return rv | (hidden ? BA_HIDDEN : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public native boolean checkAccess(File f, int access);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public native long getLastModifiedTime(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public native long getLength(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public native boolean setPermission(File f, int access, boolean enable, boolean owneronly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public native boolean createFileExclusively(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public boolean delete(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // Keep canonicalization caches in sync after file deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // and renaming operations. Could be more clever than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // (i.e., only remove/update affected entries) but probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // not worth it since these entries expire after 30 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        javaHomePrefixCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return delete0(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private native boolean delete0(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public native String[] list(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public native boolean createDirectory(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public boolean rename(File f1, File f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // Keep canonicalization caches in sync after file deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // and renaming operations. Could be more clever than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // (i.e., only remove/update affected entries) but probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // not worth it since these entries expire after 30 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        javaHomePrefixCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return rename0(f1, f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private native boolean rename0(File f1, File f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public native boolean setLastModifiedTime(File f, long time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public native boolean setReadOnly(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public File[] listRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                security.checkRead("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return new File[] { new File("/") };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return new File[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /* -- Disk usage -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public native long getSpace(File f, int t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /* -- Basic infrastructure -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public int compare(File f1, File f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return f1.getPath().compareTo(f2.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public int hashCode(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return f.getPath().hashCode() ^ 1234321;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}