annotate src/solaris/native/java/io/UnixFileSystem_md.c @ 2362:00cd9dc3c2b5

6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
author ohair
date Tue, 25 May 2010 15:58:33 -0700
parents 529d2da0aee2
children 871cffb21423
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;
duke@0 122 int mode;
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) {
duke@0 154 int amode, mode;
duke@0 155 switch (access) {
duke@0 156 case java_io_FileSystem_ACCESS_READ:
duke@0 157 if (owneronly)
duke@0 158 amode = S_IRUSR;
duke@0 159 else
duke@0 160 amode = S_IRUSR | S_IRGRP | S_IROTH;
duke@0 161 break;
duke@0 162 case java_io_FileSystem_ACCESS_WRITE:
duke@0 163 if (owneronly)
duke@0 164 amode = S_IWUSR;
duke@0 165 else
duke@0 166 amode = S_IWUSR | S_IWGRP | S_IWOTH;
duke@0 167 break;
duke@0 168 case java_io_FileSystem_ACCESS_EXECUTE:
duke@0 169 if (owneronly)
duke@0 170 amode = S_IXUSR;
duke@0 171 else
duke@0 172 amode = S_IXUSR | S_IXGRP | S_IXOTH;
duke@0 173 break;
duke@0 174 default:
duke@0 175 assert(0);
duke@0 176 }
duke@0 177 if (statMode(path, &mode)) {
duke@0 178 if (enable)
duke@0 179 mode |= amode;
duke@0 180 else
duke@0 181 mode &= ~amode;
duke@0 182 if (chmod(path, mode) >= 0) {
duke@0 183 rv = JNI_TRUE;
duke@0 184 }
duke@0 185 }
duke@0 186 } END_PLATFORM_STRING(env, path);
duke@0 187 return rv;
duke@0 188 }
duke@0 189
duke@0 190 JNIEXPORT jlong JNICALL
duke@0 191 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
duke@0 192 jobject file)
duke@0 193 {
duke@0 194 jlong rv = 0;
duke@0 195
duke@0 196 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 197 struct stat64 sb;
alanb@358 198 if (stat64(path, &sb) == 0) {
alanb@358 199 rv = 1000 * (jlong)sb.st_mtime;
duke@0 200 }
duke@0 201 } END_PLATFORM_STRING(env, path);
duke@0 202 return rv;
duke@0 203 }
duke@0 204
duke@0 205
duke@0 206 JNIEXPORT jlong JNICALL
duke@0 207 Java_java_io_UnixFileSystem_getLength(JNIEnv *env, jobject this,
duke@0 208 jobject file)
duke@0 209 {
duke@0 210 jlong rv = 0;
duke@0 211
duke@0 212 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 213 struct stat64 sb;
alanb@358 214 if (stat64(path, &sb) == 0) {
alanb@358 215 rv = sb.st_size;
duke@0 216 }
duke@0 217 } END_PLATFORM_STRING(env, path);
duke@0 218 return rv;
duke@0 219 }
duke@0 220
duke@0 221
duke@0 222 /* -- File operations -- */
duke@0 223
duke@0 224
duke@0 225 JNIEXPORT jboolean JNICALL
duke@0 226 Java_java_io_UnixFileSystem_createFileExclusively(JNIEnv *env, jclass cls,
duke@0 227 jstring pathname)
duke@0 228 {
duke@0 229 jboolean rv = JNI_FALSE;
duke@0 230
duke@0 231 WITH_PLATFORM_STRING(env, pathname, path) {
duke@0 232 int fd;
duke@0 233 if (!strcmp (path, "/")) {
duke@0 234 fd = JVM_EEXIST; /* The root directory always exists */
duke@0 235 } else {
duke@0 236 fd = JVM_Open(path, JVM_O_RDWR | JVM_O_CREAT | JVM_O_EXCL, 0666);
duke@0 237 }
duke@0 238 if (fd < 0) {
duke@0 239 if (fd != JVM_EEXIST) {
duke@0 240 JNU_ThrowIOExceptionWithLastError(env, path);
duke@0 241 }
duke@0 242 } else {
duke@0 243 JVM_Close(fd);
duke@0 244 rv = JNI_TRUE;
duke@0 245 }
duke@0 246 } END_PLATFORM_STRING(env, path);
duke@0 247 return rv;
duke@0 248 }
duke@0 249
duke@0 250
duke@0 251 JNIEXPORT jboolean JNICALL
duke@0 252 Java_java_io_UnixFileSystem_delete0(JNIEnv *env, jobject this,
duke@0 253 jobject file)
duke@0 254 {
duke@0 255 jboolean rv = JNI_FALSE;
duke@0 256
duke@0 257 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 258 if (remove(path) == 0) {
duke@0 259 rv = JNI_TRUE;
duke@0 260 }
duke@0 261 } END_PLATFORM_STRING(env, path);
duke@0 262 return rv;
duke@0 263 }
duke@0 264
duke@0 265
duke@0 266 JNIEXPORT jobjectArray JNICALL
duke@0 267 Java_java_io_UnixFileSystem_list(JNIEnv *env, jobject this,
duke@0 268 jobject file)
duke@0 269 {
duke@0 270 DIR *dir = NULL;
duke@0 271 struct dirent64 *ptr;
duke@0 272 struct dirent64 *result;
duke@0 273 int len, maxlen;
duke@0 274 jobjectArray rv, old;
duke@0 275
duke@0 276 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 277 dir = opendir(path);
duke@0 278 } END_PLATFORM_STRING(env, path);
duke@0 279 if (dir == NULL) return NULL;
duke@0 280
duke@0 281 ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
duke@0 282 if (ptr == NULL) {
duke@0 283 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
duke@0 284 closedir(dir);
duke@0 285 return NULL;
duke@0 286 }
duke@0 287
duke@0 288 /* Allocate an initial String array */
duke@0 289 len = 0;
duke@0 290 maxlen = 16;
duke@0 291 rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL);
duke@0 292 if (rv == NULL) goto error;
duke@0 293
duke@0 294 /* Scan the directory */
duke@0 295 while ((readdir64_r(dir, ptr, &result) == 0) && (result != NULL)) {
duke@0 296 jstring name;
duke@0 297 if (!strcmp(ptr->d_name, ".") || !strcmp(ptr->d_name, ".."))
duke@0 298 continue;
duke@0 299 if (len == maxlen) {
duke@0 300 old = rv;
duke@0 301 rv = (*env)->NewObjectArray(env, maxlen <<= 1,
duke@0 302 JNU_ClassString(env), NULL);
duke@0 303 if (rv == NULL) goto error;
duke@0 304 if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
duke@0 305 (*env)->DeleteLocalRef(env, old);
duke@0 306 }
duke@0 307 name = JNU_NewStringPlatform(env, ptr->d_name);
duke@0 308 if (name == NULL) goto error;
duke@0 309 (*env)->SetObjectArrayElement(env, rv, len++, name);
duke@0 310 (*env)->DeleteLocalRef(env, name);
duke@0 311 }
duke@0 312 closedir(dir);
duke@0 313 free(ptr);
duke@0 314
duke@0 315 /* Copy the final results into an appropriately-sized array */
duke@0 316 old = rv;
duke@0 317 rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL);
duke@0 318 if (rv == NULL) {
duke@0 319 return NULL;
duke@0 320 }
duke@0 321 if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
duke@0 322 return NULL;
duke@0 323 }
duke@0 324 return rv;
duke@0 325
duke@0 326 error:
duke@0 327 closedir(dir);
duke@0 328 free(ptr);
duke@0 329 return NULL;
duke@0 330 }
duke@0 331
duke@0 332
duke@0 333 JNIEXPORT jboolean JNICALL
duke@0 334 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
duke@0 335 jobject file)
duke@0 336 {
duke@0 337 jboolean rv = JNI_FALSE;
duke@0 338
duke@0 339 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 340 if (mkdir(path, 0777) == 0) {
duke@0 341 rv = JNI_TRUE;
duke@0 342 }
duke@0 343 } END_PLATFORM_STRING(env, path);
duke@0 344 return rv;
duke@0 345 }
duke@0 346
duke@0 347
duke@0 348 JNIEXPORT jboolean JNICALL
duke@0 349 Java_java_io_UnixFileSystem_rename0(JNIEnv *env, jobject this,
duke@0 350 jobject from, jobject to)
duke@0 351 {
duke@0 352 jboolean rv = JNI_FALSE;
duke@0 353
duke@0 354 WITH_FIELD_PLATFORM_STRING(env, from, ids.path, fromPath) {
duke@0 355 WITH_FIELD_PLATFORM_STRING(env, to, ids.path, toPath) {
duke@0 356 if (rename(fromPath, toPath) == 0) {
duke@0 357 rv = JNI_TRUE;
duke@0 358 }
duke@0 359 } END_PLATFORM_STRING(env, toPath);
duke@0 360 } END_PLATFORM_STRING(env, fromPath);
duke@0 361 return rv;
duke@0 362 }
duke@0 363
duke@0 364 JNIEXPORT jboolean JNICALL
duke@0 365 Java_java_io_UnixFileSystem_setLastModifiedTime(JNIEnv *env, jobject this,
duke@0 366 jobject file, jlong time)
duke@0 367 {
duke@0 368 jboolean rv = JNI_FALSE;
duke@0 369
duke@0 370 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@358 371 struct stat64 sb;
duke@0 372
alanb@358 373 if (stat64(path, &sb) == 0) {
alanb@358 374 struct timeval tv[2];
alanb@358 375
alanb@358 376 /* Preserve access time */
alanb@358 377 tv[0].tv_sec = sb.st_atime;
alanb@358 378 tv[0].tv_usec = 0;
alanb@358 379
alanb@358 380 /* Change last-modified time */
alanb@358 381 tv[1].tv_sec = time / 1000;
alanb@358 382 tv[1].tv_usec = (time % 1000) * 1000;
alanb@358 383
alanb@358 384 if (utimes(path, tv) == 0)
alanb@358 385 rv = JNI_TRUE;
duke@0 386 }
duke@0 387 } END_PLATFORM_STRING(env, path);
duke@0 388
duke@0 389 return rv;
duke@0 390 }
duke@0 391
duke@0 392
duke@0 393 JNIEXPORT jboolean JNICALL
duke@0 394 Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
duke@0 395 jobject file)
duke@0 396 {
duke@0 397 jboolean rv = JNI_FALSE;
duke@0 398
duke@0 399 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
duke@0 400 int mode;
duke@0 401 if (statMode(path, &mode)) {
duke@0 402 if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {
duke@0 403 rv = JNI_TRUE;
duke@0 404 }
duke@0 405 }
duke@0 406 } END_PLATFORM_STRING(env, path);
duke@0 407 return rv;
duke@0 408 }
duke@0 409
duke@0 410 JNIEXPORT jlong JNICALL
duke@0 411 Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
duke@0 412 jobject file, jint t)
duke@0 413 {
duke@0 414 jlong rv = 0L;
duke@0 415
duke@0 416 WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
alanb@2193 417 struct statvfs64 fsstat;
alanb@2193 418 memset(&fsstat, 0, sizeof(fsstat));
alanb@2193 419 if (statvfs64(path, &fsstat) == 0) {
duke@0 420 switch(t) {
duke@0 421 case java_io_FileSystem_SPACE_TOTAL:
duke@0 422 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 423 long_to_jlong(fsstat.f_blocks));
duke@0 424 break;
duke@0 425 case java_io_FileSystem_SPACE_FREE:
duke@0 426 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 427 long_to_jlong(fsstat.f_bfree));
duke@0 428 break;
duke@0 429 case java_io_FileSystem_SPACE_USABLE:
duke@0 430 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
duke@0 431 long_to_jlong(fsstat.f_bavail));
duke@0 432 break;
duke@0 433 default:
duke@0 434 assert(0);
duke@0 435 }
duke@0 436 }
duke@0 437 } END_PLATFORM_STRING(env, path);
duke@0 438 return rv;
duke@0 439 }