blob: eae4a5d8a9a87db95f930978cce50400dda30e7d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2007 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#include "jni.h"
27#include "jni_util.h"
28#include "jvm.h"
29#include "io_util.h"
30#include "io_util_md.h"
31#include <string.h>
32
33void
34fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
35{
36 WITH_PLATFORM_STRING(env, path, ps) {
37 FD fd;
38
39#ifdef __linux__
40 /* Remove trailing slashes, since the kernel won't */
41 char *p = (char *)ps + strlen(ps) - 1;
42 while ((p > ps) && (*p == '/'))
43 *p-- = '\0';
44#endif
45 fd = JVM_Open(ps, flags, 0666);
46 if (fd >= 0) {
47 SET_FD(this, fd, fid);
48 } else {
49 throwFileNotFoundException(env, path);
50 }
51 } END_PLATFORM_STRING(env, ps);
52}
53
54
55void
56fileClose(JNIEnv *env, jobject this, jfieldID fid)
57{
58 FD fd = GET_FD(this, fid);
59 if (fd == -1) {
60 return;
61 }
62
63 /* Set the fd to -1 before closing it so that the timing window
64 * of other threads using the wrong fd (closed but recycled fd,
65 * that gets re-opened with some other filename) is reduced.
66 * Practically the chance of its occurance is low, however, we are
67 * taking extra precaution over here.
68 */
69 SET_FD(this, -1, fid);
70
71 /*
72 * Don't close file descriptors 0, 1, or 2. If we close these stream
73 * then a subsequent file open or socket will use them. Instead we
74 * just redirect these file descriptors to /dev/null.
75 */
76 if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
77 int devnull = open("/dev/null", O_WRONLY);
78 if (devnull < 0) {
79 SET_FD(this, fd, fid); // restore fd
80 JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
81 } else {
82 dup2(devnull, fd);
83 close(devnull);
84 }
85 } else if (JVM_Close(fd) == -1) {
86 SET_FD(this, fd, fid); // restore fd
87 printf("JVM_Close returned -1\n");
88 JNU_ThrowIOExceptionWithLastError(env, "close failed");
89 }
90}