From 103c9465744d35d6ade7c0520a5faa2c6375e7b2 Mon Sep 17 00:00:00 2001 From: brian Date: Wed, 26 May 2004 21:44:44 -0700 Subject: [PATCH] implement truncate and fstat better darcs-hash:20040527044444-24bed-ce50a73d70d3db7f788906294ab86e15f4a16926.gz --- src/org/ibex/nestedvm/Runtime.java | 8 +++----- src/org/ibex/nestedvm/UnixRuntime.java | 4 ++-- src/org/ibex/nestedvm/util/InodeCache.java | 2 -- src/org/ibex/nestedvm/util/Platform.java | 24 +++++++++++++++++++++--- src/org/ibex/nestedvm/util/Seekable.java | 10 ++++------ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/org/ibex/nestedvm/Runtime.java b/src/org/ibex/nestedvm/Runtime.java index ff33ba9..5a1a895 100644 --- a/src/org/ibex/nestedvm/Runtime.java +++ b/src/org/ibex/nestedvm/Runtime.java @@ -732,10 +732,9 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { return hostFSDirFD(f,data); } - // FIXME: Truncate final Seekable.File sf; try { - sf = new Seekable.File(f,write); + sf = new Seekable.File(f,write,(flags & O_TRUNC) != 0); } catch(FileNotFoundException e) { if(e.getMessage() != null && e.getMessage().indexOf("Permission denied") >= 0) throw new ErrnoException(EACCES); return null; @@ -810,12 +809,11 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { } /** The stat/fstat syscall helper */ - // FIXME: Populate uid/gid/nlink int stat(FStat fs, int addr) throws FaultException { memWrite(addr+0,(fs.dev()<<16)|(fs.inode()&0xffff)); // st_dev (top 16), // st_ino (bottom 16) memWrite(addr+4,((fs.type()&0xf000))|(fs.mode()&0xfff)); // st_mode - memWrite(addr+8,1<<16); // st_nlink (top 16) // st_uid (bottom 16) - memWrite(addr+12,0); // st_gid (top 16) // st_rdev (bottom 16) + memWrite(addr+8,fs.nlink()<<16|fs.uid()&0xffff); // st_nlink (top 16) // st_uid (bottom 16) + memWrite(addr+12,fs.gid()<<16|0); // st_gid (top 16) // st_rdev (bottom 16) memWrite(addr+16,fs.size()); // st_size memWrite(addr+20,fs.atime()); // st_atime // memWrite(addr+24,0) // st_spare1 diff --git a/src/org/ibex/nestedvm/UnixRuntime.java b/src/org/ibex/nestedvm/UnixRuntime.java index b4f651d..24a5ece 100644 --- a/src/org/ibex/nestedvm/UnixRuntime.java +++ b/src/org/ibex/nestedvm/UnixRuntime.java @@ -533,7 +533,7 @@ public abstract class UnixRuntime extends Runtime implements Cloneable { return n; } - // FIXME: UDP is totally broken + // FEATURE: UDP is totally broken static class SocketFD extends FD { public static final int TYPE_STREAM = 0; @@ -1094,7 +1094,7 @@ public abstract class UnixRuntime extends Runtime implements Cloneable { } public synchronized Object exec(UnixRuntime r, String path) throws ErrnoException { - // FIXME: Hideous hack to make a standalone busybox possible + // HACK: Hideous hack to make a standalone busybox possible if(path.equals("bin/busybox") && Boolean.valueOf(getSystemProperty("nestedvm.busyboxhack")).booleanValue()) return r.getClass(); FStat fstat = stat(r,path); diff --git a/src/org/ibex/nestedvm/util/InodeCache.java b/src/org/ibex/nestedvm/util/InodeCache.java index cb1d7f0..a169d5f 100644 --- a/src/org/ibex/nestedvm/util/InodeCache.java +++ b/src/org/ibex/nestedvm/util/InodeCache.java @@ -1,7 +1,5 @@ package org.ibex.nestedvm.util; -import java.util.Arrays; - // Based on the various org.xwt.util.* classes by Adam Megacz public class InodeCache { diff --git a/src/org/ibex/nestedvm/util/Platform.java b/src/org/ibex/nestedvm/util/Platform.java index f9dca10..ed59627 100644 --- a/src/org/ibex/nestedvm/util/Platform.java +++ b/src/org/ibex/nestedvm/util/Platform.java @@ -13,7 +13,7 @@ public abstract class Platform { static { float version; try { - version = Float.parseFloat(System.getProperty("java.specification.version")); + version = Float.valueOf(System.getProperty("java.specification.version")).floatValue(); } catch(Exception e) { System.err.println("WARNING: " + e + " while trying to find jvm version - assuming 1.1"); version = 1.1f; @@ -49,10 +49,17 @@ public abstract class Platform { public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); } public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); } + abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException; + public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); } + + // FEATURE: Make sure GCClass can get rid of uncalled superclass methdos + static class Jdk11 extends Platform { boolean _atomicCreateFile(File f) throws IOException { - // FIXME: Just do this non-atomicly - throw new RuntimeException("FIXME"); + // This is not atomic, but its the best we can do on jdk 1.1 + if(f.exists()) return false; + new FileOutputStream(f).close(); + return true; } void _socketHalfClose(Socket s, boolean output) throws IOException { throw new IOException("half closing sockets not supported"); @@ -79,6 +86,11 @@ public abstract class Platform { if(off > 0) sb.append(":").append(off); return sb.toString(); } + + RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException { + new FileOutputStream(f).close(); + return new RandomAccessFile(f,mode); + } } static class Jdk12 extends Jdk11 { @@ -89,6 +101,12 @@ public abstract class Platform { String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l); } + + RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException { + RandomAccessFile raf = new RandomAccessFile(f,mode); + raf.setLength(0); + return raf; + } } static class Jdk13 extends Jdk12 { diff --git a/src/org/ibex/nestedvm/util/Seekable.java b/src/org/ibex/nestedvm/util/Seekable.java index cf8cca7..51c7a39 100644 --- a/src/org/ibex/nestedvm/util/Seekable.java +++ b/src/org/ibex/nestedvm/util/Seekable.java @@ -66,15 +66,13 @@ public abstract class Seekable { private final RandomAccessFile raf; public File(String fileName) throws IOException { this(fileName,false); } - public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable); } + public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable,false); } - public File(java.io.File file, boolean writable) throws IOException { - raf = new RandomAccessFile(file,writable ? "rw" : "r"); + public File(java.io.File file, boolean writable, boolean truncate) throws IOException { + String mode = writable ? "rw" : "r"; + raf = truncate ? Platform.truncatedRandomAccessFile(file,mode) : new RandomAccessFile(file,mode); } - // NOTE: RandomAccessFile.setLength() is a Java2 function - public void setLength(int n) throws IOException { raf.setLength(n); } - public int read(byte[] buf, int offset, int length) throws IOException { return raf.read(buf,offset,length); } public int write(byte[] buf, int offset, int length) throws IOException { raf.write(buf,offset,length); return length; } public void seek(int pos) throws IOException{ raf.seek(pos); } -- 1.7.10.4