+ }
+
+ private static class MP {
+ public MP(String path, FS fs) { this.path = path; this.fs = fs; }
+ public String path;
+ public FS fs;
+ public int compareTo(Object o) {
+ if(!(o instanceof MP)) return 1;
+ return -path.compareTo(((MP)o).path);
+ }
+ }
+
+ public synchronized FS getMount(String path) {
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+ if(path.equals("/")) return root;
+ path = path.substring(1);
+ for(int i=0;i<mps.length;i++)
+ if(mps[i].path.equals(path)) return mps[i].fs;
+ return null;
+ }
+
+ public synchronized void addMount(String path, FS fs) {
+ if(getMount(path) != null) throw new IllegalArgumentException("mount point already exists");
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+
+ if(fs.owner != null) fs.owner.removeMount(fs);
+ fs.owner = this;
+
+ if(path.equals("/")) { root = fs; fs.devno = 1; return; }
+ path = path.substring(1);
+ int oldLength = mps.length;
+ MP[] newMPS = new MP[oldLength + 1];
+ if(oldLength != 0) System.arraycopy(mps,0,newMPS,0,oldLength);
+ newMPS[oldLength] = new MP(path,fs);
+ Arrays.sort(newMPS);
+ mps = newMPS;
+ int highdevno = 0;
+ for(int i=0;i<mps.length;i++) highdevno = max(highdevno,mps[i].fs.devno);
+ fs.devno = highdevno + 2;
+ }
+
+ public synchronized void removeMount(FS fs) {
+ for(int i=0;i<mps.length;i++) if(mps[i].fs == fs) { removeMount(i); return; }
+ throw new IllegalArgumentException("mount point doesn't exist");
+ }
+
+ public synchronized void removeMount(String path) {
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+ if(path.equals("/")) {
+ removeMount(-1);
+ } else {
+ path = path.substring(1);
+ int p;
+ for(p=0;p<mps.length;p++) if(mps[p].path.equals(path)) break;
+ if(p == mps.length) throw new IllegalArgumentException("mount point doesn't exist");
+ removeMount(p);
+ }
+ }
+
+ private void removeMount(int index) {
+ if(index == -1) { root.owner = null; root = null; return; }
+ MP[] newMPS = new MP[mps.length - 1];
+ System.arraycopy(mps,0,newMPS,0,index);
+ System.arraycopy(mps,0,newMPS,index,mps.length-index-1);
+ mps = newMPS;
+ }
+
+ private Object fsop(int op, UnixRuntime r, String normalizedPath, int arg1, int arg2) throws ErrnoException {
+ int pl = normalizedPath.length();
+ if(pl != 0) {
+ MP[] list;
+ synchronized(this) { list = mps; }
+ for(int i=0;i<list.length;i++) {
+ MP mp = list[i];
+ int mpl = mp.path.length();
+ if(normalizedPath.startsWith(mp.path) && (pl == mpl || (pl < mpl && normalizedPath.charAt(mpl) == '/')))
+ return dispatch(mp.fs,op,r,pl == mpl ? "" : normalizedPath.substring(mpl+1),arg1,arg2);
+ }
+ }
+ return dispatch(root,op,r,normalizedPath,arg1,arg2);
+ }
+
+ private static Object dispatch(FS fs, int op, UnixRuntime r, String path, int arg1, int arg2) throws ErrnoException {
+ switch(op) {
+ case OPEN: return fs.open(r,path,arg1,arg2);
+ case STAT: return fs.stat(r,path);
+ case LSTAT: return fs.lstat(r,path);
+ case MKDIR: fs.mkdir(r,path,arg1); return null;
+ default: throw new Error("should never happen");
+ }
+ }
+
+ public final FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException { return (FD) fsop(OPEN,r,path,flags,mode); }
+ public final FStat stat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(STAT,r,path,0,0); }
+ public final FStat lstat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(LSTAT,r,path,0,0); }
+ public final void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { fsop(MKDIR,r,path,mode,0); }
+
+ private Hashtable execCache = new Hashtable();
+ private static class CacheEnt {
+ public final long time;
+ public final long size;
+ public final Object o;
+ public CacheEnt(long time, long size, Object o) { this.time = time; this.size = size; this.o = o; }
+ }
+
+ public synchronized Object exec(UnixRuntime r, String path) throws ErrnoException {
+ FStat fstat = stat(r,path);
+ if(fstat == null) return null;
+ long mtime = fstat.mtime();
+ long size = fstat.size();
+ CacheEnt ent = (CacheEnt) execCache.get(path);
+ if(ent != null) {
+ //System.err.println("Found cached entry for " + path);
+ if(ent.time == mtime && ent.size == size) return ent.o;
+ //System.err.println("Cache was out of date");
+ execCache.remove(path);
+ }
+ FD fd = open(r,path,RD_ONLY,0);
+ if(fd == null) return null;
+ Seekable s = fd.seekable();
+
+ String[] command = null;
+
+ if(s == null) throw new ErrnoException(EACCES);
+ byte[] buf = new byte[4096];
+
+ try {
+ int n = s.read(buf,0,buf.length);
+ if(n == -1) throw new ErrnoException(ENOEXEC);
+
+ switch(buf[0]) {
+ case '\177': // possible ELF
+ if(n < 4 && s.tryReadFully(buf,n,4-n) != 4-n) throw new ErrnoException(ENOEXEC);
+ if(buf[1] != 'E' || buf[2] != 'L' || buf[3] != 'F') throw new ErrnoException(ENOEXEC);
+ break;
+ case '#':
+ if(n == 1) {
+ int n2 = s.read(buf,1,buf.length-1);
+ if(n2 == -1) throw new ErrnoException(ENOEXEC);
+ n += n2;
+ }
+ if(buf[1] != '!') throw new ErrnoException(ENOEXEC);
+ int p = 2;
+ n -= 2;
+ OUTER: for(;;) {
+ for(int i=p;i<p+n;i++) if(buf[i] == '\n') { p = i; break OUTER; }
+ p += n;
+ if(p == buf.length) break OUTER;
+ n = s.read(buf,p,buf.length-p);
+ }
+ command = new String[2];
+ int arg;
+ for(arg=2;arg<p;arg++) if(buf[arg] == ' ') break;
+ if(arg < p) {
+ int cmdEnd = arg;
+ while(arg < p && buf[arg] == ' ') arg++;
+ command[0] = new String(buf,2,cmdEnd);
+ command[1] = arg < p ? new String(buf,arg,p-arg) : null;
+ } else {
+ command[0] = new String(buf,2,p-2);
+ }
+ //System.err.println("command[0]: " + command[0] + " command[1]: " + command[1]);
+ break;
+ default:
+ throw new ErrnoException(ENOEXEC);
+ }
+ } catch(IOException e) {
+ fd.close();
+ throw new ErrnoException(EIO);
+ }
+
+ if(command == null) {
+ // its an elf binary
+ try {
+ s.seek(0);
+ Class c = RuntimeCompiler.compile(s);
+ //System.err.println("Compile succeeded: " + c);
+ ent = new CacheEnt(mtime,size,c);
+ } catch(Compiler.Exn e) {
+ if(STDERR_DIAG) e.printStackTrace();
+ throw new ErrnoException(ENOEXEC);
+ } catch(IOException e) {
+ if(STDERR_DIAG) e.printStackTrace();
+ throw new ErrnoException(EIO);
+ }
+ } else {
+ ent = new CacheEnt(mtime,size,command);
+ }
+
+ fd.close();
+
+ execCache.put(path,ent);
+ return ent.o;