classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fwd: [cp-patches] [RFC, Concept proposal]: Easing "target" dependency]


From: Guilhem Lavaux
Subject: [Fwd: [cp-patches] [RFC, Concept proposal]: Easing "target" dependency]
Date: Wed, 30 Nov 2005 20:31:27 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)

Hi,

As I got no answer on classpath-patches I send this email to this list.

Regards,

Guilhem.
--- Begin Message --- Subject: [cp-patches] [RFC,Concept proposal]: Easing "target" dependency Date: Sat, 26 Nov 2005 14:50:03 +0100 User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)
Hi,

I would like to propose a code split to split the java interface from accessing syscalls in File IO (and generally for all native IO). Some VM may want (like us in kaffe) to change the way syscalls are called without touching the java interface logic. So I am proposing to keep the basic skeleton of the target layer but put the real code not in macro but in real C functions. That way we will be able to add autoconf macros without bothering the java interface and if some persons still want to use the TARGET layer it is possible by simply using the macro inside the C functions.

So here is a patch which shows what I want to do. An idealistic situation would be to put all these functions which are using syscalls into a libjavasyscalls which will be implemented by VM writers (and of course we will propose a default implementation).

This is not the definite patch. So don't complain about missing ChangeLog and so on ... I ask whether people agree on using that concept.

Cheers,

Guilhem.
Index: native/jni/java-nio/Makefile.am
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/java-nio/Makefile.am,v
retrieving revision 1.19
diff -u -r1.19 Makefile.am
--- native/jni/java-nio/Makefile.am     23 Oct 2005 16:59:08 -0000      1.19
+++ native/jni/java-nio/Makefile.am     26 Nov 2005 13:48:57 -0000
@@ -6,7 +6,9 @@
                        gnu_java_nio_charset_iconv_IconvDecoder.c \
                        gnu_java_nio_charset_iconv_IconvEncoder.c \
                        java_nio_MappedByteBufferImpl.c \
-                       java_nio_VMDirectByteBuffer.c
+                       java_nio_VMDirectByteBuffer.c \
+                       javafileio.c \
+                       javafileio.h
 
 libjavanio_la_LIBADD = $(top_builddir)/native/jni/classpath/jcl.lo \
                       $(LTLIBICONV) 
Index: native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c,v
retrieving revision 1.27
diff -u -r1.27 gnu_java_nio_channels_FileChannelImpl.c
--- native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 3 Aug 2005 
13:12:59 -0000       1.27
+++ native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 26 Nov 2005 
13:48:57 -0000
@@ -40,16 +40,15 @@
 
 #include <stdlib.h>
 #include <errno.h>
+#include <string.h>
 
-#include <jni.h>
-#include <jcl.h>
-
-#include "target_native.h"
-#ifndef WITHOUT_FILESYSTEM
-#include "target_native_file.h"
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
 #endif
-#include "target_native_math_int.h"
 
+#include <jni.h>
+#include <jcl.h>
+#include "javafileio.h"
 #include "gnu_java_nio_channels_FileChannelImpl.h"
 
 #ifdef HAVE_FCNTL_H
@@ -80,12 +79,12 @@
 #define IO_EXCEPTION "java/io/IOException"
 
 /* FIXME: This can't be right.  Need converter macros. */
-#define CONVERT_JLONG_TO_INT(x) TARGET_NATIVE_MATH_INT_INT64_TO_INT32(x)
-#define CONVERT_INT_TO_JLONG(x) TARGET_NATIVE_MATH_INT_INT32_TO_INT64(x)
+#define CONVERT_JLONG_TO_INT(x) ((int)(x))
+#define CONVERT_INT_TO_JLONG(x) ((jlong)(x))
 
 /* FIXME: This can't be right.  Need converter macros. */
