Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/util/FileSystemPreferences.c
32287 views
/*1* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Solaris/Linux platform specific code to support the Prefs API.27*/2829#include <unistd.h>30#include <sys/types.h>31#include <sys/stat.h>32#include <fcntl.h>33#include <errno.h>34#include <utime.h>35#include "jni_util.h"3637JNIEXPORT jint JNICALL38Java_java_util_prefs_FileSystemPreferences_chmod(JNIEnv *env,39jclass thisclass, jstring java_fname, jint permission) {40const char *fname = JNU_GetStringPlatformChars(env, java_fname, NULL);41int result = -1;42if (fname) {43result = chmod(fname, permission);44if (result != 0)45result = errno;46JNU_ReleaseStringPlatformChars(env, java_fname, fname);47}48return (jint) result;49}5051#if defined(_ALLBSD_SOURCE)52typedef struct flock FLOCK;53#else54typedef struct flock64 FLOCK;55#endif5657/**58* Try to open a named lock file.59* The result is a cookie that can be used later to unlock the file.60* On failure the result is zero.61*/62JNIEXPORT jintArray JNICALL63Java_java_util_prefs_FileSystemPreferences_lockFile0(JNIEnv *env,64jclass thisclass, jstring java_fname, jint permission, jboolean shared) {65const char *fname = JNU_GetStringPlatformChars(env, java_fname, NULL);66int fd, rc;67int result[2];68jintArray javaResult = NULL;69int old_umask;70FLOCK fl;7172if (!fname)73return javaResult;7475fl.l_whence = SEEK_SET;76fl.l_len = 0;77fl.l_start = 0;78if (shared == JNI_TRUE) {79fl.l_type = F_RDLCK;80} else {81fl.l_type = F_WRLCK;82}8384if (shared == JNI_TRUE) {85fd = open(fname, O_RDONLY, 0);86} else {87old_umask = umask(0);88fd = open(fname, O_WRONLY|O_CREAT, permission);89result[1] = errno;90umask(old_umask);91}9293if (fd < 0) {94result[0] = 0;95} else {96#if defined(_ALLBSD_SOURCE)97rc = fcntl(fd, F_SETLK, &fl);98#else99rc = fcntl(fd, F_SETLK64, &fl);100#endif101result[1] = errno;102if (rc < 0) {103result[0]= 0;104close(fd);105} else {106result[0] = fd;107}108}109JNU_ReleaseStringPlatformChars(env, java_fname, fname);110javaResult = (*env)->NewIntArray(env,2);111if (javaResult)112(*env)->SetIntArrayRegion(env, javaResult, 0, 2, result);113return javaResult;114}115116117/**118* Try to unlock a lock file, using a cookie returned by lockFile.119*/120JNIEXPORT jint JNICALL121Java_java_util_prefs_FileSystemPreferences_unlockFile0(JNIEnv *env,122jclass thisclass, jint fd) {123124int rc;125FLOCK fl;126fl.l_whence = SEEK_SET;127fl.l_len = 0;128fl.l_start = 0;129fl.l_type = F_UNLCK;130131#if defined(_ALLBSD_SOURCE)132rc = fcntl(fd, F_SETLK, &fl);133#else134rc = fcntl(fd, F_SETLK64, &fl);135#endif136137if (rc < 0) {138close(fd);139return (jint)errno;140}141rc = close(fd);142if (rc < 0) {143return (jint) errno;144}145return 0;146}147148149