Win32FileSystem.java revision 2362
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
class Win32FileSystem extends FileSystem {
private final char slash;
private final char altSlash;
private final char semicolon;
public Win32FileSystem() {
}
private boolean isSlash(char c) {
return (c == '\\') || (c == '/');
}
private boolean isLetter(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
else return p;
}
/* -- Normalization and construction -- */
public char getSeparator() {
return slash;
}
public char getPathSeparator() {
return semicolon;
}
/* A normal Win32 pathname contains no duplicate slashes, except possibly
for a UNC prefix, and does not end with a slash. It may be the empty
string. Normalized Win32 pathnames have the convenient property that
the length of the prefix almost uniquely identifies the type of the path
and whether it is absolute or relative:
0 relative to both drive and directory
1 drive-relative (begins with '\\')
2 absolute UNC (if first char is '\\'),
else directory-relative (has form "z:foo")
3 absolute local pathname (begins with "z:\\")
*/
int src = 0;
char c;
/* Remove leading slashes if followed by drive specifier.
This hack is necessary to support file URLs containing drive
specifiers (e.g., "file://c:/path"). As a side effect,
"/c:/path" can be used as an alternative to "c:/path". */
src += 2;
} else {
src = 0;
if ((len >= 2)
/* UNC pathname: Retain first slash; leave src pointed at
second slash so that further slashes will be collapsed
into the second slash. The result will be a pathname
beginning with "\\\\" followed (most likely) by a host
name. */
src = 1;
}
}
return src;
}
/* Normalize the given pathname, whose length is len, starting at the given
offset; everything before this offset is already normal. */
int src;
if (off == 0) {
/* Complete normalization, including prefix */
} else {
/* Partial normalization */
}
/* Remove redundant slashes from the remainder of the path, forcing all
slashes into the preferred slash */
if (isSlash(c)) {
/* Check for trailing separator */
/* "z:\\" */
break;
}
if (sn == 0) {
/* "\\" */
break;
}
/* "\\\\" is not collapsed to "\\" because "\\\\" marks
the beginning of a UNC pathname. Even though it is
not, by itself, a valid UNC pathname, we leave it as
is in order to be consistent with the win32 APIs,
which treat this case as an invalid UNC pathname
rather than as an alias for the root directory of
the current drive. */
break;
}
/* Path does not denote a root directory, so do not append
trailing slash */
break;
} else {
}
} else {
}
}
return rv;
}
/* Check that the given pathname is normal. If not, invoke the real
normalizer on the part of the pathname that requires normalization.
This way we iterate through the whole pathname string only once. */
char prev = 0;
for (int i = 0; i < n; i++) {
if (c == altSlash)
if ((c == ':') && (i > 1))
prev = c;
}
return path;
}
if (n == 0) return 0;
return 1; /* Drive-relative "\\foo" */
}
return 3; /* Absolute local pathname "z:\\foo" */
return 2; /* Directory-relative "z:foo" */
}
return 0; /* Completely relative */
}
int childStart = 0;
/* Drop prefix when child is a UNC pathname */
childStart = 2;
} else {
/* Drop prefix when child is drive-relative */
childStart = 1;
}
return parent;
}
}
parentEnd--;
} else {
}
}
public String getDefaultParent() {
return ("" + slash);
}
// "/c:/foo" --> "c:/foo"
p = p.substring(1);
// "c:/foo/" --> "c:/foo", but "c:/" --> "c:/"
// "/foo/" --> "/foo"
}
return p;
}
/* -- Path operations -- */
public boolean isAbsolute(File f) {
int pl = f.getPrefixLength();
|| (pl == 3));
}
private static int driveIndex(char d) {
if ((d >= 'a') && (d <= 'z')) return d - 'a';
if ((d >= 'A') && (d <= 'Z')) return d - 'A';
return -1;
}
int i = driveIndex(drive);
if (i < 0) return null;
String s = driveDirCache[i];
if (s != null) return s;
s = getDriveDirectory(i + 1);
driveDirCache[i] = s;
return s;
}
private String getUserPath() {
/* For both compatibility and security,
we must look this up every time */
}
}
int pl = f.getPrefixLength();
return path; /* UNC */
if (pl == 3)
return path; /* Absolute local */
if (pl == 0)
}
/* When resolving a directory-relative path that refers to a
drive other than the current drive, insist that the caller
have read permission on the result */
try {
} catch (SecurityException x) {
/* Don't disclose the drive's directory in the exception */
}
return p;
}
}
}
// Caches for canonicalization results to improve startup performance.
// The first cache handles repeated canonicalizations of the same path
// name. The prefix cache handles repeated canonicalizations within the
// same directory, and must not create results differing from the true
// canonicalization algorithm in canonicalize_md.c. For this reason the
// prefix cache is conservative and is not used for complex path names.
// If path is a drive letter only then skip canonicalization
if ((len == 2) &&
if ((c >= 'A') && (c <= 'Z'))
return path;
return "" + ((char) (c-32)) + ':';
} else if ((len == 3) &&
if ((c >= 'A') && (c <= 'Z'))
return path;
return "" + ((char) (c-32)) + ':' + '\\';
}
if (!useCanonCaches) {
return canonicalize0(path);
} else {
if (useCanonPrefixCache) {
// Hit only in prefix cache; full path is canonical,
// but we need to get the canonical name of the file
// in this directory to get the appropriate capitalization
}
}
}
if (f.exists() && !f.isDirectory()) {
}
}
}
}
}
return res;
}
}
throws IOException;
{
}
// Run the canonicalization operation assuming that the prefix
// (everything up to the last filename) is canonical; just gets
// the canonical name of the last element of the path
throws IOException;
// Best-effort attempt to get parent of this path; used for
// optimization of filename canonicalization. This must return null for
// any cases where the code in canonicalize_md.c would throw an
// exception or otherwise deal with non-simple pathnames like handling
// of "." and "..". It may conservatively return null in other
// situations as well. Returning null will cause the underlying
// (expensive) canonicalization routine to be called.
char altSep = '/';
int adjacentDots = 0;
int nonDotCount = 0;
while (idx > 0) {
if (c == '.') {
if (++adjacentDots >= 2) {
// Punt on pathnames containing . and ..
return null;
}
if (nonDotCount == 0) {
// Punt on pathnames ending in a .
return null;
}
} else if (c == sep) {
// Punt on pathnames containing . and ..
return null;
}
if (idx == 0 ||
// Punt on pathnames containing adjacent slashes
// toward the end
return null;
}
} else if (c == altSep) {
// Punt on pathnames containing both backward and
// forward slashes
return null;
} else if (c == '*' || c == '?') {
// Punt on pathnames containing wildcards
return null;
} else {
++nonDotCount;
adjacentDots = 0;
}
--idx;
}
return null;
}
/* -- Attribute accessors -- */
public native int getBooleanAttributes(File f);
public native long getLastModifiedTime(File f);
/* -- File operations -- */
throws IOException;
// Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this
// not worth it since these entries expire after 30 seconds
// anyway.
prefixCache.clear();
return delete0(f);
}
public native boolean createDirectory(File f);
// Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this
// not worth it since these entries expire after 30 seconds
// anyway.
prefixCache.clear();
}
public native boolean setReadOnly(File f);
/* -- Filesystem interface -- */
try {
return true;
} catch (SecurityException x) {
return false;
}
}
private static native int listRoots0();
int ds = listRoots0();
int n = 0;
for (int i = 0; i < 26; i++) {
ds &= ~(1 << i);
else
n++;
}
}
int j = 0;
for (int i = 0; i < 26; i++) {
}
return fs;
}
/* -- Disk usage -- */
if (f.exists()) {
}
return 0;
}
/* -- Basic infrastructure -- */
}
/* Could make this more efficient: String.hashCodeIgnoreCase */
}
private static native void initIDs();
static {
initIDs();
}
}