提交 7142b15a 编写于 作者: A alanb

7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037

Reviewed-by: sherman, ulfzibis
上级 07b9e3b6
......@@ -27,6 +27,9 @@ package sun.nio.fs;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.Charset;
import java.security.*;
import sun.security.action.*;
/**
* Utility methods
......@@ -35,6 +38,33 @@ import java.nio.file.*;
class Util {
private Util() { }
private static final Charset jnuEncoding = Charset.forName(
AccessController.doPrivileged(new GetPropertyAction("sun.jnu.encoding")));
/**
* Returns {@code Charset} corresponding to the sun.jnu.encoding property
*/
static Charset jnuEncoding() {
return jnuEncoding;
}
/**
* Encodes the given String into a sequence of bytes using the {@code Charset}
* specified by the sun.jnu.encoding property.
*/
static byte[] toBytes(String s) {
return s.getBytes(jnuEncoding);
}
/**
* Constructs a new String by decoding the specified array of bytes using the
* {@code Charset} specified by the sun.jnu.encoding property.
*/
static String toString(byte[] bytes) {
return new String(bytes, jnuEncoding);
}
/**
* Splits a string around the given character. The array returned by this
* method contains each substring that is terminated by the character. Use
......
......@@ -70,12 +70,12 @@ public class GnomeFileTypeDetector
// GIO may access file so need permission check
path.checkRead();
byte[] type = probeUsingGio(buffer.address());
return (type == null) ? null : new String(type);
return (type == null) ? null : Util.toString(type);
} else {
byte[] type = probeUsingGnomeVfs(buffer.address());
if (type == null)
return null;
String s = new String(type);
String s = Util.toString(type);
return s.equals(GNOME_VFS_MIME_TYPE_UNKNOWN) ? null : s;
}
} finally {
......
......@@ -51,7 +51,7 @@ class LinuxDosFileAttributeView
private static final String HIDDEN_NAME = "hidden";
private static final String DOS_XATTR_NAME = "user.DOSATTRIB";
private static final byte[] DOS_XATTR_NAME_AS_BYTES = DOS_XATTR_NAME.getBytes();
private static final byte[] DOS_XATTR_NAME_AS_BYTES = Util.toBytes(DOS_XATTR_NAME);
private static final int DOS_XATTR_READONLY = 0x01;
private static final int DOS_XATTR_HIDDEN = 0x02;
......@@ -225,7 +225,7 @@ class LinuxDosFileAttributeView
byte[] buf = new byte[len];
unsafe.copyMemory(null, buffer.address(), buf,
Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
String value = new String(buf); // platform encoding
String value = Util.toString(buf);
// should be something like 0x20
if (value.length() >= 3 && value.startsWith("0x")) {
......@@ -263,7 +263,7 @@ class LinuxDosFileAttributeView
newValue &= ~flag;
}
if (newValue != oldValue) {
byte[] value = ("0x" + Integer.toHexString(newValue)).getBytes();
byte[] value = Util.toBytes("0x" + Integer.toHexString(newValue));
NativeBuffer buffer = NativeBuffers.asNativeBuffer(value);
try {
LinuxNativeDispatcher.fsetxattr(fd, DOS_XATTR_NAME_AS_BYTES,
......
......@@ -98,7 +98,8 @@ class LinuxFileStore
int fd = path.openForAttributeAccess(false);
try {
// fgetxattr returns size if called with size==0
LinuxNativeDispatcher.fgetxattr(fd, "user.java".getBytes(), 0L, 0);
byte[] name = Util.toBytes("user.java");
LinuxNativeDispatcher.fgetxattr(fd, name, 0L, 0);
return true;
} catch (UnixException e) {
// attribute does not exist
......
......@@ -78,7 +78,7 @@ class LinuxFileSystem extends UnixFileSystem {
Iterable<UnixMountEntry> getMountEntries(String fstab) {
ArrayList<UnixMountEntry> entries = new ArrayList<>();
try {
long fp = setmntent(fstab.getBytes(), "r".getBytes());
long fp = setmntent(Util.toBytes(fstab), Util.toBytes("r"));
try {
for (;;) {
UnixMountEntry entry = new UnixMountEntry();
......
......@@ -53,7 +53,7 @@ class LinuxUserDefinedFileAttributeView
if (name == null)
throw new NullPointerException("'name' is null");
name = USER_NAMESPACE + name;
byte[] bytes = name.getBytes();
byte[] bytes = Util.toBytes(name);
if (bytes.length > XATTR_NAME_MAX) {
throw new FileSystemException(file.getPathForExceptionMessage(),
null, "'" + name + "' is too big");
......@@ -72,7 +72,7 @@ class LinuxUserDefinedFileAttributeView
byte[] value = new byte[len];
unsafe.copyMemory(null, address+start, value,
Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
String s = new String(value);
String s = Util.toString(value);
if (s.startsWith(USER_NAMESPACE)) {
s = s.substring(USER_NAMESPACE.length());
list.add(s);
......
......@@ -42,16 +42,15 @@ import static sun.nio.fs.SolarisConstants.*;
class SolarisUserDefinedFileAttributeView
extends AbstractUserDefinedFileAttributeView
{
private static final byte[] HERE = { '.' };
private byte[] nameAsBytes(UnixPath file, String name) throws IOException {
byte[] bytes = name.getBytes();
byte[] bytes = Util.toBytes(name);
// "", "." and ".." not allowed
if (bytes.length == 0 || bytes[0] == '.') {
if (bytes.length <= 1 ||
(bytes.length == 2 && bytes[1] == '.'))
{
throw new FileSystemException(file.getPathForExceptionMessage(),
null, "'" + name + "' is not a valid name");
}
if ((bytes.length == 0 || bytes[0] == '.') &&
((bytes.length <= 1 || (bytes.length == 2 && bytes[1] == '.')) {
throw new FileSystemException(file.getPathForExceptionMessage(),
null, "'" + name + "' is not a valid name");
}
return bytes;
}
......@@ -73,7 +72,7 @@ class SolarisUserDefinedFileAttributeView
try {
try {
// open extended attribute directory
int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
int dfd = openat(fd, HERE, (O_RDONLY|O_XATTR), 0);
long dp;
try {
dp = fdopendir(dfd);
......@@ -87,7 +86,7 @@ class SolarisUserDefinedFileAttributeView
try {
byte[] name;
while ((name = readdir(dp)) != null) {
String s = new String(name);
String s = Util.toString(name);
if (!s.equals(".") && !s.equals(".."))
list.add(s);
}
......@@ -217,7 +216,7 @@ class SolarisUserDefinedFileAttributeView
int fd = file.openForAttributeAccess(followLinks);
try {
int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
int dfd = openat(fd, HERE, (O_RDONLY|O_XATTR), 0);
try {
unlinkat(dfd, nameAsBytes(file,name), 0);
} finally {
......@@ -243,7 +242,7 @@ class SolarisUserDefinedFileAttributeView
static void copyExtendedAttributes(int ofd, int nfd) {
try {
// open extended attribute directory
int dfd = openat(ofd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
int dfd = openat(ofd, HERE, (O_RDONLY|O_XATTR), 0);
long dp = 0L;
try {
dp = fdopendir(dfd);
......
......@@ -61,7 +61,7 @@ class UnixException extends Exception {
if (msg != null) {
return msg;
} else {
return new String(UnixNativeDispatcher.strerror(errno()));
return Util.toString(UnixNativeDispatcher.strerror(errno()));
}
}
......
......@@ -196,7 +196,7 @@ abstract class UnixFileStore
@Override
public String toString() {
StringBuilder sb = new StringBuilder(new String(entry.dir()));
StringBuilder sb = new StringBuilder(Util.toString(entry.dir()));
sb.append(" (");
sb.append(entry.name());
sb.append(")");
......
......@@ -49,7 +49,7 @@ abstract class UnixFileSystem
// package-private
UnixFileSystem(UnixFileSystemProvider provider, String dir) {
this.provider = provider;
this.defaultDirectory = UnixPath.normalizeAndCheck(dir).getBytes();
this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
if (this.defaultDirectory[0] != '/') {
throw new RuntimeException("default directory must be absolute");
}
......@@ -204,7 +204,7 @@ abstract class UnixFileSystem
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkRead(new String(entry.dir()));
sm.checkRead(Util.toString(entry.dir()));
} catch (SecurityException x) {
continue;
}
......
......@@ -43,12 +43,12 @@ class UnixMountEntry {
}
String name() {
return new String(name);
return Util.toString(name);
}
String fstype() {
if (fstypeAsString == null)
fstypeAsString = new String(fstype);
fstypeAsString = Util.toString(fstype);
return fstypeAsString;
}
......@@ -65,7 +65,7 @@ class UnixMountEntry {
*/
boolean hasOption(String requested) {
if (optionsAsString == null)
optionsAsString = new String(opts);
optionsAsString = Util.toString(opts);
for (String opt: Util.split(optionsAsString, ',')) {
if (opt.equals(requested))
return true;
......
......@@ -100,7 +100,7 @@ class UnixNativeDispatcher {
*/
static long fopen(UnixPath filename, String mode) throws UnixException {
NativeBuffer pathBuffer = copyToNativeBuffer(filename);
NativeBuffer modeBuffer = NativeBuffers.asNativeBuffer(mode.getBytes());
NativeBuffer modeBuffer = NativeBuffers.asNativeBuffer(Util.toBytes(mode));
try {
return fopen0(pathBuffer.address(), modeBuffer.address());
} finally {
......@@ -473,7 +473,7 @@ class UnixNativeDispatcher {
* @return passwd->pw_uid
*/
static int getpwnam(String name) throws UnixException {
NativeBuffer buffer = NativeBuffers.asNativeBuffer(name.getBytes());
NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));
try {
return getpwnam0(buffer.address());
} finally {
......@@ -488,7 +488,7 @@ class UnixNativeDispatcher {
* @return group->gr_name
*/
static int getgrnam(String name) throws UnixException {
NativeBuffer buffer = NativeBuffers.asNativeBuffer(name.getBytes());
NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));
try {
return getgrnam0(buffer.address());
} finally {
......
......@@ -120,7 +120,7 @@ class UnixPath
SoftReference<CharsetEncoder> ref = encoder.get();
CharsetEncoder ce = (ref != null) ? ref.get() : null;
if (ce == null) {
ce = Charset.defaultCharset().newEncoder()
ce = Util.jnuEncoding().newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
encoder.set(new SoftReference<CharsetEncoder>(ce));
......@@ -186,7 +186,7 @@ class UnixPath
// use this path for permission checks
String getPathForPermissionCheck() {
if (getFileSystem().needToResolveAgainstDefaultDirectory()) {
return new String(getByteArrayForSysCalls());
return Util.toString(getByteArrayForSysCalls());
} else {
return toString();
}
......@@ -758,7 +758,7 @@ class UnixPath
public String toString() {
// OK if two or more threads create a String
if (stringValue == null) {
stringValue = fs.normalizeJavaPath(new String(path)); // platform encoding
stringValue = fs.normalizeJavaPath(Util.toString(path)); // platform encoding
}
return stringValue;
}
......
......@@ -115,7 +115,7 @@ class UnixUserPrincipals {
static User fromUid(int uid) {
String name = null;
try {
name = new String(getpwuid(uid));
name = Util.toString(getpwuid(uid));
} catch (UnixException x) {
name = Integer.toString(uid);
}
......@@ -126,7 +126,7 @@ class UnixUserPrincipals {
static Group fromGid(int gid) {
String name = null;
try {
name = new String(getgrgid(gid));
name = Util.toString(getgrgid(gid));
} catch (UnixException x) {
name = Integer.toString(gid);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册