-#define CONVERT_JLONG_TO_OFF_T(x) TARGET_NATIVE_MATH_INT_INT64_TO_INT32(x)
-#define CONVERT_OFF_T_TO_JLONG(x) TARGET_NATIVE_MATH_INT_INT32_TO_INT64(x)
+#define CONVERT_JLONG_TO_OFF_T(x) ((off_t)(x))
+#define CONVERT_OFF_T_TO_JLONG(x) ((jlong)(x))
 
 /* FIXME: This can't be right.  Need converter macros */
 #define CONVERT_JINT_TO_INT(x) ((int)(x & 0xFFFFFFFF))
@@ -162,53 +161,54 @@
       && (mode & FILECHANNELIMPL_FILEOPEN_FLAG_WRITE))
     {
       /* read/write */
-      flags =
-       TARGET_NATIVE_FILE_FILEFLAG_CREATE |
-       TARGET_NATIVE_FILE_FILEFLAG_READWRITE;
-      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;
+      flags = O_CREAT | O_RDWR;
+      permissions = FILEPERMISSION_NORMAL;
     }
   else if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_READ))
     {
       /* read */
-      flags = TARGET_NATIVE_FILE_FILEFLAG_READ;
-      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;
+      flags = O_RDONLY;
+      permissions = FILEPERMISSION_NORMAL;
     }
   else
     {
       /* write */
-      flags =
-       TARGET_NATIVE_FILE_FILEFLAG_CREATE |
-       TARGET_NATIVE_FILE_FILEFLAG_WRITE;
+      flags = O_CREAT | O_WRONLY;
       if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_APPEND))
        {
-         flags |= TARGET_NATIVE_FILE_FILEFLAG_APPEND;
+         flags |= O_APPEND;
        }
       else
        {
-         flags |= TARGET_NATIVE_FILE_FILEFLAG_TRUNCATE;
+         flags |= O_TRUNC;
        }
-      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;
+      permissions = FILEPERMISSION_NORMAL;
     }
 
   if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_SYNC))
     {
-      flags |= TARGET_NATIVE_FILE_FILEFLAG_SYNC;
+#if !defined(O_SYNC) && defined(O_FSYNC)
+      flags |= O_FSYNC;
+#else
+      flags |= O_SYNC;
+#endif
     }
 
   if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_DSYNC))
     {
-      flags |= TARGET_NATIVE_FILE_FILEFLAG_DSYNC;
+#if defined(O_DSYNC)
+      flags |= O_DSYNC;
+#endif
     }
 #ifdef O_BINARY
-  flags |= TARGET_NATIVE_FILE_FILEFLAG_BINARY;
+  flags |= O_BINARY;
 #endif
 
-  TARGET_NATIVE_FILE_OPEN (filename, native_fd, flags, permissions, result);
-
-  if (result != TARGET_NATIVE_OK)
+  result = javafileio_open (filename, &native_fd, flags, permissions);
+  if (result != 0)
     {
       char message[256]; /* Fixed size we don't need to malloc. */
-      char *error_string = TARGET_NATIVE_LAST_ERROR_STRING ();
+      char *error_string = strerror(result);
 
       snprintf(message, 256, "%s: %s", error_string, filename);
       /* We are only allowed to throw FileNotFoundException.  */
@@ -216,7 +216,7 @@
                          "java/io/FileNotFoundException",
                          message);
       JCL_free_cstring (env, name, filename);
-      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
+      return -1;
     }
 
   JCL_free_cstring (env, name, filename);
@@ -238,17 +238,15 @@
 
   do
     {
-      TARGET_NATIVE_FILE_CLOSE (native_fd, result);
-      if (result != TARGET_NATIVE_OK
-         && (TARGET_NATIVE_LAST_ERROR ()
-             != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = javafileio_close (native_fd);
+      if (result != 0 && result != EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             strerror (result));
          return;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != 0);
 }
 
 /*
@@ -267,20 +265,18 @@
 
   do
     {
-      TARGET_NATIVE_FILE_AVAILABLE (native_fd, bytes_available, result);
-      if (result != TARGET_NATIVE_OK
-         && (TARGET_NATIVE_LAST_ERROR ()
-             != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = javafileio_available (native_fd, &bytes_available);
+      if (result != 0 && result != EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             strerror(result));
          return 0;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != 0);
 
   /* FIXME NYI ??? why only jint and not jlong? */
-  return TARGET_NATIVE_MATH_INT_INT64_TO_INT32 (bytes_available);
+  return (jint) (bytes_available);
 }
 
 JNIEXPORT jlong JNICALL
