finally do win32 paths right
[nestedvm.git] / src / org / ibex / nestedvm / UnixRuntime.java
index adf27f8..0bc1028 100644 (file)
@@ -46,28 +46,9 @@ public abstract class UnixRuntime extends Runtime implements Cloneable {
         if(!exec) {
             gs = defaultGS;
             String userdir = Platform.getProperty("user.dir");
         if(!exec) {
             gs = defaultGS;
             String userdir = Platform.getProperty("user.dir");
-            String nvroot = Platform.getProperty("nestedvm.root");
-            cwd = "";
-            if(userdir != null && nvroot == null) {
-                if(userdir.startsWith("/") && File.separatorChar == '/') {
-                    cwd = userdir.substring(1);
-                } else {
-                    Vector vec = new Vector();
-                    File root = HostFS.hostRootDir();
-                    String s = new File(userdir).getAbsolutePath();
-                    File d = new File(s);
-                    System.err.println(s);
-                    System.err.println(d);
-                    while(!d.equals(root)) {
-                        System.err.println("Got " + d.getName());
-                        vec.addElement(d.getName());
-                        if((s = d.getParent()) == null) break;
-                        d = new File(s);
-                    }
-                    if(s != null)
-                        for(int i=vec.size()-1;i>=0;i--) cwd += (String) vec.elementAt(i) + (i==0?"":"/");
-                }
-            }
+            cwd = (userdir == null || Platform.getProperty("nestedvm.root") != null) ? null : HostFS.reverseMap(userdir);
+            if(cwd == null) cwd = "/";
+            cwd = cwd.substring(1);
         }
     }
     
         }
     }
     
@@ -93,13 +74,16 @@ public abstract class UnixRuntime extends Runtime implements Cloneable {
     }
     
     String[] createEnv(String[] extra) {
     }
     
     String[] createEnv(String[] extra) {
-        String[] defaults = new String[6];
+        String[] defaults = new String[7];
         int n=0;
         if(extra == null) extra = new String[0];
         int n=0;
         if(extra == null) extra = new String[0];
+        String tmp;
         if(!envHas("USER",extra) && Platform.getProperty("user.name") != null)
             defaults[n++] = "USER=" + Platform.getProperty("user.name");
         if(!envHas("USER",extra) && Platform.getProperty("user.name") != null)
             defaults[n++] = "USER=" + Platform.getProperty("user.name");
-        if(!envHas("HOME",extra) && Platform.getProperty("user.home") != null)
-            defaults[n++] = "HOME=" + Platform.getProperty("user.home");
+        if(!envHas("HOME",extra) && (tmp=Platform.getProperty("user.home")) != null && (tmp=HostFS.reverseMap(tmp)) != null)
+            defaults[n++] = "HOME=" + tmp;
+        if(!envHas("TMPDIR",extra) && (tmp=Platform.getProperty("java.io.tmpdir")) != null && (tmp=HostFS.reverseMap(tmp)) != null)
+            defaults[n++] = "TMPDIR=" + tmp;
         if(!envHas("SHELL",extra)) defaults[n++] = "SHELL=/bin/sh";
         if(!envHas("TERM",extra) && !win32Hacks)  defaults[n++] = "TERM=vt100";
         if(!envHas("TZ",extra))    defaults[n++] = "TZ=" + posixTZ();
         if(!envHas("SHELL",extra)) defaults[n++] = "SHELL=/bin/sh";
         if(!envHas("TERM",extra) && !win32Hacks)  defaults[n++] = "TERM=vt100";
         if(!envHas("TZ",extra))    defaults[n++] = "TZ=" + posixTZ();
@@ -1188,6 +1172,7 @@ public abstract class UnixRuntime extends Runtime implements Cloneable {
             if(defaultMounts) {
                 addMount("/",new HostFS());
                 addMount("/dev",new DevFS());
             if(defaultMounts) {
                 addMount("/",new HostFS());
                 addMount("/dev",new DevFS());
+                addMount("/resource",new ResourceFS());
             }
         }
         
             }
         }
         
