aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Rizwan <omar.rizwan@gmail.com>2019-03-02 12:16:08 -0800
committerOmar Rizwan <omar.rizwan@gmail.com>2019-03-02 12:16:08 -0800
commit53de6736feb5ab5d5a3039190ea770bfddc67ccf (patch)
treedd49243ebb0d6248fe6f73d986f45eb1c76c5d04
parenta81c021204bcc46241ba9f1c2290f392d8bf8df2 (diff)
write works to close tabs!
-rw-r--r--extension/background.js33
-rw-r--r--fs/tabfs.c33
2 files changed, 61 insertions, 5 deletions
diff --git a/extension/background.js b/extension/background.js
index 73bb579..b204876 100644
--- a/extension/background.js
+++ b/extension/background.js
@@ -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 = {
diff --git a/fs/tabfs.c b/fs/tabfs.c
index 7a02e83..b992548 100644
--- a/fs/tabfs.c
+++ b/fs/tabfs.c
@@ -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,