classpath
[Top][All Lists]
Advanced

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

Replacing target layer (2)


From: Guilhem Lavaux
Subject: Replacing target layer (2)
Date: Fri, 06 Jan 2006 20:38:46 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)

Hi,

Ok. Here is an update of the previous patch. After some discussion that one should be compatible with AICAS' and kaffe's needs. I have slightly abstractized the syscall interfaces for file's IO. Now I provide an interface for doing jlong math and handling errors. For most architecture the definition will only be these dummy macro. However in some cases they may be useful (for compiler not supporting 64 bits for example). A few function has been left in the original file but they should be moved to the new interface too (mmap and lock).

This is the a preliminary patch as I am going now to "abstractize" the network layer.

Regards,

Guilhem.
/* cpio.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 "cpnative.h"
#include "cpmath.h"
#include "cpio.h"

JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int 
permissions)
{
  int sflags = 0;
  int rwflags = flags & CPFILE_FLAG_READWRITE;
  int perms;

  if (flags & CPFILE_FLAG_CREATE)
    sflags |= O_CREAT;
  if (flags & CPFILE_FLAG_APPEND)
    sflags |= O_APPEND;
  if (flags & CPFILE_FLAG_TRUNCATE)
    sflags |= O_TRUNC;
  if (flags & CPFILE_FLAG_SYNC)
    sflags |= O_SYNC;
  if (flags & CPFILE_FLAG_DSYNC)
    sflags |= O_DSYNC;
#if defined(O_BINARY)
  if (flags & CPFILE_FLAG_BINARY)
    sflags |= O_BINARY;
#endif

  switch (rwflags)
    {
    case CPFILE_FLAG_READ:
      sflags |= O_RDONLY;
      break;
    case CPFILE_FLAG_WRITE:
      sflags |= O_WRONLY;
      break;
    case CPFILE_FLAG_READWRITE:
      sflags |= O_RDWR;
      break;
    }

  if (permissions == CPFILE_PERMISSION_NORMAL)
          perms = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  else
          perms = 0;

  *fd = open (filename, sflags, perms);
  
  if (*fd < 0)
    return errno;

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_closeFile (int fd)
{
  if (close (fd) < 0)
    return errno;
  
  return CPNATIVE_OK;
}

JNIEXPORT int cpio_availableBytes (int fd, jlong *bytes_available)
{
#if defined (FIONREAD)
  ssize_t n;

  if (ioctl (fd, FIONREAD, (char *)&n) != 0)
    return errno;

  *bytes_available = n;
  return CPNATIVE_OK;  
#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 = CPNATIVE_OK;
      break;      
    default: 
      *bytes_available = 1;
      result = CPNATIVE_OK;
      break;
    }
  return result;

#else
  *bytes_available = 0;
  return ENOTSUP;
#endif
}

JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize)
{
  struct stat statBuffer;

  if (fstat(fd, &statBuffer) < 0)
    return errno;
  
  *filesize = statBuffer.st_size;
  return CPNATIVE_OK;
}

JNIEXPORT int cpio_getFilePosition (int fd, jlong *offset)
{
  *offset = lseek (fd, 0, SEEK_CUR);
  if (*offset < 0)
    return errno;
  
  return CPNATIVE_OK;
}

JNIEXPORT int cpio_setFilePosition (int fd, jlong position)
{
  if (lseek (fd, position, SEEK_SET) < 0)
    return errno;

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_read (int fd, void *buffer, jint length, jint *bytes_read)
{
  *bytes_read = read (fd, buffer, length);
  
  if (*bytes_read < 0)
  {
    return errno;
  }

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_write (int fd, const void *buffer, jint length, jint 
*bytes_written)
{
  *bytes_written = write (fd, buffer, length);
  
  if (*bytes_written < 0)
    return errno;

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_fsync (int fd)
{
  if (fsync (fd) < 0)
    return errno;

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_truncate (int fd, jlong size)
{
  if (ftruncate (fd, size) < 0)
    return errno;

  return CPNATIVE_OK;
}

JNIEXPORT int cpio_setFileSize (int native_fd, jlong new_size)
{
  jlong file_size;
  jlong save_offset;
  int result;
  char data;
  jint bytes_written;
  
  result = cpio_getFileSize (native_fd, &file_size);
  if (result != CPNATIVE_OK)
    return result;

  /* Save off current position */
  result = cpio_getFilePosition (native_fd, &save_offset);
  if (result != CPNATIVE_OK)
    return result;

  if (cpmath_jlong_lt (file_size, new_size))
    {
      /* File is too short -- seek to one byte short of where we want,
       * then write a byte */

      /* move to position n-1 */
      result = cpio_setFilePosition (native_fd, cpmath_jlong_sub_jint 
(new_size, 1));
      if (result != CPNATIVE_OK)
        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 = cpio_write (native_fd, &data, 1, &bytes_written);
      if (result != CPNATIVE_OK)
        return result;

      /* Reposition file pointer to where we started if not beyond new len. */
      if (cpmath_jlong_lt (save_offset, new_size))
        {
          result = cpio_setFilePosition (native_fd, save_offset);
          if (result != CPNATIVE_OK)
            return result;
        }
    }
  else if (cpmath_jlong_lt (new_size, file_size))
    {
      /* File is too long - use ftruncate if available */
      result = cpio_truncate (native_fd, new_size);
      if (result != CPNATIVE_OK)
          return result;

      /* Reposition file pointer when it now is beyond the end of file. */
      if (cpmath_jlong_lt (new_size, save_offset))
        {
          result = cpio_setFilePosition (native_fd, new_size);
          if (result != CPNATIVE_OK)
            return result;
        }
    }

  return CPNATIVE_OK;
}
/* cpio.h -
   Copyright (C) 2003, 2004, 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 _CLASSPATH_IO_H_INCLUDED
#define _CLASSPATH_IO_H_INCLUDED

#include <jni.h>

#define CPFILE_FLAG_CREATE   0x0001
#define CPFILE_FLAG_APPEND   0x0002
#define CPFILE_FLAG_TRUNCATE 0x0004
#define CPFILE_FLAG_SYNC     0x0008
#define CPFILE_FLAG_DSYNC    0x0010
#define CPFILE_FLAG_BINARY   0x0020
#define CPFILE_FLAG_READ     0x0040
#define CPFILE_FLAG_WRITE    0x0080

#define CPFILE_PERMISSION_NORMAL 1

#define CPFILE_FLAG_READWRITE (CPFILE_FLAG_READ|CPFILE_FLAG_WRITE)

JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int 
permissions);
JNIEXPORT int cpio_closeFile (int fd);
JNIEXPORT int cpio_availableBytes (int fd, jlong *avail);
JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize);
JNIEXPORT int cpio_setFileSize (int fd, jlong filesize);
JNIEXPORT int cpio_getFilePosition (int fd, jlong *position);
JNIEXPORT int cpio_setFilePosition (int fd, jlong position);
JNIEXPORT int cpio_read (int fd, void *data, jint len, jint *bytes_read);
JNIEXPORT int cpio_write (int fd, const void *data, jint len, jint 
*bytes_written);
JNIEXPORT int cpio_fsync (int fd);
JNIEXPORT int cpio_truncate (int fd, jlong size);

#endif
/* cpmath.h -
   Copyright (C) 2003, 2004, 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 _CLASSPATH_MATH_H_INCLUDED
#define _CLASSPATH_MATH_H_INCLUDED

#include <jni.h>

#define cpmath_jint_to_jlong(i) ((jlong)(i))
#define cpmath_jlong_to_jint(l) ((jint)(l))
#define cpmath_jlong_lt(l1, l2)  ((l1) < (l2))
#define cpmath_jlong_sub_jint(l1, i2) ((l1) - (i2))

#endif
/* cpnative.h -
   Copyright (C) 2003, 2004, 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 _CLASSPATH_NATIVE_H_INCLUDED
#define _CLASSPATH_NATIVE_H_INCLUDED

#include <errno.h>
#include <string.h>

#define CPNATIVE_OK 0
#define CPNATIVE_EINTR EINTR

#define cpnative_getErrorString strerror

#endif
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 6 Jan 2006 
19:33:47 -0000
@@ -44,11 +44,9 @@
 #include <jni.h>
 #include <jcl.h>
 
-#include "target_native.h"
-#ifndef WITHOUT_FILESYSTEM
-#include "target_native_file.h"
-#endif
-#include "target_native_math_int.h"
+#include "cpnative.h"
+#include "cpio.h"
+#include "cpmath.h"
 
 #include "gnu_java_nio_channels_FileChannelImpl.h"
 
@@ -60,6 +58,10 @@
 #include <sys/mman.h>
 #endif /* HAVE_SYS_MMAN_H */
 
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
+#endif
+
 /* These values must be kept in sync with FileChannelImpl.java.  */
 #define FILECHANNELIMPL_READ   1
 #define FILECHANNELIMPL_WRITE  2
