1 /*
2 * Copyright (c) 1998, 2017, 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
48 #include "io_util.h"
49 #include "io_util_md.h"
50 #include "java_io_FileSystem.h"
51 #include "java_io_UnixFileSystem.h"
52
53 #if defined(_AIX)
54 #if !defined(NAME_MAX)
55 #define NAME_MAX MAXNAMLEN
56 #endif
57 #define DIR DIR64
58 #define opendir opendir64
59 #define closedir closedir64
60 #endif
61
62 #if defined(__solaris__) && !defined(NAME_MAX)
63 #define NAME_MAX MAXNAMLEN
64 #endif
65
66 #if defined(_ALLBSD_SOURCE)
67 #define dirent64 dirent
68 #define readdir64_r readdir_r
69 #define stat64 stat
70 #ifndef MACOSX
71 #define statvfs64 statvfs
72 #endif
73 #endif
74
75 /* -- Field IDs -- */
76
77 static struct {
78 jfieldID path;
79 } ids;
80
81
82 JNIEXPORT void JNICALL
83 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
84 {
85 jclass fileClass = (*env)->FindClass(env, "java/io/File");
86 if (!fileClass) return;
87 ids.path = (*env)->GetFieldID(env, fileClass,
88 "path", "Ljava/lang/String;");
295 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
296 jobject file)
297 {
298 jboolean rv = JNI_FALSE;
299
300 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
301 if (remove(path) == 0) {
302 rv = JNI_TRUE;
303 }
304 } END_PLATFORM_STRING(env, path);
305 return rv;
306 }
307
308
309 JNIEXPORT jobjectArray JNICALL
310 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
311 jobject file)
312 {
313 DIR *dir = NULL;
314 struct dirent64 *ptr;
315 struct dirent64 *result;
316 int len, maxlen;
317 jobjectArray rv, old;
318 jclass str_class;
319
320 str_class = JNU_ClassString(env);
321 CHECK_NULL_RETURN(str_class, NULL);
322
323 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
324 dir = opendir(path);
325 } END_PLATFORM_STRING(env, path);
326 if (dir == NULL) return NULL;
327
328 ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
329 if (ptr == NULL) {
330 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
331 closedir(dir);
332 return NULL;
333 }
334
335 /* Allocate an initial String array */
336 len = 0;
337 maxlen = 16;
338 rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
339 if (rv == NULL) goto error;
340
341 /* Scan the directory */
342 while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) {
343 jstring name;
344 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
345 continue;
346 if (len == maxlen) {
347 old = rv;
348 rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
349 if (rv == NULL) goto error;
350 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
351 (*env)->DeleteLocalRef(env, old);
352 }
353 #ifdef MACOSX
354 name = newStringPlatform(env, ptr->d_name);
355 #else
356 name = JNU_NewStringPlatform(env, ptr->d_name);
357 #endif
358 if (name == NULL) goto error;
359 (*env)->SetObjectArrayElement(env, rv, len++, name);
360 (*env)->DeleteLocalRef(env, name);
361 }
362 closedir(dir);
363 free(ptr);
364
365 /* Copy the final results into an appropriately-sized array */
366 old = rv;
367 rv = (*env)->NewObjectArray(env, len, str_class, NULL);
368 if (rv == NULL) {
369 return NULL;
370 }
371 if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
372 return NULL;
373 }
374 return rv;
375
376 error:
377 closedir(dir);
378 free(ptr);
379 return NULL;
380 }
381
382
383 JNIEXPORT jboolean JNICALL
384 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
385 jobject file)
386 {
387 jboolean rv = JNI_FALSE;
388
389 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
390 if (mkdir(path, 0777) == 0) {
391 rv = JNI_TRUE;
392 }
393 } END_PLATFORM_STRING(env, path);
394 return rv;
395 }
396
397
398 JNIEXPORT jboolean JNICALL
|
1 /*
2 * Copyright (c) 1998, 2018, 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
48 #include "io_util.h"
49 #include "io_util_md.h"
50 #include "java_io_FileSystem.h"
51 #include "java_io_UnixFileSystem.h"
52
53 #if defined(_AIX)
54 #if !defined(NAME_MAX)
55 #define NAME_MAX MAXNAMLEN
56 #endif
57 #define DIR DIR64
58 #define opendir opendir64
59 #define closedir closedir64
60 #endif
61
62 #if defined(__solaris__) && !defined(NAME_MAX)
63 #define NAME_MAX MAXNAMLEN
64 #endif
65
66 #if defined(_ALLBSD_SOURCE)
67 #define dirent64 dirent
68 #define readdir64 readdir
69 #define stat64 stat
70 #ifndef MACOSX
71 #define statvfs64 statvfs
72 #endif
73 #endif
74
75 /* -- Field IDs -- */
76
77 static struct {
78 jfieldID path;
79 } ids;
80
81
82 JNIEXPORT void JNICALL
83 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
84 {
85 jclass fileClass = (*env)->FindClass(env, "java/io/File");
86 if (!fileClass) return;
87 ids.path = (*env)->GetFieldID(env, fileClass,
88 "path", "Ljava/lang/String;");
295 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
296 jobject file)
297 {
298 jboolean rv = JNI_FALSE;
299
300 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
301 if (remove(path) == 0) {
302 rv = JNI_TRUE;
303 }
304 } END_PLATFORM_STRING(env, path);
305 return rv;
306 }
307
308
309 JNIEXPORT jobjectArray JNICALL
310 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
311 jobject file)
312 {
313 DIR *dir = NULL;
314 struct dirent64 *ptr;
315 int len, maxlen;
316 jobjectArray rv, old;
317 jclass str_class;
318
319 str_class = JNU_ClassString(env);
320 CHECK_NULL_RETURN(str_class, NULL);
321
322 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
323 dir = opendir(path);
324 } END_PLATFORM_STRING(env, path);
325 if (dir == NULL) return NULL;
326
327 /* Allocate an initial String array */
328 len = 0;
329 maxlen = 16;
330 rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
331 if (rv == NULL) goto error;
332
333 /* Scan the directory */
334 while ((ptr = readdir64(dir)) != NULL) {
335 jstring name;
336 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
337 continue;
338 if (len == maxlen) {
339 old = rv;
340 rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
341 if (rv == NULL) goto error;
342 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
343 (*env)->DeleteLocalRef(env, old);
344 }
345 #ifdef MACOSX
346 name = newStringPlatform(env, ptr->d_name);
347 #else
348 name = JNU_NewStringPlatform(env, ptr->d_name);
349 #endif
350 if (name == NULL) goto error;
351 (*env)->SetObjectArrayElement(env, rv, len++, name);
352 (*env)->DeleteLocalRef(env, name);
353 }
354 closedir(dir);
355
356 /* Copy the final results into an appropriately-sized array */
357 old = rv;
358 rv = (*env)->NewObjectArray(env, len, str_class, NULL);
359 if (rv == NULL) {
360 return NULL;
361 }
362 if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
363 return NULL;
364 }
365 return rv;
366
367 error:
368 closedir(dir);
369 return NULL;
370 }
371
372
373 JNIEXPORT jboolean JNICALL
374 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
375 jobject file)
376 {
377 jboolean rv = JNI_FALSE;
378
379 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
380 if (mkdir(path, 0777) == 0) {
381 rv = JNI_TRUE;
382 }
383 } END_PLATFORM_STRING(env, path);
384 return rv;
385 }
386
387
388 JNIEXPORT jboolean JNICALL
|