Rahul Ravikumar | 0533600 | 2019-10-14 15:04:32 -0700 | [diff] [blame] | 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 | // 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 199 | public abstract boolean setReadOnly(File f); |
| 200 | |
| 201 | |
| 202 | /* -- Filesystem interface -- */ |
| 203 | |
| 204 | /** |
| 205 | * List the available filesystem roots. |
| 206 | */ |
| 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 | |
| 214 | public abstract long getSpace(File f, int t); |
| 215 | |
| 216 | /* -- Basic infrastructure -- */ |
| 217 | |
| 218 | /** |
| 219 | * Compare two abstract pathnames lexicographically. |
| 220 | */ |
| 221 | public abstract int compare(File f1, File f2); |
| 222 | |
| 223 | /** |
| 224 | * Compute the hash code of an abstract pathname. |
| 225 | */ |
| 226 | public abstract int hashCode(File f); |
| 227 | |
| 228 | // Flags for enabling/disabling performance optimizations for file |
| 229 | // name canonicalization |
| 230 | // Android-changed: Disabled caches for security reasons (b/62301183) |
| 231 | //static boolean useCanonCaches = true; |
| 232 | //static boolean useCanonPrefixCache = true; |
| 233 | static boolean useCanonCaches = false; |
| 234 | static boolean useCanonPrefixCache = false; |
| 235 | |
| 236 | private static boolean getBooleanProperty(String prop, boolean defaultVal) { |
| 237 | String val = System.getProperty(prop); |
| 238 | if (val == null) return defaultVal; |
| 239 | if (val.equalsIgnoreCase("true")) { |
| 240 | return true; |
| 241 | } else { |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static { |
| 247 | useCanonCaches = getBooleanProperty("sun.io.useCanonCaches", |
| 248 | useCanonCaches); |
| 249 | useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache", |
| 250 | useCanonPrefixCache); |
| 251 | } |
| 252 | } |