Path: blob/master/src/java.prefs/unix/native/libprefs/FileSystemPreferences.c
41149 views
/*1* Copyright (c) 2001, 2018, 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"36#include "java_util_prefs_FileSystemPreferences.h"3738/*39* Declare library specific JNI_Onload entry if static build40*/41DEF_STATIC_JNI_OnLoad4243JNIEXPORT jint JNICALL44Java_java_util_prefs_FileSystemPreferences_chmod(JNIEnv *env,45jclass thisclass, jstring java_fname, jint permission) {46const char *fname = JNU_GetStringPlatformChars(env, java_fname, NULL);47int result = -1;48if (fname) {49result = chmod(fname, permission);50if (result != 0)51result = errno;52JNU_ReleaseStringPlatformChars(env, java_fname, fname);53}54return (jint) result;55}5657#if defined(_ALLBSD_SOURCE)58typedef struct flock FLOCK;59#else60typedef struct flock64 FLOCK;61#endif6263/**64* Try to open a named lock file.65* The result is a cookie that can be used later to unlock the file.66* On failure the result is zero.67*/68JNIEXPORT jintArray JNICALL69Java_java_util_prefs_FileSystemPreferences_lockFile0(JNIEnv *env,70jclass thisclass, jstring java_fname, jint permission, jboolean shared) {71const char *fname = JNU_GetStringPlatformChars(env, java_fname, NULL);72int fd, rc;73int result[2];74jintArray javaResult = NULL;75int old_umask;76FLOCK fl;7778if (!fname)79return javaResult;8081fl.l_whence = SEEK_SET;82fl.l_len = 0;83fl.l_start = 0;84if (shared == JNI_TRUE) {85fl.l_type = F_RDLCK;86} else {87fl.l_type = F_WRLCK;88}8990if (shared == JNI_TRUE) {91fd = open(fname, O_RDONLY, 0);92} else {93old_umask = umask(0);94fd = open(fname, O_WRONLY|O_CREAT, permission);95result[1] = errno;96umask(old_umask);97}9899if (fd < 0) {100result[0] = 0;101} else {102#if defined(_ALLBSD_SOURCE)103rc = fcntl(fd, F_SETLK, &fl);104#else105rc = fcntl(fd, F_SETLK64, &fl);106#endif107result[1] = errno;108if (rc < 0) {109result[0]= 0;110close(fd);111} else {112result[0] = fd;113}114}115JNU_ReleaseStringPlatformChars(env, java_fname, fname);116javaResult = (*env)->NewIntArray(env,2);117if (javaResult)118(*env)->SetIntArrayRegion(env, javaResult, 0, 2, result);119return javaResult;120}121122123/**124* Try to unlock a lock file, using a cookie returned by lockFile.125*/126JNIEXPORT jint JNICALL127Java_java_util_prefs_FileSystemPreferences_unlockFile0(JNIEnv *env,128jclass thisclass, jint fd) {129130int rc;131FLOCK fl;132fl.l_whence = SEEK_SET;133fl.l_len = 0;134fl.l_start = 0;135fl.l_type = F_UNLCK;136137#if defined(_ALLBSD_SOURCE)138rc = fcntl(fd, F_SETLK, &fl);139#else140rc = fcntl(fd, F_SETLK64, &fl);141#endif142143if (rc < 0) {144close(fd);145return (jint)errno;146}147rc = close(fd);148if (rc < 0) {149return (jint) errno;150}151return 0;152}153154155