1 /*
   2  * Copyright (c) 1998, 2013, 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 import java.lang.annotation.Native;
  29 
  30 /**
  31  * Package-private abstract class for the local filesystem abstraction.
  32  */
  33 
  34 abstract class FileSystem {
  35 
  36     /* -- Normalization and construction -- */
  37 
  38     /**
  39      * Return the local filesystem's name-separator character.
  40      */
  41     public abstract char getSeparator();
  42 
  43     /**
  44      * Return the local filesystem's path-separator character.
  45      */
  46     public abstract char getPathSeparator();
  47 
  48     /**
  49      * Convert the given pathname string to normal form.  If the string is
  50      * already in normal form then it is simply returned.
  51      */
  52     public abstract String normalize(String path);
  53 
  54     /**
  55      * Compute the length of this pathname string's prefix.  The pathname
  56      * string must be in normal form.
  57      */
  58     public abstract int prefixLength(String path);
  59 
  60     /**
  61      * Resolve the child pathname string against the parent.
  62      * Both strings must be in normal form, and the result
  63      * will be in normal form.
  64      */
  65     public abstract String resolve(String parent, String child);
  66 
  67     /**
  68      * Return the parent pathname string to be used when the parent-directory
  69      * argument in one of the two-argument File constructors is the empty
  70      * pathname.
  71      */
  72     public abstract String getDefaultParent();
  73 
  74     /**
  75      * Post-process the given URI path string if necessary.  This is used on
  76      * win32, e.g., to transform "/c:/foo" into "c:/foo".  The path string
  77      * still has slash separators; code in the File class will translate them
  78      * after this method returns.
  79      */
  80     public abstract String fromURIPath(String path);
  81 
  82 
  83     /* -- Path operations -- */
  84 
  85     /**
  86      * Tell whether or not the given abstract pathname is absolute.
  87      */
  88     public abstract boolean isAbsolute(File f);
  89 
  90     /**
  91      * Resolve the given abstract pathname into absolute form.  Invoked by the
  92      * getAbsolutePath and getCanonicalPath methods in the File class.
  93      */
  94     public abstract String resolve(File f);
  95 
  96     public abstract String canonicalize(String path) throws IOException;
  97 
  98 
  99     /* -- Attribute accessors -- */
 100 
 101     /* Constants for simple boolean attributes */
 102     @Native public static final int BA_EXISTS    = 0x01;
 103     @Native public static final int BA_REGULAR   = 0x02;
 104     @Native public static final int BA_DIRECTORY = 0x04;
 105     @Native public static final int BA_HIDDEN    = 0x08;
 106 
 107     /**
 108      * Return the simple boolean attributes for the file or directory denoted
 109      * by the given abstract pathname, or zero if it does not exist or some
 110      * other I/O error occurs.
 111      */
 112     public abstract int getBooleanAttributes(File f);
 113 
 114     @Native public static final int ACCESS_READ    = 0x04;
 115     @Native public static final int ACCESS_WRITE   = 0x02;
 116     @Native public static final int ACCESS_EXECUTE = 0x01;
 117 
 118     /**
 119      * Check whether the file or directory denoted by the given abstract
 120      * pathname may be accessed by this process.  The second argument specifies
 121      * which access, ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE, to check.
 122      * Return false if access is denied or an I/O error occurs
 123      */
 124     public abstract boolean checkAccess(File f, int access);
 125     /**
 126      * Set on or off the access permission (to owner only or to all) to the file
 127      * or directory denoted by the given abstract pathname, based on the parameters
 128      * enable, access and oweronly.
 129      */
 130     public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);
 131 
 132     /**
 133      * Return the time at which the file or directory denoted by the given
 134      * abstract pathname was last modified, or zero if it does not exist or
 135      * some other I/O error occurs.
 136      */
 137     public abstract long getLastModifiedTime(File f);
 138 
 139     /**
 140      * Return the length in bytes of the file denoted by the given abstract
 141      * pathname, or zero if it does not exist, is a directory, or some other
 142      * I/O error occurs.
 143      */
 144     public abstract long getLength(File f);
 145 
 146 
 147     /* -- File operations -- */
 148 
 149     /**
 150      * Create a new empty file with the given pathname.  Return
 151      * <code>true</code> if the file was created and <code>false</code> if a
 152      * file or directory with the given pathname already exists.  Throw an
 153      * IOException if an I/O error occurs.
 154      */
 155     public abstract boolean createFileExclusively(String pathname)
 156         throws IOException;
 157 
 158     /**
 159      * Delete the file or directory denoted by the given abstract pathname,
 160      * returning <code>true</code> if and only if the operation succeeds.
 161      */
 162     public abstract boolean delete(File f);
 163 
 164     /**
 165      * List the elements of the directory denoted by the given abstract
 166      * pathname.  Return an array of strings naming the elements of the
 167      * directory if successful; otherwise, return <code>null</code>.
 168      */
 169     public abstract String[] list(File f);
 170 
 171     /**
 172      * Create a new directory denoted by the given abstract pathname,
 173      * returning <code>true</code> if and only if the operation succeeds.
 174      */
 175     public abstract boolean createDirectory(File f);
 176 
 177     /**
 178      * Rename the file or directory denoted by the first abstract pathname to
 179      * the second abstract pathname, returning <code>true</code> if and only if
 180      * the operation succeeds.
 181      */
 182     public abstract boolean rename(File f1, File f2);
 183 
 184     /**
 185      * Set the last-modified time of the file or directory denoted by the
 186      * given abstract pathname, returning <code>true</code> if and only if the
 187      * operation succeeds.
 188      */
 189     public abstract boolean setLastModifiedTime(File f, long time);
 190 
 191     /**
 192      * Mark the file or directory denoted by the given abstract pathname as
 193      * read-only, returning <code>true</code> if and only if the operation
 194      * succeeds.
 195      */
 196     public abstract boolean setReadOnly(File f);
 197 
 198 
 199     /* -- Filesystem interface -- */
 200 
 201     /**
 202      * List the available filesystem roots.
 203      */
 204     public abstract File[] listRoots();
 205 
 206     /* -- Disk usage -- */
 207     @Native public static final int SPACE_TOTAL  = 0;
 208     @Native public static final int SPACE_FREE   = 1;
 209     @Native public static final int SPACE_USABLE = 2;
 210 
 211     public abstract long getSpace(File f, int t);
 212 
 213     /* -- Basic infrastructure -- */
 214 
 215     /**
 216      * Compare two abstract pathnames lexicographically.
 217      */
 218     public abstract int compare(File f1, File f2);
 219 
 220     /**
 221      * Compute the hash code of an abstract pathname.
 222      */
 223     public abstract int hashCode(File f);
 224 
 225     // Flags for enabling/disabling performance optimizations for file
 226     // name canonicalization
 227     static boolean useCanonCaches      = true;
 228     static boolean useCanonPrefixCache = true;
 229 
 230     private static boolean getBooleanProperty(String prop, boolean defaultVal) {
 231         String val = System.getProperty(prop);
 232         if (val == null) return defaultVal;
 233         if (val.equalsIgnoreCase("true")) {
 234             return true;
 235         } else {
 236             return false;
 237         }
 238     }
 239 
 240     // NUL character
 241     static final char CHAR_NUL = '\u0000';
 242 
 243     static {
 244         useCanonCaches      = getBooleanProperty("sun.io.useCanonCaches",
 245                                                  useCanonCaches);
 246         useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache",
 247                                                  useCanonPrefixCache);
 248     }
 249 }