@@ -79,22 +81,6 @@
 
 #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)
-
-/* 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)
-
-/* FIXME: This can't be right.  Need converter macros */
-#define CONVERT_JINT_TO_INT(x) ((int)(x & 0xFFFFFFFF))
-#define CONVERT_INT_TO_JINT(x) ((int)(x & 0xFFFFFFFF))
-
-/* FIXME: This can't be right.  Need converter macros. */
-#define CONVERT_SSIZE_T_TO_JINT(x) ((jint)(x & 0xFFFFFFFF))
-#define CONVERT_JINT_TO_SSIZE_T(x) (x)
-
 /* Align a value up or down to a multiple of the pagesize. */
 #define ALIGN_DOWN(p,s) ((p) - ((p) % (s)))
 #define ALIGN_UP(p,s) ((p) + ((s) - ((p) % (s))))
@@ -162,53 +148,46 @@
       && (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 = CPFILE_FLAG_CREATE | CPFILE_FLAG_READWRITE;
+      permissions = CPFILE_PERMISSION_NORMAL;
     }
   else if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_READ))
     {
       /* read */
-      flags = TARGET_NATIVE_FILE_FILEFLAG_READ;
-      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;
+      flags = CPFILE_FLAG_READ;
+      permissions = CPFILE_PERMISSION_NORMAL;
     }
   else
     {
       /* write */
-      flags =
-       TARGET_NATIVE_FILE_FILEFLAG_CREATE |
-       TARGET_NATIVE_FILE_FILEFLAG_WRITE;
+      flags = CPFILE_FLAG_CREATE | CPFILE_FLAG_WRITE;
       if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_APPEND))
        {
-         flags |= TARGET_NATIVE_FILE_FILEFLAG_APPEND;
+         flags |= CPFILE_FLAG_APPEND;
        }
       else
        {
-         flags |= TARGET_NATIVE_FILE_FILEFLAG_TRUNCATE;
+         flags |= CPFILE_FLAG_TRUNCATE;
        }
