write works to close tabs!

This commit is contained in:
Omar Rizwan 2019-03-02 12:16:08 -08:00
parent a81c021204
commit 53de6736fe
2 changed files with 61 additions and 5 deletions

View file

@ -198,6 +198,17 @@ const router = {
throw new UnixError(unix.ENOENT);
}
}
},
"control": {
async write(path, buf) {
const tabId = parseInt(pathComponent(path, -2));
if (buf.trim() === 'close') {
await new Promise(resolve => chrome.tabs.remove(tabId, resolve));
} else {
throw new UnixError(unix.EIO);
}
}
}
}
}
@ -223,10 +234,10 @@ async function getattr(path) {
let route = findRoute(path);
if (route.getattr) {
return route.getattr(path);
} else if (route.read) {
} else if (route.read || route.write) {
// default file attrs
return {
st_mode: unix.S_IFREG | 0444,
st_mode: unix.S_IFREG | ((route.read && 0444) || (route.write && 0222)),
st_nlink: 1,
st_size: 100 // FIXME
};
@ -249,16 +260,20 @@ async function read(path, fh, size, offset) {
let route = findRoute(path);
if (route.read) return route.read(path, fh, size, offset);
}
async function readlink(path) {
async function write(path, buf, offset) {
let route = findRoute(path);
if (route.readlink) return route.readlink(path);
if (route.write) return route.write(path, buf, offset);
}
async function release(path, fh) {
let route = findRoute(path);
if (route.release) return route.release(path, fh);
}
async function readlink(path) {
let route = findRoute(path);
if (route.readlink) return route.readlink(path);
}
async function opendir(path) {
let route = findRoute(path);
if (route.opendir) return route.opendir(path);
@ -305,6 +320,14 @@ async function onmessage(event) {
};
if (ret.base64Encoded) response.base64Encoded = ret.base64Encoded;
} else if (req.op === 'write') {
// FIXME: decide whether base64 should be handled here
// or in a higher layer?
const ret = await write(req.path, atob(req.buf), req.offset)
response = {
op: 'write'
};
} else if (req.op === 'release') {
await release(req.path, req.fh);
response = {

View file

@ -32,6 +32,22 @@ static cJSON *send_request_then_await_response(cJSON *req) {
return resp;
}
// This helper macro is used to implement all the FUSE fs operations.
//
// It constructs a JSON object to represent the incoming request, then
// dispatches it to the WebSocket server in ws.c (which then
// dispatches it to our browser extension). It then awaits the
// response from the browser and lets us pull that apart to ultimately
// return the data to FUSE.
//
// OP is an opcode string which the extension handles in JS.
// REQ_BUILDER_BODY is a block which should add whatever request
// properties you want to send to the browser to the `req` cJSON
// object. RESP_HANDLER_BODY should handle whatever response
// properties are on the `resp` cJSON object and pass them back to the
// kernel. It should also set the value of `ret` to the desired
// return value. (MAKE_REQ takes over return from the containing
// function so it can automatically return error values.)
#define MAKE_REQ(OP, REQ_BUILDER_BODY, RESP_HANDLER_BODY) \
do { \
int ret = -1; \
@ -135,6 +151,22 @@ tabfs_read(const char *path, char *buf, size_t size, off_t offset,
});
}
static int
tabfs_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
MAKE_REQ("write", {
cJSON_AddStringToObject(req, "path", path);
char base64_buf[size + 1]; // ughh.
base64_encode((const unsigned char *) buf, size, base64_buf);
cJSON_AddStringToObject(req, "buf", base64_buf);
cJSON_AddNumberToObject(req, "offset", offset);
}, {
ret = size;
});
}
static int tabfs_release(const char *path, struct fuse_file_info *fi) {
MAKE_REQ("release", {
cJSON_AddStringToObject(req, "path", path);
@ -187,6 +219,7 @@ static struct fuse_operations tabfs_filesystem_operations = {
.readlink = tabfs_readlink,
.open = tabfs_open, /* To enforce read-only access. */
.read = tabfs_read, /* To provide file content. */
.write = tabfs_write,
.release = tabfs_release,
.opendir = tabfs_opendir,