--- old/make/java/nio/Makefile 2012-11-30 08:17:22.419054472 -0800 +++ new/make/java/nio/Makefile 2012-11-30 08:17:22.247140478 -0800 @@ -69,6 +69,7 @@ sun/nio/ch/UnixAsynchronousSocketChannelImpl.java \ \ sun/nio/fs/GnomeFileTypeDetector.java \ + sun/nio/fs/MimeTypesFileTypeDetector.java \ sun/nio/fs/PollingWatchService.java \ sun/nio/fs/SolarisAclFileAttributeView.java \ sun/nio/fs/SolarisFileStore.java \ @@ -202,6 +203,8 @@ sun/nio/ch/UnixAsynchronousSocketChannelImpl.java \ \ sun/nio/fs/GnomeFileTypeDetector.java \ + sun/nio/fs/MagicFileTypeDetector.java \ + sun/nio/fs/MimeTypesFileTypeDetector.java \ sun/nio/fs/LinuxDosFileAttributeView.java \ sun/nio/fs/LinuxFileStore.java \ sun/nio/fs/LinuxFileSystem.java \ @@ -239,6 +242,7 @@ UnixAsynchronousSocketChannelImpl.c \ \ GnomeFileTypeDetector.c \ + MagicFileTypeDetector.c \ LinuxNativeDispatcher.c \ LinuxWatchService.c \ UnixCopyFile.c \ @@ -254,6 +258,7 @@ sun/nio/ch/UnixAsynchronousSocketChannelImpl.java \ \ sun/nio/fs/GnomeFileTypeDetector.java \ + sun/nio/fs/MagicFileTypeDetector.java \ sun/nio/fs/LinuxNativeDispatcher.java \ sun/nio/fs/LinuxWatchService.java \ sun/nio/fs/UnixCopyFile.java \ @@ -277,6 +282,7 @@ sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java \ sun/nio/ch/UnixAsynchronousSocketChannelImpl.java \ \ + sun/nio/fs/MimeTypesFileTypeDetector.java \ sun/nio/fs/BsdFileStore.java \ sun/nio/fs/BsdFileSystem.java \ sun/nio/fs/BsdFileSystemProvider.java \ --- old/make/java/nio/mapfile-linux 2012-11-30 08:17:23.138694452 -0800 +++ new/make/java/nio/mapfile-linux 2012-11-30 08:17:22.942792456 -0800 @@ -130,6 +130,8 @@ Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio; Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs; Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs; + Java_sun_nio_fs_MagicFileTypeDetector_initialize0; + Java_sun_nio_fs_MagicFileTypeDetector_probe0; Java_sun_nio_fs_LinuxWatchService_eventSize; Java_sun_nio_fs_LinuxWatchService_eventOffsets; Java_sun_nio_fs_LinuxWatchService_inotifyInit; --- old/makefiles/CompileJavaClasses.gmk 2012-11-30 08:17:23.934296427 -0800 +++ new/makefiles/CompileJavaClasses.gmk 2012-11-30 08:17:23.718404434 -0800 @@ -121,6 +121,7 @@ sun/nio/fs/LinuxFileStore.java \ sun/nio/fs/LinuxFileSystem.java \ sun/nio/fs/LinuxFileSystemProvider.java \ + sun/nio/fs/MagicFileTypeDetector.java \ sun/nio/fs/LinuxNativeDispatcher.java \ sun/nio/fs/LinuxUserDefinedFileAttributeView.java \ sun/nio/fs/LinuxWatchService.java --- old/makefiles/CompileNativeLibraries.gmk 2012-11-30 08:17:24.482022411 -0800 +++ new/makefiles/CompileNativeLibraries.gmk 2012-11-30 08:17:24.322102417 -0800 @@ -1928,6 +1928,7 @@ UnixAsynchronousServerSocketChannelImpl.c \ UnixAsynchronousSocketChannelImpl.c \ GnomeFileTypeDetector.c \ + MagicFileTypeDetector.c \ LinuxNativeDispatcher.c \ LinuxWatchService.c \ UnixCopyFile.c \ --- old/makefiles/mapfiles/libnio/mapfile-linux 2012-11-30 08:17:25.181672390 -0800 +++ new/makefiles/mapfiles/libnio/mapfile-linux 2012-11-30 08:17:24.989768396 -0800 @@ -130,6 +130,8 @@ Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio; Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs; Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs; + Java_sun_nio_fs_MagicFileTypeDetector_initialize0; + Java_sun_nio_fs_MagicFileTypeDetector_probe0; Java_sun_nio_fs_LinuxWatchService_eventSize; Java_sun_nio_fs_LinuxWatchService_eventOffsets; Java_sun_nio_fs_LinuxWatchService_inotifyInit; --- old/src/solaris/classes/sun/nio/fs/LinuxFileSystemProvider.java 2012-11-30 08:17:25.733396374 -0800 +++ new/src/solaris/classes/sun/nio/fs/LinuxFileSystemProvider.java 2012-11-30 08:17:25.573476380 -0800 @@ -29,6 +29,8 @@ import java.nio.file.attribute.*; import java.nio.file.spi.FileTypeDetector; import java.io.IOException; +import java.security.AccessController; +import sun.security.action.GetPropertyAction; /** * Linux implementation of FileSystemProvider @@ -100,6 +102,14 @@ @Override FileTypeDetector getFileTypeDetector() { - return new GnomeFileTypeDetector(); + final Path userMimeTypes = Paths.get(AccessController.doPrivileged( + new GetPropertyAction("user.home")), ".mime.types"); + + final Path sysMimeTypes = Paths.get("/etc/mime.types"); + + return chain(new GnomeFileTypeDetector(), + new MimeTypesFileTypeDetector(userMimeTypes), + new MimeTypesFileTypeDetector(sysMimeTypes), + new MagicFileTypeDetector()); } } --- old/src/solaris/classes/sun/nio/fs/MacOSXFileSystemProvider.java 2012-11-30 08:17:26.401062355 -0800 +++ new/src/solaris/classes/sun/nio/fs/MacOSXFileSystemProvider.java 2012-11-30 08:17:26.221152360 -0800 @@ -25,9 +25,11 @@ package sun.nio.fs; -import java.nio.file.*; -import java.nio.file.attribute.*; -import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.spi.FileTypeDetector; +import java.security.AccessController; +import sun.security.action.GetPropertyAction; /** * MacOSX implementation of FileSystemProvider @@ -42,4 +44,12 @@ MacOSXFileSystem newFileSystem(String dir) { return new MacOSXFileSystem(this, dir); } + + @Override + FileTypeDetector getFileTypeDetector() { + final Path userMimeTypes = Paths.get(AccessController.doPrivileged( + new GetPropertyAction("user.home")), ".mime.types"); + + return new MimeTypesFileTypeDetector(userMimeTypes); + } } --- old/src/solaris/classes/sun/nio/fs/SolarisFileSystemProvider.java 2012-11-30 08:17:27.196664332 -0800 +++ new/src/solaris/classes/sun/nio/fs/SolarisFileSystemProvider.java 2012-11-30 08:17:26.944790339 -0800 @@ -29,6 +29,8 @@ import java.nio.file.attribute.*; import java.nio.file.spi.FileTypeDetector; import java.io.IOException; +import java.security.AccessController; +import sun.security.action.GetPropertyAction; /** * Solaris implementation of FileSystemProvider @@ -83,6 +85,13 @@ @Override FileTypeDetector getFileTypeDetector() { - return new GnomeFileTypeDetector(); + final Path userMimeTypes = Paths.get(AccessController.doPrivileged( + new GetPropertyAction("user.home")), ".mime.types"); + + final Path sysMimeTypes = Paths.get("/etc/mime.types"); + + return chain(new GnomeFileTypeDetector(), + new MimeTypesFileTypeDetector(userMimeTypes), + new MimeTypesFileTypeDetector(sysMimeTypes)); } } --- old/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java 2012-11-30 08:17:27.948288310 -0800 +++ new/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java 2012-11-30 08:17:27.752386316 -0800 @@ -509,4 +509,28 @@ }; } + /** + * It accepts a list of file type detectors as the parameter, and return + * the first {@code FileTypeDetector} that can find a file's MIME type. + * If no detectors can find the MIME type, null is returned. + * + * @param detectors List of file type detectors. + * @return The first detector that finds the file type or null. + */ + final FileTypeDetector chain(final AbstractFileTypeDetector... detectors) { + return new AbstractFileTypeDetector() { + @Override + protected String implProbeContentType(Path file) + throws IOException + { + for (AbstractFileTypeDetector detector : detectors) { + String result = detector.implProbeContentType(file); + if (result != null && !result.isEmpty()) { + return result; + } + } + return null; + } + }; + } } --- /dev/null 2012-11-30 08:11:16.467359291 -0800 +++ new/src/solaris/classes/sun/nio/fs/MagicFileTypeDetector.java 2012-11-30 08:17:28.324100298 -0800 @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * 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. + */ + +package sun.nio.fs; + +import java.io.IOException; +import java.nio.file.Path; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * File type detector that uses the libmagic to guess the MIME type of a file. + */ + +class MagicFileTypeDetector extends AbstractFileTypeDetector { + + private static final String UNKNOW_MIME_TYPE = "application/octet-stream"; + + // true if libmagic is available and successfully loaded + private final boolean libmagicAvailable; + + public MagicFileTypeDetector() { + libmagicAvailable = initialize0(); + } + + @Override + protected String implProbeContentType(Path obj) throws IOException { + if (!libmagicAvailable || !(obj instanceof UnixPath)) { + return null; + } + + UnixPath path = (UnixPath) obj; + NativeBuffer buffer = + NativeBuffers.asNativeBuffer(path.getByteArrayForSysCalls()); + + try { + byte[] type = probe0(buffer.address()); + String mimeType = (type == null) ? null : new String(type); + return UNKNOW_MIME_TYPE.equals(mimeType) ? null : mimeType; + } finally { + buffer.release(); + } + } + + private static native boolean initialize0(); + + private static native byte[] probe0(long pathAddress); + + static { + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Void run() { + System.loadLibrary("nio"); + return null; + } + }); + } +} --- /dev/null 2012-11-30 08:11:16.467359291 -0800 +++ new/src/solaris/classes/sun/nio/fs/MimeTypesFileTypeDetector.java 2012-11-30 08:17:28.719902287 -0800 @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * 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. + */ + +package sun.nio.fs; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.HashMap; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.util.List; + +/** + * File type detector that uses a file extension to look up its MIME type + * by checking the mappings recorded in a mime.types file. + */ + +class MimeTypesFileTypeDetector extends AbstractFileTypeDetector { + + private Map mimeTypeMap; + + private volatile boolean loaded = false; + + private final Path mimeTypesFile; + + public MimeTypesFileTypeDetector(final Path filePath) { + mimeTypesFile = filePath; + } + + @Override + protected String implProbeContentType(final Path file) { + if (file == null || file.getFileName() == null) { + return null; + } + + final String extension = getExtension(file.getFileName().toString()); + if (extension.isEmpty()) { + return null; + } + + loadMimeTypes(); + if (mimeTypeMap == null || mimeTypeMap.isEmpty()) { + return null; + } + + String mimeType = null; + // Case-sensitive search + String ext = extension; + do { + mimeType = mimeTypeMap.get(ext); + if (mimeType == null) { + ext = getExtension(ext); + } + } while (mimeType == null && !ext.isEmpty()); + + return mimeType; + } + + // Get the extension of a file name. + private static String getExtension(final String name) { + String ext = ""; + if (name != null && !name.isEmpty()) { + int dot = name.indexOf('.'); + if ((dot >= 0) && (dot < name.length() - 1)) { + ext = name.substring(dot + 1); + } + } + return ext; + } + + /* + * Parse the mime types file, and store the type-extension mappings into + * mimeTypeMap. The mime types file is not loaded until the first probe + * to achieve the lazy initialization. It adopts double-checked locking + * optimization to reduce the locking overhead. + */ + private void loadMimeTypes() { + boolean done = loaded; + if (!done) { + synchronized (this) { + done = loaded; + if (!done) { + if (mimeTypesFile == null || + !mimeTypesFile.toFile().isFile()) + { + return; + } + + List lines = null; + try { + lines = Files.readAllLines(mimeTypesFile, + Charset.defaultCharset()); + } catch (IOException | SecurityException ex) { + } + + if (lines != null && !lines.isEmpty()) { + mimeTypeMap = new HashMap<>(lines.size()); + String command = ""; + for (String line : lines) { + command += line; + if (command.endsWith("\\")) { + command = command.substring(0, + command.length() - 1); + continue; + } + parseMimeCommand(command); + command = ""; + } + if (!command.isEmpty()) { + parseMimeCommand(command); + } + } + loaded = true; + } + } + } + } + + /* + * Parse a mime-types command, which can have the following formats: + * 1) Simple space-delimited format + * image/jpeg jpeg jpg jpe JPG + * + * 2) Netscape key-value pair format + * type=application/x-java-jnlp-file desc="Java Web Start" exts="jnlp" + * or + * type=text/html exts=htm,html + */ + private void parseMimeCommand(String command) { + command = command.trim(); + if (command.isEmpty() || command.charAt(0) == '#') { + return; + } + + command = command.replaceAll("\\s*#.*", ""); + int equalIdx = command.indexOf('='); + if (equalIdx > 0) { + // Parse a mime-types command having the key-value pair format + final String TypeSign = "type="; + String typeRegex = "\\b" + TypeSign + + "(\"\\p{Graph}+?/\\p{Graph}+?\"|\\p{Graph}+/\\p{Graph}+\\b)"; + Pattern typePattern = Pattern.compile(typeRegex); + Matcher typeMatcher = typePattern.matcher(command); + + if (typeMatcher.find()) { + String type = typeMatcher.group().substring(TypeSign.length()); + if (type.charAt(0) == '"') { + type = type.substring(1, type.length() - 1); + } + + final String ExtSign = "exts="; + String extRegex = "\\b" + ExtSign + + "(\"[\\p{Graph}|\\p{Blank}]+?\"|\\p{Graph}+\\b)"; + Pattern extPattern = Pattern.compile(extRegex); + Matcher extMatcher = extPattern.matcher(command); + + if (extMatcher.find()) { + String exts = + extMatcher.group().substring(ExtSign.length()); + if (exts.charAt(0) == '"') { + exts = exts.substring(1, exts.length() - 1); + } + String[] extList = exts.split("[\\p{Blank}|\\p{Punct}]+"); + for (String ext : extList) { + putIfAbsent(ext, type); + } + } + } + } else { + // Parse a mime-types command having the space-delimited format + String[] elements = command.split("\\s+"); + int i = 1; + while (i < elements.length) { + putIfAbsent(elements[i++], elements[0]); + } + } + } + + private void putIfAbsent(String key, String value) { + if (key != null && !key.isEmpty() && + value != null && !value.isEmpty() && + !mimeTypeMap.containsKey(key)) + { + mimeTypeMap.put(key, value); + } + } +} --- /dev/null 2012-11-30 08:11:16.467359291 -0800 +++ new/src/solaris/native/sun/nio/fs/MagicFileTypeDetector.c 2012-11-30 08:17:29.103710275 -0800 @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * 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. + */ + +#include "jni.h" +#include "jni_util.h" +#include "jvm.h" +#include "jlong.h" + +#include +#include + +#define MAGIC_MIME_TYPE 0x000010 /* Return the MIME type */ + +typedef struct magic_set magic_t; + +typedef magic_t* (*magic_open_func)(int flags); +typedef int (*magic_load_func)(magic_t* cookie, const char* filename); +typedef const char* (*magic_file_func)(magic_t* cookie, const char* filename); +typedef void (*magic_close_func)(magic_t* cookie); + +static void* magic_handle; +static magic_open_func magic_open; +static magic_load_func magic_load; +static magic_file_func magic_file; +static magic_close_func magic_close; + +#include "sun_nio_fs_MagicFileTypeDetector.h" + +JNIEXPORT jboolean JNICALL +Java_sun_nio_fs_MagicFileTypeDetector_initialize0 + (JNIEnv* env, jclass this) +{ + magic_handle = dlopen("libmagic.so", RTLD_LAZY); + if (magic_handle == NULL) { + magic_handle = dlopen("libmagic.so.1", RTLD_LAZY); + if (magic_handle == NULL) { + return JNI_FALSE; + } + } + + magic_open = (magic_open_func)dlsym(magic_handle, "magic_open"); + + magic_load = (magic_load_func)dlsym(magic_handle, "magic_load"); + + magic_file = (magic_file_func)dlsym(magic_handle, "magic_file"); + + magic_close = (magic_close_func)dlsym(magic_handle, "magic_close"); + + if (magic_open == NULL || + magic_load == NULL || + magic_file == NULL || + magic_close == NULL) + { + dlclose(magic_handle); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +JNIEXPORT jbyteArray JNICALL +Java_sun_nio_fs_MagicFileTypeDetector_probe0 + (JNIEnv* env, jclass this, jlong pathAddress) +{ + char* path = (char*)jlong_to_ptr(pathAddress); + magic_t* cookie; + jbyteArray result = NULL; + + cookie = (*magic_open)(MAGIC_MIME_TYPE); + + if (cookie != NULL) { + if ((*magic_load)(cookie, NULL) != -1) { + const char* type = (*magic_file)(cookie, path); + if (type != NULL) { + jsize len = strlen(type); + result = (*env)->NewByteArray(env, len); + if (result != NULL) { + (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)type); + } + } + } + (*magic_close)(cookie); + } + + return result; +}