< prev index next >

src/java.base/unix/native/libjava/UnixFileSystem_md.c

Print this page
rev 52679 : 8214077: test java/io/File/SetLastModified.java fails on ARM32
Summary: replace uses of stat with stat64 in java.base
Reviewed-by: duke
Contributed-by: nick.gasson@arm.com

Jira: ENTLLT-1484
Change-Id: I7eb190fdc1f60db7cc13327eb2e9fe233063ffd1


  52 
  53 #if defined(_AIX)
  54   #if !defined(NAME_MAX)
  55     #define NAME_MAX MAXNAMLEN
  56   #endif
  57   #define DIR DIR64
  58   #define dirent dirent64
  59   #define opendir opendir64
  60   #define readdir readdir64
  61   #define closedir closedir64
  62   #define stat stat64
  63 #endif
  64 
  65 #if defined(__solaris__) && !defined(NAME_MAX)
  66   #define NAME_MAX MAXNAMLEN
  67 #endif
  68 
  69 #if defined(_ALLBSD_SOURCE)
  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;");
  89 }
  90 
  91 /* -- Path operations -- */


 104                          canonicalPath, PATH_MAX) < 0) {
 105             JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
 106         } else {
 107 #ifdef MACOSX
 108             rv = newStringPlatform(env, canonicalPath);
 109 #else
 110             rv = JNU_NewStringPlatform(env, canonicalPath);
 111 #endif
 112         }
 113     } END_PLATFORM_STRING(env, path);
 114     return rv;
 115 }
 116 
 117 
 118 /* -- Attribute accessors -- */
 119 
 120 
 121 static jboolean
 122 statMode(const char *path, int *mode)
 123 {
 124     struct stat sb;
 125     if (stat(path, &sb) == 0) {
 126         *mode = sb.st_mode;
 127         return JNI_TRUE;
 128     }
 129     return JNI_FALSE;
 130 }
 131 
 132 
 133 JNIEXPORT jint JNICALL
 134 Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
 135                                                   jobject file)
 136 {
 137     jint rv = 0;
 138 
 139     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 140         int mode;
 141         if (statMode(path, &mode)) {
 142             int fmt = mode & S_IFMT;
 143             rv = (jint) (java_io_FileSystem_BA_EXISTS
 144                   | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
 145                   | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));


 212         if (statMode(path, &mode)) {
 213             if (enable)
 214                 mode |= amode;
 215             else
 216                 mode &= ~amode;
 217             if (chmod(path, mode) >= 0) {
 218                 rv = JNI_TRUE;
 219             }
 220         }
 221     } END_PLATFORM_STRING(env, path);
 222     return rv;
 223 }
 224 
 225 JNIEXPORT jlong JNICALL
 226 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
 227                                                 jobject file)
 228 {
 229     jlong rv = 0;
 230 
 231     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 232         struct stat sb;
 233         if (stat(path, &sb) == 0) {
 234 #if defined(_AIX)
 235             rv =  (jlong)sb.st_mtime * 1000;
 236             rv += (jlong)sb.st_mtime_n / 1000000;
 237 #elif defined(MACOSX)
 238             rv  = (jlong)sb.st_mtimespec.tv_sec * 1000;
 239             rv += (jlong)sb.st_mtimespec.tv_nsec / 1000000;
 240 #else
 241             rv  = (jlong)sb.st_mtim.tv_sec * 1000;
 242             rv += (jlong)sb.st_mtim.tv_nsec / 1000000;
 243 #endif
 244         }
 245     } END_PLATFORM_STRING(env, path);
 246     return rv;
 247 }
 248 
 249 
 250 JNIEXPORT jlong JNICALL
 251 Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
 252                                       jobject file)
 253 {
 254     jlong rv = 0;
 255 
 256     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 257         struct stat sb;
 258         if (stat(path, &sb) == 0) {
 259             rv = sb.st_size;
 260         }
 261     } END_PLATFORM_STRING(env, path);
 262     return rv;
 263 }
 264 
 265 
 266 /* -- File operations -- */
 267 
 268 
 269 JNIEXPORT jboolean JNICALL
 270 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
 271                                                   jstring pathname)
 272 {
 273     jboolean rv = JNI_FALSE;
 274 
 275     WITH_PLATFORM_STRING(env, pathname, path) {
 276         FD fd;
 277         /* The root directory always exists */
 278         if (strcmp (path, "/")) {


 391 {
 392     jboolean rv = JNI_FALSE;
 393 
 394     WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
 395         WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
 396             if (rename(fromPath, toPath) == 0) {
 397                 rv = JNI_TRUE;
 398             }
 399         } END_PLATFORM_STRING(env, toPath);
 400     } END_PLATFORM_STRING(env, fromPath);
 401     return rv;
 402 }
 403 
 404 JNIEXPORT jboolean JNICALL
 405 Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
 406                                                 jobject file, jlong time)
 407 {
 408     jboolean rv = JNI_FALSE;
 409 
 410     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 411         struct stat sb;
 412 
 413         if (stat(path, &sb) == 0) {
 414             struct timeval tv[2];
 415 
 416             /* Preserve access time */
 417 #if defined(_AIX)
 418             tv[0].tv_sec = sb.st_atime;
 419             tv[0].tv_usec = sb.st_atime_n / 1000;
 420 #elif defined(MACOSX)
 421             tv[0].tv_sec = sb.st_atimespec.tv_sec;
 422             tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
 423 #else
 424             tv[0].tv_sec = sb.st_atim.tv_sec;
 425             tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;
 426 #endif
 427             /* Change last-modified time */
 428             tv[1].tv_sec = time / 1000;
 429             tv[1].tv_usec = (time % 1000) * 1000;
 430 
 431             if (utimes(path, tv) == 0)
 432                 rv = JNI_TRUE;
 433         }




  52 
  53 #if defined(_AIX)
  54   #if !defined(NAME_MAX)
  55     #define NAME_MAX MAXNAMLEN
  56   #endif
  57   #define DIR DIR64
  58   #define dirent dirent64
  59   #define opendir opendir64
  60   #define readdir readdir64
  61   #define closedir closedir64
  62   #define stat stat64
  63 #endif
  64 
  65 #if defined(__solaris__) && !defined(NAME_MAX)
  66   #define NAME_MAX MAXNAMLEN
  67 #endif
  68 
  69 #if defined(_ALLBSD_SOURCE)
  70   #ifndef MACOSX
  71     #define statvfs64 statvfs
  72     #define stat64 stat
  73   #endif
  74 #endif
  75 
  76 /* -- Field IDs -- */
  77 
  78 static struct {
  79     jfieldID path;
  80 } ids;
  81 
  82 
  83 JNIEXPORT void JNICALL
  84 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
  85 {
  86     jclass fileClass = (*env)->FindClass(env, "java/io/File");
  87     if (!fileClass) return;
  88     ids.path = (*env)->GetFieldID(env, fileClass,
  89                                   "path", "Ljava/lang/String;");
  90 }
  91 
  92 /* -- Path operations -- */


 105                          canonicalPath, PATH_MAX) < 0) {
 106             JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
 107         } else {
 108 #ifdef MACOSX
 109             rv = newStringPlatform(env, canonicalPath);
 110 #else
 111             rv = JNU_NewStringPlatform(env, canonicalPath);
 112 #endif
 113         }
 114     } END_PLATFORM_STRING(env, path);
 115     return rv;
 116 }
 117 
 118 
 119 /* -- Attribute accessors -- */
 120 
 121 
 122 static jboolean
 123 statMode(const char *path, int *mode)
 124 {
 125     struct stat64 sb;
 126     if (stat64(path, &sb) == 0) {
 127         *mode = sb.st_mode;
 128         return JNI_TRUE;
 129     }
 130     return JNI_FALSE;
 131 }
 132 
 133 
 134 JNIEXPORT jint JNICALL
 135 Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
 136                                                   jobject file)
 137 {
 138     jint rv = 0;
 139 
 140     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 141         int mode;
 142         if (statMode(path, &mode)) {
 143             int fmt = mode & S_IFMT;
 144             rv = (jint) (java_io_FileSystem_BA_EXISTS
 145                   | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
 146                   | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));


 213         if (statMode(path, &mode)) {
 214             if (enable)
 215                 mode |= amode;
 216             else
 217                 mode &= ~amode;
 218             if (chmod(path, mode) >= 0) {
 219                 rv = JNI_TRUE;
 220             }
 221         }
 222     } END_PLATFORM_STRING(env, path);
 223     return rv;
 224 }
 225 
 226 JNIEXPORT jlong JNICALL
 227 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
 228                                                 jobject file)
 229 {
 230     jlong rv = 0;
 231 
 232     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 233         struct stat64 sb;
 234         if (stat64(path, &sb) == 0) {
 235 #if defined(_AIX)
 236             rv =  (jlong)sb.st_mtime * 1000;
 237             rv += (jlong)sb.st_mtime_n / 1000000;
 238 #elif defined(MACOSX)
 239             rv  = (jlong)sb.st_mtimespec.tv_sec * 1000;
 240             rv += (jlong)sb.st_mtimespec.tv_nsec / 1000000;
 241 #else
 242             rv  = (jlong)sb.st_mtim.tv_sec * 1000;
 243             rv += (jlong)sb.st_mtim.tv_nsec / 1000000;
 244 #endif
 245         }
 246     } END_PLATFORM_STRING(env, path);
 247     return rv;
 248 }
 249 
 250 
 251 JNIEXPORT jlong JNICALL
 252 Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
 253                                       jobject file)
 254 {
 255     jlong rv = 0;
 256 
 257     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 258         struct stat64 sb;
 259         if (stat64(path, &sb) == 0) {
 260             rv = sb.st_size;
 261         }
 262     } END_PLATFORM_STRING(env, path);
 263     return rv;
 264 }
 265 
 266 
 267 /* -- File operations -- */
 268 
 269 
 270 JNIEXPORT jboolean JNICALL
 271 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
 272                                                   jstring pathname)
 273 {
 274     jboolean rv = JNI_FALSE;
 275 
 276     WITH_PLATFORM_STRING(env, pathname, path) {
 277         FD fd;
 278         /* The root directory always exists */
 279         if (strcmp (path, "/")) {


 392 {
 393     jboolean rv = JNI_FALSE;
 394 
 395     WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
 396         WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
 397             if (rename(fromPath, toPath) == 0) {
 398                 rv = JNI_TRUE;
 399             }
 400         } END_PLATFORM_STRING(env, toPath);
 401     } END_PLATFORM_STRING(env, fromPath);
 402     return rv;
 403 }
 404 
 405 JNIEXPORT jboolean JNICALL
 406 Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
 407                                                 jobject file, jlong time)
 408 {
 409     jboolean rv = JNI_FALSE;
 410 
 411     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 412         struct stat64 sb;
 413 
 414         if (stat64(path, &sb) == 0) {
 415             struct timeval tv[2];
 416 
 417             /* Preserve access time */
 418 #if defined(_AIX)
 419             tv[0].tv_sec = sb.st_atime;
 420             tv[0].tv_usec = sb.st_atime_n / 1000;
 421 #elif defined(MACOSX)
 422             tv[0].tv_sec = sb.st_atimespec.tv_sec;
 423             tv[0].tv_usec = sb.st_atimespec.tv_nsec / 1000;
 424 #else
 425             tv[0].tv_sec = sb.st_atim.tv_sec;
 426             tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;
 427 #endif
 428             /* Change last-modified time */
 429             tv[1].tv_sec = time / 1000;
 430             tv[1].tv_usec = (time % 1000) * 1000;
 431 
 432             if (utimes(path, tv) == 0)
 433                 rv = JNI_TRUE;
 434         }


< prev index next >