-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmr-fuse.c
197 lines (159 loc) · 4.84 KB
/
cmr-fuse.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <jansson.h>
#include <libgen.h>
#define FUSE_USE_VERSION 30
#include <fuse.h>
#include "cmrapi.h"
#include "list.h"
#include "filelist_cache.h"
#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)
#ifdef PROJECT_VERSION
#define VERSION STR(PROJECT_VERSION)
#else
#define VERSION "???"
#endif
cmr_t cmr;
static int cmrops_getattr(const char *path, struct stat *stbuf) {
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
char *dir = dirname(strdup(path));
if (strcmp(dir, cmr.filelist_cache->basedir) != 0) {
struct list_t *lst;
cmr_list_dir(&cmr, dir, &lst); // TODO: Ignore list in cmr_list_dir() on lst=NULL
}
memset(stbuf, 0, sizeof(struct stat));
filelist_cache_data_t *data;
data = filelist_cache_find(cmr.filelist_cache, path);
if (data == NULL)
return -ENOENT;
if (data->kind == FK_FOLDER) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
stbuf->st_size = data->filesize;
}
else
{
stbuf->st_mode = S_IFREG | 0644;
stbuf->st_nlink = 1;
stbuf->st_size = data->filesize;
#ifdef __APPLE__
struct timespec ts = {.tv_sec=data->mtime, .tv_nsec=0};
stbuf->st_mtimespec = ts;
stbuf->st_ctimespec = ts;
stbuf->st_atimespec = ts;
stbuf->st_birthtimespec = ts;
#else
time_t ts = data->mtime;
stbuf->st_mtime = ts;
stbuf->st_ctime = ts;
stbuf->st_atime = ts;
#endif
}
return 0;
}
static int cmrops_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
struct list_t *lst, *current;
cmr_list_dir(&cmr, path, &lst);
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
current = lst;
while(current != NULL) {
filler(buf, (char*)(current->data), NULL, 0);
current = current->next;
}
list_free(&lst);
return 0;
}
int cmrops_read(const char *filename, char *buffer, size_t size, off_t offset, struct fuse_file_info *file_info) {
(void) file_info;
return cmr_get_file(&cmr, filename, size, offset, buffer);
}
static struct fuse_operations cmr_ops = {
.readdir = cmrops_readdir,
.getattr = cmrops_getattr,
.read = cmrops_read,
};
struct app_config {
char *config_file_name;
};
enum {
KEY_HELP,
KEY_VERSION,
};
#define APP_OPT(t, p, v) { t, offsetof(struct app_config, p), v }
static struct fuse_opt myfs_opts[] = {
APP_OPT("config=%s", config_file_name, 0),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_END
};
static int app_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
(void) data;
(void) arg;
switch (key) {
case KEY_HELP:
fprintf(stderr,
"usage: %s mountpoint [options]\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
"app options:\n"
" -o config=STRING\n"
, outargs->argv[0]);
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, &cmr_ops, NULL);
exit(EXIT_SUCCESS);
case KEY_VERSION:
fprintf(stderr, "%s version %s\n", outargs->argv[0], VERSION);
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, &cmr_ops, NULL);
exit(EXIT_SUCCESS);
}
return 1;
}
void fail(const char* message) {
fprintf(stderr, "ERROR: %s\n", message);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct app_config conf;
conf.config_file_name = NULL;
fuse_opt_parse(&args, &conf, myfs_opts, app_opt_proc);
if (conf.config_file_name == NULL) fail("You must set a config file with \"-o config=FILE\" option");
json_t *root;
json_error_t error;
root = json_load_file(conf.config_file_name, 0, &error);
if (root == NULL) fail("Cannot open or parse config file");
const char *user = json_string_value(json_object_get(root, "user"));
const char *domain = json_string_value(json_object_get(root, "domain"));
const char *password = json_string_value(json_object_get(root, "password"));
if (user == NULL || domain == NULL || password == NULL)
fail("There isn't user/domain/password setting in the config file");
cmr_init(&cmr, user, domain, password);
cmr_login(&cmr);
cmr_sdc_cookies(&cmr);
cmr_get_token(&cmr);
cmr_get_shard_urls(&cmr);
int ret = fuse_main(args.argc, args.argv, &cmr_ops, NULL);
cmr_finalize(&cmr);
return ret;
}