1 // natFileDescriptor.cc - Native part of FileDescriptor class.
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
19 #include <sys/param.h>
22 #ifdef HAVE_SYS_IOCTL_H
23 #define BSD_COMP /* Get FIONREAD on Solaris2. */
24 #include <sys/ioctl.h>
27 // Pick up FIONREAD on Solaris 2.5.
28 #ifdef HAVE_SYS_FILIO_H
29 #include <sys/filio.h>
34 #include <java/io/FileDescriptor.h>
35 #include <java/io/SyncFailedException.h>
36 #include <java/io/IOException.h>
37 #include <java/io/InterruptedIOException.h>
38 #include <java/io/EOFException.h>
39 #include <java/lang/ArrayIndexOutOfBoundsException.h>
40 #include <java/lang/NullPointerException.h>
41 #include <java/lang/System.h>
42 #include <java/lang/String.h>
43 #include <java/lang/Thread.h>
44 #include <java/io/FileNotFoundException.h>
46 #define NO_FSYNC_MESSAGE "sync unsupported"
49 java::io::FileDescriptor::valid (void)
52 return ::fstat (fd, &sb) == 0;
56 java::io::FileDescriptor::sync (void)
58 // Some files don't support fsync. We don't bother reporting these
61 if (::fsync (fd) && errno != EROFS && errno != EINVAL)
62 throw new SyncFailedException (JvNewStringLatin1 (strerror (errno)));
64 throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
69 java::io::FileDescriptor::open (jstring path, jint jflags)
71 char *buf = (char *) _Jv_AllocBytes (_Jv_GetStringUTFLength (path) + 1);
72 jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
79 JvAssert ((jflags & READ) || (jflags & WRITE));
81 if ((jflags & READ) && (jflags & WRITE))
82 flags |= O_RDWR | O_CREAT;
83 else if ((jflags & READ))
87 flags |= O_WRONLY | O_CREAT;
88 if ((jflags & APPEND))
96 // In this case we are making a temp file.
101 int fd = ::open (buf, flags, mode);
102 if (fd == -1 && errno == EMFILE)
104 // Because finalize () calls close () we might be able to continue.
105 java::lang::System::gc ();
106 java::lang::System::runFinalization ();
107 fd = ::open (buf, flags, mode);
111 char msg[MAXPATHLEN + 200];
112 // We choose the formatting here for JDK compatibility, believe
114 sprintf (msg, "%s (%s)", buf, strerror (errno));
115 throw new FileNotFoundException (JvNewStringLatin1 (msg));
121 java::io::FileDescriptor::write (jint b)
127 r = ::write (fd, &d, 1);
128 if (java::lang::Thread::interrupted())
130 InterruptedIOException *iioe
131 = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
132 iioe->bytesTransferred = r == -1 ? 0 : r;
136 throw new IOException (JvNewStringLatin1 (strerror (errno)));
141 java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
144 throw new java::lang::NullPointerException;
145 if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
146 throw new java::lang::ArrayIndexOutOfBoundsException;
147 jbyte *bytes = elements (b) + offset;
152 int r = ::write (fd, bytes, len);
155 if (java::lang::Thread::interrupted())
157 InterruptedIOException *iioe
158 = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
159 iioe->bytesTransferred = written;
163 throw new IOException (JvNewStringLatin1 (strerror (errno)));
171 java::io::FileDescriptor::close (void)
176 throw new IOException (JvNewStringLatin1 (strerror (errno)));
180 java::io::FileDescriptor::seek (jlong pos, jint whence)
182 JvAssert (whence == SET || whence == CUR);
184 jlong len = length ();
185 jlong here = getFilePointer ();
187 if ((whence == SET && pos > len) || (whence == CUR && here + pos > len))
188 throw new EOFException;
190 off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
192 throw new IOException (JvNewStringLatin1 (strerror (errno)));
197 java::io::FileDescriptor::length (void)
200 if (::fstat (fd, &sb))
201 throw new IOException (JvNewStringLatin1 (strerror (errno)));
206 java::io::FileDescriptor::getFilePointer (void)
208 off_t r = ::lseek (fd, 0, SEEK_CUR);
210 throw new IOException (JvNewStringLatin1 (strerror (errno)));
215 java::io::FileDescriptor::read (void)
218 int r = ::read (fd, &b, 1);
221 if (java::lang::Thread::interrupted())
223 InterruptedIOException *iioe
224 = new InterruptedIOException (JvNewStringLatin1 ("read interrupted"));
225 iioe->bytesTransferred = r == -1 ? 0 : r;
229 throw new IOException (JvNewStringLatin1 (strerror (errno)));
234 java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
237 throw new java::lang::NullPointerException;
238 jsize bsize = JvGetArrayLength (buffer);
239 if (offset < 0 || count < 0 || offset + count > bsize)
240 throw new java::lang::ArrayIndexOutOfBoundsException;
241 jbyte *bytes = elements (buffer) + offset;
242 int r = ::read (fd, bytes, count);
245 if (java::lang::Thread::interrupted())
247 InterruptedIOException *iioe
248 = new InterruptedIOException (JvNewStringLatin1 ("read interrupted"));
249 iioe->bytesTransferred = r == -1 ? 0 : r;
253 throw new IOException (JvNewStringLatin1 (strerror (errno)));
258 java::io::FileDescriptor::available (void)
260 #if defined (FIONREAD) || defined (HAVE_SELECT) || defined (HAVE_FSTAT)
263 bool num_set = false;
265 #if defined (FIONREAD)
266 r = ::ioctl (fd, FIONREAD, &num);
267 if (r == -1 && errno == ENOTTY)
269 // If the ioctl doesn't work, we don't care.
275 #elif defined (HAVE_SELECT)
286 throw new IOException (JvNewStringLatin1 (strerror (errno)));
289 // If we didn't get anything, and we have fstat, then see if see if
290 // we're reading a regular file. On many systems, FIONREAD does not
291 // work on regular files; select() likewise returns a useless
292 // result. This is run incorrectly when FIONREAD does work on
293 // regular files and we are at the end of the file. However, this
294 // case probably isn't very important.
295 #if defined (HAVE_FSTAT)
300 if (fstat (fd, &sb) != -1
301 && S_ISREG (sb.st_mode)
302 && (where = lseek (fd, SEEK_CUR, 0)) != (off_t) -1)
304 num = (long) (sb.st_size - where);
308 #endif /* HAVE_FSTAT */
310 #if defined (HAVE_SELECT)
319 r = _Jv_select (fd + 1, &rd, NULL, NULL, &tv);
322 num = r == 0 ? 0 : 1;
324 #endif /* HAVE_SELECT */
328 throw new IOException (JvNewStringLatin1 ("unimplemented"));