+
+ 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 || normalizedPath.charAt(mpl) == '/'))
+ return mp.fs.dispatch(op,r,pl == mpl ? "" : normalizedPath.substring(mpl+1),arg1,arg2);
+ }
+ }
+ return root.dispatch(op,r,normalizedPath,arg1,arg2);
+ }
+
+ public final FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException { return (FD) fsop(FS.OPEN,r,path,flags,mode); }
+ public final FStat stat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(FS.STAT,r,path,0,0); }
+ public final FStat lstat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(FS.LSTAT,r,path,0,0); }
+ public final void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { fsop(FS.MKDIR,r,path,mode,0); }
+ public final void unlink(UnixRuntime r, String path) throws ErrnoException { fsop(FS.UNLINK,r,path,0,0); }
+
+ 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 abstract static class FS {
+ static final int OPEN = 1;
+ static final int STAT = 2;
+ static final int LSTAT = 3;
+ static final int MKDIR = 4;
+ static final int UNLINK = 5;
+
+ GlobalState owner;
+ int devno;
+
+ Object dispatch(int op, UnixRuntime r, String path, int arg1, int arg2) throws ErrnoException {
+ switch(op) {
+ case OPEN: return open(r,path,arg1,arg2);
+ case STAT: return stat(r,path);
+ case LSTAT: return lstat(r,path);
+ case MKDIR: mkdir(r,path,arg1); return null;
+ case UNLINK: unlink(r,path); return null;
+ default: throw new Error("should never happen");
+ }
+ }
+
+ public FStat lstat(UnixRuntime r, String path) throws ErrnoException { return stat(r,path); }
+
+ // If this returns null it'll be truned into an ENOENT
+ public abstract FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException;
+ // If this returns null it'll be turned into an ENOENT
+ public abstract FStat stat(UnixRuntime r, String path) throws ErrnoException;
+ public abstract void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException;
+ public abstract void unlink(UnixRuntime r, String path) throws ErrnoException;
+ }
+
+ // chroot support should go in here if it is ever implemented chroot support in here
+ private String normalizePath(String path) {
+ boolean absolute = path.startsWith("/");
+ int cwdl = cwd.length();
+
+ // NOTE: This isn't just a fast path, it handles cases the code below doesn't
+ if(!path.startsWith(".") && path.indexOf("./") == -1 && path.indexOf("//") == -1 && !path.endsWith("."))
+ return absolute ? path.substring(1) : cwdl == 0 ? path : path.length() == 0 ? cwd : cwd + "/" + path;
+
+ char[] in = new char[path.length()+1];
+ char[] out = new char[in.length + (absolute ? -1 : cwd.length())];
+ path.getChars(0,path.length(),in,0);
+ int inp=0, outp=0;
+
+ if(absolute) {
+ do { inp++; } while(in[inp] == '/');
+ } else if(cwdl != 0) {
+ cwd.getChars(0,cwdl,out,0);
+ outp = cwdl;