@@ -1413,6 +1398,29 @@ public abstract class UnixRuntime extends Runtime implements Cloneable {
             return f;
         }
         
             return f;
         }
         
+        static String reverseMap(String f) {
+            if(f.startsWith("/") && File.separatorChar == '/') return f;
+                
+            Vector vec = new Vector();
+            File root = HostFS.hostRootDir();
+            String s = new File(f).getAbsolutePath();
+            File d = new File(s);
+            System.err.println(s);
+            System.err.println(d);
+            while(!d.equals(root)) {
+                vec.addElement(d.getName());
+                if((s = d.getParent()) == null) break;
+                d = new File(s);
+            }
+            String ret = null;
+            if(s != null) {
+                ret="/";
+                for(int i=vec.size()-1;i>=0;i--) ret += (String) vec.elementAt(i) + (i==0?"":"/");
+            }
+            System.err.println("reverseMap: " + f + " => " + ret);
+            return ret;
+        }
+        
         private File hostFile(String path) {
             char sep = File.separatorChar;
             if(sep != '/') {
         private File hostFile(String path) {
             char sep = File.separatorChar;
             if(sep != '/') {
@@ -1664,5 +1672,55 @@ public abstract class UnixRuntime extends Runtime implements Cloneable {
         
         public void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { throw new ErrnoException(EROFS); }
         public void unlink(UnixRuntime r, String path) throws ErrnoException { throw new ErrnoException(EROFS); }
         
         public void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { throw new ErrnoException(EROFS); }
         public void unlink(UnixRuntime r, String path) throws ErrnoException { throw new ErrnoException(EROFS); }
-    }    
+    }
+    
+    
+    public static class ResourceFS extends FS {
+        final InodeCache inodes = new InodeCache(500);
+        
+        public FStat lstat(UnixRuntime r, String path) throws ErrnoException { return stat(r,path); }
+        public void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { throw new ErrnoException(EROFS); }
+        public void unlink(UnixRuntime r, String path) throws ErrnoException { throw new ErrnoException(EROFS); }
+        
+        FStat connFStat(final URLConnection conn) {
+            return new FStat() {
+                public int type() { return S_IFREG; }
+                public int nlink() { return 1; }
+                public int mode() { return 0444; }
+                public int size() { return conn.getContentLength(); }
+                public int mtime() { return (int)(conn.getDate() / 1000); }
+                public int inode() { return inodes.get(conn.getURL().toString()); }
+                public int dev() { return devno; }
+            };
+        }
+        
+        public FStat stat(UnixRuntime r, String path) throws ErrnoException {
+            URL url = r.getClass().getResource("/" + path);
+            if(url == null) return null;
+            try {
+                return connFStat(url.openConnection());
+            } catch(IOException e) {
+                throw new ErrnoException(EIO);
+            }
+        }
+        
+        public FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException {
+            if((flags & ~3) != 0) {
+                if(STDERR_DIAG)
+                    System.err.println("WARNING: Unsupported flags passed to ResourceFS.open(\"" + path + "\"): " + toHex(flags & ~3));
+                throw new ErrnoException(ENOTSUP);
+            }
+            if((flags&3) != RD_ONLY) throw new ErrnoException(EROFS);
+            URL url = r.getClass().getResource("/" + path);
+            if(url == null) return null;
+            try {
+                final URLConnection conn = url.openConnection();
+                Seekable.InputStream si = new Seekable.InputStream(conn.getInputStream());
+                return new SeekableFD(si,flags) { protected FStat _fstat() { return connFStat(conn); } };
+            } catch(FileNotFoundException e) {
+                if(e.getMessage() != null && e.getMessage().indexOf("Permission denied") >= 0) throw new ErrnoException(EACCES);
+                return null;
+            } catch(IOException e) { throw new ErrnoException(EIO); }
+        }
+    }
 }
 }