< prev index next >

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

Print this page
rev 54131 : imported patch 6307456-UnixFileSystem_md-c-use-of-chmod-and-access-should-handle-EINTR-signal-appropriately-unix
   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


 151 
 152 JNIEXPORT jboolean JNICALL
 153 Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this,
 154                                         jobject file, jint a)
 155 {
 156     jboolean rv = JNI_FALSE;
 157     int mode = 0;
 158     switch (a) {
 159     case java_io_FileSystem_ACCESS_READ:
 160         mode = R_OK;
 161         break;
 162     case java_io_FileSystem_ACCESS_WRITE:
 163         mode = W_OK;
 164         break;
 165     case java_io_FileSystem_ACCESS_EXECUTE:
 166         mode = X_OK;
 167         break;
 168     default: assert(0);
 169     }
 170     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 171         if (access(path, mode) == 0) {


 172             rv = JNI_TRUE;
 173         }
 174     } END_PLATFORM_STRING(env, path);
 175     return rv;
 176 }
 177 
 178 
 179 JNIEXPORT jboolean JNICALL
 180 Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this,
 181                                           jobject file,
 182                                           jint access,
 183                                           jboolean enable,
 184                                           jboolean owneronly)
 185 {
 186     jboolean rv = JNI_FALSE;
 187 
 188     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 189         int amode = 0;
 190         int mode;

 191         switch (access) {
 192         case java_io_FileSystem_ACCESS_READ:
 193             if (owneronly)
 194                 amode = S_IRUSR;
 195             else
 196                 amode = S_IRUSR | S_IRGRP | S_IROTH;
 197             break;
 198         case java_io_FileSystem_ACCESS_WRITE:
 199             if (owneronly)
 200                 amode = S_IWUSR;
 201             else
 202                 amode = S_IWUSR | S_IWGRP | S_IWOTH;
 203             break;
 204         case java_io_FileSystem_ACCESS_EXECUTE:
 205             if (owneronly)
 206                 amode = S_IXUSR;
 207             else
 208                 amode = S_IXUSR | S_IXGRP | S_IXOTH;
 209             break;
 210         default:
 211             assert(0);
 212         }
 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)


 338             continue;
 339         if (len == maxlen) {
 340             old = rv;
 341             rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
 342             if (rv == NULL) goto error;
 343             if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
 344             (*env)->DeleteLocalRef(env, old);
 345         }
 346 #ifdef MACOSX
 347         name = newStringPlatform(env, ptr->d_name);
 348 #else
 349         name = JNU_NewStringPlatform(env, ptr->d_name);
 350 #endif
 351         if (name == NULL) goto error;
 352         (*env)->SetObjectArrayElement(env, rv, len++, name);
 353         (*env)->DeleteLocalRef(env, name);
 354     }
 355     closedir(dir);
 356 
 357     /* Copy the final results into an appropriately-sized array */

 358     old = rv;
 359     rv = (*env)->NewObjectArray(env, len, str_class, NULL);
 360     if (rv == NULL) {
 361         return NULL;
 362     }
 363     if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
 364         return NULL;
 365     }

 366     return rv;
 367 
 368  error:
 369     closedir(dir);
 370     return NULL;
 371 }
 372 
 373 
 374 JNIEXPORT jboolean JNICALL
 375 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
 376                                             jobject file)
 377 {
 378     jboolean rv = JNI_FALSE;
 379 
 380     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 381         if (mkdir(path, 0777) == 0) {
 382             rv = JNI_TRUE;
 383         }
 384     } END_PLATFORM_STRING(env, path);
 385     return rv;


 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         }
 435     } END_PLATFORM_STRING(env, path);
 436 
 437     return rv;
 438 }
 439 
 440 
 441 JNIEXPORT jboolean JNICALL
 442 Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
 443                                         jobject file)
 444 {
 445     jboolean rv = JNI_FALSE;
 446 
 447     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 448         int mode;

 449         if (statMode(path, &mode)) {
 450             if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {

 451                 rv = JNI_TRUE;
 452             }
 453         }
 454     } END_PLATFORM_STRING(env, path);
 455     return rv;
 456 }
 457 
 458 JNIEXPORT jlong JNICALL
 459 Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
 460                                      jobject file, jint t)
 461 {
 462     jlong rv = 0L;
 463 
 464     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 465 #ifdef MACOSX
 466         struct statfs fsstat;
 467 #else
 468         struct statvfs64 fsstat;

 469 #endif
 470         memset(&fsstat, 0, sizeof(fsstat));
 471 #ifdef MACOSX
 472         if (statfs(path, &fsstat) == 0) {
 473             switch(t) {
 474                 case java_io_FileSystem_SPACE_TOTAL:
 475                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 476                                    long_to_jlong(fsstat.f_blocks));
 477                     break;
 478                 case java_io_FileSystem_SPACE_FREE:
 479                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 480                                    long_to_jlong(fsstat.f_bfree));
 481                     break;
 482                 case java_io_FileSystem_SPACE_USABLE:
 483                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 484                                    long_to_jlong(fsstat.f_bavail));
 485                     break;
 486                 default:
 487                     assert(0);
 488             }
 489         }
 490 #else
 491         if (statvfs64(path, &fsstat) == 0) {

 492             switch(t) {
 493             case java_io_FileSystem_SPACE_TOTAL:
 494                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 495                                long_to_jlong(fsstat.f_blocks));
 496                 break;
 497             case java_io_FileSystem_SPACE_FREE:
 498                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 499                                long_to_jlong(fsstat.f_bfree));
 500                 break;
 501             case java_io_FileSystem_SPACE_USABLE:
 502                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 503                                long_to_jlong(fsstat.f_bavail));
 504                 break;
 505             default:
 506                 assert(0);
 507             }
 508         }
 509 #endif
 510     } END_PLATFORM_STRING(env, path);
 511     return rv;
   1 /*
   2  * Copyright (c) 1998, 2019, 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


 151 
 152 JNIEXPORT jboolean JNICALL
 153 Java_java_io_UnixFileSystem_checkAccess(JNIEnv *env, jobject this,
 154                                         jobject file, jint a)
 155 {
 156     jboolean rv = JNI_FALSE;
 157     int mode = 0;
 158     switch (a) {
 159     case java_io_FileSystem_ACCESS_READ:
 160         mode = R_OK;
 161         break;
 162     case java_io_FileSystem_ACCESS_WRITE:
 163         mode = W_OK;
 164         break;
 165     case java_io_FileSystem_ACCESS_EXECUTE:
 166         mode = X_OK;
 167         break;
 168     default: assert(0);
 169     }
 170     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 171         int res;
 172         RESTARTABLE(access(path, mode), res);
 173         if (res == 0) {
 174             rv = JNI_TRUE;
 175         }
 176     } END_PLATFORM_STRING(env, path);
 177     return rv;
 178 }
 179 
 180 
 181 JNIEXPORT jboolean JNICALL
 182 Java_java_io_UnixFileSystem_setPermission(JNIEnv *env, jobject this,
 183                                           jobject file,
 184                                           jint access,
 185                                           jboolean enable,
 186                                           jboolean owneronly)
 187 {
 188     jboolean rv = JNI_FALSE;
 189 
 190     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 191         int amode = 0;
 192         int mode;
 193         int res;
 194         switch (access) {
 195         case java_io_FileSystem_ACCESS_READ:
 196             if (owneronly)
 197                 amode = S_IRUSR;
 198             else
 199                 amode = S_IRUSR | S_IRGRP | S_IROTH;
 200             break;
 201         case java_io_FileSystem_ACCESS_WRITE:
 202             if (owneronly)
 203                 amode = S_IWUSR;
 204             else
 205                 amode = S_IWUSR | S_IWGRP | S_IWOTH;
 206             break;
 207         case java_io_FileSystem_ACCESS_EXECUTE:
 208             if (owneronly)
 209                 amode = S_IXUSR;
 210             else
 211                 amode = S_IXUSR | S_IXGRP | S_IXOTH;
 212             break;
 213         default:
 214             assert(0);
 215         }
 216         if (statMode(path, &mode)) {
 217             if (enable)
 218                 mode |= amode;
 219             else
 220                 mode &= ~amode;
 221             RESTARTABLE(chmod(path, mode), res);
 222             if (res == 0) {
 223                 rv = JNI_TRUE;
 224             }
 225         }
 226     } END_PLATFORM_STRING(env, path);
 227     return rv;
 228 }
 229 
 230 JNIEXPORT jlong JNICALL
 231 Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
 232                                                 jobject file)
 233 {
 234     jlong rv = 0;
 235 
 236     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 237         struct stat64 sb;
 238         if (stat64(path, &sb) == 0) {
 239 #if defined(_AIX)
 240             rv =  (jlong)sb.st_mtime * 1000;
 241             rv += (jlong)sb.st_mtime_n / 1000000;
 242 #elif defined(MACOSX)


 342             continue;
 343         if (len == maxlen) {
 344             old = rv;
 345             rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
 346             if (rv == NULL) goto error;
 347             if (JNU_CopyObjectArray(env, rv, old, len) < 0) goto error;
 348             (*env)->DeleteLocalRef(env, old);
 349         }
 350 #ifdef MACOSX
 351         name = newStringPlatform(env, ptr->d_name);
 352 #else
 353         name = JNU_NewStringPlatform(env, ptr->d_name);
 354 #endif
 355         if (name == NULL) goto error;
 356         (*env)->SetObjectArrayElement(env, rv, len++, name);
 357         (*env)->DeleteLocalRef(env, name);
 358     }
 359     closedir(dir);
 360 
 361     /* Copy the final results into an appropriately-sized array */
 362     if (len < maxlen) {
 363         old = rv;
 364         rv = (*env)->NewObjectArray(env, len, str_class, NULL);
 365         if (rv == NULL) {
 366             return NULL;
 367         }
 368         if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
 369             return NULL;
 370         }
 371     }
 372     return rv;
 373 
 374  error:
 375     closedir(dir);
 376     return NULL;
 377 }
 378 
 379 
 380 JNIEXPORT jboolean JNICALL
 381 Java_java_io_UnixFileSystem_createDirectory(JNIEnv *env, jobject this,
 382                                             jobject file)
 383 {
 384     jboolean rv = JNI_FALSE;
 385 
 386     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 387         if (mkdir(path, 0777) == 0) {
 388             rv = JNI_TRUE;
 389         }
 390     } END_PLATFORM_STRING(env, path);
 391     return rv;


 435             tv[1].tv_sec = time / 1000;
 436             tv[1].tv_usec = (time % 1000) * 1000;
 437 
 438             if (utimes(path, tv) == 0)
 439                 rv = JNI_TRUE;
 440         }
 441     } END_PLATFORM_STRING(env, path);
 442 
 443     return rv;
 444 }
 445 
 446 
 447 JNIEXPORT jboolean JNICALL
 448 Java_java_io_UnixFileSystem_setReadOnly(JNIEnv *env, jobject this,
 449                                         jobject file)
 450 {
 451     jboolean rv = JNI_FALSE;
 452 
 453     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 454         int mode;
 455         int res;
 456         if (statMode(path, &mode)) {
 457             RESTARTABLE(chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)), res);
 458             if (res == 0) {
 459                 rv = JNI_TRUE;
 460             }
 461         }
 462     } END_PLATFORM_STRING(env, path);
 463     return rv;
 464 }
 465 
 466 JNIEXPORT jlong JNICALL
 467 Java_java_io_UnixFileSystem_getSpace(JNIEnv *env, jobject this,
 468                                      jobject file, jint t)
 469 {
 470     jlong rv = 0L;
 471 
 472     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
 473 #ifdef MACOSX
 474         struct statfs fsstat;
 475 #else
 476         struct statvfs64 fsstat;
 477         int res;
 478 #endif
 479         memset(&fsstat, 0, sizeof(fsstat));
 480 #ifdef MACOSX
 481         if (statfs(path, &fsstat) == 0) {
 482             switch(t) {
 483                 case java_io_FileSystem_SPACE_TOTAL:
 484                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 485                                    long_to_jlong(fsstat.f_blocks));
 486                     break;
 487                 case java_io_FileSystem_SPACE_FREE:
 488                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 489                                    long_to_jlong(fsstat.f_bfree));
 490                     break;
 491                 case java_io_FileSystem_SPACE_USABLE:
 492                     rv = jlong_mul(long_to_jlong(fsstat.f_bsize),
 493                                    long_to_jlong(fsstat.f_bavail));
 494                     break;
 495                 default:
 496                     assert(0);
 497             }
 498         }
 499 #else
 500         RESTARTABLE(statvfs64(path, &fsstat), res);
 501         if (res == 0) {
 502             switch(t) {
 503             case java_io_FileSystem_SPACE_TOTAL:
 504                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 505                                long_to_jlong(fsstat.f_blocks));
 506                 break;
 507             case java_io_FileSystem_SPACE_FREE:
 508                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 509                                long_to_jlong(fsstat.f_bfree));
 510                 break;
 511             case java_io_FileSystem_SPACE_USABLE:
 512                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
 513                                long_to_jlong(fsstat.f_bavail));
 514                 break;
 515             default:
 516                 assert(0);
 517             }
 518         }
 519 #endif
 520     } END_PLATFORM_STRING(env, path);
 521     return rv;
< prev index next >