introduce default ops. it still (mostly) works!

control is broken tho
This commit is contained in:
Omar Rizwan 2020-12-02 13:45:20 -08:00
parent 45518bce56
commit 71df7d3729

View file

@ -21,12 +21,7 @@ const unix = {
}
class UnixError extends Error {
constructor(error) {
super();
this.name = "UnixError";
this.error = error;
}
constructor(error) { super(); this.name = "UnixError"; this.error = error; }
}
// tabs/by-id/ID/title
@ -44,9 +39,7 @@ function pathComponent(path, i) {
const components = path.split('/');
return components[i >= 0 ? i : components.length + i];
}
function sanitize(s) {
return s.replace(/[^A-Za-z0-9_\-\.]/gm, '_');
}
function sanitize(s) { return s.replace(/[^A-Za-z0-9_\-\.]/gm, '_'); }
function stringSize(str) {
// returns the byte length of an utf8 string
var s = str.length;
@ -100,14 +93,6 @@ browser.windows.onFocusChanged.addListener(windowId => {
view of what the whole filesystem looks like at a glance. */
const router = {};
const directoryGetattr = async function() {
return {
st_mode: unix.S_IFDIR | 0755,
st_nlink: 3,
st_size: 0,
};
};
function withTab(handler) {
return {
async getattr({path}) {
@ -145,8 +130,6 @@ function fromScript(code) {
}
router["/tabs/by-id"] = {
getattr: directoryGetattr,
async opendir({path}) { return { fh: 0 }; },
async readdir() {
const tabs = await browser.tabs.query({});
return { entries: tabs.map(tab => String(tab.id)) };
@ -156,7 +139,6 @@ router["/tabs/by-id/*/url"] = withTab(tab => tab.url + "\n");
router["/tabs/by-id/*/title"] = withTab(tab => tab.title + "\n");
router["/tabs/by-id/*/text"] = fromScript(`document.body.innerText`);
router["/tabs/by-id/*/resources"] = {
getattr: directoryGetattr,
async opendir({path}) {
const tabId = parseInt(pathComponent(path, -2));
await debugTab(tabId);
@ -170,6 +152,7 @@ router["/tabs/by-id/*/resources"] = {
};
router["/tabs/by-id/*/resources/*"] = {
async getattr({path}) {
// FIXME: cache the file
const tabId = parseInt(pathComponent(path, -3));
const suffix = pathComponent(path, -1);
@ -231,13 +214,6 @@ router["/tabs/by-id/*/resources/*"] = {
};
router["/tabs/by-id/*/control"] = {
async getattr() { // generic read-only file attr
return {
st_mode: unix.S_IFREG | 0222,
st_nlink: 1,
st_size: 100 // FIXME
};
},
// echo remove >> mnt/tabs/by-id/1644/control
async write({path, buf}) {
const tabId = parseInt(pathComponent(path, -2));
@ -249,8 +225,6 @@ router["/tabs/by-id/*/control"] = {
};
router["/tabs/by-title"] = {
getattr: directoryGetattr,
async opendir({path}) { return { fh: 0 }; },
async readdir() {
const tabs = await browser.tabs.query({});
return { entries: tabs.map(tab => sanitize(String(tab.title).slice(0, 200)) + "_" + String(tab.id)) };
@ -258,15 +232,6 @@ router["/tabs/by-title"] = {
};
router["/tabs/by-title/*"] = {
// a symbolic link to /tabs/by-id/[id for this tab]
async getattr({path}) {
const st_size = (await this.readlink({path})).buf.length + 1;
return {
st_mode: unix.S_IFLNK | 0444,
st_nlink: 1,
// You _must_ return correct linkee path length from getattr!
st_size
};
},
async readlink({path}) {
const parts = path.split("_");
const id = parts[parts.length - 1];
@ -276,15 +241,6 @@ router["/tabs/by-title/*"] = {
router["/tabs/last-focused"] = {
// a symbolic link to /tabs/by-id/[id for this tab]
async getattr({path}) {
const st_size = (await this.readlink({path})).length + 1;
return {
st_mode: unix.S_IFLNK | 0444,
st_nlink: 1,
// You _must_ return correct linkee path length from getattr!
st_size
};
},
async readlink({path}) {
const id = (await browser.tabs.query({ active: true, windowId: lastFocusedWindowId }))[0].id;
return { buf: "by-id/" + id };
@ -311,11 +267,7 @@ for (let key in router) {
.map(k => k.substr((path === '/' ? 0 : path.length) + 1).split('/')[0]);
children = [...new Set(children)];
router[path] = {
getattr: directoryGetattr,
opendir({path}) { return { fh: 0 }; },
readdir() { return { entries: children }; }
};
router[path] = { readdir() { return { entries: children }; } };
}
}
}
@ -330,6 +282,58 @@ if (TESTING) { // I wish I could color this section with... a pink background, o
})()
}
// fill in default implementations of fs ops
for (let key in router) {
// if readdir -> directory -> add getattr, opendir, releasedir
if (router[key].readdir) {
router[key] = {
getattr() {
return {
st_mode: unix.S_IFDIR | 0755,
st_nlink: 3,
st_size: 0,
};
},
opendir({path}) { return { fh: 0 }; },
releasedir({path}) { return {}; },
...router[key]
};
} else if (router[key].readlink) {
router[key] = {
async getattr({path}) {
const st_size = (await this.readlink({path})).length + 1;
return {
st_mode: unix.S_IFLNK | 0444,
st_nlink: 1,
// You _must_ return correct linkee path length from getattr!
st_size
};
},
...router[key]
};
} else if (router[key].read || router[key].write) {
router[key] = {
async getattr() {
return {
st_mode: unix.S_IFREG | ((router[key].read && 0444) || (router[key].write && 0222)),
st_nlink: 1,
st_size: 100 // FIXME
};
},
open() {
return { fh: 0 };
},
release() {
return {};
},
...router[key]
};
}
}
console.log(router);
function findRoute(path) {
let pathSegments = path.split("/");
@ -353,80 +357,6 @@ function findRoute(path) {
return router[routingPath];
}
/* const ops = {
* async getattr({path}) {
* let route = findRoute(path);
* if (route.getattr) {
* return {
* st_mode: 0,
* st_nlink: 0,
* st_size: 0,
* ...(await route.getattr(path))
* };
* } else if (route.read || route.write) {
* // default file attrs
* return {
* st_mode: unix.S_IFREG | ((route.read && 0444) || (route.write && 0222)),
* st_nlink: 1,
* st_size: 100 // FIXME
* };
* } else {
* // default dir attrs
* return {
* st_mode: unix.S_IFDIR | 0755,
* st_nlink: 3,
* st_size: 0
* };
* }
* },
*
* async open({path}) {
* let route = findRoute(path);
* if (route.open) return { fh: await route.open(path) };
* else return { fh: 0 }; // empty fh
* },
*
* async read({path, fh, size, offset}) {
* let route = findRoute(path);
* if (route.read) {
* const ret = await route.read(path, fh, size, offset);
* if (typeof ret === 'string') {
* return { buf: ret };
* } else {
* return ret;
* }
* }
* },
* async write({path, buf, offset}) {
* let route = findRoute(path);
* if (route.write) return route.write(path, atob(buf), offset);
* },
* async release({path, fh}) {
* let route = findRoute(path);
* if (route.release) return route.release(path, fh);
* },
*
* async readlink({path}) {
* let route = findRoute(path);
* if (route.readlink) return { buf: await route.readlink(path) };
* },
*
* async opendir({path}) {
* let route = findRoute(path);
* if (route.opendir) return { fh: await route.opendir(path) };
* else return { fh: 0 }; // empty fh
* },
* async readdir({path}) {
* let route = findRoute(path);
* if (route.entries) return { entries: await route.entries(path) };
* },
* async releasedir({path}) {
* let route = findRoute(path);
* if (route.releasedir) return route.releasedir(path);
* else return {};
* }
* };*/
let port;
async function onMessage(req) {
console.log('req', req);