|
1 /* |
|
2 * Copyright (c) 2011, 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 #import "com_apple_laf_AquaFileView.h" |
|
27 |
|
28 #import <sys/param.h> // for MAXPATHLEN |
|
29 #import <CoreFoundation/CoreFoundation.h> |
|
30 #import <JavaNativeFoundation/JavaNativeFoundation.h> |
|
31 |
|
32 /* |
|
33 * Class: com_apple_laf_AquaFileView |
|
34 * Method: getNativePathToRunningJDKBundle |
|
35 * Signature: ()Ljava/lang/String; |
|
36 */ |
|
37 // TODO: Un-comment this out |
|
38 /*JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToRunningJDKBundle |
|
39 (JNIEnv *env, jclass clazz) |
|
40 { |
|
41 jstring returnValue = NULL; |
|
42 JNF_COCOA_ENTER(env); |
|
43 |
|
44 returnValue = JNFNSToJavaString(env, getRunningJavaBundle()); |
|
45 |
|
46 JNF_COCOA_EXIT(env); |
|
47 return returnValue; |
|
48 }*/ |
|
49 |
|
50 /* |
|
51 * Class: com_apple_laf_AquaFileView |
|
52 * Method: getNativePathToSharedJDKBundle |
|
53 * Signature: ()Ljava/lang/String; |
|
54 */ |
|
55 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathToSharedJDKBundle |
|
56 (JNIEnv *env, jclass clazz) |
|
57 { |
|
58 jstring returnValue = NULL; |
|
59 JNF_COCOA_ENTER(env); |
|
60 |
|
61 returnValue = JNFNSToJavaString(env, [[NSBundle bundleWithIdentifier:@"com.apple.JavaVM"] bundlePath]); |
|
62 |
|
63 JNF_COCOA_EXIT(env); |
|
64 return returnValue; |
|
65 } |
|
66 |
|
67 /* |
|
68 * Class: com_apple_laf_AquaFileView |
|
69 * Method: getNativeMachineName |
|
70 * Signature: ()Ljava/lang/String; |
|
71 */ |
|
72 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeMachineName |
|
73 (JNIEnv *env, jclass clazz) |
|
74 { |
|
75 jstring returnValue = NULL; |
|
76 JNF_COCOA_ENTER(env); |
|
77 |
|
78 CFStringRef machineName = CSCopyMachineName(); |
|
79 returnValue = JNFNSToJavaString(env, (NSString*)machineName); |
|
80 |
|
81 if (machineName != NULL) { |
|
82 CFRelease(machineName); |
|
83 } |
|
84 |
|
85 JNF_COCOA_EXIT(env); |
|
86 return returnValue; |
|
87 } |
|
88 |
|
89 /* |
|
90 * Class: com_apple_laf_AquaFileView |
|
91 * Method: getNativeLSInfo |
|
92 * Signature: ([BZ)I |
|
93 */ |
|
94 JNIEXPORT jint JNICALL Java_com_apple_laf_AquaFileView_getNativeLSInfo |
|
95 (JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir) |
|
96 { |
|
97 jint returnValue = com_apple_laf_AquaFileView_UNINITALIZED_LS_INFO; |
|
98 JNF_COCOA_ENTER(env); |
|
99 |
|
100 jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL); |
|
101 jsize length = (*env)->GetArrayLength(env, absolutePath); |
|
102 |
|
103 // Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't |
|
104 // let us specify a length. |
|
105 UInt8 arrayCopy[length + 1]; |
|
106 jsize i; |
|
107 for (i = 0; i < length; i++) { |
|
108 arrayCopy[i] = (UInt8)byteArray[i]; |
|
109 } |
|
110 arrayCopy[length] = '\0'; |
|
111 (*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT); |
|
112 |
|
113 Boolean isDirectory = (isDir == JNI_TRUE ? true : false); |
|
114 FSRef ref; |
|
115 OSErr err = FSPathMakeRef((const UInt8 *)&arrayCopy, &ref, &isDirectory); |
|
116 if (err == noErr) { |
|
117 LSItemInfoRecord itemInfo; |
|
118 err = LSCopyItemInfoForRef(&ref, kLSRequestBasicFlagsOnly, &itemInfo); |
|
119 |
|
120 if (err == noErr) { |
|
121 returnValue = itemInfo.flags; |
|
122 } |
|
123 } |
|
124 |
|
125 JNF_COCOA_EXIT(env); |
|
126 return returnValue; |
|
127 } |
|
128 |
|
129 /* |
|
130 * Class: com_apple_laf_AquaFileView |
|
131 * Method: getNativeDisplayName |
|
132 * Signature: ([BZ)Ljava/lang/String; |
|
133 */ |
|
134 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativeDisplayName |
|
135 (JNIEnv *env, jclass clazz, jbyteArray absolutePath, jboolean isDir) |
|
136 { |
|
137 jstring returnValue = NULL; |
|
138 JNF_COCOA_ENTER(env); |
|
139 |
|
140 jbyte *byteArray = (*env)->GetByteArrayElements(env, absolutePath, NULL); |
|
141 jsize length = (*env)->GetArrayLength(env, absolutePath); |
|
142 |
|
143 // Can't assume that byteArray is NULL terminated and FSPathMakeRef doesn't |
|
144 // let us specify a length. |
|
145 UInt8 arrayCopy[length + 1]; |
|
146 jsize i; |
|
147 for (i = 0; i < length; i++) { |
|
148 arrayCopy[i] = (UInt8)byteArray[i]; |
|
149 } |
|
150 arrayCopy[length] = '\0'; |
|
151 (*env)->ReleaseByteArrayElements(env, absolutePath, byteArray, JNI_ABORT); |
|
152 |
|
153 Boolean isDirectory = (isDir == JNI_TRUE ? true : false); |
|
154 FSRef ref; |
|
155 |
|
156 OSErr theErr = FSPathMakeRefWithOptions((const UInt8 *)&arrayCopy, kFSPathMakeRefDoNotFollowLeafSymlink, &ref, &isDirectory); |
|
157 if (theErr == noErr) { |
|
158 CFStringRef displayName = NULL; |
|
159 |
|
160 theErr = LSCopyDisplayNameForRef(&ref, &displayName); |
|
161 |
|
162 if (theErr == noErr) { |
|
163 CFMutableStringRef mutableDisplayName = CFStringCreateMutableCopy(NULL, 0, displayName); |
|
164 CFStringNormalize(mutableDisplayName, kCFStringNormalizationFormC); |
|
165 returnValue = JNFNSToJavaString(env, (NSString *)mutableDisplayName); |
|
166 CFRelease(mutableDisplayName); |
|
167 } |
|
168 |
|
169 if (displayName != NULL) { |
|
170 CFRelease(displayName); |
|
171 } |
|
172 } |
|
173 |
|
174 JNF_COCOA_EXIT(env); |
|
175 return returnValue; |
|
176 } |
|
177 |
|
178 /* |
|
179 * Class: com_apple_laf_AquaFileView |
|
180 * Method: getNativePathForResolvedAlias |
|
181 * Signature: ([BZ)Ljava/lang/String; |
|
182 */ |
|
183 JNIEXPORT jstring JNICALL Java_com_apple_laf_AquaFileView_getNativePathForResolvedAlias |
|
184 (JNIEnv *env, jclass clazz, jbyteArray pathToAlias, jboolean isDir) |
|
185 { |
|
186 jstring returnValue = NULL; |
|
187 JNF_COCOA_ENTER(env); |
|
188 |
|
189 UInt8 pathCString[MAXPATHLEN + 1]; |
|
190 size_t pathSize = sizeof(pathCString); |
|
191 |
|
192 jbyte *byteArray = (*env)->GetByteArrayElements(env, pathToAlias, NULL); |
|
193 jsize length = (*env)->GetArrayLength(env, pathToAlias); |
|
194 |
|
195 if (length > pathSize) { |
|
196 length = pathSize; |
|
197 } |
|
198 strncpy((char *)pathCString, (char *)byteArray, length); |
|
199 // make sure it's null terminated |
|
200 pathCString[length] = '\0'; |
|
201 (*env)->ReleaseByteArrayElements(env, pathToAlias, byteArray, JNI_ABORT); |
|
202 |
|
203 Boolean isDirectory = (isDir == JNI_TRUE ? true : false); |
|
204 FSRef fileRef; |
|
205 OSErr theErr = FSPathMakeRef(pathCString, &fileRef, &isDirectory); |
|
206 |
|
207 Boolean ignored; |
|
208 theErr = FSResolveAliasFileWithMountFlags(&fileRef, false, &ignored, &ignored, kResolveAliasFileNoUI); |
|
209 if (theErr == noErr) { |
|
210 UInt8 resolvedPath[MAXPATHLEN]; |
|
211 theErr = FSRefMakePath(&fileRef, resolvedPath, MAXPATHLEN); |
|
212 |
|
213 if (theErr == noErr) { |
|
214 returnValue = (*env)->NewStringUTF(env, (char *)resolvedPath); |
|
215 } |
|
216 } |
|
217 |
|
218 JNF_COCOA_EXIT(env); |
|
219 return returnValue; |
|
220 } |