forked from redbo/cloudfuse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudfuse.c
525 lines (480 loc) · 12.2 KB
/
cloudfuse.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <pwd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stddef.h>
#include "cloudfsapi.h"
#include "config.h"
#define OPTION_SIZE 1024
static int cache_timeout;
typedef struct dir_cache
{
char *path;
dir_entry *entries;
time_t cached;
struct dir_cache *next, *prev;
} dir_cache;
static dir_cache *dcache;
static pthread_mutex_t dmut;
typedef struct
{
int fd;
int flags;
} openfile;
static void dir_for(const char *path, char *dir)
{
strncpy(dir, path, MAX_PATH_SIZE);
char *slash = strrchr(dir, '/');
if (slash)
*slash = '\0';
}
static dir_cache *new_cache(const char *path)
{
dir_cache *cw = (dir_cache *)calloc(sizeof(dir_cache), 1);
cw->path = strdup(path);
cw->prev = NULL;
cw->entries = NULL;
cw->cached = time(NULL);
if (dcache)
dcache->prev = cw;
cw->next = dcache;
return (dcache = cw);
}
static int caching_list_directory(const char *path, dir_entry **list)
{
pthread_mutex_lock(&dmut);
if (!strcmp(path, "/"))
path = "";
dir_cache *cw;
for (cw = dcache; cw; cw = cw->next)
if (!strcmp(cw->path, path))
break;
if (!cw)
{
if (!list_directory(path, list))
return 0;
cw = new_cache(path);
}
else if (cache_timeout > 0 && (time(NULL) - cw->cached > cache_timeout))
{
if (!list_directory(path, list))
return 0;
free_dir_list(cw->entries);
cw->cached = time(NULL);
}
else
*list = cw->entries;
cw->entries = *list;
pthread_mutex_unlock(&dmut);
return 1;
}
static void update_dir_cache(const char *path, off_t size, int isdir)
{
pthread_mutex_lock(&dmut);
dir_cache *cw;
dir_entry *de;
char dir[MAX_PATH_SIZE];
dir_for(path, dir);
for (cw = dcache; cw; cw = cw->next)
{
if (!strcmp(cw->path, dir))
{
for (de = cw->entries; de; de = de->next)
{
if (!strcmp(de->full_name, path))
{
de->size = size;
pthread_mutex_unlock(&dmut);
return;
}
}
de = (dir_entry *)malloc(sizeof(dir_entry));
de->size = size;
de->isdir = isdir;
de->name = strdup(&path[strlen(cw->path)+1]);
de->full_name = strdup(path);
de->content_type = strdup(isdir ? "application/directory" : "application/octet-stream");
de->last_modified = time(NULL);
de->next = cw->entries;
cw->entries = de;
if (isdir)
new_cache(path);
break;
}
}
pthread_mutex_unlock(&dmut);
}
static void dir_decache(const char *path)
{
dir_cache *cw;
pthread_mutex_lock(&dmut);
dir_entry *de, *tmpde;
char dir[MAX_PATH_SIZE];
dir_for(path, dir);
for (cw = dcache; cw; cw = cw->next)
{
if (!strcmp(cw->path, path))
{
if (cw == dcache)
dcache = cw->next;
if (cw->prev)
cw->prev->next = cw->next;
if (cw->next)
cw->next->prev = cw->prev;
free_dir_list(cw->entries);
free(cw->path);
free(cw);
}
else if (cw->entries && !strcmp(dir, cw->path))
{
if (!strcmp(cw->entries->full_name, path))
{
de = cw->entries;
cw->entries = de->next;
de->next = NULL;
free_dir_list(de);
}
else for (de = cw->entries; de->next; de = de->next)
{
if (!strcmp(de->next->full_name, path))
{
tmpde = de->next;
de->next = de->next->next;
tmpde->next = NULL;
free_dir_list(tmpde);
break;
}
}
}
}
pthread_mutex_unlock(&dmut);
}
static dir_entry *path_info(const char *path)
{
char dir[MAX_PATH_SIZE];
dir_for(path, dir);
dir_entry *tmp;
if (!caching_list_directory(dir, &tmp))
return NULL;
for (; tmp; tmp = tmp->next)
{
if (!strcmp(tmp->full_name, path))
return tmp;
}
return NULL;
}
static int cfs_getattr(const char *path, struct stat *stbuf)
{
stbuf->st_uid = geteuid();
stbuf->st_gid = getegid();
if (!strcmp(path, "/"))
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
dir_entry *de = path_info(path);
if (!de)
return -ENOENT;
stbuf->st_ctime = stbuf->st_mtime = de->last_modified;
if (de->isdir)
{
stbuf->st_size = 0;
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else
{
stbuf->st_size = de->size;
/* calc. blocks as if 4K blocksize filesystem; stat uses units of 512B */
stbuf->st_blocks = ((4095 + de->size) / 4096) * 8;
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
}
return 0;
}
static int cfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *info)
{
openfile *of = (openfile *)(uintptr_t)info->fh;
if (of)
{
stbuf->st_size = file_size(of->fd);
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
return 0;
}
return -ENOENT;
}
static int cfs_readdir(const char *path, void *buf, fuse_fill_dir_t filldir, off_t offset, struct fuse_file_info *info)
{
dir_entry *de;
if (!caching_list_directory(path, &de))
return -ENOLINK;
filldir(buf, ".", NULL, 0);
filldir(buf, "..", NULL, 0);
for (; de; de = de->next)
filldir(buf, de->name, NULL, 0);
return 0;
}
static int cfs_mkdir(const char *path, mode_t mode)
{
if (create_directory(path))
{
update_dir_cache(path, 0, 1);
return 0;
}
return -ENOENT;
}
static int cfs_create(const char *path, mode_t mode, struct fuse_file_info *info)
{
FILE *temp_file = tmpfile();
openfile *of = (openfile *)malloc(sizeof(openfile));
of->fd = dup(fileno(temp_file));
fclose(temp_file);
of->flags = info->flags;
info->fh = (uintptr_t)of;
update_dir_cache(path, 0, 0);
info->direct_io = 1;
return 0;
}
static int cfs_open(const char *path, struct fuse_file_info *info)
{
FILE *temp_file = tmpfile();
if (!(info->flags & O_WRONLY))
{
if (!object_write_fp(path, temp_file))
{
fclose(temp_file);
return -ENOENT;
}
update_dir_cache(path, 0, 0);
}
openfile *of = (openfile *)malloc(sizeof(openfile));
of->fd = dup(fileno(temp_file));
fclose(temp_file);
of->flags = info->flags;
info->fh = (uintptr_t)of;
info->direct_io = 1;
return 0;
}
static int cfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *info)
{
return pread(((openfile *)(uintptr_t)info->fh)->fd, buf, size, offset);
}
static int cfs_flush(const char *path, struct fuse_file_info *info)
{
openfile *of = (openfile *)(uintptr_t)info->fh;
if (of)
{
update_dir_cache(path, file_size(of->fd), 0);
if (of->flags & O_RDWR || of->flags & O_WRONLY)
{
FILE *fp = fdopen(dup(of->fd), "r");
rewind(fp);
if (!object_read_fp(path, fp))
{
fclose(fp);
return -ENOENT;
}
fclose(fp);
}
}
return 0;
}
static int cfs_release(const char *path, struct fuse_file_info *info)
{
close(((openfile *)(uintptr_t)info->fh)->fd);
return 0;
}
static int cfs_rmdir(const char *path)
{
if (delete_object(path))
{
dir_decache(path);
return 0;
}
return -ENOENT;
}
static int cfs_ftruncate(const char *path, off_t size, struct fuse_file_info *info)
{
openfile *of = (openfile *)(uintptr_t)info->fh;
if (ftruncate(of->fd, size))
return -errno;
lseek(of->fd, 0, SEEK_SET);
update_dir_cache(path, size, 0);
return 0;
}
static int cfs_write(const char *path, const char *buf, size_t length, off_t offset, struct fuse_file_info *info)
{
update_dir_cache(path, offset + length, 0);
return pwrite(((openfile *)(uintptr_t)info->fh)->fd, buf, length, offset);
}
static int cfs_unlink(const char *path)
{
if (delete_object(path))
{
dir_decache(path);
return 0;
}
return -ENOENT;
}
static int cfs_fsync(const char *path, int idunno, struct fuse_file_info *info)
{
return 0;
}
static int cfs_truncate(const char *path, off_t size)
{
object_truncate(path, size);
return 0;
}
static int cfs_statfs(const char *path, struct statvfs *stat)
{
stat->f_bsize = 4096;
stat->f_frsize = 4096;
stat->f_blocks = INT_MAX;
stat->f_bfree = stat->f_blocks;
stat->f_bavail = stat->f_blocks;
stat->f_files = INT_MAX;
stat->f_ffree = INT_MAX;
stat->f_favail = INT_MAX;
stat->f_namemax = INT_MAX;
return 0;
}
static int cfs_chown(const char *path, uid_t uid, gid_t gid)
{
return 0;
}
static int cfs_chmod(const char *path, mode_t mode)
{
return 0;
}
static int cfs_rename(const char *src, const char *dst)
{
dir_entry *src_de = path_info(src);
if (!src_de)
return -ENOENT;
if (src_de->isdir)
return -EISDIR;
if (copy_object(src, dst))
{
/* FIXME this isn't quite right as doesn't preserve last modified */
update_dir_cache(dst, src_de->size, 0);
return cfs_unlink(src);
}
return -EIO;
}
char *get_home_dir()
{
char *home;
if ((home = getenv("HOME")) && !access(home, R_OK))
return home;
struct passwd *pwd = getpwuid(geteuid());
if ((home = pwd->pw_dir) && !access(home, R_OK))
return home;
return "~";
}
static struct options {
char username[OPTION_SIZE];
char api_key[OPTION_SIZE];
char cache_timeout[OPTION_SIZE];
char authurl[OPTION_SIZE];
char use_snet[OPTION_SIZE];
char container[OPTION_SIZE];
} options = {
.username = "",
.api_key = "",
.cache_timeout = "600",
.authurl = "https://auth.api.rackspacecloud.com/v1.0",
.use_snet = "false",
.container = "",
};
int parse_option(void *data, const char *arg, int key, struct fuse_args *outargs)
{
if (sscanf(arg, " username = %[^\r\n ]", options.username) ||
sscanf(arg, " api_key = %[^\r\n ]", options.api_key) ||
sscanf(arg, " cache_timeout = %[^\r\n ]", options.cache_timeout) ||
sscanf(arg, " authurl = %[^\r\n ]", options.authurl) ||
sscanf(arg, " use_snet = %[^\r\n ]", options.use_snet) ||
sscanf(arg, " container = %[^\r\n ]", options.container))
return 0;
if (!strcmp(arg, "-f") || !strcmp(arg, "-d") || !strcmp(arg, "debug"))
cloudfs_debug(1);
return 1;
}
int main(int argc, char **argv)
{
char settings_filename[MAX_PATH_SIZE] = "";
FILE *settings;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
fuse_opt_parse(&args, &options, NULL, parse_option);
snprintf(settings_filename, sizeof(settings_filename), "%s/.cloudfuse", get_home_dir());
if ((settings = fopen(settings_filename, "r")))
{
char line[OPTION_SIZE];
while (fgets(line, sizeof(line), settings))
parse_option(NULL, line, -1, &args);
fclose(settings);
}
cache_timeout = atoi(options.cache_timeout);
if (!*options.username || !*options.api_key)
{
fprintf(stderr, "Unable to determine username and API key.\n\n");
fprintf(stderr, "These can be set either as mount options or in"
" a file named %s\n\n", settings_filename);
fprintf(stderr, " username=[Mosso username]\n");
fprintf(stderr, " api_key=[Mosso api key]\n\n");
fprintf(stderr, "These entries are optional:\n\n");
fprintf(stderr, " cache_timeout=[seconds for directory caching]\n");
fprintf(stderr, " use_snet=[True to connect to snet]\n");
fprintf(stderr, " authurl=[used for testing]\n");
return 1;
}
if(*options.container){
set_container(options.container);
}
if (!cloudfs_connect(options.username, options.api_key, options.authurl,
!strcasecmp(options.use_snet, "true")))
{
fprintf(stderr, "Unable to authenticate.\n");
return 1;
}
#ifndef HAVE_OPENSSL
#warning Compiling without libssl, will run single-threaded.
fuse_opt_add_arg(&args, "-s");
#endif
struct fuse_operations cfs_oper = {
.readdir = cfs_readdir,
.mkdir = cfs_mkdir,
.read = cfs_read,
.create = cfs_create,
.open = cfs_open,
.fgetattr = cfs_fgetattr,
.getattr = cfs_getattr,
.flush = cfs_flush,
.release = cfs_release,
.rmdir = cfs_rmdir,
.ftruncate = cfs_ftruncate,
.truncate = cfs_truncate,
.write = cfs_write,
.unlink = cfs_unlink,
.fsync = cfs_fsync,
.statfs = cfs_statfs,
.chmod = cfs_chmod,
.chown = cfs_chown,
.rename = cfs_rename,
};
pthread_mutex_init(&dmut, NULL);
signal(SIGPIPE, SIG_IGN);
return fuse_main(args.argc, args.argv, &cfs_oper, &options);
}