public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Implement jni/FileDescriptor.
@ 2008-05-21 19:34 cagney
  0 siblings, 0 replies; only message in thread
From: cagney @ 2008-05-21 19:34 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  8a5c1063c603875724da368a064930581cc2e202 (commit)
      from  a2d4d3e8351a6754f802a1b2b39ef63ca28d55af (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit 8a5c1063c603875724da368a064930581cc2e202
Author: Andrew Cagney <cagney@redhat.com>
Date:   Wed May 21 15:32:51 2008 -0400

    Implement jni/FileDescriptor.
    
    frysk-sys/frysk/sys/ChangeLog
    2008-05-21  Andrew Cagney  <cagney@redhat.com>
    
    	* FileDescriptor.java: Make native methods private.
    	* cni/FileDescriptor.cxx: Update.
    	* jni/FileDescriptor.cxx: Implement.

-----------------------------------------------------------------------

Summary of changes:
 frysk-sys/frysk/sys/ChangeLog              |    4 +
 frysk-sys/frysk/sys/FileDescriptor.java    |  119 +++++++++++-----
 frysk-sys/frysk/sys/cni/FileDescriptor.cxx |  105 +++++++-------
 frysk-sys/frysk/sys/jni/FileDescriptor.cxx |  209 +++++++++++++++++++++++++++-
 4 files changed, 345 insertions(+), 92 deletions(-)

First 500 lines of diff:
diff --git a/frysk-sys/frysk/sys/ChangeLog b/frysk-sys/frysk/sys/ChangeLog
index 4a39196..3cd89da 100644
--- a/frysk-sys/frysk/sys/ChangeLog
+++ b/frysk-sys/frysk/sys/ChangeLog
@@ -1,5 +1,9 @@
 2008-05-21  Andrew Cagney  <cagney@redhat.com>
 
+	* FileDescriptor.java: Make native methods private.
+	* cni/FileDescriptor.cxx: Update.
+	* jni/FileDescriptor.cxx: Implement.
+
 	* cni/Fork.cxx (class redirect_stdio): Fix typo out->err.
 	* jni/PseudoTerminal.cxx: Implement.
 	* jni/Fork.hxx: New.
diff --git a/frysk-sys/frysk/sys/FileDescriptor.java b/frysk-sys/frysk/sys/FileDescriptor.java
index dcd58dc..3cc0ed7 100644
--- a/frysk-sys/frysk/sys/FileDescriptor.java
+++ b/frysk-sys/frysk/sys/FileDescriptor.java
@@ -1,6 +1,6 @@
 // This file is part of the program FRYSK.
 //
-// Copyright 2007, Red Hat Inc.
+// Copyright 2007, 2008, Red Hat Inc.
 //
 // FRYSK is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by
@@ -50,14 +50,13 @@ import java.io.IOException;
  * This object is loosely based on the Unix file-descriptor.
  */
 
-public class FileDescriptor
-{
+public class FileDescriptor {
     private int fd = -1;
     /**
      * Package local file descriptor used by various classes when
      * returning a file descriptor.
      */
-    FileDescriptor (int fd) {
+    FileDescriptor(int fd) {
 	if (fd < 0) {
 	    throw new RuntimeException("FileDescriptor " + fd + " invalid");
 	}
@@ -67,48 +66,54 @@ public class FileDescriptor
      * Create a file descriptor for the specified FILE, open in mode
      * specified by flags.
      */
-    public FileDescriptor (String file, int accessMode) {
-	this (open (file, accessMode, 0));
+    public FileDescriptor(String file, int accessMode) {
+	this(open(file, accessMode, 0));
     }
     /**
      * Create a file descriptor for the specified FILE, open with MODE.
      */
-    public FileDescriptor (File file, int accessMode) {
-	this (open (file.getAbsolutePath (), accessMode, 0));
+    public FileDescriptor(File file, int accessMode) {
+	this(open(file.getAbsolutePath (), accessMode, 0));
     }
     /**
      * Create a new file tied to FileDescriptor with accessMode and
      * protection.
      */
     public FileDescriptor (String file, int accessMode, int prot) {
-	this (open (file, accessMode | CREAT, prot));
+	this(open(file, accessMode | CREAT, prot));
     }
     /**
      * Create a new file tied to FileDescriptor with accessMode and
      * protection.
      */
     public FileDescriptor (File file, int accessMode, int prot) {
-	this (open (file.getAbsolutePath(), accessMode | CREAT, prot));
+	this(open(file.getAbsolutePath(), accessMode | CREAT, prot));
     }
     /**
      * Open file read-only.
      */
-    public static final int RDONLY = 1;
+    public static final int RDONLY = rdonly();
+    private static native int rdonly();
+
     /**
      * Open file write-only
      */
-    public static final int WRONLY = 2;
+    public static final int WRONLY = wronly();
+    private static native int wronly();
+
     /**
      * Open file read-write
      */
-    public static final int RDWR = 4;
+    public static final int RDWR = rdwr();
+    private static native int rdwr();
+
     /**
      * Create a new file.  Implied by the three-param constructor.
      */
-    private static final int CREAT = 8;
+    private static final int CREAT = creat();
+    private static native int creat();
 
-    public int getFd ()
-    {
+    public int getFd() {
 	return fd;
     }
 
@@ -129,7 +134,10 @@ public class FileDescriptor
      * Make this FileDescriptor a dup (point at the same system
      * object) as OLD.  See dup2(2).
      */
-    public native void dup (FileDescriptor old);
+    public void dup(FileDescriptor old) {
+	dup(fd, old.fd);
+    }
+    private static native void dup(int fd, int old);
 
     /**
      * Open the specified FILE in FLAGS; returning a file descriptor.
@@ -146,40 +154,57 @@ public class FileDescriptor
      * one character, the end-of-file, or hangup indication available
      * for reading?
      */
-    public boolean ready ()
-    {
-	return ready (0);
+    public boolean ready() {
+	return ready(fd, 0);
     }
+
     /**
      * Wait on the file descriptor for upto millesecond timeout,
      * checking for at least one character, an eof indication, or
      * hangup available for reading?
      */
-    public native boolean ready (long millisecondTimeout);
+    public boolean ready(long millisecondTimeout) {
+	return ready(fd, millisecondTimeout);
+    }
+    private static native boolean ready(int fd, long millisecondTimeout);
 
     /**
      * Read a single byte from the file descriptor.  Return -1 if
      * end-of-file.
      */
-    public native int read ();
+    public int read() {
+	return read(fd);
+    }
+    private static native int read(int fd);
+
     /**
      * Read bytes from the file descriptor.  Return number of bytes
      * read, or -1 of end-of-file.
      *
      * XXX: Since read is capped by byte[].length, int is returned.
      */
-    public native int read(byte[] bytes, int start, int length);
+    public int read(byte[] bytes, int start, int length) {
+	return read(fd, bytes, start, length);
+    }
+    private static native int read(int fd, byte[] bytes, int start, int length);
 
     /**
      * Write a single byte to the file descriptor.
      */
-    public native void write (int b);
+    public void write(int b) {
+	write(fd, b);
+    }
+    private static native void write(int fd, int b);
+
     /**
      * Write elements of BUF to the file descriptor.
      *
      * XXX: Since write is capped by byte[].lenght, int is returned.
      */
-    public native int write(byte[] bytes, int start, int length);
+    public int write(byte[] bytes, int start, int length) {
+	return write(fd, bytes, start, length);
+    }
+    private static native int write(int fd, byte[] bytes, int start, int length);
 
     /**
      * Close the file descriptor.
@@ -295,32 +320,48 @@ public class FileDescriptor
 	    };
     }
 
-    public String toString ()
-    {
+    public String toString() {
 	return "{fd=" + fd + "}";
     }
 
-  /**
-   * Return the size of a terminal window. Can throw an exception if
-   * the file descriptor is not a terminal.
-   */
-  public native Size getSize();
+    /**
+     * Return the size of a terminal window. Can throw an exception if
+     * the file descriptor is not a terminal.
+     */
+    public Size getSize() {
+	return getSize(fd);
+    }
+    private static native Size getSize(int fd);
 
-  /**
-   * Set the size of a terminal window.
-   */
-  public native void setSize(Size size);
+    /**
+     * Set the size of a terminal window.
+     */
+    public void setSize(Size size) {
+	setSize(fd, size);
+    }
+    private static native void setSize(int fd, Size size);
 
     /**
      * Seek to OFFSET from start of file.
      */
-    public native long seekSet(long offset);
+    public long seekSet(long offset) {
+	return seekSet(fd, offset);
+    }
+    private static native long seekSet(int fd, long offset);
+
     /**
      * Seek to OFFSET from end of file.
      */
-    public native long seekEnd(long offset);
+    public long seekEnd(long offset) {
+	return seekEnd(fd, offset);
+    }
+    private static native long seekEnd(int fd, long offset);
+
     /**
      * Seek to OFFSET from current position.
      */
-    public native long seekCurrent(long offset);
+    public long seekCurrent(long offset) {
+	return seekCurrent(fd, offset);
+    }
+    private static native long seekCurrent(int fd, long offset);
 }
diff --git a/frysk-sys/frysk/sys/cni/FileDescriptor.cxx b/frysk-sys/frysk/sys/cni/FileDescriptor.cxx
index 056e354..230b923 100644
--- a/frysk-sys/frysk/sys/cni/FileDescriptor.cxx
+++ b/frysk-sys/frysk/sys/cni/FileDescriptor.cxx
@@ -69,7 +69,7 @@ frysk::sys::FileDescriptor::close (jint fd)
 }
 
 void
-frysk::sys::FileDescriptor::write (jint b)
+frysk::sys::FileDescriptor::write (jint fd, jint b)
 {
   char c = b;
   errno = 0;
@@ -81,7 +81,7 @@ frysk::sys::FileDescriptor::write (jint b)
 }
 
 jint
-frysk::sys::FileDescriptor::write (jbyteArray bytes, jint off, jint len)
+frysk::sys::FileDescriptor::write (jint fd, jbyteArray bytes, jint off, jint len)
 {
   verifyBounds (bytes, off, len);
   errno = 0;
@@ -94,7 +94,7 @@ frysk::sys::FileDescriptor::write (jbyteArray bytes, jint off, jint len)
 }
 
 jboolean
-frysk::sys::FileDescriptor::ready (jlong timeout)
+frysk::sys::FileDescriptor::ready (jint fd, jlong timeout)
 {
   // ::fprintf (stderr, "%d %d ready %ld?\n", getpid (), (int)fd, (long) timeout);
   struct pollfd pollfd = { fd, POLLIN, 0 };
@@ -136,7 +136,7 @@ doRead (jint fd, void *bytes, jint len)
 }
 
 jint
-frysk::sys::FileDescriptor::read (void)
+frysk::sys::FileDescriptor::read (jint fd)
 {
   jbyte b = 0;
   errno = 0;
@@ -148,36 +148,45 @@ frysk::sys::FileDescriptor::read (void)
 }
 
 jint
-frysk::sys::FileDescriptor::read (jbyteArray bytes, jint off, jint len)
+frysk::sys::FileDescriptor::read(jint fd, jbyteArray bytes, jint off, jint len)
 {
   verifyBounds (bytes, off, len);
   return doRead (fd, elements(bytes) + off, len);
 }
 
+
 jint
-frysk::sys::FileDescriptor::open (jstring file, jint f, jint mode)
-{
+frysk::sys::FileDescriptor::rdonly() {
+  return O_RDONLY;
+}
+
+jint
+frysk::sys::FileDescriptor::wronly() {
+  return O_WRONLY;
+}
+
+jint
+frysk::sys::FileDescriptor::rdwr() {
+  return O_RDWR;
+}
+
+jint
+frysk::sys::FileDescriptor::creat() {
+  return O_CREAT;
+}
+
+jint
+frysk::sys::FileDescriptor::open(jstring file, jint flags, jint mode) {
   const char* pathname = ALLOCA_STRING (file);
   // ::fprintf ("opening <<%s>>\n", pathname);
-  int flags = 0;
-  if (f & frysk::sys::FileDescriptor::RDONLY)
-    flags |= O_RDONLY;
-  if (f & frysk::sys::FileDescriptor::WRONLY)
-    flags |= O_WRONLY;
-  if (f & frysk::sys::FileDescriptor::RDWR)
-    flags |= O_RDWR;
-  if (f & frysk::sys::FileDescriptor::CREAT)
-    flags |= O_CREAT;
-
   return tryOpen(pathname, flags, mode);
 }
 
 void
-frysk::sys::FileDescriptor::dup (frysk::sys::FileDescriptor *old)
-{
+frysk::sys::FileDescriptor::dup(jint fd, jint old) {
   errno = 0;
-  // ::fprintf (stderr, "%d dup (%d, %d)\n", getpid (), (int)old->fd, (int)fd);
-  while (::dup2 (old->fd, fd) < 0) {
+  // ::fprintf (stderr, "%d dup (%d, %d)\n", getpid (), (int)old, (int)fd);
+  while (::dup2 (old, fd) < 0) {
     int err = errno;
     // ::fprintf (stderr, "err = %d %s\n", err, strerror (err));
     switch (err) {
@@ -192,37 +201,32 @@ frysk::sys::FileDescriptor::dup (frysk::sys::FileDescriptor *old)
   // ::fprintf (stderr, "%d dup done\n", getpid ());
 }
 
-frysk::sys::Size *
-frysk::sys::FileDescriptor::getSize()
-{
-    struct winsize size;
-
-    errno = 0;
-    if (::ioctl(fd, TIOCGWINSZ, (char *)&size) < 0)
-    {
-	throwErrno(errno, "ioctl");
-    }
-    return new frysk::sys::Size(size.ws_row, size.ws_col);
+frysk::sys::Size*
+frysk::sys::FileDescriptor::getSize(jint fd) {
+  struct winsize size;
+  
+  errno = 0;
+  if (::ioctl(fd, TIOCGWINSZ, (char *)&size) < 0) {
+    throwErrno(errno, "ioctl");
+  }
+  return new frysk::sys::Size(size.ws_row, size.ws_col);
 }
 
 void
-frysk::sys::FileDescriptor::setSize(frysk::sys::Size *jsize)
-{
-    struct winsize size;
-
-    errno = 0;
-    ::memset(&size, 0, sizeof(size));
-    size.ws_row = jsize->getRows();
-    size.ws_col = jsize->getColumns();
-    if (::ioctl(fd, TIOCSWINSZ, (char *)&size) < 0)
-    {
-	throwErrno(errno, "ioctl");
-    }
+frysk::sys::FileDescriptor::setSize(jint fd, frysk::sys::Size *jsize) {
+  struct winsize size;
+  
+  errno = 0;
+  ::memset(&size, 0, sizeof(size));
+  size.ws_row = jsize->getRows();
+  size.ws_col = jsize->getColumns();
+  if (::ioctl(fd, TIOCSWINSZ, (char *)&size) < 0) {
+    throwErrno(errno, "ioctl");
+  }
 }
 
 static jlong
-seek (int fd, jlong off, int where)
-{
+seek(int fd, jlong off, int where) {
   errno = 0;
   jlong pos = ::lseek64 (fd, off, where);
   int err = errno;
@@ -232,19 +236,16 @@ seek (int fd, jlong off, int where)
 }
 
 jlong
-frysk::sys::FileDescriptor::seekSet (jlong off)
-{
+frysk::sys::FileDescriptor::seekSet(jint fd, jlong off) {
   return seek (fd, off, SEEK_SET);
 }
 
 jlong
-frysk::sys::FileDescriptor::seekCurrent (jlong off)
-{
+frysk::sys::FileDescriptor::seekCurrent(jint fd, jlong off) {
   return seek (fd, off, SEEK_CUR);
 }
 
 jlong
-frysk::sys::FileDescriptor::seekEnd (jlong off)
-{
+frysk::sys::FileDescriptor::seekEnd(jint fd, jlong off) {
   return seek (fd, off, SEEK_END);
 }
diff --git a/frysk-sys/frysk/sys/jni/FileDescriptor.cxx b/frysk-sys/frysk/sys/jni/FileDescriptor.cxx
index b358932..80a52e6 100644
--- a/frysk-sys/frysk/sys/jni/FileDescriptor.cxx
+++ b/frysk-sys/frysk/sys/jni/FileDescriptor.cxx
@@ -1,6 +1,7 @@
 // This file is part of the program FRYSK.
 //
-// Copyright 2008, Red Hat Inc.
+// Copyright 2007, 2008 Red Hat Inc.
+// Copyright 2007 Oracle Corporation.
 //
 // FRYSK is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by
@@ -37,4 +38,210 @@
 // version and license this file solely under the GPL without
 // exception.
 
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/poll.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <alloca.h>
+#include <fcntl.h>
+
 #include "jni.hxx"
+
+#include "jnixx/exceptions.hxx"
+#include "jnixx/elements.hxx"
+#include "jnixx/bounds.hxx"
+
+using namespace java::lang;
+using namespace frysk::sys;
+
+void
+FileDescriptor::close(jnixx::env env, jint fd) {
+  // ::fprintf (stderr, "%d closing %d\n", getpid(), (int)fd);
+  errno = 0;
+  ::close (fd);
+  if (errno != 0)
+    errnoException(env, errno, "close", "fd %d", (int)fd);
+}
+
+void
+FileDescriptor::write(jnixx::env env, jint fd, jint b) {
+  char c = b;
+  errno = 0;
+  ::write(fd, &c, 1);
+  int err = errno;
+  // ::fprintf (stderr, "wrote <<%c>>\n", c);
+  if (err != 0)
+    errnoException(env, err, "write", "fd %d", (int)fd);
+}
+
+jint
+FileDescriptor::write(jnixx::env env, jint fd,
+		      jnixx::byteArray bytes,
+		      jint off, jint len)
+{
+  verifyBounds(env, bytes, off, len);
+  errno = 0;
+  ArrayBytes b = ArrayBytes(env, bytes);


hooks/post-receive
--
frysk system monitor/debugger


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-05-21 19:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-21 19:34 [SCM] master: Implement jni/FileDescriptor cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).