< prev index next >

src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c

Print this page

 448     }
 449     return fd;
 450 }
 451 
 452 JNIEXPORT void JNICALL
 453 Java_sun_nio_fs_UnixNativeDispatcher_close0(JNIEnv* env, jclass this, jint fd) {
 454     int res;
 455 
 456 #if defined(_AIX)
 457     /* AIX allows close to be restarted after EINTR */
 458     RESTARTABLE(close((int)fd), res);
 459 #else
 460     res = close((int)fd);
 461 #endif
 462     if (res == -1 && errno != EINTR) {
 463         throwUnixException(env, errno);
 464     }
 465 }
 466 
 467 JNIEXPORT jint JNICALL
 468 Java_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv* env, jclass this, jint fd,
 469     jlong address, jint nbytes)
 470 {
 471     ssize_t n;
 472     void* bufp = jlong_to_ptr(address);
 473     RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n);
 474     if (n == -1) {
 475         throwUnixException(env, errno);
 476     }
 477     return (jint)n;
 478 }
 479 
 480 JNIEXPORT jint JNICALL
 481 Java_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv* env, jclass this, jint fd,
 482     jlong address, jint nbytes)
 483 {
 484     ssize_t n;
 485     void* bufp = jlong_to_ptr(address);
 486     RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n);
 487     if (n == -1) {
 488         throwUnixException(env, errno);
 489     }
 490     return (jint)n;
 491 }
 492 
 493 /**
 494  * Copy stat64 members into sun.nio.fs.UnixFileAttributes
 495  */
 496 static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
 497     (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode);
 498     (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino);
 499     (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev);
 500     (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev);
 501     (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink);

 551     }
 552 }
 553 
 554 JNIEXPORT void JNICALL
 555 Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this,
 556     jlong pathAddress, jobject attrs)
 557 {
 558     int err;
 559     struct stat64 buf;
 560     const char* path = (const char*)jlong_to_ptr(pathAddress);
 561 
 562     RESTARTABLE(lstat64(path, &buf), err);
 563     if (err == -1) {
 564         throwUnixException(env, errno);
 565     } else {
 566         prepAttributes(env, &buf, attrs);
 567     }
 568 }
 569 
 570 JNIEXPORT void JNICALL
 571 Java_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv* env, jclass this, jint fd,
 572     jobject attrs)
 573 {
 574     int err;
 575     struct stat64 buf;
 576 
 577     RESTARTABLE(fstat64((int)fd, &buf), err);
 578     if (err == -1) {
 579         throwUnixException(env, errno);
 580     } else {
 581         prepAttributes(env, &buf, attrs);
 582     }
 583 }
 584 
 585 JNIEXPORT void JNICALL
 586 Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd,
 587     jlong pathAddress, jint flag, jobject attrs)
 588 {
 589     int err;
 590     struct stat64 buf;
 591     const char* path = (const char*)jlong_to_ptr(pathAddress);

 599         throwUnixException(env, errno);
 600     } else {
 601         prepAttributes(env, &buf, attrs);
 602     }
 603 }
 604 
 605 JNIEXPORT void JNICALL
 606 Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this,
 607     jlong pathAddress, jint mode)
 608 {
 609     int err;
 610     const char* path = (const char*)jlong_to_ptr(pathAddress);
 611 
 612     RESTARTABLE(chmod(path, (mode_t)mode), err);
 613     if (err == -1) {
 614         throwUnixException(env, errno);
 615     }
 616 }
 617 
 618 JNIEXPORT void JNICALL
 619 Java_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv* env, jclass this, jint filedes,
 620     jint mode)
 621 {
 622     int err;
 623 
 624     RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err);
 625     if (err == -1) {
 626         throwUnixException(env, errno);
 627     }
 628 }
 629 
 630 
 631 JNIEXPORT void JNICALL
 632 Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this,
 633     jlong pathAddress, jint uid, jint gid)
 634 {
 635     int err;
 636     const char* path = (const char*)jlong_to_ptr(pathAddress);
 637 
 638     RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err);
 639     if (err == -1) {
 640         throwUnixException(env, errno);
 641     }
 642 }
 643 
 644 JNIEXPORT void JNICALL
 645 Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid)
 646 {
 647     int err;
 648     const char* path = (const char*)jlong_to_ptr(pathAddress);
 649 
 650     RESTARTABLE(lchown(path, (uid_t)uid, (gid_t)gid), err);
 651     if (err == -1) {
 652         throwUnixException(env, errno);
 653     }
 654 }
 655 
 656 JNIEXPORT void JNICALL
 657 Java_sun_nio_fs_UnixNativeDispatcher_fchown(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid)
 658 {
 659     int err;
 660 
 661     RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err);
 662     if (err == -1) {
 663         throwUnixException(env, errno);
 664     }
 665 }
 666 
 667 JNIEXPORT void JNICALL
 668 Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this,
 669     jlong pathAddress, jlong accessTime, jlong modificationTime)
 670 {
 671     int err;
 672     struct timeval times[2];
 673     const char* path = (const char*)jlong_to_ptr(pathAddress);
 674 
 675     times[0].tv_sec = accessTime / 1000000;
 676     times[0].tv_usec = accessTime % 1000000;
 677 
 678     times[1].tv_sec = modificationTime / 1000000;
 679     times[1].tv_usec = modificationTime % 1000000;
 680 
 681     RESTARTABLE(utimes(path, &times[0]), err);
 682     if (err == -1) {
 683         throwUnixException(env, errno);
 684     }
 685 }
 686 
 687 JNIEXPORT void JNICALL
 688 Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint filedes,
 689     jlong accessTime, jlong modificationTime)
 690 {
 691     struct timeval times[2];
 692     int err = 0;
 693 
 694     times[0].tv_sec = accessTime / 1000000;
 695     times[0].tv_usec = accessTime % 1000000;
 696 
 697     times[1].tv_sec = modificationTime / 1000000;
 698     times[1].tv_usec = modificationTime % 1000000;
 699 
 700 #ifdef _ALLBSD_SOURCE
 701     RESTARTABLE(futimes(filedes, &times[0]), err);
 702 #else
 703     if (my_futimesat_func == NULL) {
 704         JNU_ThrowInternalError(env, "my_futimesat_func is NULL");
 705         return;
 706     }
 707     RESTARTABLE((*my_futimesat_func)(filedes, NULL, &times[0]), err);
 708 #endif
 709     if (err == -1) {
 710         throwUnixException(env, errno);
 711     }
 712 }
 713 
 714 JNIEXPORT void JNICALL
 715 Java_sun_nio_fs_UnixNativeDispatcher_futimens(JNIEnv* env, jclass this, jint filedes,
 716     jlong accessTime, jlong modificationTime)
 717 {
 718     struct timespec times[2];
 719     int err = 0;
 720 
 721     times[0].tv_sec = accessTime / 1000000000;
 722     times[0].tv_nsec = accessTime % 1000000000;
 723 
 724     times[1].tv_sec = modificationTime / 1000000000;
 725     times[1].tv_nsec = modificationTime % 1000000000;
 726 
 727     if (my_futimens_func == NULL) {
 728         JNU_ThrowInternalError(env, "my_futimens_func is NULL");
 729         return;
 730     }
 731     RESTARTABLE((*my_futimens_func)(filedes, &times[0]), err);
 732     if (err == -1) {
 733         throwUnixException(env, errno);
 734     }
 735 }

 787     }
 788 
 789     /* EINTR not listed as a possible error */
 790     dir = (*my_fdopendir_func)((int)dfd);
 791     if (dir == NULL) {
 792         throwUnixException(env, errno);
 793     }
 794     return ptr_to_jlong(dir);
 795 }
 796 
 797 JNIEXPORT void JNICALL
 798 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
 799     DIR* dirp = jlong_to_ptr(dir);
 800 
 801     if (closedir(dirp) == -1 && errno != EINTR) {
 802         throwUnixException(env, errno);
 803     }
 804 }
 805 
 806 JNIEXPORT jbyteArray JNICALL
 807 Java_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv* env, jclass this, jlong value) {
 808     DIR* dirp = jlong_to_ptr(value);
 809     struct dirent* ptr;
 810 
 811     errno = 0;
 812     ptr = readdir(dirp);
 813     if (ptr == NULL) {
 814         if (errno != 0) {
 815             throwUnixException(env, errno);
 816         }
 817         return NULL;
 818     } else {
 819         jsize len = strlen(ptr->d_name);
 820         jbyteArray bytes = (*env)->NewByteArray(env, len);
 821         if (bytes != NULL) {
 822             (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
 823         }
 824         return bytes;
 825     }
 826 }
 827 

 448     }
 449     return fd;
 450 }
 451 
 452 JNIEXPORT void JNICALL
 453 Java_sun_nio_fs_UnixNativeDispatcher_close0(JNIEnv* env, jclass this, jint fd) {
 454     int res;
 455 
 456 #if defined(_AIX)
 457     /* AIX allows close to be restarted after EINTR */
 458     RESTARTABLE(close((int)fd), res);
 459 #else
 460     res = close((int)fd);
 461 #endif
 462     if (res == -1 && errno != EINTR) {
 463         throwUnixException(env, errno);
 464     }
 465 }
 466 
 467 JNIEXPORT jint JNICALL
 468 Java_sun_nio_fs_UnixNativeDispatcher_read0(JNIEnv* env, jclass this, jint fd,
 469     jlong address, jint nbytes)
 470 {
 471     ssize_t n;
 472     void* bufp = jlong_to_ptr(address);
 473     RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n);
 474     if (n == -1) {
 475         throwUnixException(env, errno);
 476     }
 477     return (jint)n;
 478 }
 479 
 480 JNIEXPORT jint JNICALL
 481 Java_sun_nio_fs_UnixNativeDispatcher_write0(JNIEnv* env, jclass this, jint fd,
 482     jlong address, jint nbytes)
 483 {
 484     ssize_t n;
 485     void* bufp = jlong_to_ptr(address);
 486     RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n);
 487     if (n == -1) {
 488         throwUnixException(env, errno);
 489     }
 490     return (jint)n;
 491 }
 492 
 493 /**
 494  * Copy stat64 members into sun.nio.fs.UnixFileAttributes
 495  */
 496 static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
 497     (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode);
 498     (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino);
 499     (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev);
 500     (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev);
 501     (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink);

 551     }
 552 }
 553 
 554 JNIEXPORT void JNICALL
 555 Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this,
 556     jlong pathAddress, jobject attrs)
 557 {
 558     int err;
 559     struct stat64 buf;
 560     const char* path = (const char*)jlong_to_ptr(pathAddress);
 561 
 562     RESTARTABLE(lstat64(path, &buf), err);
 563     if (err == -1) {
 564         throwUnixException(env, errno);
 565     } else {
 566         prepAttributes(env, &buf, attrs);
 567     }
 568 }
 569 
 570 JNIEXPORT void JNICALL
 571 Java_sun_nio_fs_UnixNativeDispatcher_fstat0(JNIEnv* env, jclass this, jint fd,
 572     jobject attrs)
 573 {
 574     int err;
 575     struct stat64 buf;
 576 
 577     RESTARTABLE(fstat64((int)fd, &buf), err);
 578     if (err == -1) {
 579         throwUnixException(env, errno);
 580     } else {
 581         prepAttributes(env, &buf, attrs);
 582     }
 583 }
 584 
 585 JNIEXPORT void JNICALL
 586 Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd,
 587     jlong pathAddress, jint flag, jobject attrs)
 588 {
 589     int err;
 590     struct stat64 buf;
 591     const char* path = (const char*)jlong_to_ptr(pathAddress);

 599         throwUnixException(env, errno);
 600     } else {
 601         prepAttributes(env, &buf, attrs);
 602     }
 603 }
 604 
 605 JNIEXPORT void JNICALL
 606 Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this,
 607     jlong pathAddress, jint mode)
 608 {
 609     int err;
 610     const char* path = (const char*)jlong_to_ptr(pathAddress);
 611 
 612     RESTARTABLE(chmod(path, (mode_t)mode), err);
 613     if (err == -1) {
 614         throwUnixException(env, errno);
 615     }
 616 }
 617 
 618 JNIEXPORT void JNICALL
 619 Java_sun_nio_fs_UnixNativeDispatcher_fchmod0(JNIEnv* env, jclass this, jint filedes,
 620     jint mode)
 621 {
 622     int err;
 623 
 624     RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err);
 625     if (err == -1) {
 626         throwUnixException(env, errno);
 627     }
 628 }
 629 

 630 JNIEXPORT void JNICALL
 631 Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this,
 632     jlong pathAddress, jint uid, jint gid)
 633 {
 634     int err;
 635     const char* path = (const char*)jlong_to_ptr(pathAddress);
 636 
 637     RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err);
 638     if (err == -1) {
 639         throwUnixException(env, errno);
 640     }
 641 }
 642 
 643 JNIEXPORT void JNICALL
 644 Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid)
 645 {
 646     int err;
 647     const char* path = (const char*)jlong_to_ptr(pathAddress);
 648 
 649     RESTARTABLE(lchown(path, (uid_t)uid, (gid_t)gid), err);
 650     if (err == -1) {
 651         throwUnixException(env, errno);
 652     }
 653 }
 654 
 655 JNIEXPORT void JNICALL
 656 Java_sun_nio_fs_UnixNativeDispatcher_fchown0(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid)
 657 {
 658     int err;
 659 
 660     RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err);
 661     if (err == -1) {
 662         throwUnixException(env, errno);
 663     }
 664 }
 665 
 666 JNIEXPORT void JNICALL
 667 Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this,
 668     jlong pathAddress, jlong accessTime, jlong modificationTime)
 669 {
 670     int err;
 671     struct timeval times[2];
 672     const char* path = (const char*)jlong_to_ptr(pathAddress);
 673 
 674     times[0].tv_sec = accessTime / 1000000;
 675     times[0].tv_usec = accessTime % 1000000;
 676 
 677     times[1].tv_sec = modificationTime / 1000000;
 678     times[1].tv_usec = modificationTime % 1000000;
 679 
 680     RESTARTABLE(utimes(path, &times[0]), err);
 681     if (err == -1) {
 682         throwUnixException(env, errno);
 683     }
 684 }
 685 
 686 JNIEXPORT void JNICALL
 687 Java_sun_nio_fs_UnixNativeDispatcher_futimes0(JNIEnv* env, jclass this, jint filedes,
 688     jlong accessTime, jlong modificationTime)
 689 {
 690     struct timeval times[2];
 691     int err = 0;
 692 
 693     times[0].tv_sec = accessTime / 1000000;
 694     times[0].tv_usec = accessTime % 1000000;
 695 
 696     times[1].tv_sec = modificationTime / 1000000;
 697     times[1].tv_usec = modificationTime % 1000000;
 698 
 699 #ifdef _ALLBSD_SOURCE
 700     RESTARTABLE(futimes(filedes, &times[0]), err);
 701 #else
 702     if (my_futimesat_func == NULL) {
 703         JNU_ThrowInternalError(env, "my_futimesat_func is NULL");
 704         return;
 705     }
 706     RESTARTABLE((*my_futimesat_func)(filedes, NULL, &times[0]), err);
 707 #endif
 708     if (err == -1) {
 709         throwUnixException(env, errno);
 710     }
 711 }
 712 
 713 JNIEXPORT void JNICALL
 714 Java_sun_nio_fs_UnixNativeDispatcher_futimens0(JNIEnv* env, jclass this, jint filedes,
 715     jlong accessTime, jlong modificationTime)
 716 {
 717     struct timespec times[2];
 718     int err = 0;
 719 
 720     times[0].tv_sec = accessTime / 1000000000;
 721     times[0].tv_nsec = accessTime % 1000000000;
 722 
 723     times[1].tv_sec = modificationTime / 1000000000;
 724     times[1].tv_nsec = modificationTime % 1000000000;
 725 
 726     if (my_futimens_func == NULL) {
 727         JNU_ThrowInternalError(env, "my_futimens_func is NULL");
 728         return;
 729     }
 730     RESTARTABLE((*my_futimens_func)(filedes, &times[0]), err);
 731     if (err == -1) {
 732         throwUnixException(env, errno);
 733     }
 734 }

 786     }
 787 
 788     /* EINTR not listed as a possible error */
 789     dir = (*my_fdopendir_func)((int)dfd);
 790     if (dir == NULL) {
 791         throwUnixException(env, errno);
 792     }
 793     return ptr_to_jlong(dir);
 794 }
 795 
 796 JNIEXPORT void JNICALL
 797 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
 798     DIR* dirp = jlong_to_ptr(dir);
 799 
 800     if (closedir(dirp) == -1 && errno != EINTR) {
 801         throwUnixException(env, errno);
 802     }
 803 }
 804 
 805 JNIEXPORT jbyteArray JNICALL
 806 Java_sun_nio_fs_UnixNativeDispatcher_readdir0(JNIEnv* env, jclass this, jlong value) {
 807     DIR* dirp = jlong_to_ptr(value);
 808     struct dirent* ptr;
 809 
 810     errno = 0;
 811     ptr = readdir(dirp);
 812     if (ptr == NULL) {
 813         if (errno != 0) {
 814             throwUnixException(env, errno);
 815         }
 816         return NULL;
 817     } else {
 818         jsize len = strlen(ptr->d_name);
 819         jbyteArray bytes = (*env)->NewByteArray(env, len);
 820         if (bytes != NULL) {
 821             (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
 822         }
 823         return bytes;
 824     }
 825 }
 826 
< prev index next >