@@ -292,12 +288,12 @@
 
   native_fd = get_native_fd (env, obj);
 
-  TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);
-  if (result != TARGET_NATIVE_OK)
+  result = javafileio_getSize (native_fd, &file_size);
+  if (result != 0)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
+                         strerror (result));
+      return -1;
     }
 
   return file_size;
@@ -317,12 +313,12 @@
 
   native_fd = get_native_fd (env, obj);
 
-  TARGET_NATIVE_FILE_TELL (native_fd, current_offset, result);
-  if (result != TARGET_NATIVE_OK)
+  result = javafileio_getPosition(native_fd, &current_offset);
+  if (result != 0)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
+                         strerror(result));
+      return -1;
     }
 
   return current_offset;
@@ -337,7 +333,6 @@
                                                 jlong offset)
 {
   int native_fd;
-  jlong new_offset;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -362,14 +357,11 @@
     }
 #endif /* 0 */
 
-  result = TARGET_NATIVE_ERROR;
-  new_offset = TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
-  TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd, offset, new_offset, result);
-
-  if (result != TARGET_NATIVE_OK)
+  result = javafileio_setPosition (native_fd, offset);
+  if (result != 0)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
+                         strerror (result));
     }
 }
 
@@ -383,10 +375,6 @@
                                                         jlong len)
 {
   int native_fd;
-  jlong file_size;
-  int bytes_written;
-  jlong save_offset, new_offset;
-  char data;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -412,98 +400,14 @@
     }
 #endif /* 0 */
 
-  /* get file size */
-  TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);
-  if (result != TARGET_NATIVE_OK)
-    {
-      JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      return;
-    }
 
-  /* Save off current position */
-  TARGET_NATIVE_FILE_TELL (native_fd, save_offset, result);
-  if (result != TARGET_NATIVE_OK)
-    {
+  result = javafileio_setSize (native_fd, len);
+  if (result != 0)
+    { 
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
+                         strerror (result));
       return;
     }
-
-  if (TARGET_NATIVE_MATH_INT_INT64_LT (file_size, len))
-    {
-      /* File is too short -- seek to one byte short of where we want,
-       * then write a byte */
-
-      /* move to position n-1 */
-      TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd,
-                                    TARGET_NATIVE_MATH_INT_INT64_SUB (len,
-                                                                      1),
-                                    new_offset, result);
-      if (result != TARGET_NATIVE_OK)
-       {
-         JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
-         return;
-       }
-
-      /* write a byte
-         Note: This will fail if we somehow get here in read only mode
-         * That shouldn't happen */
-      data = '\0';
-      TARGET_NATIVE_FILE_WRITE (native_fd, &data, 1, bytes_written, result);
-      if (result != TARGET_NATIVE_OK)
-       {
-         JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
-         return;
-       }
-
-      /* Reposition file pointer to where we started if not beyond new len. */
-      if (TARGET_NATIVE_MATH_INT_INT64_LT (save_offset, len))
-       {
-         TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd, save_offset,
-                                        new_offset, result);
-         if (result != TARGET_NATIVE_OK)
-           {
-             JCL_ThrowException (env, IO_EXCEPTION,
-                                 TARGET_NATIVE_LAST_ERROR_STRING ());
-             return;
-           }
-       }
-    }
-  else if (TARGET_NATIVE_MATH_INT_INT64_GT (file_size, len))
-    {
-      /* File is too long - use ftruncate if available */
-#ifdef HAVE_FTRUNCATE
-      TARGET_NATIVE_FILE_TRUNCATE (native_fd, len, result);
-      if (result != TARGET_NATIVE_OK)
-       {
-         JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
-         return;
-       }
-#else /* HAVE_FTRUNCATE */
-      /* FIXME: Probably operation isn't supported, but this exception
-       * is too harsh as it will probably crash the program without need
-       JCL_ThrowException(env, "java/lang/UnsupportedOperationException",
-       "not implemented - can't shorten files on this platform");
-       */
-      JCL_ThrowException (env, IO_EXCEPTION, "Unable to shorten file length");
-#endif /* HAVE_FTRUNCATE */
-
-      /* Reposition file pointer when it now is beyond the end of file. */
-      if (TARGET_NATIVE_MATH_INT_INT64_GT (save_offset, len))
-       {
-         TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd, len, new_offset, result);
-         if (result != TARGET_NATIVE_OK)
-           {
-             JCL_ThrowException (env, IO_EXCEPTION,
-                                 TARGET_NATIVE_LAST_ERROR_STRING ());
-             return;
-           }
-       }
-    }
 }
 
 JNIEXPORT jobject JNICALL
