1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 #include <JNIUtilities.h>
  28 
  29 #import "com_apple_laf_AquaFileView.h"
  30 
  31 #import <sys/param.h> // for MAXPATHLEN
  32 #import <CoreFoundation/CoreFoundation.h>
  33 
  34 /*
  35  * Class:     com_apple_laf_AquaFileView
  36  * Method:    getNativePathToRunningJDKBundle
  37  * Signature: ()Ljava/lang/String;
  38  */
  39 // TODO: Un-comment this out
  40 /*JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToRunningJDKBundle
  41 (JNIEnv *env, jclass clazz)
  42 {
  43     jstring returnValue = NULL;
  44 JNI_COCOA_ENTER(env);
  45 
  46     returnValue = NSStringToJavaString(env, getRunningJavaBundle());
  47 
  48 JNI_COCOA_EXIT(env);
  49     return returnValue;
  50 }*/
  51 
  52 /*
  53  * Class:     com_apple_laf_AquaFileView
  54  * Method:    getNativePathToSharedJDKBundle
  55  * Signature: ()Ljava/lang/String;
  56  */
  57 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToSharedJDKBundle
  58 (JNIEnv *env, jclass clazz)
  59 {
  60     jstring returnValue = NULL;
  61 JNI_COCOA_ENTER(env);
  62 
  63     returnValue = NSStringToJavaString(env, [[NSBundle bundleWithIdentifier:@"com.apple.JavaVM"] bundlePath]);
  64 
  65 JNI_COCOA_EXIT(env);
  66     return returnValue;
  67 }
  68 
  69 /*
  70  * Class:     com_apple_laf_AquaFileView
  71  * Method:    getNativeMachineName
  72  * Signature: ()Ljava/lang/String;
  73  */
  74 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeMachineName
  75 (JNIEnv *env, jclass clazz)
  76 {
  77     jstring returnValue = NULL;
  78 JNI_COCOA_ENTER(env);
  79 
  80     CFStringRef machineName = CSCopyMachineName();
  81     returnValue = NSStringToJavaString(env, (NSString*)machineName);
  82 
  83     if (machineName != NULL) {
  84         CFRelease(machineName);
  85     }
  86 
  87 JNI_COCOA_EXIT(env);
  88     return returnValue;
  89 }
  90 
  91 /*
  92  * Class:     com_apple_laf_AquaFileView
  93  * Method:    getNativeLSInfo
  94  * Signature: ([BZ)I
  95  */
  96 JNIEXPORT jint JNICALL Java_com_apple_laf_AquaFileView_getNativeLSInfo
  97 (JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir)
  98 {
  99     jint returnValue = com_apple_laf_AquaFileView_UNINITALIZED_LS_INFO;
 100 JNI_COCOA_ENTER(env);
 101 
 102     jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL);
 103     CHECK_NULL_RETURN(byteArray, returnValue);
 104     jsize length = (*env)->GetArrayLength(env, absolutePath);
 105 
 106     // Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't
 107     // let us specify a length.
 108     UInt8 arrayCopy[length + 1];
 109     jsize i;
 110     for (i = 0; i < length; i++) {
 111         arrayCopy[i] = (UInt8)byteArray[i];
 112     }
 113     arrayCopy[length] = '\0';
 114     (*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT);
 115 
 116     Boolean isDirectory = (isDir == JNI_TRUE ? true : false);
 117     FSRef ref;
 118     OSErr err = FSPathMakeRef((const UInt8 *)&arrayCopy, &ref, &isDirectory);
 119     if (err == noErr) {
 120         LSItemInfoRecord itemInfo;
 121         err = LSCopyItemInfoForRef(&ref, kLSRequestBasicFlagsOnly, &itemInfo);
 122 
 123         if (err == noErr) {
 124             returnValue = itemInfo.flags;
 125         }
 126     }
 127 
 128 JNI_COCOA_EXIT(env);
 129     return returnValue;
 130 }
 131 
 132 /*
 133  * Class:     com_apple_laf_AquaFileView
 134  * Method:    getNativeDisplayName
 135  * Signature: ([BZ)Ljava/lang/String;
 136  */
 137 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeDisplayName
 138 (JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir)
 139 {
 140     jstring returnValue = NULL;
 141 JNI_COCOA_ENTER(env);
 142 
 143     jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL);
 144     CHECK_NULL_RETURN(byteArray, returnValue);
 145     jsize length = (*env)->GetArrayLength(env, absolutePath);
 146 
 147     // Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't
 148     // let us specify a length.
 149     UInt8 arrayCopy[length + 1];
 150     jsize i;
 151     for (i = 0; i < length; i++) {
 152         arrayCopy[i] = (UInt8)byteArray[i];
 153     }
 154     arrayCopy[length] = '\0';
 155     (*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT);
 156 
 157     Boolean isDirectory = (isDir == JNI_TRUE ? true : false);
 158     FSRef ref;
 159 
 160     OSErr theErr = FSPathMakeRefWithOptions((const UInt8 *)&arrayCopy,
 161                                             kFSPathMakeRefDoNotFollowLeafSymlink,
 162                                             &ref, &isDirectory);
 163     if (theErr == noErr) {
 164         CFStringRef displayName = NULL;
 165 
 166         theErr = LSCopyDisplayNameForRef(&ref, &displayName);
 167 
 168         if (theErr == noErr) {
 169             CFMutableStringRef mutableDisplayName = CFStringCreateMutableCopy(NULL, 0, displayName);
 170             CFStringNormalize(mutableDisplayName, kCFStringNormalizationFormC);
 171             returnValue = NSStringToJavaString(env, (NSString *)mutableDisplayName);
 172             CFRelease(mutableDisplayName);
 173         }
 174 
 175         if (displayName != NULL) {
 176             CFRelease(displayName);
 177         }
 178     }
 179 
 180 JNI_COCOA_EXIT(env);
 181     return returnValue;
 182 }
 183 
 184 /*
 185  * Class:     com_apple_laf_AquaFileView
 186  * Method:    getNativePathForResolvedAlias
 187  * Signature: ([BZ)Ljava/lang/String;
 188  */
 189 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathForResolvedAlias
 190 (JNIEnv *env, jclass clazz, jbyteArray pathToAlias, jboolean isDir)
 191 {
 192     jstring returnValue = NULL;
 193 JNI_COCOA_ENTER(env);
 194 
 195     UInt8 pathCString[MAXPATHLEN + 1];
 196     size_t maxPathLen = sizeof(pathCString) - 1;
 197 
 198     jbyte *byteArray = (*env)->GetByteArrayElements(env, pathToAlias, NULL);
 199     CHECK_NULL_RETURN(byteArray, returnValue);
 200     jsize length = (*env)->GetArrayLength(env, pathToAlias);
 201 
 202     if (length > maxPathLen) {
 203         length = maxPathLen;
 204     }
 205     strncpy((char *)pathCString, (char *)byteArray, length);
 206     // make sure it's null terminated
 207     pathCString[length] = '\0';
 208     (*env)->ReleaseByteArrayElements(env, pathToAlias, byteArray, JNI_ABORT);
 209 
 210     Boolean isDirectory = (isDir == JNI_TRUE ? true : false);
 211     FSRef fileRef;
 212     OSErr theErr = FSPathMakeRef(pathCString, &fileRef, &isDirectory);
 213 
 214     Boolean ignored;
 215     theErr = FSResolveAliasFileWithMountFlags(&fileRef, false, &ignored,
 216                                               &ignored, kResolveAliasFileNoUI);
 217     if (theErr == noErr) {
 218         UInt8 resolvedPath[MAXPATHLEN];
 219         theErr = FSRefMakePath(&fileRef, resolvedPath, MAXPATHLEN);
 220 
 221         if (theErr == noErr) {
 222             returnValue = (*env)->NewStringUTF(env, (char *)resolvedPath);
 223         }
 224     }
 225 
 226 JNI_COCOA_EXIT(env);
 227     return returnValue;
 228 }