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