[PATCH] JDK 1.2 style native functions for java.io.File

Frohwalt Egerer froh at iconsult.com
Thu Nov 11 16:03:29 PST 1999



First things first: Before I start annonying people here, is there
some place to send patches to besides the mailing list?



==== native functions for java.io.File implemented.

I've implemented most of the missing functions for java.io.File,
leaving deleteOnExit() untouched since I've got no idea where to look
for a 'on exit' hook in Kaffe. The patch is small, so I'll append it
below.

Having implemented createNewFile I could fix a race condition in
createTempFile which created a new random name but not a File as
documented by Sun.

I had a look at Mauve to check for results, but I couldn't find a
testlet for java.io.File in the snapshot of Mauve (1999-11-06).



==== a lot of unix functions get called directly

kaffe/libraries/clib/io/File.c calls a lot of unix functions directly
without going through the jsyscall interface. These syscalls are
access, opendir, readdir and closedir. Implementing the missing
functions I've gone the same way not using jsyscall for utime and
chmod.  I should have added these to jsyscall but I don't dare to
touch threading related things, yet. (And I don't want to touch the
Win32 and BeOs versions.)



==== What the heck ?!?

Implementing java.io.File.setReadOnly() it occured to me it is a one
way function. In Java there doesn't seem to be a way to remove
ReadOnly status again. Strange stuff, such things make you wonder what
they smoked before designing that API ...


Froh



It's ridiculous for such a small patch, but let there be legalese:
The following stuff may be included in all versions (commercial and
open souce) of Kaffe.  I don't mind any copyright on this. No warranty
attached, if it breaks you keep the pieces.

diff -b -x *CVS* -u -r original/kaffe/libraries/clib/io/File.c kaffe/libraries/clib/io/File.c
--- original/kaffe/libraries/clib/io/File.c	Tue Sep 14 01:01:29 1999
+++ kaffe/libraries/clib/io/File.c	Fri Nov 12 00:35:25 1999
@@ -17,6 +17,7 @@
 #include <assert.h>
 #include <native.h>
 #include <jsyscall.h>
+#include <utime.h>
 #include "defs.h"
 #include "files.h"
 #include "../../../include/system.h"
@@ -326,3 +327,74 @@
 		return (0);
 	}
 }
+
+
+jboolean
+java_io_File_createNewFile0(struct Hjava_io_File* this)
+{
+	int fd;
+	int rc;
+	char str[MAXPATHLEN];
+
+	stringJava2CBuf(unhand(this)->path, str, sizeof(str));
+
+	rc = KOPEN(str, O_EXCL|O_WRONLY|O_CREAT, 0666, &fd);
+	if (rc != 0)
+		return 0;
+
+	
+	rc = KCLOSE(fd);
+	if (rc != 0)
+		SignalError("java.io.IOException", SYS_ERROR(rc));
+
+	return 1;
+}
+
+jboolean
+java_io_File_setLastModified0(struct Hjava_io_File* this, jlong thetime)
+{
+	char path[MAXPATHLEN];
+	int r;
+	struct utimbuf ub;
+
+	stringJava2CBuf(unhand(this)->path, path, sizeof(path));
+
+
+	ub.actime = (time_t)(thetime / 1000);
+	ub.modtime = ub.actime;
+
+	/* XXX make part of jsyscall interface !? */
+	r = utime(path, &ub);
+	return (r < 0 ? 0 : 1);
+}
+
+jboolean
+java_io_File_setReadOnly0(struct Hjava_io_File* this)
+{
+	struct stat buf;
+	char str[MAXPATHLEN];
+	int r;
+
+	stringJava2CBuf(unhand(this)->path, str, sizeof(str));
+
+	r = KSTAT(str, &buf);
+
+	if (r<0)
+		return 0;
+
+	/* XXX make part of jsyscall interface !? */
+	r = chmod(str, buf.st_mode & ~(S_IWOTH|S_IWGRP|S_IWUSR));
+	return (r < 0 ? 0 : 1);
+}
+
+
+/***********
+
+Missing in jsyscall:
+access, opendir, readdir, closedir, utime, chmod
+
+
+Missing in configure:
+Check for utimes/utime/utime.h
+
+***********/
Binary files original/kaffe/libraries/javalib/Klasses.jar and kaffe/libraries/javalib/Klasses.jar differ
diff -b -x *CVS* -u -r original/kaffe/libraries/javalib/java/io/File.java kaffe/libraries/javalib/java/io/File.java
--- original/kaffe/libraries/javalib/java/io/File.java	Tue Oct 12 21:05:45 1999
+++ kaffe/libraries/javalib/java/io/File.java	Fri Nov 12 00:38:51 1999
@@ -106,14 +106,14 @@
 		dir = new File(System.getProperties().getProperty(
 			"java.io.tmpdir"));
 	}
+
 	while (true) {
 		File f = new File(dir, prefix
 		    + Integer.toHexString(
 			random.nextInt(0x100000)).toUpperCase() + suffix);
-		if (!f.exists()) {
+               if (f.createNewFile())
 			return f;
 		}
-	}
 }
 
 public boolean delete() {
@@ -168,7 +168,10 @@
         for (int i = 0; i < len; i++) {
                 String str = tok.nextToken();
                 if (str.equals("..")) {
+                    if (j>0)
                         j--;
+                } else if (str.equals(".")) {
+                    // do nothing.
                 }
                 else {
                         array[j] = str;
@@ -339,4 +342,77 @@
 public String toString() {
 	return path;
 }
+
+
+//JDK1.2 updates ...
+
+public java.net.URL toURL()
+    throws java.net.MalformedURLException
+{
+    if (isDirectory())
+        return new java.net.URL("file", "", getAbsolutePath()+separator);
+    else
+        return new java.net.URL("file", "", getAbsolutePath());
+}
+
+public boolean isHidden()
+{
+    checkReadAccess();
+    return getName().startsWith(".");
+}
+
+public File getAbsoluteFile() {
+    return new File(getAbsolutePath());
+}
+
+public File getParentFile() 
+{
+    String p = getParent();
+    if (p==null)
+        return null;
+    return new File(p);
+}
+
+public File getCanonicalFile()
+    throws java.io.IOException
+{
+    return new File(getCanonicalPath());
+}
+
+public boolean createNewFile()
+    throws IOException
+{
+    checkWriteAccess();
+    return createNewFile0();
+}
+
+// creat/open
+native private boolean createNewFile0();
+
+public boolean setLastModified(long time)
+    throws IOException
+{
+    checkWriteAccess();
+    return setLastModified0(time);
+}
+
+// utime
+native private boolean setLastModified0(long time);
+
+public boolean setReadOnly()
+    throws IOException
+{
+    checkWriteAccess();
+    return setReadOnly0();
+}
+
+// chmod
+native private boolean setReadOnly0();
+
+public static File[] listRoots()
+{
+    return new File[] { new File("/") };
+}
+
+
 }



More information about the kaffe mailing list