blob: d1e2cbf0dad9645b1706bf4c5205aceaa79543ed [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * Solaris/Linux platform specific code to support the Prefs API.
28 */
29
30#include <unistd.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <utime.h>
36#include "jni_util.h"
37
38JNIEXPORT jint JNICALL
39Java_java_util_prefs_FileSystemPreferences_chmod(JNIEnv *env,
40 jclass thisclass, jstring java_fname, jint permission) {
41 const char *fname = JNU_GetStringPlatformChars(env, java_fname, JNI_FALSE);
42 int result;
43 result = chmod(fname, permission);
44 if (result != 0)
45 result = errno;
46 JNU_ReleaseStringPlatformChars(env, java_fname, fname);
47 return (jint) result;
48}
49
50
51typedef struct flock64 FLOCK;
52
53/**
54 * Try to open a named lock file.
55 * The result is a cookie that can be used later to unlock the file.
56 * On failure the result is zero.
57 */
58JNIEXPORT jintArray JNICALL
59Java_java_util_prefs_FileSystemPreferences_lockFile0(JNIEnv *env,
60 jclass thisclass, jstring java_fname, jint permission, jboolean shared) {
61 const char *fname = JNU_GetStringPlatformChars(env, java_fname, JNI_FALSE);
62 int fd, rc;
63 int result[2];
64 jintArray javaResult;
65 int old_umask;
66 FLOCK fl;
67
68 fl.l_whence = SEEK_SET;
69 fl.l_len = 0;
70 fl.l_start = 0;
71 if (shared == JNI_TRUE) {
72 fl.l_type = F_RDLCK;
73 } else {
74 fl.l_type = F_WRLCK;
75 }
76
77 if (shared == JNI_TRUE) {
78 fd = open(fname, O_RDONLY, 0);
79 } else {
80 old_umask = umask(0);
81 fd = open(fname, O_WRONLY|O_CREAT, permission);
82 result[1] = errno;
83 umask(old_umask);
84 }
85
86 if (fd < 0) {
87 result[0] = 0;
88 } else {
89 rc = fcntl(fd, F_SETLK64, &fl);
90 result[1] = errno;
91 if (rc < 0) {
92 result[0]= 0;
93 close(fd);
94 } else {
95 result[0] = fd;
96 }
97 }
98 JNU_ReleaseStringPlatformChars(env, java_fname, fname);
99 javaResult = (*env)->NewIntArray(env,2);
100 (*env)->SetIntArrayRegion(env, javaResult, 0, 2, result);
101 return javaResult;
102}
103
104
105/**
106 * Try to unlock a lock file, using a cookie returned by lockFile.
107 */
108JNIEXPORT jint JNICALL
109Java_java_util_prefs_FileSystemPreferences_unlockFile0(JNIEnv *env,
110 jclass thisclass, jint fd) {
111
112 int rc;
113 FLOCK fl;
114 fl.l_whence = SEEK_SET;
115 fl.l_len = 0;
116 fl.l_start = 0;
117 fl.l_type = F_UNLCK;
118
119 rc = fcntl(fd, F_SETLK64, &fl);
120
121 if (rc < 0) {
122 close(fd);
123 return (jint)errno;
124 }
125 rc = close(fd);
126 if (rc < 0) {
127 return (jint) errno;
128 }
129 return 0;
130}