-
Notifications
You must be signed in to change notification settings - Fork 2
/
mount_cbfs.c
370 lines (312 loc) · 9.78 KB
/
mount_cbfs.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#define FUSE_USE_VERSION 25
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <libcouchbase/couchbase.h>
#include "cJSON.h"
#include "config.h"
static lcb_t instance;
struct config *cfg;
static void error_handler(lcb_t instance, lcb_error_t err, const char *info)
{
fprintf(stderr, "FATAL! an error occured: %s (%s)\n",
lcb_strerror(instance, err), info ? info : "none");
exit(EXIT_FAILURE);
}
struct SizedBuffer {
char *data;
size_t size;
};
static const char *urlencode(const char *path) {
static char buffer[1024];
int len = strlen(path);
int n = 0;
for (int ii = 0; ii < len; ++ii) {
if (isalpha(path[ii]) || isdigit(path[ii])) {
buffer[n++] = path[ii];
} else {
sprintf(buffer + n, "%%%02X", path[ii]);
n += 3;
}
}
buffer[n] = '\0';
return buffer;
}
static void complete_http_callback(lcb_http_request_t req, lcb_t instance,
const void *cookie, lcb_error_t error,
const lcb_http_resp_t *resp)
{
struct SizedBuffer *sb = (void*)cookie;
if (error == LCB_SUCCESS) {
/* Allocate one byte extra for a zero term */
sb->data = malloc(resp->v.v0.nbytes + 1);
sb->size = resp->v.v0.nbytes;
memcpy(sb->data, resp->v.v0.bytes, resp->v.v0.nbytes);
sb->data[resp->v.v0.nbytes] = '\0';
}
}
static void initialize(void)
{
struct lcb_create_st copt;
lcb_error_t error;
cfg = get_configuration();
memset(&copt, 0, sizeof(copt));
copt.v.v0.host = cfg->couchbase_host;
copt.v.v0.user = cfg->couchbase_username;
copt.v.v0.passwd = cfg->couchbase_password;
copt.v.v0.bucket = cfg->couchbase_bucket;
if ((error = lcb_create(&instance, &copt)) != LCB_SUCCESS) {
fprintf(stderr, "Failed to create libcuchbase instance: %s\n",
lcb_strerror(NULL, error));
exit(EXIT_FAILURE);
}
lcb_behavior_set_syncmode(instance, LCB_SYNCHRONOUS);
lcb_set_error_callback(instance, error_handler);
lcb_set_http_complete_callback(instance, complete_http_callback);
if ((error = lcb_connect(instance)) != LCB_SUCCESS) {
fprintf(stderr, "Failed to connect to cluster: %s\n",
lcb_strerror(instance, error));
exit(EXIT_FAILURE);
}
}
static lcb_error_t uri_execute_get(const char *uri, struct SizedBuffer *sb) {
lcb_http_cmd_t cmd = {
.version = 1,
.v.v1 = {
.path = uri,
.npath = strlen(uri),
.body = NULL,
.nbody = 0,
.method = LCB_HTTP_METHOD_GET,
.chunked = 0,
.content_type = "application/x-www-form-urlencoded",
.host = cfg->cbfs_host,
.username = cfg->cbfs_username,
.password = cfg->cbfs_password
}
};
return lcb_make_http_request(instance, sb, LCB_HTTP_TYPE_RAW, &cmd, NULL);
}
static char *ls(const char *path, int *error) {
*error = 0;
char buffer[1024];
int len = snprintf(buffer, sizeof(buffer), "/.cbfs/list/%s",
urlencode(path + 1));
struct SizedBuffer sb = { .data = NULL, .size = 0 };
lcb_error_t err = uri_execute_get(buffer, &sb);
if (err != LCB_SUCCESS) {
fprintf(stderr, "Failed to do http: %s\n", lcb_strerror(instance, err));
}
if (sb.data == NULL || memcmp(sb.data, "{\"dirs\":{},\"files\":{}", 21) == 0) {
free(sb.data);
*error = -ENOENT;
return 0;
}
return sb.data;
}
static char *mystat(const char *path, int *error) {
*error = 0;
struct SizedBuffer sb = {.data = 0, .size = 0};
char buffer[1024];
snprintf(buffer, sizeof(buffer), "/.cbfs/list/%s?includeMeta=true",
urlencode(path + 1));
lcb_error_t err = uri_execute_get(buffer, &sb);
if (err != LCB_SUCCESS) {
fprintf(stderr, "Failed to do http: %s\n", lcb_strerror(instance, err));
free(sb.data);
*error = -EIO;
return NULL;
}
if (sb.data == NULL || memcmp(sb.data, "{\"dirs\":{},\"files\":{}", 21) == 0) {
free(sb.data);
*error = -ENOENT;
return NULL;
}
return sb.data;
}
static int my_stat(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
int error;
char *data = mystat(path, &error);
if (error != 0) {
return error;
}
cJSON *entry = cJSON_Parse(data);
if (entry == NULL) {
fprintf(stderr, "Failed to parse\n");
free(data);
return -EIO; /* any better? */
}
cJSON *dirs = cJSON_GetObjectItem(entry, "dirs");
cJSON *files = cJSON_GetObjectItem(entry, "files");
if (dirs == NULL || files == NULL) {
cJSON_Delete(entry);
free(data);
return -EIO; /* any better?? */
}
/*
* This is a directory if it contains something in the dirs array,
* of if there are multiple entries in the files array
*/
if (cJSON_GetArraySize(dirs) != 0 || cJSON_GetArraySize(files) > 1){
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
cJSON_Delete(entry);
free(data);
return 0;
}
assert(cJSON_GetArraySize(files) == 1);
/*
** Ok, this is a regular file unless the only file doesn't point to
** myself
*/
char *me = strrchr(path, '/') + 1; /* (all path's have a leading / ) */
cJSON *file = files->child;
if (strcmp(me, file->string) != 0) {
/* Yeah, this isn't me.. */
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
cJSON_Delete(entry);
free(data);
return 0;
}
/* Ok, time to look at the attributes... */
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
/* Pick out the fields */
cJSON *obj = file->child;
while (obj != NULL) {
if (strcmp("length", obj->string) == 0) {
if (obj->type == cJSON_Number) {
stbuf->st_size = obj->valueint;
} else {
fprintf(stderr, "Illegal type specified for size\n");
}
} else if (strcmp("modified", obj->string) == 0) {
char *ptr = strchr(obj->valuestring, '.');
*ptr = '\0';
struct tm tm;
if (strptime(obj->valuestring, "%Y-%m-%dT%T", &tm) == NULL) {
fprintf(stderr, "Failed to parse date\n");
} else {
stbuf->st_mtime = mktime(&tm);
stbuf->st_ctime = stbuf->st_atime = stbuf->st_mtime;
}
} else if (strcmp("ctype", obj->string) == 0 ||
strcmp("headers", obj->string) == 0 ||
strcmp("oid", obj->string) == 0 ||
strcmp("revno", obj->string) == 0 ||
strcmp("type", obj->string) == 0 ||
strcmp("userdata", obj->string) == 0) {
/* Ignored */
} else {
fprintf(stderr, "I've never seen this attribute before: %s\n",
obj->string);
}
obj = obj->next;
}
cJSON_Delete(entry);
free(data);
return 0;
}
static int cbfs_getattr(const char *path, struct stat *stbuf)
{
return my_stat(path, stbuf);
}
static int cbfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
int error;
char *data = ls(path, &error);
if (error != 0) {
fprintf(stderr, "Failed to read\n");
return error;
}
cJSON *json = cJSON_Parse(data);
if (json == NULL) {
fprintf(stderr, "Failed to parse\n");
free(data);
return -EIO; /* any better? */
}
/* I guess I should add these ;-) */
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
/* Add the stuff from the json doc */
const char * const keys[] = { "dirs", "files" };
for (int ii = 0; ii < 2; ++ii) {
cJSON *list = cJSON_GetObjectItem(json, keys[ii]);
if (list == NULL) {
continue;
}
cJSON *obj = list->child;
while (obj != NULL) {
filler(buf, obj->string, NULL, 0);
obj = obj->next;
}
}
cJSON_Delete(json);
free(data);
return 0;
}
static int cbfs_open(const char *path, struct fuse_file_info *fi)
{
/*
* Just ensure that you're not trying to write.. after all
* this is a read only filesystem..
*/
if ((fi->flags & O_ACCMODE) != O_RDONLY) {
return -EACCES;
}
struct stat st;
if (my_stat(path, &st) != 0) {
fprintf(stderr, "stat error:\n");
return -ENOENT;
}
return 0;
}
static int cbfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct SizedBuffer sb = {.data = 0, .size = 0};
lcb_error_t err = uri_execute_get(path, &sb);
if (err != LCB_SUCCESS || sb.data == NULL) {
fprintf(stderr, "Failed to do http: %s\n", lcb_strerror(instance, err));
free(sb.data);
return -EIO;
}
if (offset > sb.size) {
free(sb.data);
return -E2BIG;
}
if (size > (sb.size - offset)) {
size = sb.size - offset;
}
// Copy the data
memcpy(buf, sb.data + offset, size);
free(sb.data);
return size;
}
static struct fuse_operations cbfs_oper = {
.getattr = cbfs_getattr,
.open = cbfs_open,
.read = cbfs_read,
.readdir = cbfs_readdir,
};
int main(int argc, char **argv)
{
initialize();
return fuse_main(argc, argv, &cbfs_oper);
}