jdk/src/java.base/unix/classes/java/io/UnixFileSystem.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 31471 ae27c6f1d8bf
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6300
diff changeset
     2
 * Copyright (c) 1998, 2010, 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
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 31471
diff changeset
    28
import java.util.Properties;
2
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() {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 31471
diff changeset
    39
        Properties props = GetPropertyAction.getProperties();
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 31471
diff changeset
    40
        slash = props.getProperty("file.separator").charAt(0);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 31471
diff changeset
    41
        colon = props.getProperty("path.separator").charAt(0);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 31471
diff changeset
    42
        javaHome = props.getProperty("java.home");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* -- Normalization and construction -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public char getSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public char getPathSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* A normal Unix pathname contains no duplicate slashes and does not end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
       with a slash.  It may be the empty string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Normalize the given pathname, whose length is len, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
       offset; everything before this offset is already normal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private String normalize(String pathname, int len, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (len == 0) return pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (n == 0) return "/";
31471
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 25859
diff changeset
    66
        StringBuilder sb = new StringBuilder(pathname.length());
ae27c6f1d8bf 8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents: 25859
diff changeset
    67
        if (off > 0) sb.append(pathname, 0, off);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (int i = off; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            char c = pathname.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if ((prevChar == '/') && (c == '/')) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /* Check that the given pathname is normal.  If not, invoke the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       normalizer on the part of the pathname that requires normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       This way we iterate through the whole pathname string only once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public String normalize(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int n = pathname.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            char c = pathname.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if ((prevChar == '/') && (c == '/'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return normalize(pathname, n, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (prevChar == '/') return normalize(pathname, n, n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public int prefixLength(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (pathname.length() == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return (pathname.charAt(0) == '/') ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public String resolve(String parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (child.equals("")) return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (child.charAt(0) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (parent.equals("/")) return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return parent + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (parent.equals("/")) return parent + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return parent + '/' + child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public String getDefaultParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public String fromURIPath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String p = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (p.endsWith("/") && (p.length() > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // "/foo/" --> "/foo", but "/" --> "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            p = p.substring(0, p.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public boolean isAbsolute(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return (f.getPrefixLength() != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public String resolve(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (isAbsolute(f)) return f.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return resolve(System.getProperty("user.dir"), f.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // Caches for canonicalization results to improve startup performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // The first cache handles repeated canonicalizations of the same path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // name. The prefix cache handles repeated canonicalizations within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // same directory, and must not create results differing from the true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // canonicalization algorithm in canonicalize_md.c. For this reason the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // prefix cache is conservative and is not used for complex path names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private ExpiringCache cache = new ExpiringCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // On Unix symlinks can jump anywhere in the file system, so we only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // treat prefixes in java.home as trusted and cacheable in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // canonicalization algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private ExpiringCache javaHomePrefixCache = new ExpiringCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public String canonicalize(String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!useCanonCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return canonicalize0(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            String res = cache.get(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                String dir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                String resDir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (useCanonPrefixCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    // Note that this can cause symlinks that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // be resolved to a destination directory to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // resolved to the directory they're contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    dir = parentOrNull(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (dir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        resDir = javaHomePrefixCache.get(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        if (resDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            // Hit only in prefix cache; full path is canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            String filename = path.substring(1 + dir.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            res = resDir + slash + filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            cache.put(dir + slash + filename, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (res == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    res = canonicalize0(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    cache.put(path, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (useCanonPrefixCache &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        dir != null && dir.startsWith(javaHome)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        resDir = parentOrNull(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        // Note that we don't allow a resolved symlink
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        // to elsewhere in java.home to pollute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        // prefix cache (java.home prefix cache could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        // just as easily be a set at this point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        if (resDir != null && resDir.equals(dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            File f = new File(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            if (f.exists() && !f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                javaHomePrefixCache.put(dir, resDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        }
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
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private native String canonicalize0(String path) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // Best-effort attempt to get parent of this path; used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // optimization of filename canonicalization. This must return null for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // any cases where the code in canonicalize_md.c would throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // exception or otherwise deal with non-simple pathnames like handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // of "." and "..". It may conservatively return null in other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // situations as well. Returning null will cause the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // (expensive) canonicalization routine to be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static String parentOrNull(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (path == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        char sep = File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int last = path.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int idx = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int adjacentDots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int nonDotCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        while (idx > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            char c = path.charAt(idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (++adjacentDots >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    // Punt on pathnames containing . and ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else if (c == sep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (adjacentDots == 1 && nonDotCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    // Punt on pathnames containing . and ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (idx == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    idx >= last - 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    path.charAt(idx - 1) == sep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    // Punt on pathnames containing adjacent slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    // toward the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return path.substring(0, idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ++nonDotCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                adjacentDots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            --idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public native int getBooleanAttributes0(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public int getBooleanAttributes(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int rv = getBooleanAttributes0(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        boolean hidden = (name.length() > 0) && (name.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return rv | (hidden ? BA_HIDDEN : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public native boolean checkAccess(File f, int access);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public native long getLastModifiedTime(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public native long getLength(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public native boolean setPermission(File f, int access, boolean enable, boolean owneronly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public native boolean createFileExclusively(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public boolean delete(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // Keep canonicalization caches in sync after file deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // and renaming operations. Could be more clever than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // (i.e., only remove/update affected entries) but probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // not worth it since these entries expire after 30 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        javaHomePrefixCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return delete0(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private native boolean delete0(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public native String[] list(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public native boolean createDirectory(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public boolean rename(File f1, File f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Keep canonicalization caches in sync after file deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // and renaming operations. Could be more clever than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // (i.e., only remove/update affected entries) but probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // not worth it since these entries expire after 30 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        javaHomePrefixCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return rename0(f1, f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private native boolean rename0(File f1, File f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public native boolean setLastModifiedTime(File f, long time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public native boolean setReadOnly(File f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public File[] listRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                security.checkRead("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return new File[] { new File("/") };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return new File[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /* -- Disk usage -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public native long getSpace(File f, int t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /* -- Basic infrastructure -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public int compare(File f1, File f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return f1.getPath().compareTo(f2.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public int hashCode(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return f.getPath().hashCode() ^ 1234321;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}