aboutsummaryrefslogtreecommitdiffstats
path: root/fs/tabfs.c
blob: fecec221368d0da5f007e792969d1ef0d34e7ba6 (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
// This file should rarely need to be changed. (which is intentional,
// because it is a pain to program here, it's a pain to recompile and
// reload it, and it's a pain to debug it.)  Most of the real meat of
// TabFS is on the extension side, not here.

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <fuse.h>

#include "vendor/frozen.h"
#include "vendor/frozen.c"

FILE* l;

static void send_request(const char *fmt, ...) {
    va_list args; va_start(args, fmt);

    char request_data[1024*1024]; // max size of native->Chrome message
    struct json_out out = JSON_OUT_BUF(request_data, sizeof(request_data));
    unsigned int request_len = json_vprintf(&out, fmt, args);

    va_end(args);

    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); */
}

static int await_response(char **resp) {
    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.
        *resp = "{ \"error\": 5 }";
        return strlen(*resp);
    }

    *resp = response_data;
    return response_len;
}

#define receive_response(fmt, ...)                                      \
    do {                                                                \
        char *resp; int resp_len;                                       \
        resp_len = await_response(&resp);                               \
        if (!resp_len) return -EIO;                                     \
                                                                        \
        int err;                                                        \
        if (json_scanf(resp, resp_len, "{error: %d}", &err) && err) {   \
            free(resp); return -err;                                    \
        }                                                               \
                                                                        \
        json_scanf(resp, resp_len, fmt, __VA_ARGS__);                   \
        free(resp);                                                     \
    } while (0)

static int tabfs_getattr(const char *path, struct stat *stbuf) {
    send_request("{op: %Q, path: %Q}", "getattr", path);

    memset(stbuf, 0, sizeof(struct stat));
    receive_response("{st_mode: %d, st_nlink: %d, st_size: %d}",
                     &stbuf->st_mode, &stbuf->st_nlink, &stbuf->st_size);
    return 0;
}

static int tabfs_readlink(const char *path, char *buf, size_t size) {
    send_request("{op: %Q, path: %Q}", "readlink", path);

    char *scan_buf; int scan_len;
    receive_response("{buf: %V}", &scan_buf, &scan_len);
    memcpy(buf, scan_buf, scan_len < size ? scan_len : size); free(scan_buf);

    return 0;
}

static int tabfs_open(const char *path, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, flags: %d}", "open", path, fi->flags);

    receive_response("{fh: %d}", &fi->fh);

    return 0;
}

static int
tabfs_read(const char *path, char *buf, size_t size, off_t offset,
           struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, size: %d, offset: %d, fh: %d, flags: %d}",
                 "read", path, size, offset, fi->fh, fi->flags);

    char *scan_buf; int scan_len;
    receive_response("{buf: %V}", &scan_buf, &scan_len);
    memcpy(buf, scan_buf, scan_len < size ? scan_len : size); free(scan_buf);

    return scan_len;
}

static int
tabfs_write(const char *path, const char *buf, size_t size, off_t offset,
            struct fuse_file_info *fi) {
    
    send_request("{op: %Q, path: %Q, buf: %V, offset: %d, fh: %d, flags: %d}",
                 "write", path, buf, size, offset, fi->fh, fi->flags);

    int ret; receive_response("{size: %d}", &ret); return ret;
}

static int tabfs_release(const char *path, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, fh: %d}",
                 "release", path, fi->fh);

    receive_response("{}", NULL);
    return 0;
}

static int tabfs_opendir(const char *path, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, flags: %d}",
                 "opendir", path, fi->flags);
    
    receive_response("{fh: %d}", &fi->fh);
    return 0;
}

static int
tabfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
              off_t offset, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, offset: %d}",
                 "readdir", path, offset);

    char *resp; int resp_len;
    resp_len = await_response(&resp);
    if (!resp_len) return -EIO;

    struct json_token t;
    for (int i = 0; json_scanf_array_elem(resp, resp_len, ".entries", i, &t) > 0; i++) {
        char entry[t.len + 1]; snprintf(entry, t.len + 1, "%.*s", t.len, t.ptr);
        filler(buf, entry, NULL, 0);
    }

    free(resp);
    return 0;
}

static int tabfs_releasedir(const char *path, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, fh: %d}",
                 "releasedir", path, fi->fh);

    receive_response("{}", NULL);
    return 0;
}

static int tabfs_truncate(const char *path, off_t size) {
    send_request("{op: %Q, path: %Q, size: %d}",
                 "truncate", path, size);

    receive_response("{}", NULL);
    return 0;
}

static int tabfs_unlink(const char *path) {
    send_request("{op: %Q, path: %Q}", "unlink", path);

    receive_response("{}", NULL);
    return 0;
}

static int tabfs_mkdir(const char *path, mode_t mode) {
    send_request("{op: %Q, path: %Q, mode: %d}", "mkdir", path, mode);

    receive_response("{}", NULL);
    return 0;
}

static int tabfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
    send_request("{op: %Q, path: %Q, mode: %d}", "mkdir", path, mode);

    receive_response("{}", NULL);
    return 0;
}

static struct fuse_operations tabfs_filesystem_operations = {
    .getattr  = tabfs_getattr,
    .readlink = tabfs_readlink,

    .open     = tabfs_open,
    .read     = tabfs_read,
    .write    = tabfs_write,
    .release  = tabfs_release,

    .opendir  = tabfs_opendir,
    .readdir  = tabfs_readdir,
    .releasedir = tabfs_releasedir,

    .truncate  = tabfs_truncate,
    .unlink = tabfs_unlink,

    .mkdir = tabfs_mkdir,
    .create = tabfs_create
};

int main(int argc, char **argv) {
    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");
#elif __FreeBSD__
    system("umount -f mnt");
#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);
}