1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1998, 2016, 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 */ getSeparator()42 public abstract char getSeparator(); 43 44 /** 45 * Return the local filesystem's path-separator character. 46 */ getPathSeparator()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 */ normalize(String path)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 */ prefixLength(String path)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 */ resolve(String parent, String child)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 */ getDefaultParent()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 */ fromURIPath(String path)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 */ isAbsolute(File f)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 */ resolve(File f)95 public abstract String resolve(File f); 96 canonicalize(String path)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 */ getBooleanAttributes(File f)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 // Android-added: b/25878034, to support File.exists() reimplementation. 119 public static final int ACCESS_OK = 0x08; 120 121 /** 122 * Check whether the file or directory denoted by the given abstract 123 * pathname may be accessed by this process. The second argument specifies 124 * which access, ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE, to check. 125 * Return false if access is denied or an I/O error occurs 126 */ checkAccess(File f, int access)127 public abstract boolean checkAccess(File f, int access); 128 /** 129 * Set on or off the access permission (to owner only or to all) to the file 130 * or directory denoted by the given abstract pathname, based on the parameters 131 * enable, access and oweronly. 132 */ setPermission(File f, int access, boolean enable, boolean owneronly)133 public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly); 134 135 /** 136 * Return the time at which the file or directory denoted by the given 137 * abstract pathname was last modified, or zero if it does not exist or 138 * some other I/O error occurs. 139 */ getLastModifiedTime(File f)140 public abstract long getLastModifiedTime(File f); 141 142 /** 143 * Return the length in bytes of the file denoted by the given abstract 144 * pathname, or zero if it does not exist, is a directory, or some other 145 * I/O error occurs. 146 */ getLength(File f)147 public abstract long getLength(File f); 148 149 150 /* -- File operations -- */ 151 152 /** 153 * Create a new empty file with the given pathname. Return 154 * <code>true</code> if the file was created and <code>false</code> if a 155 * file or directory with the given pathname already exists. Throw an 156 * IOException if an I/O error occurs. 157 */ createFileExclusively(String pathname)158 public abstract boolean createFileExclusively(String pathname) 159 throws IOException; 160 161 /** 162 * Delete the file or directory denoted by the given abstract pathname, 163 * returning <code>true</code> if and only if the operation succeeds. 164 */ delete(File f)165 public abstract boolean delete(File f); 166 167 /** 168 * List the elements of the directory denoted by the given abstract 169 * pathname. Return an array of strings naming the elements of the 170 * directory if successful; otherwise, return <code>null</code>. 171 */ list(File f)172 public abstract String[] list(File f); 173 174 /** 175 * Create a new directory denoted by the given abstract pathname, 176 * returning <code>true</code> if and only if the operation succeeds. 177 */ createDirectory(File f)178 public abstract boolean createDirectory(File f); 179 180 /** 181 * Rename the file or directory denoted by the first abstract pathname to 182 * the second abstract pathname, returning <code>true</code> if and only if 183 * the operation succeeds. 184 */ rename(File f1, File f2)185 public abstract boolean rename(File f1, File f2); 186 187 /** 188 * Set the last-modified time of the file or directory denoted by the 189 * given abstract pathname, returning <code>true</code> if and only if the 190 * operation succeeds. 191 */ setLastModifiedTime(File f, long time)192 public abstract boolean setLastModifiedTime(File f, long time); 193 194 /** 195 * Mark the file or directory denoted by the given abstract pathname as 196 * read-only, returning <code>true</code> if and only if the operation 197 * succeeds. 198 */ setReadOnly(File f)199 public abstract boolean setReadOnly(File f); 200 201 202 /* -- Filesystem interface -- */ 203 204 /** 205 * List the available filesystem roots. 206 */ listRoots()207 public abstract File[] listRoots(); 208 209 /* -- Disk usage -- */ 210 @Native public static final int SPACE_TOTAL = 0; 211 @Native public static final int SPACE_FREE = 1; 212 @Native public static final int SPACE_USABLE = 2; 213 getSpace(File f, int t)214 public abstract long getSpace(File f, int t); 215 216 /* -- Basic infrastructure -- */ 217 218 /** 219 * Retrieve the maximum length of a component of a file path. 220 * 221 * @return The maximum length of a file path component. 222 */ getNameMax(String path)223 public abstract int getNameMax(String path); 224 225 /** 226 * Compare two abstract pathnames lexicographically. 227 */ compare(File f1, File f2)228 public abstract int compare(File f1, File f2); 229 230 /** 231 * Compute the hash code of an abstract pathname. 232 */ hashCode(File f)233 public abstract int hashCode(File f); 234 235 // Flags for enabling/disabling performance optimizations for file 236 // name canonicalization 237 // Android-changed: Disabled caches for security reasons (b/62301183) 238 //static boolean useCanonCaches = true; 239 //static boolean useCanonPrefixCache = true; 240 static boolean useCanonCaches = false; 241 static boolean useCanonPrefixCache = false; 242 getBooleanProperty(String prop, boolean defaultVal)243 private static boolean getBooleanProperty(String prop, boolean defaultVal) { 244 return Boolean.parseBoolean(System.getProperty(prop, 245 String.valueOf(defaultVal))); 246 } 247 248 static { 249 useCanonCaches = getBooleanProperty("sun.io.useCanonCaches", 250 useCanonCaches); 251 useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache", 252 useCanonPrefixCache); 253 } 254 } 255