@@ -611,21 +515,17 @@
   bytes_read = 0;
   do
     {
-      TARGET_NATIVE_FILE_READ (native_fd, &data, 1, bytes_read, result);
-      if ((result == TARGET_NATIVE_OK) && (bytes_read == 0))
-       {
-         return (-1);
-       }
-      if ((result != TARGET_NATIVE_OK)
-         && (TARGET_NATIVE_LAST_ERROR () !=
-             TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = javafileio_read (native_fd, &data, 1, &bytes_read);
+      if (result == 0 && bytes_read == 0)
+       return -1;
+      if (result != 0 && result != EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
-         return (-1);
+                             strerror (result));
+         return -1;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != 0);
 
   return ((jint) (data & 0xFF));
 }
@@ -676,9 +576,9 @@
   bytes_read = 0;
   do
     {
-      TARGET_NATIVE_FILE_READ (native_fd, (bufptr + offset + bytes_read),
-                              (length - bytes_read), n, result);
-      if ((result == TARGET_NATIVE_OK) && (n == 0))
+      result = javafileio_read (native_fd, (bufptr + offset + bytes_read),
+                               (length - bytes_read), &n);
+      if ((result == 0) && (n == 0))
        {
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          if (bytes_read == 0)
@@ -686,16 +586,14 @@
          else
            return CONVERT_SSIZE_T_TO_JINT (bytes_read);
        }
-      if ((result != TARGET_NATIVE_OK)
-         && (TARGET_NATIVE_LAST_ERROR () !=
-             TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      if ((result != 0) && result != EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             strerror (result));
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          return -1;
        }
-      if (result == TARGET_NATIVE_OK)
+      if (result == 0)
        bytes_read += n;
     }
   while (bytes_read < 1);
@@ -722,18 +620,15 @@
 
   do
     {
-      TARGET_NATIVE_FILE_WRITE (native_fd, &native_data, 1, bytes_written,
-                               result);
-      if ((result != TARGET_NATIVE_OK)
-         && (TARGET_NATIVE_LAST_ERROR () !=
-             TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = javafileio_write (native_fd, &native_data, 1, &bytes_written);
+      if ((result != 0) && (result != EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             strerror (result));
          return;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != 0);
 }
 
 /*
@@ -745,11 +640,13 @@
 {
   int native_fd;
   int result;
+
   native_fd = get_native_fd (env, obj);
-  TARGET_NATIVE_FILE_FSYNC (native_fd, result);
-  if (result != TARGET_NATIVE_OK)
+
+  result = javafileio_fsync (native_fd);
+  if (result != 0)
     JCL_ThrowException (env, IO_EXCEPTION,
-                       TARGET_NATIVE_LAST_ERROR_STRING ());
+                       strerror (result));
 }
 
 /*
@@ -785,18 +682,16 @@
   bytes_written = 0;
   while (bytes_written < CONVERT_JINT_TO_SSIZE_T (length))
     {
-      TARGET_NATIVE_FILE_WRITE (native_fd, (bufptr + offset + bytes_written),
-                               (length - bytes_written), n, result);
-      if ((result != TARGET_NATIVE_OK)
-         && (TARGET_NATIVE_LAST_ERROR () !=
-             TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      result = javafileio_write (native_fd, (bufptr + offset + bytes_written),
+                                (length - bytes_written), &n);
+      if ((result != 0) && (result != EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             strerror (result));
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          return;
        }
-      if (result == TARGET_NATIVE_OK)
+      if (result == 0)
        bytes_written += n;
     }
 
Index: native/jni/java-nio/javafileio.c
===================================================================
RCS file: native/jni/java-nio/javafileio.c
diff -N native/jni/java-nio/javafileio.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ native/jni/java-nio/javafileio.c    26 Nov 2005 13:48:57 -0000
@@ -0,0 +1,284 @@
+/* javafileio.c - Common java file IO native functions
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+ 
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+/* do not move; needed here because of some macro definitions */
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <jni.h>
+
+#if defined(HAVE_SYS_IOCTL_H)
+#define BSD_COMP /* Get FIONREAD on Solaris2 */
+#include <sys/ioctl.h>
+#endif
+#if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
+#include <sys/filio.h>
+#endif
+
+#if defined(HAVE_SYS_STAT_H)
+#include <sys/stat.h>
+#endif
+
+#if defined(HAVE_FCNTL_H)
+#include <fcntl.h>
+#endif
+
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
+#endif
+
+#if defined(HAVE_SYS_SELECT_H)
+#include <sys/select.h>
+#endif
+
+#include "javafileio.h"
+
+
+int javafileio_open (const char *filename, int *fd, int flags, int permissions)
+{
+  *fd = open (filename, flags, permissions);
+
+  if (*fd < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_close (int fd)
+{
+
+  if (close (fd) < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_available (int fd, jlong *bytes_available)
+{
+#if defined (FIONREAD)
+  ssize_t n;
+
+  if (ioctl (fd, FIONREAD, (char *)&n) == 0)
+    return errno;
+
+  *bytes_available = n;
+  return 0;  
+#elif defined(HAVE_FSTAT)
+  struct stat statBuffer;
+  off_t n;
+  int result;
+
+  *bytes_available = 0
+  if ((fstat (fd, &statBuffer) == 0) && S_ISREG (statBuffer.st_mode))
+    {
+      n = lseek (fd, 0, SEEK_CUR);
+      if (n != -1) 
+       { 
+         *bytes_available = statBuffer.st_size - n; 
+         result = 0;
+       } 
+      else 
+       { 
+         result = errno;
+       } 
+    } 
+  else 
+    { 
+      result = errno;
+    } 
+  
+  return result;
+
+#elif defined(HAVE_SELECT)
+  fd_set filedescriptset;
+  struct timeval tv;
+  int result;
+
+  *bytes_available = 0;
+  
+  FD_ZERO (&filedescriptset);
+  FD_SET (fd,&filedescriptset);
+  memset (&tv, 0, sizeof(tv));
+
+  switch (select (fd+1, &filedescriptset, NULL, NULL, &timeval)) \
+    {
+    case -1: 
+      result=errno; 
+      break;
+    case  0:
+      *bytes_available = 0;
+      result = 0;
+      break;      
+    default: 
+      *bytes_available = 1;
+      result = 0;
+      break;
+    }
+#else
+  *bytes_available = 0;
+  result = ENOTSUP;
+#endif
+}
+
+int javafileio_getSize (int fd, jlong *size)
+{
+  struct stat statBuffer;
+
+  if (fstat(fd, &statBuffer) < 0)
+    return errno;
+  
+  *size = statBuffer.st_size;
+  return 0;
+}
+
+int javafileio_getPosition (int fd, jlong *offset)
+{
+  *offset = lseek (fd, 0, SEEK_CUR);
+  if (*offset < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_setPosition (int fd, jlong offset)
+{
+  if (lseek (fd, offset, SEEK_SET) < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_read (int fd, void *buffer, jlong length, ssize_t *bytes_read)
+{
+  *bytes_read = read (fd, buffer, length);
+  
+  if (*bytes_read < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_write (int fd, void *buffer, jlong length, ssize_t 
*bytes_written)
+{
+  *bytes_written = write (fd, buffer, length);
+  
+  if (*bytes_written < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_fsync (int fd)
+{
+  if (fsync (fd) < 0)
+    return errno;
+
+  return 0;
+}
+
+int javafileio_setSize (int native_fd, jlong len)
+{
+  int result;
+  jlong file_size;
+  jlong save_offset;
+  char data;
+  ssize_t bytes_written;
+
+  /* get file size */
+  result = javafileio_getSize (native_fd, &file_size);
+  if (result != 0)
+    return result;
+
+  /* Save off current position */
+  result = javafileio_getPosition (native_fd, &save_offset);
+  if (result != 0)
+    return result;
+
+  if (file_size < len)
+    {
+      /* File is too short -- seek to one byte short of where we want,
+       * then write a byte */
+
+      /* move to position n-1 */
+      result = javafileio_setPosition (native_fd, len-1);
+      if (result != 0)
+       return result;
+
+
+      /* write a byte
+         Note: This will fail if we somehow get here in read only mode
+         * That shouldn't happen */
+      data = '\0';
+      result = javafileio_write (native_fd, &data, 1, &bytes_written);
+      if (result != 0)
+       return result;
+
+      /* Reposition file pointer to where we started if not beyond new len. */
+      if (save_offset < len)
+       {
+         result = javafileio_setPosition (native_fd, save_offset);
+         if (result != 0)
+           return result;
+       }
+    }
+  else if (file_size > len)
+    {
+      /* File is too long - use ftruncate if available */
+#ifdef HAVE_FTRUNCATE
+      result = ftruncate (native_fd, len);
+      if (ftruncate(native_fd, len) < 0)
+         return errno;
+#else /* HAVE_FTRUNCATE */
+      return ENOTSUP;
+#endif /* HAVE_FTRUNCATE */
+
+      /* Reposition file pointer when it now is beyond the end of file. */
+      if (save_offset > len)
+       {
+         result = javafileio_setPosition (native_fd, len);
+         if (result != 0)
+           return result;
+       }
+    }
+
+  return 0;
+}
Index: native/jni/java-nio/javafileio.h
===================================================================
RCS file: native/jni/java-nio/javafileio.h
diff -N native/jni/java-nio/javafileio.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ native/jni/java-nio/javafileio.h    26 Nov 2005 13:48:57 -0000
@@ -0,0 +1,59 @@
+/* javafileio.h -
+   Copyright (C) 2005  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+#ifndef JAVAFILEIO_H_INCLUDED
+#define JAVAFILEIO_H_INCLUDED
+
+
+#include <stddef.h>
+#include <jni.h>
+
+#define FILEPERMISSION_NORMAL 0
+
+extern int javafileio_open (const char *filename, int *fd, int flags, int 
permissions);
+extern int javafileio_close (int fd);
+extern int javafileio_available (int fd, jlong *bytes_available);
+extern int javafileio_getSize (int fd, jlong *size);
+extern int javafileio_setSize (int fd, jlong size);
+extern int javafileio_getPosition (int fd, jlong *offset);
+extern int javafileio_setPosition (int fd, jlong offset);
+extern int javafileio_read (int fd, void *buffer, jlong length, ssize_t 
*bytes_read);
+extern int javafileio_write (int fd, void *buffer, jlong length, ssize_t 
*bytes_written);
+extern int javafileio_fsync (int fd);
+
+
+#endif
_______________________________________________
Classpath-patches mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/classpath-patches

--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]