add ftruncate() syscall
authorDavid Crawshaw <david@zentus.com>
Sat, 18 Nov 2006 01:56:08 +0000 (17:56 -0800)
committerDavid Crawshaw <david@zentus.com>
Sat, 18 Nov 2006 01:56:08 +0000 (17:56 -0800)
darcs-hash:20061118015608-0c629-c674bc92b29c3a46e6ff2cea2d9bbaf92ff9fd9e.gz

src/org/ibex/nestedvm/Runtime.java
src/org/ibex/nestedvm/util/Seekable.java

index bc55744..f286620 100644 (file)
@@ -809,6 +809,17 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
         copyout(buf,addr,n);
         return n;
     }
+
+    /** The ftruncate syscall */
+    private int sys_ftruncate(int fdn, long length) {
+      if (fdn < 0 || fdn >= OPEN_MAX) return -EBADFD;
+      if (fds[fdn] == null) return -EBADFD;
+
+      Seekable seekable = fds[fdn].seekable();
+      if (length < 0 || seekable == null) return -EINVAL;
+      try { seekable.resize(length); } catch (IOException e) { return -EIO; }
+      return 0;
+    }
     
     /** The close syscall */
     private int sys_close(int fdn) {
@@ -1058,6 +1069,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
             case SYS_close: return sys_close(a);
             case SYS_read: return sys_read(a,b,c);
             case SYS_lseek: return sys_lseek(a,b,c);
+            case SYS_ftruncate: return sys_ftruncate(a,b);
             case SYS_getpid: return sys_getpid();
             case SYS_calljava: return sys_calljava(a,b,c,d);
             case SYS_gettimeofday: return sys_gettimeofday(a,b);
index bc21a55..cacca0a 100644 (file)
@@ -13,6 +13,10 @@ public abstract class Seekable {
     public abstract void seek(int pos) throws IOException;
     public abstract void close() throws IOException;
     public abstract int pos() throws IOException;
+
+    public void resize(long length) throws IOException {
+        throw new IOException("resize not implemented for " + getClass());
+    }
     
     public int read() throws IOException {
         byte[] buf = new byte[1];
@@ -83,6 +87,7 @@ public abstract class Seekable {
         public int pos()  throws IOException { return (int) raf.getFilePointer(); }
         public int length() throws IOException { return (int)raf.length(); }
         public void close() throws IOException { raf.close(); }
+        public void resize(long length) throws IOException { raf.setLength(length); }
     }
     
     public static class InputStream extends Seekable {