-      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;
+      permissions = CPFILE_PERMISSION_NORMAL;
     }
 
   if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_SYNC))
     {
-      flags |= TARGET_NATIVE_FILE_FILEFLAG_SYNC;
+      flags |= CPFILE_FLAG_SYNC;
     }
 
   if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_DSYNC))
     {
-      flags |= TARGET_NATIVE_FILE_FILEFLAG_DSYNC;
+      flags |= CPFILE_FLAG_DSYNC;
     }
-#ifdef O_BINARY
-  flags |= TARGET_NATIVE_FILE_FILEFLAG_BINARY;
-#endif
-
-  TARGET_NATIVE_FILE_OPEN (filename, native_fd, flags, permissions, result);
+  flags |= CPFILE_FLAG_BINARY;
 
-  if (result != TARGET_NATIVE_OK)
+  result = cpio_openFile (filename, &native_fd, flags, permissions);
+  if (result != CPNATIVE_OK)
     {
       char message[256]; /* Fixed size we don't need to malloc. */
-      char *error_string = TARGET_NATIVE_LAST_ERROR_STRING ();
+      const char *error_string = cpnative_getErrorString (result);
 
       snprintf(message, 256, "%s: %s", error_string, filename);
       /* We are only allowed to throw FileNotFoundException.  */
@@ -216,7 +195,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 +217,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 = cpio_closeFile (native_fd);
+      if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             cpnative_getErrorString (result));
          return;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != CPNATIVE_OK);
 }
 
 /*
@@ -267,20 +244,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 = cpio_availableBytes (native_fd, &bytes_available);
+      if (result != CPNATIVE_OK && result != CPNATIVE_EINTR)
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             cpnative_getErrorString (result));
          return 0;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != CPNATIVE_OK);
 
   /* FIXME NYI ??? why only jint and not jlong? */
-  return TARGET_NATIVE_MATH_INT_INT64_TO_INT32 (bytes_available);
+  return cpmath_jlong_to_jint (bytes_available);
 }
 
 JNIEXPORT jlong JNICALL
@@ -292,12 +267,12 @@
 
   native_fd = get_native_fd (env, obj);
 
-  TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpio_getFileSize (native_fd, &file_size);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
+                         cpnative_getErrorString (result));
+      return cpmath_jint_to_jlong(-1);
     }
 
   return file_size;
@@ -317,12 +292,12 @@
 
   native_fd = get_native_fd (env, obj);
 
-  TARGET_NATIVE_FILE_TELL (native_fd, current_offset, result);
-  if (result != TARGET_NATIVE_OK)
+  result = cpio_getFilePosition (native_fd, &current_offset);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;
+                         cpnative_getErrorString (result));
+      return cpmath_jint_to_jlong(-1);
     }
 
   return current_offset;
@@ -337,7 +312,6 @@
                                                 jlong offset)
 {
   int native_fd;
-  jlong new_offset;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -362,14 +336,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 = cpio_setFilePosition (native_fd, offset);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
+                         cpnative_getErrorString (result));
     }
 }
 
@@ -383,10 +354,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,97 +379,11 @@
     }
 #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 = cpio_setFileSize (native_fd, len);
+  if (result != CPNATIVE_OK)
     {
       JCL_ThrowException (env, IO_EXCEPTION,
-                         TARGET_NATIVE_LAST_ERROR_STRING ());
-      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;
-           }
-       }
+                         cpnative_getErrorString (result));
     }
 }
 
