266 jboolean rv = JNI_FALSE; 267 268 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { 269 if (remove(path) == 0) { 270 rv = JNI_TRUE; 271 } 272 } END_PLATFORM_STRING(env, path); 273 return rv; 274 } 275 276 277 JNIEXPORT jobjectArray JNICALL 278 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, 279 jobject file) 280 { 281 DIR *dir = NULL; 282 struct dirent64 *ptr; 283 struct dirent64 *result; 284 int len, maxlen; 285 jobjectArray rv, old; 286 287 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { 288 dir = opendir(path); 289 } END_PLATFORM_STRING(env, path); 290 if (dir == NULL) return NULL; 291 292 ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1)); 293 if (ptr == NULL) { 294 JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); 295 closedir(dir); 296 return NULL; 297 } 298 299 /* Allocate an initial String array */ 300 len = 0; 301 maxlen = 16; 302 rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL); 303 if (rv == NULL) goto error; 304 305 /* Scan the directory */ 306 while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) { 307 jstring name; 308 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) 309 continue; 310 if (len == maxlen) { 311 old = rv; 312 rv = (*env)->NewObjectArray(env, maxlen <<= 1, 313 JNU_ClassString(env), NULL); 314 if (rv == NULL) goto error; 315 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error; 316 (*env)->DeleteLocalRef(env, old); 317 } 318 #ifdef MACOSX 319 name = newStringPlatform(env, ptr->d_name); 320 #else 321 name = JNU_NewStringPlatform(env, ptr->d_name); 322 #endif 323 if (name == NULL) goto error; 324 (*env)->SetObjectArrayElement(env, rv, len++, name); 325 (*env)->DeleteLocalRef(env, name); 326 } 327 closedir(dir); 328 free(ptr); 329 330 /* Copy the final results into an appropriately-sized array */ 331 old = rv; 332 rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL); 333 if (rv == NULL) { 334 return NULL; 335 } 336 if (JNU_CopyObjectArray(env, rv, old, len) < 0) { 337 return NULL; 338 } 339 return rv; 340 341 error: 342 closedir(dir); 343 free(ptr); 344 return NULL; 345 } 346 347 348 JNIEXPORT jboolean JNICALL 349 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this, 350 jobject file) 351 { 352 jboolean rv = JNI_FALSE; | 266 jboolean rv = JNI_FALSE; 267 268 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { 269 if (remove(path) == 0) { 270 rv = JNI_TRUE; 271 } 272 } END_PLATFORM_STRING(env, path); 273 return rv; 274 } 275 276 277 JNIEXPORT jobjectArray JNICALL 278 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this, 279 jobject file) 280 { 281 DIR *dir = NULL; 282 struct dirent64 *ptr; 283 struct dirent64 *result; 284 int len, maxlen; 285 jobjectArray rv, old; 286 jclass str_class; 287 288 str_class = JNU_ClassString(env); 289 CHECK_NULL_RETURN(str_class, NULL); 290 291 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) { 292 dir = opendir(path); 293 } END_PLATFORM_STRING(env, path); 294 if (dir == NULL) return NULL; 295 296 ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1)); 297 if (ptr == NULL) { 298 JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); 299 closedir(dir); 300 return NULL; 301 } 302 303 /* Allocate an initial String array */ 304 len = 0; 305 maxlen = 16; 306 rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL); 307 if (rv == NULL) goto error; 308 309 /* Scan the directory */ 310 while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) { 311 jstring name; 312 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, "..")) 313 continue; 314 if (len == maxlen) { 315 old = rv; 316 rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL); 317 if (rv == NULL) goto error; 318 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error; 319 (*env)->DeleteLocalRef(env, old); 320 } 321 #ifdef MACOSX 322 name = newStringPlatform(env, ptr->d_name); 323 #else 324 name = JNU_NewStringPlatform(env, ptr->d_name); 325 #endif 326 if (name == NULL) goto error; 327 (*env)->SetObjectArrayElement(env, rv, len++, name); 328 (*env)->DeleteLocalRef(env, name); 329 } 330 closedir(dir); 331 free(ptr); 332 333 /* Copy the final results into an appropriately-sized array */ 334 old = rv; 335 rv = (*env)->NewObjectArray(env, len, str_class, NULL); 336 if (rv == NULL) { 337 return NULL; 338 } 339 if (JNU_CopyObjectArray(env, rv, old, len) < 0) { 340 return NULL; 341 } 342 return rv; 343 344 error: 345 closedir(dir); 346 free(ptr); 347 return NULL; 348 } 349 350 351 JNIEXPORT jboolean JNICALL 352 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this, 353 jobject file) 354 { 355 jboolean rv = JNI_FALSE; |