natFileChannelPosix.cc (mapImpl): Extend file, when writing, if it is too short.
authorTom Tromey <tromey@redhat.com>
Tue, 3 Apr 2007 01:05:50 +0000 (01:05 +0000)
committerTom Tromey <tromey@gcc.gnu.org>
Tue, 3 Apr 2007 01:05:50 +0000 (01:05 +0000)
http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=205157
* gnu/java/nio/channels/natFileChannelPosix.cc (mapImpl): Extend
file, when writing, if it is too short.

From-SVN: r123447

libjava/ChangeLog
libjava/gnu/java/nio/channels/natFileChannelPosix.cc

index ca939837674ff3d444a612f9d3f676907089b50e..9602e296f1d157b0c040bea86c3ab243efcce2c9 100644 (file)
@@ -1,3 +1,9 @@
+2007-04-02  Tom Tromey  <tromey@redhat.com>
+
+       http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=205157
+       * gnu/java/nio/channels/natFileChannelPosix.cc (mapImpl): Extend
+       file, when writing, if it is too short.
+
 2007-04-02  Tom Tromey  <tromey@redhat.com>
 
        https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=233406
index 52caf828345c173b324ff3a129deb4904b7eeb71..e05904c5026fd02e8c36b4b287100e8d0fa72546 100644 (file)
@@ -1,7 +1,7 @@
 
 // natFileChannelImplPosix.cc - Native part of FileChannelImpl class.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -499,6 +499,18 @@ FileChannelImpl::mapImpl (jchar mmode, jlong position, jint size)
     {
       prot = PROT_READ|PROT_WRITE;
       flags = mmode == '+' ? MAP_SHARED : MAP_PRIVATE;
+
+      // If the file is too short, we must extend it.  While using
+      // ftruncate() to extend a file is not portable in general, it
+      // should work on all systems where you can mmap() a file.
+      struct stat st;
+      if (fstat (fd, &st) == -1)
+       throw new IOException (JvNewStringLatin1 (strerror (errno)));
+      if (position + size > st.st_size)
+       {
+         if (ftruncate (fd, position + size) == -1)
+           throw new IOException (JvNewStringLatin1 (strerror (errno)));
+       }
     }
   jint page_size = ::getpagesize();
   jint offset = position & ~(page_size-1);