This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: write(2) -vs- signals
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Subject: Patch: write(2) -vs- signals
- From: Tom Tromey <tromey at redhat dot com>
- Date: 31 May 2001 11:43:07 -0600
- Reply-To: tromey at redhat dot com
I'm checking this in on the trunk. It fixes a couple minor problems
in natFileDescriptorPosix.
The first problem is that we really ought to allocate a
correctly-sized buffer in open.
The second problem is that if the application installs a signal
handler that doesn't cause write(2) to restart, then we can end up
writing less data than expected. libgcj itself doesn't do this, but
it could happen if some application including libgcj does it.
2001-05-31 Tom Tromey <tromey@redhat.com>
* java/io/natFileDescriptorPosix.cc (open): Allocate buffer to
correct size.
(write): Loop until write completes. From Corey Minyard.
Tom
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.12
diff -u -r1.12 natFileDescriptorPosix.cc
--- natFileDescriptorPosix.cc 2001/03/26 07:05:32 1.12
+++ natFileDescriptorPosix.cc 2001/05/31 17:30:38
@@ -68,10 +68,8 @@
jint
java::io::FileDescriptor::open (jstring path, jint jflags)
{
- // FIXME: eww.
- char buf[MAXPATHLEN];
+ char *buf = (char *) _Jv_AllocBytes (_Jv_GetStringUTFLength (path) + 1);
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
- // FIXME?
buf[total] = '\0';
int flags = 0;
#ifdef O_BINARY
@@ -121,17 +119,20 @@
java::io::FileDescriptor::write (jint b)
{
jbyte d = (jbyte) b;
- int r = ::write (fd, &d, 1);
- if (java::lang::Thread::interrupted())
+ int r = 0;
+ while (r != 1)
{
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
+ r = ::write (fd, &d, 1);
+ if (java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe
+ = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = r == -1 ? 0 : r;
+ throw iioe;
+ }
+ else if (r == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
- else if (r == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- // FIXME: loop if r != 1.
}
void
@@ -142,17 +143,26 @@
if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
throw new java::lang::ArrayIndexOutOfBoundsException;
jbyte *bytes = elements (b) + offset;
- int r = ::write (fd, bytes, len);
- if (java::lang::Thread::interrupted())
+
+ int written = 0;
+ while (len > 0)
{
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
+ int r = ::write (fd, bytes, len);
+ if (r != -1)
+ written += r;
+ if (java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe
+ = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = written;
+ throw iioe;
+ }
+ else if (r == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+
+ len -= r;
+ bytes += r;
}
- else if (r == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- // FIXME: loop if r != len.
}
void