8221262: Cleanups in UnixFileSystem/WinNTFileSystem implementation classes

Reviewed-by: alanb, igerasim
This commit is contained in:
Christoph Langer 2019-03-26 09:16:07 +01:00
parent 57bf6a1800
commit 6ede0b6bb8
3 changed files with 43 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -51,10 +51,12 @@ class UnixFileSystem extends FileSystem {
/* -- Normalization and construction -- */ /* -- Normalization and construction -- */
@Override
public char getSeparator() { public char getSeparator() {
return slash; return slash;
} }
@Override
public char getPathSeparator() { public char getPathSeparator() {
return colon; return colon;
} }
@ -84,6 +86,7 @@ class UnixFileSystem extends FileSystem {
/* Check that the given pathname is normal. If not, invoke the real /* Check that the given pathname is normal. If not, invoke the real
normalizer on the part of the pathname that requires normalization. normalizer on the part of the pathname that requires normalization.
This way we iterate through the whole pathname string only once. */ This way we iterate through the whole pathname string only once. */
@Override
public String normalize(String pathname) { public String normalize(String pathname) {
int n = pathname.length(); int n = pathname.length();
char prevChar = 0; char prevChar = 0;
@ -97,11 +100,13 @@ class UnixFileSystem extends FileSystem {
return pathname; return pathname;
} }
@Override
public int prefixLength(String pathname) { public int prefixLength(String pathname) {
if (pathname.isEmpty()) return 0; if (pathname.isEmpty()) return 0;
return (pathname.charAt(0) == '/') ? 1 : 0; return (pathname.charAt(0) == '/') ? 1 : 0;
} }
@Override
public String resolve(String parent, String child) { public String resolve(String parent, String child) {
if (child.isEmpty()) return parent; if (child.isEmpty()) return parent;
if (child.charAt(0) == '/') { if (child.charAt(0) == '/') {
@ -112,10 +117,12 @@ class UnixFileSystem extends FileSystem {
return parent + '/' + child; return parent + '/' + child;
} }
@Override
public String getDefaultParent() { public String getDefaultParent() {
return "/"; return "/";
} }
@Override
public String fromURIPath(String path) { public String fromURIPath(String path) {
String p = path; String p = path;
if (p.endsWith("/") && (p.length() > 1)) { if (p.endsWith("/") && (p.length() > 1)) {
@ -128,10 +135,12 @@ class UnixFileSystem extends FileSystem {
/* -- Path operations -- */ /* -- Path operations -- */
@Override
public boolean isAbsolute(File f) { public boolean isAbsolute(File f) {
return (f.getPrefixLength() != 0); return (f.getPrefixLength() != 0);
} }
@Override
public String resolve(File f) { public String resolve(File f) {
if (isAbsolute(f)) return f.getPath(); if (isAbsolute(f)) return f.getPath();
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
@ -153,6 +162,7 @@ class UnixFileSystem extends FileSystem {
// canonicalization algorithm // canonicalization algorithm
private final ExpiringCache javaHomePrefixCache; private final ExpiringCache javaHomePrefixCache;
@Override
public String canonicalize(String path) throws IOException { public String canonicalize(String path) throws IOException {
if (!useCanonCaches) { if (!useCanonCaches) {
return canonicalize0(path); return canonicalize0(path);
@ -246,6 +256,7 @@ class UnixFileSystem extends FileSystem {
public native int getBooleanAttributes0(File f); public native int getBooleanAttributes0(File f);
@Override
public int getBooleanAttributes(File f) { public int getBooleanAttributes(File f) {
int rv = getBooleanAttributes0(f); int rv = getBooleanAttributes0(f);
String name = f.getName(); String name = f.getName();
@ -253,15 +264,25 @@ class UnixFileSystem extends FileSystem {
return rv | (hidden ? BA_HIDDEN : 0); return rv | (hidden ? BA_HIDDEN : 0);
} }
@Override
public native boolean checkAccess(File f, int access); public native boolean checkAccess(File f, int access);
@Override
public native long getLastModifiedTime(File f); public native long getLastModifiedTime(File f);
@Override
public native long getLength(File f); public native long getLength(File f);
@Override
public native boolean setPermission(File f, int access, boolean enable, boolean owneronly); public native boolean setPermission(File f, int access, boolean enable, boolean owneronly);
/* -- File operations -- */ /* -- File operations -- */
@Override
public native boolean createFileExclusively(String path) public native boolean createFileExclusively(String path)
throws IOException; throws IOException;
@Override
public boolean delete(File f) { public boolean delete(File f) {
// Keep canonicalization caches in sync after file deletion // Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this // and renaming operations. Could be more clever than this
@ -277,8 +298,14 @@ class UnixFileSystem extends FileSystem {
return delete0(f); return delete0(f);
} }
private native boolean delete0(File f); private native boolean delete0(File f);
@Override
public native String[] list(File f); public native String[] list(File f);
@Override
public native boolean createDirectory(File f); public native boolean createDirectory(File f);
@Override
public boolean rename(File f1, File f2) { public boolean rename(File f1, File f2) {
// Keep canonicalization caches in sync after file deletion // Keep canonicalization caches in sync after file deletion
// and renaming operations. Could be more clever than this // and renaming operations. Could be more clever than this
@ -294,12 +321,16 @@ class UnixFileSystem extends FileSystem {
return rename0(f1, f2); return rename0(f1, f2);
} }
private native boolean rename0(File f1, File f2); private native boolean rename0(File f1, File f2);
public native boolean setLastModifiedTime(File f, long time);
public native boolean setReadOnly(File f);
@Override
public native boolean setLastModifiedTime(File f, long time);
@Override
public native boolean setReadOnly(File f);
/* -- Filesystem interface -- */ /* -- Filesystem interface -- */
@Override
public File[] listRoots() { public File[] listRoots() {
try { try {
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
@ -313,12 +344,15 @@ class UnixFileSystem extends FileSystem {
} }
/* -- Disk usage -- */ /* -- Disk usage -- */
@Override
public native long getSpace(File f, int t); public native long getSpace(File f, int t);
/* -- Basic infrastructure -- */ /* -- Basic infrastructure -- */
private native long getNameMax0(String path); private native long getNameMax0(String path);
@Override
public int getNameMax(String path) { public int getNameMax(String path) {
long nameMax = getNameMax0(path); long nameMax = getNameMax0(path);
if (nameMax > Integer.MAX_VALUE) { if (nameMax > Integer.MAX_VALUE) {
@ -327,10 +361,12 @@ class UnixFileSystem extends FileSystem {
return (int)nameMax; return (int)nameMax;
} }
@Override
public int compare(File f1, File f2) { public int compare(File f1, File f2) {
return f1.getPath().compareTo(f2.getPath()); return f1.getPath().compareTo(f2.getPath());
} }
@Override
public int hashCode(File f) { public int hashCode(File f) {
return f.getPath().hashCode() ^ 1234321; return f.getPath().hashCode() ^ 1234321;
} }
@ -341,5 +377,4 @@ class UnixFileSystem extends FileSystem {
static { static {
initIDs(); initIDs();
} }
} }

View File

@ -284,10 +284,10 @@ Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666); fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd < 0) { if (fd < 0) {
if (errno != EEXIST) if (errno != EEXIST)
JNU_ThrowIOExceptionWithLastError(env, path); JNU_ThrowIOExceptionWithLastError(env, "Could not open file");
} else { } else {
if (close(fd) == -1) if (close(fd) == -1)
JNU_ThrowIOExceptionWithLastError(env, path); JNU_ThrowIOExceptionWithLastError(env, "Could not close file");
rv = JNI_TRUE; rv = JNI_TRUE;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -328,7 +328,6 @@ class WinNTFileSystem extends FileSystem {
return up + slashify(path.substring(2)); return up + slashify(path.substring(2));
char drive = path.charAt(0); char drive = path.charAt(0);
String dir = getDriveDirectory(drive); String dir = getDriveDirectory(drive);
String np;
if (dir != null) { if (dir != null) {
/* When resolving a directory-relative path that refers to a /* When resolving a directory-relative path that refers to a
drive other than the current drive, insist that the caller drive other than the current drive, insist that the caller
@ -641,6 +640,7 @@ class WinNTFileSystem extends FileSystem {
// expects the path to be null or a root component ending in a backslash // expects the path to be null or a root component ending in a backslash
private native int getNameMax0(String path); private native int getNameMax0(String path);
@Override
public int getNameMax(String path) { public int getNameMax(String path) {
String s = null; String s = null;
if (path != null) { if (path != null) {