Path: blob/master/src/java.desktop/macosx/native/libosxui/AquaFileView.m
41149 views
/*1* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526#include <JNIUtilities.h>2728#import "com_apple_laf_AquaFileView.h"2930#import <sys/param.h> // for MAXPATHLEN31#import <CoreFoundation/CoreFoundation.h>3233/*34* Class: com_apple_laf_AquaFileView35* Method: getNativePathToRunningJDKBundle36* Signature: ()Ljava/lang/String;37*/38// TODO: Un-comment this out39/*JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToRunningJDKBundle40(JNIEnv *env, jclass clazz)41{42jstring returnValue = NULL;43JNI_COCOA_ENTER(env);4445returnValue = NSStringToJavaString(env, getRunningJavaBundle());4647JNI_COCOA_EXIT(env);48return returnValue;49}*/5051/*52* Class: com_apple_laf_AquaFileView53* Method: getNativePathToSharedJDKBundle54* Signature: ()Ljava/lang/String;55*/56JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToSharedJDKBundle57(JNIEnv *env, jclass clazz)58{59jstring returnValue = NULL;60JNI_COCOA_ENTER(env);6162returnValue = NSStringToJavaString(env, [[NSBundle bundleWithIdentifier:@"com.apple.JavaVM"] bundlePath]);6364JNI_COCOA_EXIT(env);65return returnValue;66}6768/*69* Class: com_apple_laf_AquaFileView70* Method: getNativeMachineName71* Signature: ()Ljava/lang/String;72*/73JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeMachineName74(JNIEnv *env, jclass clazz)75{76jstring returnValue = NULL;77JNI_COCOA_ENTER(env);7879CFStringRef machineName = CSCopyMachineName();80returnValue = NSStringToJavaString(env, (NSString*)machineName);8182if (machineName != NULL) {83CFRelease(machineName);84}8586JNI_COCOA_EXIT(env);87return returnValue;88}8990/*91* Class: com_apple_laf_AquaFileView92* Method: getNativeLSInfo93* Signature: ([BZ)I94*/95JNIEXPORT jint JNICALL Java_com_apple_laf_AquaFileView_getNativeLSInfo96(JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir)97{98jint returnValue = com_apple_laf_AquaFileView_UNINITALIZED_LS_INFO;99JNI_COCOA_ENTER(env);100101jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL);102CHECK_NULL_RETURN(byteArray, returnValue);103jsize length = (*env)->GetArrayLength(env, absolutePath);104105// Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't106// let us specify a length.107UInt8 arrayCopy[length + 1];108jsize i;109for (i = 0; i < length; i++) {110arrayCopy[i] = (UInt8)byteArray[i];111}112arrayCopy[length] = '\0';113(*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT);114115Boolean isDirectory = (isDir == JNI_TRUE ? true : false);116FSRef ref;117OSErr err = FSPathMakeRef((const UInt8 *)&arrayCopy, &ref, &isDirectory);118if (err == noErr) {119LSItemInfoRecord itemInfo;120err = LSCopyItemInfoForRef(&ref, kLSRequestBasicFlagsOnly, &itemInfo);121122if (err == noErr) {123returnValue = itemInfo.flags;124}125}126127JNI_COCOA_EXIT(env);128return returnValue;129}130131/*132* Class: com_apple_laf_AquaFileView133* Method: getNativeDisplayName134* Signature: ([BZ)Ljava/lang/String;135*/136JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeDisplayName137(JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir)138{139jstring returnValue = NULL;140JNI_COCOA_ENTER(env);141142jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL);143CHECK_NULL_RETURN(byteArray, returnValue);144jsize length = (*env)->GetArrayLength(env, absolutePath);145146// Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't147// let us specify a length.148UInt8 arrayCopy[length + 1];149jsize i;150for (i = 0; i < length; i++) {151arrayCopy[i] = (UInt8)byteArray[i];152}153arrayCopy[length] = '\0';154(*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT);155156Boolean isDirectory = (isDir == JNI_TRUE ? true : false);157FSRef ref;158159OSErr theErr = FSPathMakeRefWithOptions((const UInt8 *)&arrayCopy,160kFSPathMakeRefDoNotFollowLeafSymlink,161&ref, &isDirectory);162if (theErr == noErr) {163CFStringRef displayName = NULL;164165theErr = LSCopyDisplayNameForRef(&ref, &displayName);166167if (theErr == noErr) {168CFMutableStringRef mutableDisplayName = CFStringCreateMutableCopy(NULL, 0, displayName);169CFStringNormalize(mutableDisplayName, kCFStringNormalizationFormC);170returnValue = NSStringToJavaString(env, (NSString *)mutableDisplayName);171CFRelease(mutableDisplayName);172}173174if (displayName != NULL) {175CFRelease(displayName);176}177}178179JNI_COCOA_EXIT(env);180return returnValue;181}182183/*184* Class: com_apple_laf_AquaFileView185* Method: getNativePathForResolvedAlias186* Signature: ([BZ)Ljava/lang/String;187*/188JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathForResolvedAlias189(JNIEnv *env, jclass clazz, jbyteArray pathToAlias, jboolean isDir)190{191jstring returnValue = NULL;192JNI_COCOA_ENTER(env);193194UInt8 pathCString[MAXPATHLEN + 1];195size_t maxPathLen = sizeof(pathCString) - 1;196197jbyte *byteArray = (*env)->GetByteArrayElements(env, pathToAlias, NULL);198CHECK_NULL_RETURN(byteArray, returnValue);199jsize length = (*env)->GetArrayLength(env, pathToAlias);200201if (length > maxPathLen) {202length = maxPathLen;203}204strncpy((char *)pathCString, (char *)byteArray, length);205// make sure it's null terminated206pathCString[length] = '\0';207(*env)->ReleaseByteArrayElements(env, pathToAlias, byteArray, JNI_ABORT);208209Boolean isDirectory = (isDir == JNI_TRUE ? true : false);210FSRef fileRef;211OSErr theErr = FSPathMakeRef(pathCString, &fileRef, &isDirectory);212213Boolean ignored;214theErr = FSResolveAliasFileWithMountFlags(&fileRef, false, &ignored,215&ignored, kResolveAliasFileNoUI);216if (theErr == noErr) {217UInt8 resolvedPath[MAXPATHLEN];218theErr = FSRefMakePath(&fileRef, resolvedPath, MAXPATHLEN);219220if (theErr == noErr) {221returnValue = (*env)->NewStringUTF(env, (char *)resolvedPath);222}223}224225JNI_COCOA_EXIT(env);226return returnValue;227}228229230