aboutsummaryrefslogtreecommitdiffstats
path: root/fs/tabfs.c
blob: f650632e99f0b77a18f3b2c8f7a9d1f2ec110e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <fuse.h>

#include "cJSON/cJSON.h"
#include "cJSON/cJSON.c"

#include "base64/base64.h"
#include "base64/base64.c"

FILE* l;

static cJSON *send_request_then_await_response(cJSON *req) {
    char *request_data = cJSON_Print(req);
    unsigned int request_len = strlen(request_data);
    write(1, (char *) &request_len, 4); // stdout
    unsigned int bytes_written = 0;
    while (bytes_written < request_len) {
        bytes_written += write(1, request_data, request_len);
    }
    /* fprintf(l, "req[%s]\n", request_data); fflush(l); */

    unsigned int response_len;
    read(0, (char *) &response_len, 4); // stdin
    char *response_data = malloc(response_len);
    unsigned int bytes_read = 0;
    while (bytes_read < response_len) {
        bytes_read += read(0, response_data + bytes_read, response_len);
    }
    /* fprintf(l, "resp(%d; expected %d)[%s]\n", bytes_read, response_len, response_data); fflush(l); */
    if (response_data == NULL) {
        // Connection is dead.
        return cJSON_Parse("{ \"error\": 5 }");
    }

    cJSON *resp = cJSON_Parse((const char *) response_data);
    free(response_data);

    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
// forwards that object to `send_request_then_await_response` (which
// then dispatches it to our browser extension over stdout). It awaits
// the response from the browser over stdin, then 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;                                 \
        cJSON *req = NULL;                                              \
        cJSON *resp = NULL;                                             \
                                                                        \
        req = cJSON_CreateObject();                                     \
        cJSON_AddStringToObject(req, "op", OP);                         \
        REQ_BUILDER_BODY                                                \
                                                                        \
        resp = send_request_then_await_response(req);                   \
                                                                        \
        cJSON *error_item = cJSON_GetObjectItemCaseSensitive(resp, "error"); \
        if (error_item) {                                               \
            ret = -error_item->valueint;                                \
            if (ret != 0) goto done;                                    \
        }                                                               \
                                                                        \
        ret = -1;                                                       \
        RESP_HANDLER_BODY                                               \
                                                                        \
    done:                                                       \
        if (req != NULL) cJSON_Delete(req);                     \
        if (resp != NULL) cJSON_Delete(resp);                       \
        return ret;                                             \
    } while (0)

#define JSON_GET_PROP_INT(LVALUE, KEY) \
    do { \
        LVALUE = cJSON_GetObjectItemCaseSensitive(resp, KEY)->valueint;     \
    } while (0)

static int tabfs_getattr(const char *path, struct stat *stbuf) {
    memset(stbuf, 0, sizeof(struct stat));

    MAKE_REQ("getattr", {
        cJSON_AddStringToObject(req, "path", path);
    }, {
        JSON_GET_PROP_INT(stbuf->st_mode, "st_mode");
        JSON_GET_PROP_INT(stbuf->st_nlink, "st_nlink");
        JSON_GET_PROP_INT(stbuf->st_size, "st_size");

        ret = 0;
    });
}

static int tabfs_readlink(const char *path, char *buf, size_t size) {
    MAKE_REQ("readlink", {
        cJSON_AddStringToObject(req, "path", path);
    }, {
        cJSON *resp_buf_item = cJSON_GetObjectItemCaseSensitive(resp, "buf");
        // FIXME: fix
        char *resp_buf = cJSON_GetStringValue(resp_buf_item);
        size_t resp_buf_size = strlen(resp_buf) + 1;
        size = resp_buf_size < size ? resp_buf_size : size;

        memcpy(buf, resp_buf, size);

        ret = 0;
    });
}

static int tabfs_open(const char *path, struct fuse_file_info *fi) {
    MAKE_REQ("open", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "flags", fi->flags);
    }, {
        cJSON *fh_item = cJSON_GetObjectItemCaseSensitive(resp, "fh");
        if (fh_item) fi->fh = fh_item->valueint;

        ret = 0;
    });
}

static int
tabfs_read(const char *path, char *buf, size_t size, off_t offset,
           struct fuse_file_info *fi) {
    MAKE_REQ("read", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "size", size);
        cJSON_AddNumberToObject(req, "offset", offset);

        cJSON_AddNumberToObject(req, "fh", fi->fh);
        cJSON_AddNumberToObject(req, "flags", fi->flags);
    }, {
        cJSON *resp_buf_item = cJSON_GetObjectItemCaseSensitive(resp, "buf");
        if (!resp_buf_item) return -EIO;

        char *resp_buf = cJSON_GetStringValue(resp_buf_item);
        if (!resp_buf) return -EIO;
        size_t resp_buf_len = strlen(resp_buf);

        cJSON *base64_encoded_item = cJSON_GetObjectItemCaseSensitive(resp, "base64Encoded");
        if (base64_encoded_item && cJSON_IsTrue(base64_encoded_item)) {
            size = base64_decode(resp_buf, resp_buf_len, (unsigned char *) buf);
        } else {
            size = resp_buf_len < size ? resp_buf_len : size;
            memcpy(buf, resp_buf, size);
        }
        ret = size;
    });
}

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);
        cJSON_AddNumberToObject(req, "fh", fi->fh);
    }, {
        ret = 0;
    });
}

static int tabfs_opendir(const char *path, struct fuse_file_info *fi) {
    MAKE_REQ("opendir", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "flags", fi->flags);
    }, {
        cJSON *fh_item = cJSON_GetObjectItemCaseSensitive(resp, "fh");
        if (fh_item) fi->fh = fh_item->valueint;

        ret = 0;
    });
}

static int
tabfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
              off_t offset, struct fuse_file_info *fi) {
    MAKE_REQ("readdir", {
        cJSON_AddStringToObject(req, "path", path);
    }, {
        cJSON *entries = cJSON_GetObjectItemCaseSensitive(resp, "entries");
        cJSON *entry;
        cJSON_ArrayForEach(entry, entries) {
            filler(buf, cJSON_GetStringValue(entry), NULL, 0);
        }

        ret = 0;
    });
}

static int
tabfs_releasedir(const char *path, struct fuse_file_info *fi) {
    MAKE_REQ("releasedir", {
        cJSON_AddStringToObject(req, "path", path);
        cJSON_AddNumberToObject(req, "fh", fi->fh);
    }, {
        ret = 0;
    });
}

static struct fuse_operations tabfs_filesystem_operations = {
    .getattr  = tabfs_getattr, /* To provide size, permissions, etc. */
    .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,
    .readdir  = tabfs_readdir, /* To provide directory listing.      */
    .releasedir = tabfs_releasedir
};

int main(int argc, char **argv) {
    /* system("killall -9 tabfs"); */
    char killcmd[1000];
    sprintf(killcmd, "pgrep tabfs | grep -v %d | xargs kill -9", getpid());
    system(killcmd);
#ifdef __APPLE__
    system("diskutil umount force mnt > /dev/null");
#else
    system("fusermount -u mnt");
#endif

    l = fopen("log.txt", "w");
    for (int i = 0; i < argc; i++) {
        fprintf(l, "arg%d: [%s]\n", i, argv[i]); fflush(l);
    }
    char* fuse_argv[] = {argv[0], "-odirect_io", "-s", "-f", "mnt"};
    return fuse_main(5, fuse_argv, &tabfs_filesystem_operations, NULL);
}