src/solaris/native/java/io/UnixFileSystem_md.c

Print this page




  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 #include <assert.h>
  27 #include <sys/types.h>
  28 #include <sys/time.h>
  29 #include <sys/stat.h>
  30 #include <sys/statvfs.h>
  31 #include <string.h>
  32 #include <stdlib.h>
  33 #include <dlfcn.h>
  34 #include <limits.h>
  35 
  36 #include "jni.h"
  37 #include "jni_util.h"
  38 #include "jlong.h"
  39 #include "jvm.h"
  40 #include "io_util.h"

  41 #include "java_io_FileSystem.h"
  42 #include "java_io_UnixFileSystem.h"
  43 
  44 #if defined(_ALLBSD_SOURCE)
  45 #define dirent64 dirent
  46 #define readdir64_r readdir_r
  47 #define stat64 stat
  48 #define statvfs64 statvfs
  49 #endif
  50 
  51 /* -- Field IDs -- */
  52 
  53 static struct {
  54     jfieldID path;
  55 } ids;
  56 
  57 
  58 JNIEXPORT void JNICALL
  59 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
  60 {


  63     ids.path = (*env)->GetFieldID(env, fileClass,
  64                                   "path", "Ljava/lang/String;");
  65 }
  66 
  67 /* -- Path operations -- */
  68 
  69 extern int canonicalize(char *path, const char *out, int len);
  70 
  71 JNIEXPORT jstring JNICALL
  72 Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
  73                                           jstring pathname)
  74 {
  75     jstring rv = NULL;
  76 
  77     WITH_PLATFORM_STRING(env, pathname, path) {
  78         char canonicalPath[JVM_MAXPATHLEN];
  79         if (canonicalize(JVM_NativePath((char *)path),
  80                          canonicalPath, JVM_MAXPATHLEN) < 0) {
  81             JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
  82         } else {



  83             rv = JNU_NewStringPlatform(env, canonicalPath);

  84         }
  85     } END_PLATFORM_STRING(env, path);
  86     return rv;
  87 }
  88 
  89 
  90 /* -- Attribute accessors -- */
  91 
  92 
  93 static jboolean
  94 statMode(const char *path, int *mode)
  95 {
  96     struct stat64 sb;
  97     if (stat64(path, &sb) == 0) {
  98         *mode = sb.st_mode;
  99         return JNI_TRUE;
 100     }
 101     return JNI_FALSE;
 102 }
 103 


 294 
 295     /* Allocate an initial String array */
 296     len = 0;
 297     maxlen = 16;
 298     rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
 299     if (rv == NULL) goto error;
 300 
 301     /* Scan the directory */
 302     while ((readdir64_r(dir, ptr, &result) == 0)  && (result != NULL)) {
 303         jstring name;
 304         if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
 305             continue;
 306         if (len == maxlen) {
 307             old = rv;
 308             rv = (*env)->NewObjectArray(env, maxlen <<= 1,
 309                                         JNU_ClassString(env), NULL);
 310             if (rv == NULL) goto error;
 311             if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
 312             (*env)->DeleteLocalRef(env, old);
 313         }



 314         name = JNU_NewStringPlatform(env, ptr->d_name);

 315         if (name == NULL) goto error;
 316         (*env)->SetObjectArrayElement(env, rv, len++, name);
 317         (*env)->DeleteLocalRef(env, name);
 318     }
 319     closedir(dir);
 320     free(ptr);
 321 
 322     /* Copy the final results into an appropriately-sized array */
 323     old = rv;
 324     rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
 325     if (rv == NULL) {
 326         return NULL;
 327     }
 328     if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
 329         return NULL;
 330     }
 331     return rv;
 332 
 333  error:
 334     closedir(dir);




  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 #include <assert.h>
  27 #include <sys/types.h>
  28 #include <sys/time.h>
  29 #include <sys/stat.h>
  30 #include <sys/statvfs.h>
  31 #include <string.h>
  32 #include <stdlib.h>
  33 #include <dlfcn.h>
  34 #include <limits.h>
  35 
  36 #include "jni.h"
  37 #include "jni_util.h"
  38 #include "jlong.h"
  39 #include "jvm.h"
  40 #include "io_util.h"
  41 #include "io_util_md.h"
  42 #include "java_io_FileSystem.h"
  43 #include "java_io_UnixFileSystem.h"
  44 
  45 #if defined(_ALLBSD_SOURCE)
  46 #define dirent64 dirent
  47 #define readdir64_r readdir_r
  48 #define stat64 stat
  49 #define statvfs64 statvfs
  50 #endif
  51 
  52 /* -- Field IDs -- */
  53 
  54 static struct {
  55     jfieldID path;
  56 } ids;
  57 
  58 
  59 JNIEXPORT void JNICALL
  60 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
  61 {


  64     ids.path = (*env)->GetFieldID(env, fileClass,
  65                                   "path", "Ljava/lang/String;");
  66 }
  67 
  68 /* -- Path operations -- */
  69 
  70 extern int canonicalize(char *path, const char *out, int len);
  71 
  72 JNIEXPORT jstring JNICALL
  73 Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
  74                                           jstring pathname)
  75 {
  76     jstring rv = NULL;
  77 
  78     WITH_PLATFORM_STRING(env, pathname, path) {
  79         char canonicalPath[JVM_MAXPATHLEN];
  80         if (canonicalize(JVM_NativePath((char *)path),
  81                          canonicalPath, JVM_MAXPATHLEN) < 0) {
  82             JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
  83         } else {
  84 #ifdef MACOSX
  85             rv = newStringPlatform(env, canonicalPath);
  86 #else
  87             rv = JNU_NewStringPlatform(env, canonicalPath);
  88 #endif
  89         }
  90     } END_PLATFORM_STRING(env, path);
  91     return rv;
  92 }
  93 
  94 
  95 /* -- Attribute accessors -- */
  96 
  97 
  98 static jboolean
  99 statMode(const char *path, int *mode)
 100 {
 101     struct stat64 sb;
 102     if (stat64(path, &sb) == 0) {
 103         *mode = sb.st_mode;
 104         return JNI_TRUE;
 105     }
 106     return JNI_FALSE;
 107 }
 108 


 299 
 300     /* Allocate an initial String array */
 301     len = 0;
 302     maxlen = 16;
 303     rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
 304     if (rv == NULL) goto error;
 305 
 306     /* Scan the directory */
 307     while ((readdir64_r(dir, ptr, &result) == 0)  && (result != NULL)) {
 308         jstring name;
 309         if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
 310             continue;
 311         if (len == maxlen) {
 312             old = rv;
 313             rv = (*env)->NewObjectArray(env, maxlen <<= 1,
 314                                         JNU_ClassString(env), NULL);
 315             if (rv == NULL) goto error;
 316             if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
 317             (*env)->DeleteLocalRef(env, old);
 318         }
 319 #ifdef MACOSX
 320         name = newStringPlatform(env, ptr->d_name);
 321 #else
 322         name = JNU_NewStringPlatform(env, ptr->d_name);
 323 #endif
 324         if (name == NULL) goto error;
 325         (*env)->SetObjectArrayElement(env, rv, len++, name);
 326         (*env)->DeleteLocalRef(env, name);
 327     }
 328     closedir(dir);
 329     free(ptr);
 330 
 331     /* Copy the final results into an appropriately-sized array */
 332     old = rv;
 333     rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
 334     if (rv == NULL) {
 335         return NULL;
 336     }
 337     if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
 338         return NULL;
 339     }
 340     return rv;
 341 
 342  error:
 343     closedir(dir);