annotate src/solaris/native/java/io/UnixFileSystem_md.c @ 2884:871cffb21423

6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code Reviewed-by: andrew, mchung, ohair
author alanb
date Thu, 07 Oct 2010 14:36:17 +0100
parents 00cd9dc3c2b5
children a06412e13bf7
rev   line source
duke@0 1 /*
ohair@2362 2 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 #include <assert.h>
duke@0 27 #include <sys/types.h>
duke@0 28 #include <sys/time.h>
duke@0 29 #include <sys/stat.h>
duke@0 30 #include <sys/statvfs.h>
duke@0 31 #include <string.h>
duke@0 32 #include <stdlib.h>
duke@0 33 #include <dlfcn.h>
duke@0 34 #include <limits.h>
duke@0 35
duke@0 36 #include "jni.h"
duke@0 37 #include "jni_util.h"
duke@0 38 #include "jlong.h"
duke@0 39 #include "jvm.h"
duke@0 40 #include "io_util.h"
duke@0 41 #include "java_io_FileSystem.h"
duke@0 42 #include "java_io_UnixFileSystem.h"
duke@0 43
duke@0 44
duke@0 45 /* -- Field IDs -- */
duke@0 46
duke@0 47 static struct {
duke@0 48 jfieldID path;
duke@0 49 } ids;
duke@0 50
duke@0 51
duke@0 52 JNIEXPORT void JNICALL
duke@0 53 Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
duke@0 54 {
duke@0 55 jclass fileClass = (*env)->FindClass(env, "java/io/File");
duke@0 56 if (!fileClass) return;
duke@0 57 ids.path = (*env)->GetFieldID(env, fileClass,
duke@0 58 "path", "Ljava/lang/String;");
duke@0 59 }
duke@0 60
duke@0 61 /* -- Path operations -- */
duke@0 62
duke@0 63 extern int canonicalize(char *path, const char *out, int len);
duke@0 64
duke@0 65 JNIEXPORT jstring JNICALL
duke@0 66 Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
duke@0 67 jstring pathname)
duke@0 68 {
duke@0 69 jstring rv = NULL;
duke@0 70
duke@0 71 WITH_PLATFORM_STRING(env, pathname, path) {
duke@0 72 char canonicalPath[JVM_MAXPATHLEN];
duke@0 73 if (canonicalize(JVM_NativePath((char *)path),
duke@0 74 canonicalPath, JVM_MAXPATHLEN) < 0) {
duke@0 75 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
duke@0 76 } else {
duke@0 77 rv = JNU_NewStringPlatform(env, canonicalPath);
duke@0 78 }
duke@0 79 } END_PLATFORM_STRING(env, path);
duke@0 80 return rv;
duke@0 81 }
duke@0 82
duke@0 83
duke@0 84 /* -- Attribute accessors -- */
duke@0 85
duke@0 86
duke@0 87 static jboolean
duke@0 88 statMode(const char *path, int *mode)
duke@0 89 {
alanb@358 90 struct stat64 sb;
alanb@358 91 if (stat64(path, &sb) == 0) {
alanb@358 92 *mode = sb.st_mode;
alanb@358 93 return JNI_TRUE;
duke@0 94 }
duke@0 95 return JNI_FALSE;
duke@0 96 }
duke@0 97
duke@0 98
duke@0 99 JNIEXPORT jint JNICALL
duke@0 100 Java_java_io_UnixFileSystem_getBooleanAttributes0(JNIEnv *env, jobject this,
duke@0 101 jobject file)
duke@0 102 {
duke@0 103 jint rv = 0;
duke@0 104
duke@0 105 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 106 int mode;
duke@0 107 if (statMode(path, &mode)) {
duke@0 108 int fmt = mode & S_IFMT;
duke@0 109 rv = (jint) (java_io_FileSystem_BA_EXISTS
duke@0 110 | ((fmt == S_IFREG) ? java_io_FileSystem_BA_REGULAR : 0)
duke@0 111 | ((fmt == S_IFDIR) ? java_io_FileSystem_BA_DIRECTORY : 0));
duke@0 112 }
duke@0 113 } END_PLATFORM_STRING(env, path);
duke@0 114 return rv;
duke@0 115 }
duke@0 116
duke@0 117 JNIEXPORT jboolean JNICALL
duke@0 118 Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this,
duke@0 119 jobject file, jint a)
duke@0 120 {
duke@0 121 jboolean rv = JNI_FALSE;
alanb@2884 122 int mode = 0;
duke@0 123 switch (a) {
duke@0 124 case java_io_FileSystem_ACCESS_READ:
duke@0 125 mode = R_OK;
duke@0 126 break;
duke@0 127 case java_io_FileSystem_ACCESS_WRITE:
duke@0 128 mode = W_OK;
duke@0 129 break;
duke@0 130 case java_io_FileSystem_ACCESS_EXECUTE:
duke@0 131 mode = X_OK;
duke@0 132 break;
duke@0 133 default: assert(0);
duke@0 134 }
duke@0 135 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 136 if (access(path, mode) == 0) {
duke@0 137 rv = JNI_TRUE;
duke@0 138 }
duke@0 139 } END_PLATFORM_STRING(env, path);
duke@0 140 return rv;
duke@0 141 }
duke@0 142
duke@0 143
duke@0 144 JNIEXPORT jboolean JNICALL
duke@0 145 Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this,
duke@0 146 jobject file,
duke@0 147 jint access,
duke@0 148 jboolean enable,
duke@0 149 jboolean owneronly)
duke@0 150 {
duke@0 151 jboolean rv = JNI_FALSE;
duke@0 152
duke@0 153 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@2884 154 int amode = 0;
alanb@2884 155 int mode;
duke@0 156 switch (access) {
duke@0 157 case java_io_FileSystem_ACCESS_READ:
duke@0 158 if (owneronly)
duke@0 159 amode = S_IRUSR;
duke@0 160 else
duke@0 161 amode = S_IRUSR | S_IRGRP | S_IROTH;
duke@0 162 break;
duke@0 163 case java_io_FileSystem_ACCESS_WRITE:
duke@0 164 if (owneronly)
duke@0 165 amode = S_IWUSR;
duke@0 166 else
duke@0 167 amode = S_IWUSR | S_IWGRP | S_IWOTH;
duke@0 168 break;
duke@0 169 case java_io_FileSystem_ACCESS_EXECUTE:
duke@0 170 if (owneronly)
duke@0 171 amode = S_IXUSR;
duke@0 172 else
duke@0 173 amode = S_IXUSR | S_IXGRP | S_IXOTH;
duke@0 174 break;
duke@0 175 default:
duke@0 176 assert(0);
duke@0 177 }
duke@0 178 if (statMode(path, &mode)) {
duke@0 179 if (enable)
duke@0 180 mode |= amode;
duke@0 181 else
duke@0 182 mode &= ~amode;
duke@0 183 if (chmod(path, mode) >= 0) {
duke@0 184 rv = JNI_TRUE;
duke@0 185 }
duke@0 186 }
duke@0 187 } END_PLATFORM_STRING(env, path);
duke@0 188 return rv;
duke@0 189 }
duke@0 190
duke@0 191 JNIEXPORT jlong JNICALL
duke@0 192 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
duke@0 193 jobject file)
duke@0 194 {
duke@0 195 jlong rv = 0;
duke@0 196
duke@0 197 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 198 struct stat64 sb;
alanb@358 199 if (stat64(path, &sb) == 0) {
alanb@358 200 rv = 1000 * (jlong)sb.st_mtime;
duke@0 201 }
duke@0 202 } END_PLATFORM_STRING(env, path);
duke@0 203 return rv;
duke@0 204 }
duke@0 205
duke@0 206
duke@0 207 JNIEXPORT jlong JNICALL
duke@0 208 Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
duke@0 209 jobject file)
duke@0 210 {
duke@0 211 jlong rv = 0;
duke@0 212
duke@0 213 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 214 struct stat64 sb;
alanb@358 215 if (stat64(path, &sb) == 0) {
alanb@358 216 rv = sb.st_size;
duke@0 217 }
duke@0 218 } END_PLATFORM_STRING(env, path);
duke@0 219 return rv;
duke@0 220 }
duke@0 221
duke@0 222
duke@0 223 /* -- File operations -- */
duke@0 224
duke@0 225
duke@0 226 JNIEXPORT jboolean JNICALL
duke@0 227 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
duke@0 228 jstring pathname)
duke@0 229 {
duke@0 230 jboolean rv = JNI_FALSE;
duke@0 231
duke@0 232 WITH_PLATFORM_STRING(env, pathname, path) {
duke@0 233 int fd;
duke@0 234 if (!strcmp (path, "/")) {
duke@0 235 fd = JVM_EEXIST; /* The root directory always exists */
duke@0 236 } else {
duke@0 237 fd = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666);
duke@0 238 }
duke@0 239 if (fd < 0) {
duke@0 240 if (fd != JVM_EEXIST) {
duke@0 241 JNU_ThrowIOExceptionWithLastError(env, path);
duke@0 242 }
duke@0 243 } else {
duke@0 244 JVM_Close(fd);
duke@0 245 rv = JNI_TRUE;
duke@0 246 }
duke@0 247 } END_PLATFORM_STRING(env, path);
duke@0 248 return rv;
duke@0 249 }
duke@0 250
duke@0 251
duke@0 252 JNIEXPORT jboolean JNICALL
duke@0 253 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
duke@0 254 jobject file)
duke@0 255 {
duke@0 256 jboolean rv = JNI_FALSE;
duke@0 257
duke@0 258 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 259 if (remove(path) == 0) {
duke@0 260 rv = JNI_TRUE;
duke@0 261 }
duke@0 262 } END_PLATFORM_STRING(env, path);
duke@0 263 return rv;
duke@0 264 }
duke@0 265
duke@0 266
duke@0 267 JNIEXPORT jobjectArray JNICALL
duke@0 268 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
duke@0 269 jobject file)
duke@0 270 {
duke@0 271 DIR *dir = NULL;
duke@0 272 struct dirent64 *ptr;
duke@0 273 struct dirent64 *result;
duke@0 274 int len, maxlen;
duke@0 275 jobjectArray rv, old;
duke@0 276
duke@0 277 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 278 dir = opendir(path);
duke@0 279 } END_PLATFORM_STRING(env, path);
duke@0 280 if (dir == NULL) return NULL;
duke@0 281
duke@0 282 ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
duke@0 283 if (ptr == NULL) {
duke@0 284 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
duke@0 285 closedir(dir);
duke@0 286 return NULL;
duke@0 287 }
duke@0 288
duke@0 289 /* Allocate an initial String array */
duke@0 290 len = 0;
duke@0 291 maxlen = 16;
duke@0 292 rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
duke@0 293 if (rv == NULL) goto error;
duke@0 294
duke@0 295 /* Scan the directory */
duke@0 296 while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) {
duke@0 297 jstring name;
duke@0 298 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
duke@0 299 continue;
duke@0 300 if (len == maxlen) {
duke@0 301 old = rv;
duke@0 302 rv = (*env)->NewObjectArray(env, maxlen <<= 1,
duke@0 303 JNU_ClassString(env), NULL);
duke@0 304 if (rv == NULL) goto error;
duke@0 305 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
duke@0 306 (*env)->DeleteLocalRef(env, old);
duke@0 307 }
duke@0 308 name = JNU_NewStringPlatform(env, ptr->d_name);
duke@0 309 if (name == NULL) goto error;
duke@0 310 (*env)->SetObjectArrayElement(env, rv, len++, name);
duke@0 311 (*env)->DeleteLocalRef(env, name);
duke@0 312 }
duke@0 313 closedir(dir);
duke@0 314 free(ptr);
duke@0 315
duke@0 316 /* Copy the final results into an appropriately-sized array */
duke@0 317 old = rv;
duke@0 318 rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
duke@0 319 if (rv == NULL) {
duke@0 320 return NULL;
duke@0 321 }
duke@0 322 if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
duke@0 323 return NULL;
duke@0 324 }
duke@0 325 return rv;
duke@0 326
duke@0 327 error:
duke@0 328 closedir(dir);
duke@0 329 free(ptr);
duke@0 330 return NULL;
duke@0 331 }
duke@0 332
duke@0 333
duke@0 334 JNIEXPORT jboolean JNICALL
duke@0 335 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
duke@0 336 jobject file)
duke@0 337 {
duke@0 338 jboolean rv = JNI_FALSE;
duke@0 339
duke@0 340 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 341 if (mkdir(path, 0777) == 0) {
duke@0 342 rv = JNI_TRUE;
duke@0 343 }
duke@0 344 } END_PLATFORM_STRING(env, path);
duke@0 345 return rv;
duke@0 346 }
duke@0 347
duke@0 348
duke@0 349 JNIEXPORT jboolean JNICALL
duke@0 350 Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
duke@0 351 jobject from, jobject to)
duke@0 352 {
duke@0 353 jboolean rv = JNI_FALSE;
duke@0 354
duke@0 355 WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
duke@0 356 WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
duke@0 357 if (rename(fromPath, toPath) == 0) {
duke@0 358 rv = JNI_TRUE;
duke@0 359 }
duke@0 360 } END_PLATFORM_STRING(env, toPath);
duke@0 361 } END_PLATFORM_STRING(env, fromPath);
duke@0 362 return rv;
duke@0 363 }
duke@0 364
duke@0 365 JNIEXPORT jboolean JNICALL
duke@0 366 Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
duke@0 367 jobject file, jlong time)
duke@0 368 {
duke@0 369 jboolean rv = JNI_FALSE;
duke@0 370
duke@0 371 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 372 struct stat64 sb;
duke@0 373
alanb@358 374 if (stat64(path, &sb) == 0) {
alanb@358 375 struct timeval tv[2];
alanb@358 376
alanb@358 377 /* Preserve access time */
alanb@358 378 tv[0].tv_sec = sb.st_atime;
alanb@358 379 tv[0].tv_usec = 0;
alanb@358 380
alanb@358 381 /* Change last-modified time */
alanb@358 382 tv[1].tv_sec = time / 1000;
alanb@358 383 tv[1].tv_usec = (time % 1000) * 1000;
alanb@358 384
alanb@358 385 if (utimes(path, tv) == 0)
alanb@358 386 rv = JNI_TRUE;
duke@0 387 }
duke@0 388 } END_PLATFORM_STRING(env, path);
duke@0 389
duke@0 390 return rv;
duke@0 391 }
duke@0 392
duke@0 393
duke@0 394 JNIEXPORT jboolean JNICALL
duke@0 395 Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
duke@0 396 jobject file)
duke@0 397 {
duke@0 398 jboolean rv = JNI_FALSE;
duke@0 399
duke@0 400 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 401 int mode;
duke@0 402 if (statMode(path, &mode)) {
duke@0 403 if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {
duke@0 404 rv = JNI_TRUE;
duke@0 405 }
duke@0 406 }
duke@0 407 } END_PLATFORM_STRING(env, path);
duke@0 408 return rv;
duke@0 409 }
duke@0 410
duke@0 411 JNIEXPORT jlong JNICALL
duke@0 412 Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
duke@0 413 jobject file, jint t)
duke@0 414 {
duke@0 415 jlong rv = 0L;
duke@0 416
duke@0 417 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@2193 418 struct statvfs64 fsstat;
alanb@2193 419 memset(&fsstat, 0, sizeof(fsstat));
alanb@2193 420 if (statvfs64(path, &fsstat) == 0) {
duke@0 421 switch(t) {
duke@0 422 case java_io_FileSystem_SPACE_TOTAL:
duke@0 423 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 424 long_to_jlong(fsstat.f_blocks));
duke@0 425 break;
duke@0 426 case java_io_FileSystem_SPACE_FREE:
duke@0 427 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 428 long_to_jlong(fsstat.f_bfree));
duke@0 429 break;
duke@0 430 case java_io_FileSystem_SPACE_USABLE:
duke@0 431 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 432 long_to_jlong(fsstat.f_bavail));
duke@0 433 break;
duke@0 434 default:
duke@0 435 assert(0);
duke@0 436 }
duke@0 437 }
duke@0 438 } END_PLATFORM_STRING(env, path);
duke@0 439 return rv;
duke@0 440 }