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