--- old/make/java/nio/Makefile 2012-11-29 01:02:48.062540686 -0800 +++ new/make/java/nio/Makefile 2012-11-29 01:02:47.930540690 -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-29 01:02:48.702540667 -0800 +++ new/make/java/nio/mapfile-linux 2012-11-29 01:02:48.562540671 -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-29 01:02:49.210540652 -0800 +++ new/makefiles/CompileJavaClasses.gmk 2012-11-29 01:02:49.078540656 -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-29 01:02:49.814540634 -0800 +++ new/makefiles/CompileNativeLibraries.gmk 2012-11-29 01:02:49.674540639 -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-29 01:02:50.438540616 -0800 +++ new/makefiles/mapfiles/libnio/mapfile-linux 2012-11-29 01:02:50.306540621 -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/share/classes/sun/nio/fs/AbstractFileSystemProvider.java 2012-11-29 01:02:50.978540600 -0800 +++ new/src/share/classes/sun/nio/fs/AbstractFileSystemProvider.java 2012-11-29 01:02:50.810540605 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -35,8 +35,6 @@ */ abstract class AbstractFileSystemProvider extends FileSystemProvider { - protected AbstractFileSystemProvider() { } - /** * Splits the given attribute name into the name of an attribute view and * the attribute. If the attribute view is not identified then it assumed --- old/src/share/classes/sun/nio/fs/AbstractFileTypeDetector.java 2012-11-29 01:02:51.498540586 -0800 +++ new/src/share/classes/sun/nio/fs/AbstractFileTypeDetector.java 2012-11-29 01:02:51.346540589 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 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 @@ -37,10 +37,6 @@ public abstract class AbstractFileTypeDetector extends FileTypeDetector { - protected AbstractFileTypeDetector() { - super(); - } - /** * Invokes the appropriate probe method to guess a file's content type, * and checks that the content type's syntax is valid. --- old/src/solaris/classes/sun/nio/fs/BsdFileSystemProvider.java 2012-11-29 01:02:52.150540565 -0800 +++ new/src/solaris/classes/sun/nio/fs/BsdFileSystemProvider.java 2012-11-29 01:02:52.002540571 -0800 @@ -25,8 +25,6 @@ package sun.nio.fs; -import java.nio.file.*; -import java.nio.file.attribute.*; import java.io.IOException; /** @@ -34,10 +32,6 @@ */ public class BsdFileSystemProvider extends UnixFileSystemProvider { - public BsdFileSystemProvider() { - super(); - } - @Override BsdFileSystem newFileSystem(String dir) { return new BsdFileSystem(this, dir); --- old/src/solaris/classes/sun/nio/fs/LinuxFileSystemProvider.java 2012-11-29 01:02:52.762540547 -0800 +++ new/src/solaris/classes/sun/nio/fs/LinuxFileSystemProvider.java 2012-11-29 01:02:52.602540552 -0800 @@ -35,10 +35,6 @@ */ public class LinuxFileSystemProvider extends UnixFileSystemProvider { - public LinuxFileSystemProvider() { - super(); - } - @Override LinuxFileSystem newFileSystem(String dir) { return new LinuxFileSystem(this, dir); @@ -100,6 +96,14 @@ @Override FileTypeDetector getFileTypeDetector() { - return new GnomeFileTypeDetector(); + final Path userMimeTypes = + Paths.get(System.getProperty("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-29 01:02:53.350540530 -0800 +++ new/src/solaris/classes/sun/nio/fs/MacOSXFileSystemProvider.java 2012-11-29 01:02:53.202540534 -0800 @@ -25,21 +25,25 @@ 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; /** * MacOSX implementation of FileSystemProvider */ public class MacOSXFileSystemProvider extends BsdFileSystemProvider { - public MacOSXFileSystemProvider() { - super(); - } - @Override MacOSXFileSystem newFileSystem(String dir) { return new MacOSXFileSystem(this, dir); } + + @Override + FileTypeDetector getFileTypeDetector() { + final Path userMimeTypes = + Paths.get(System.getProperty("user.home"), ".mime.types"); + + return new MimeTypesFileTypeDetector(userMimeTypes); + } } --- old/src/solaris/classes/sun/nio/fs/SolarisFileSystemProvider.java 2012-11-29 01:02:53.970540512 -0800 +++ new/src/solaris/classes/sun/nio/fs/SolarisFileSystemProvider.java 2012-11-29 01:02:53.822540516 -0800 @@ -35,9 +35,6 @@ */ public class SolarisFileSystemProvider extends UnixFileSystemProvider { - public SolarisFileSystemProvider() { - super(); - } @Override SolarisFileSystem newFileSystem(String dir) { @@ -83,6 +80,13 @@ @Override FileTypeDetector getFileTypeDetector() { - return new GnomeFileTypeDetector(); + final Path userMimeTypes = + Paths.get(System.getProperty("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-29 01:02:54.570540495 -0800 +++ new/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java 2012-11-29 01:02:54.410540499 -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-29 00:32:57.270518291 -0800 +++ new/src/solaris/classes/sun/nio/fs/MagicFileTypeDetector.java 2012-11-29 01:02:55.030540480 -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-29 00:32:57.270518291 -0800 +++ new/src/solaris/classes/sun/nio/fs/MimeTypesFileTypeDetector.java 2012-11-29 01:02:55.434540468 -0800 @@ -0,0 +1,210 @@ +/* + * 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 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()); + + // Case-insensitive search if no mime type is found + if (mimeType == null) { + ext = extension; + do { + for (String key : mimeTypeMap.keySet()) { + if (ext.equalsIgnoreCase(key)) { + mimeType = mimeTypeMap.get(key); + break; + } + } + 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. + private void loadMimeTypes() { + if (mimeTypeMap != null || 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); + } + } + } + + /* + * 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 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 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-29 00:32:57.270518291 -0800 +++ new/src/solaris/native/sun/nio/fs/MagicFileTypeDetector.c 2012-11-29 01:02:55.846540456 -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; +}