Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 package java.io;
     28 
     29 import java.lang.annotation.Native;
     30 
     31 /**
     32  * Package-private abstract class for the local filesystem abstraction.
     33  */
     34 
     35 abstract class FileSystem {
     36 
     37     /* -- Normalization and construction -- */
     38 
     39     /**
     40      * Return the local filesystem's name-separator character.
     41      */
     42     public abstract char getSeparator();
     43 
     44     /**
     45      * Return the local filesystem's path-separator character.
     46      */
     47     public abstract char getPathSeparator();
     48 
     49     /**
     50      * Convert the given pathname string to normal form.  If the string is
     51      * already in normal form then it is simply returned.
     52      */
     53     public abstract String normalize(String path);
     54 
     55     /**
     56      * Compute the length of this pathname string's prefix.  The pathname
     57      * string must be in normal form.
     58      */
     59     public abstract int prefixLength(String path);
     60 
     61     /**
     62      * Resolve the child pathname string against the parent.
     63      * Both strings must be in normal form, and the result
     64      * will be in normal form.
     65      */
     66     public abstract String resolve(String parent, String child);
     67 
     68     /**
     69      * Return the parent pathname string to be used when the parent-directory
     70      * argument in one of the two-argument File constructors is the empty
     71      * pathname.
     72      */
     73     public abstract String getDefaultParent();
     74 
     75     /**
     76      * Post-process the given URI path string if necessary.  This is used on
     77      * win32, e.g., to transform "/c:/foo" into "c:/foo".  The path string
     78      * still has slash separators; code in the File class will translate them
     79      * after this method returns.
     80      */
     81     public abstract String fromURIPath(String path);
     82 
     83 
     84     /* -- Path operations -- */
     85 
     86     /**
     87      * Tell whether or not the given abstract pathname is absolute.
     88      */
     89     public abstract boolean isAbsolute(File f);
     90 
     91     /**
     92      * Resolve the given abstract pathname into absolute form.  Invoked by the
     93      * getAbsolutePath and getCanonicalPath methods in the File class.
     94      */
     95     public abstract String resolve(File f);
     96 
     97     public abstract String canonicalize(String path) throws IOException;
     98 
     99 
    100     /* -- Attribute accessors -- */
    101 
    102     /* Constants for simple boolean attributes */
    103     @Native public static final int BA_EXISTS    = 0x01;
    104     @Native public static final int BA_REGULAR   = 0x02;
    105     @Native public static final int BA_DIRECTORY = 0x04;
    106     @Native public static final int BA_HIDDEN    = 0x08;
    107 
    108     /**
    109      * Return the simple boolean attributes for the file or directory denoted
    110      * by the given abstract pathname, or zero if it does not exist or some
    111      * other I/O error occurs.
    112      */
    113     public abstract int getBooleanAttributes(File f);
    114 
    115     @Native public static final int ACCESS_READ    = 0x04;
    116     @Native public static final int ACCESS_WRITE   = 0x02;
    117     @Native public static final int ACCESS_EXECUTE = 0x01;
    118     public static final int ACCESS_OK      = 0x08;
    119 
    120     /**
    121      * Check whether the file or directory denoted by the given abstract
    122      * pathname may be accessed by this process.  The second argument specifies
    123      * which access, ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE, to check.
    124      * Return false if access is denied or an I/O error occurs
    125      */
    126     public abstract boolean checkAccess(File f, int access);
    127     /**
    128      * Set on or off the access permission (to owner only or to all) to the file
    129      * or directory denoted by the given abstract pathname, based on the parameters
    130      * enable, access and oweronly.
    131      */
    132     public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);
    133 
    134     /**
    135      * Return the time at which the file or directory denoted by the given
    136      * abstract pathname was last modified, or zero if it does not exist or
    137      * some other I/O error occurs.
    138      */
    139     public abstract long getLastModifiedTime(File f);
    140 
    141     /**
    142      * Return the length in bytes of the file denoted by the given abstract
    143      * pathname, or zero if it does not exist, is a directory, or some other
    144      * I/O error occurs.
    145      */
    146     public abstract long getLength(File f);
    147 
    148 
    149     /* -- File operations -- */
    150 
    151     /**
    152      * Create a new empty file with the given pathname.  Return
    153      * <code>true</code> if the file was created and <code>false</code> if a
    154      * file or directory with the given pathname already exists.  Throw an
    155      * IOException if an I/O error occurs.
    156      */
    157     public abstract boolean createFileExclusively(String pathname)
    158         throws IOException;
    159 
    160     /**
    161      * Delete the file or directory denoted by the given abstract pathname,
    162      * returning <code>true</code> if and only if the operation succeeds.
    163      */
    164     public abstract boolean delete(File f);
    165 
    166     /**
    167      * List the elements of the directory denoted by the given abstract
    168      * pathname.  Return an array of strings naming the elements of the
    169      * directory if successful; otherwise, return <code>null</code>.
    170      */
    171     public abstract String[] list(File f);
    172 
    173     /**
    174      * Create a new directory denoted by the given abstract pathname,
    175      * returning <code>true</code> if and only if the operation succeeds.
    176      */
    177     public abstract boolean createDirectory(File f);
    178 
    179     /**
    180      * Rename the file or directory denoted by the first abstract pathname to
    181      * the second abstract pathname, returning <code>true</code> if and only if
    182      * the operation succeeds.
    183      */
    184     public abstract boolean rename(File f1, File f2);
    185 
    186     /**
    187      * Set the last-modified time of the file or directory denoted by the
    188      * given abstract pathname, returning <code>true</code> if and only if the
    189      * operation succeeds.
    190      */
    191     public abstract boolean setLastModifiedTime(File f, long time);
    192 
    193     /**
    194      * Mark the file or directory denoted by the given abstract pathname as
    195      * read-only, returning <code>true</code> if and only if the operation
    196      * succeeds.
    197      */
    198     public abstract boolean setReadOnly(File f);
    199 
    200 
    201     /* -- Filesystem interface -- */
    202 
    203     /**
    204      * List the available filesystem roots.
    205      */
    206     public abstract File[] listRoots();
    207 
    208     /* -- Disk usage -- */
    209     @Native public static final int SPACE_TOTAL  = 0;
    210     @Native public static final int SPACE_FREE   = 1;
    211     @Native public static final int SPACE_USABLE = 2;
    212 
    213     public abstract long getSpace(File f, int t);
    214 
    215     /* -- Basic infrastructure -- */
    216 
    217     /**
    218      * Compare two abstract pathnames lexicographically.
    219      */
    220     public abstract int compare(File f1, File f2);
    221 
    222     /**
    223      * Compute the hash code of an abstract pathname.
    224      */
    225     public abstract int hashCode(File f);
    226 
    227     // Flags for enabling/disabling performance optimizations for file
    228     // name canonicalization
    229     static boolean useCanonCaches      = true;
    230     static boolean useCanonPrefixCache = true;
    231 
    232     private static boolean getBooleanProperty(String prop, boolean defaultVal) {
    233         String val = System.getProperty(prop);
    234         if (val == null) return defaultVal;
    235         if (val.equalsIgnoreCase("true")) {
    236             return true;
    237         } else {
    238             return false;
    239         }
    240     }
    241 
    242     static {
    243         useCanonCaches      = getBooleanProperty("sun.io.useCanonCaches",
    244                                                  useCanonCaches);
    245         useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache",
    246                                                  useCanonPrefixCache);
    247     }
    248 }
    249