@@ -547,7 +428,7 @@
            fd, ALIGN_DOWN (position, pagesize));
   if (p == MAP_FAILED)
     {
-      JCL_ThrowException (env, IO_EXCEPTION, strerror (errno));
+      JCL_ThrowException (env, IO_EXCEPTION, cpnative_getErrorString (errno));
       return NULL;
     }
 
@@ -603,7 +484,7 @@
 {
   int native_fd;
   char data;
-  ssize_t bytes_read;
+  jint bytes_read;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -611,21 +492,18 @@
   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 = cpio_read (native_fd, &data, 1, &bytes_read);
+      if ((result == CPNATIVE_OK) && (bytes_read == 0))
+         return -1;
+
+      if ((result != CPNATIVE_OK) && (result != CPNATIVE_EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
-         return (-1);
+                             cpnative_getErrorString (result));
+         return -1;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != CPNATIVE_OK);
 
   return ((jint) (data & 0xFF));
 }
@@ -643,8 +521,8 @@
 {
   int native_fd;
   jbyte *bufptr;
-  ssize_t bytes_read;
-  ssize_t n;
+  jint bytes_read;
+  jint n;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -676,32 +554,30 @@
   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 = cpio_read (native_fd, (bufptr + offset + bytes_read),
+                         (length - bytes_read), &n);
+      if ((result == CPNATIVE_OK) && (n == 0))
        {
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          if (bytes_read == 0)
            return -1;          /* Signal end of file to Java */
          else
-           return CONVERT_SSIZE_T_TO_JINT (bytes_read);
+           return bytes_read;
        }
-      if ((result != TARGET_NATIVE_OK)
-         && (TARGET_NATIVE_LAST_ERROR () !=
-             TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))
+      if ((result != CPNATIVE_OK) && (result != CPNATIVE_EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             cpnative_getErrorString (result));
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          return -1;
        }
-      if (result == TARGET_NATIVE_OK)
+      if (result == CPNATIVE_OK)
        bytes_read += n;
     }
   while (bytes_read < 1);
 
   (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
-  return CONVERT_SSIZE_T_TO_JINT (bytes_read);
+  return bytes_read;
 }
 
 /*
@@ -714,26 +590,23 @@
 {
   int native_fd;
   char native_data;
-  ssize_t bytes_written;
+  jint bytes_written;
   int result;
 
   native_fd = get_native_fd (env, obj);
-  native_data = (char) (CONVERT_JINT_TO_INT (b) & 0xFF);
+  native_data = (char) (b & 0xFF);
 
   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 = cpio_write (native_fd, &native_data, 1, &bytes_written);
+      if ((result != CPNATIVE_OK) && (result != CPNATIVE_EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             cpnative_getErrorString (result));
          return;
        }
     }
-  while (result != TARGET_NATIVE_OK);
+  while (result != CPNATIVE_OK);
 }
 
 /*
@@ -746,10 +619,11 @@
   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 = cpio_fsync (native_fd);
+  if (result != CPNATIVE_OK)
     JCL_ThrowException (env, IO_EXCEPTION,
-                       TARGET_NATIVE_LAST_ERROR_STRING ());
+                       cpnative_getErrorString (result));
 }
 
 /*
@@ -765,8 +639,8 @@
 {
   int native_fd;
   jbyte *bufptr;
-  ssize_t bytes_written;
-  ssize_t n;
+  jint bytes_written;
+  jint n;
   int result;
 
   native_fd = get_native_fd (env, obj);
@@ -783,20 +657,18 @@
     }
 
   bytes_written = 0;
-  while (bytes_written < CONVERT_JINT_TO_SSIZE_T (length))
+  while (bytes_written < 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 = cpio_write (native_fd, (bufptr + offset + bytes_written),
+                          (length - bytes_written), &n);
+      if ((result != CPNATIVE_OK) && (result != CPNATIVE_EINTR))
        {
          JCL_ThrowException (env, IO_EXCEPTION,
-                             TARGET_NATIVE_LAST_ERROR_STRING ());
+                             cpnative_getErrorString (result));
          (*env)->ReleaseByteArrayElements (env, buffer, bufptr, 0);
          return;
        }
-      if (result == TARGET_NATIVE_OK)
+      if (result == CPNATIVE_OK)
        bytes_written += n;
     }
 
@@ -833,7 +705,7 @@
       if (errno != EACCES && errno != EAGAIN)
         {
           JCL_ThrowException (env, "java/lang/InternalError",
-                             strerror (errno));
+                             cpnative_getErrorString (errno));
         }
       return JNI_FALSE;
     }
@@ -874,7 +746,7 @@
   if (ret)
     {
       JCL_ThrowException (env, "java/lang/InternalError",
-                         strerror (errno));
+                         cpnative_getErrorString (errno));
     }
 #else
   (void) obj;

reply via email to

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