-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlzopfs.cc
209 lines (177 loc) · 4.62 KB
/
lzopfs.cc
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
#include "lzopfs.h"
#include "BlockCache.h"
#include "FileList.h"
#include "CompressedFile.h"
#include "OpenCompressedFile.h"
#include "ThreadPool.h"
#include "PathUtils.h"
#include "GzipFile.h"
#include <cstddef>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define FUSE_USE_VERSION 30
#include <fuse.h>
namespace {
typedef uint64_t FuseFH;
const size_t CacheSize = 1024 * 1024 * 32;
const size_t DefaultBlockFactor = 32;
struct FSData {
FileList *files;
ThreadPool pool;
BlockCache cache;
FSData(FileList* f) : files(f), pool(), cache(pool) {
cache.maxSize(CacheSize);
}
~FSData() { delete files; }
};
FSData *fsdata() {
return reinterpret_cast<FSData*>(fuse_get_context()->private_data);
}
void except(std::runtime_error& e) {
fprintf(stderr, "%s: %s\n", typeid(e).name(), e.what());
exit(1);
}
extern "C" void *lf_init(struct fuse_conn_info *conn
#if FUSE_MAJOR_VERSION >= 3
, struct fuse_config *cfg
#endif
) {
void *priv = fuse_get_context()->private_data;
return new FSData(reinterpret_cast<FileList*>(priv));
}
extern "C" int lf_getattr(const char *path, struct stat *stbuf
#if FUSE_MAJOR_VERSION >= 3
, struct fuse_file_info *fi
#endif
) {
memset(stbuf, 0, sizeof(*stbuf));
CompressedFile *file;
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 3;
} else if ((file = fsdata()->files->find(path))) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = file->uncompressedSize();
} else {
return -ENOENT;
}
return 0;
}
struct DirFiller {
void *buf;
fuse_fill_dir_t filler;
DirFiller(void *b, fuse_fill_dir_t f) : buf(b), filler(f) { }
void operator()(const std::string& path) {
filler(buf, path.c_str() + 1, NULL, 0
#if FUSE_MAJOR_VERSION >= 3
, (fuse_fill_dir_flags)0
#endif
);
}
};
extern "C" int lf_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi
#if FUSE_MAJOR_VERSION >= 3
, fuse_readdir_flags flags
#endif
) {
if (strcmp(path, "/") != 0)
return -ENOENT;
DirFiller dirFiller(buf, filler);
dirFiller("/.");
dirFiller("/..");
fsdata()->files->forNames(dirFiller);
return 0;
}
extern "C" int lf_open(const char *path, struct fuse_file_info *fi) {
CompressedFile *file;
if (!(file = fsdata()->files->find(path)))
return -ENOENT;
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
try {
fi->fh = FuseFH(new OpenCompressedFile(file, O_RDONLY));
return 0;
} catch (FileHandle::Exception& e) {
return e.error_code;
}
}
extern "C" int lf_release(const char *path, struct fuse_file_info *fi) {
delete reinterpret_cast<OpenCompressedFile*>(fi->fh);
fi->fh = 0;
return 0;
}
extern "C" int lf_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
int ret = -1;
try {
ret = reinterpret_cast<OpenCompressedFile*>(fi->fh)->read(
fsdata()->cache, buf, size, offset);
} catch (std::runtime_error& e) {
except(e);
}
return ret;
}
typedef std::vector<std::string> paths_t;
struct OptData {
const char *nextSource;
paths_t* files;
unsigned blockFactor;
const char *indexRoot;
};
static struct fuse_opt lf_opts[] = {
{ "--block-factor=%lu", offsetof(OptData, blockFactor), 0 },
{ "--index-root=%s", offsetof(OptData, indexRoot), 0 },
{NULL, -1U, 0},
};
extern "C" int lf_opt_proc(void *data, const char *arg, int key,
struct fuse_args *outargs) {
OptData *optd = reinterpret_cast<OptData*>(data);
if (key == FUSE_OPT_KEY_NONOPT) {
if (optd->nextSource) {
try {
fprintf(stderr, "%s\n", optd->nextSource);
optd->files->push_back(PathUtils::realpath(optd->nextSource));
} catch (std::runtime_error& e) {
except(e);
}
}
optd->nextSource = arg;
return 0;
}
return 1;
}
} // anon namespace
int main(int argc, char *argv[]) {
try {
umask(0);
struct fuse_operations ops;
memset(&ops, 0, sizeof(ops));
ops.getattr = lf_getattr;
ops.readdir = lf_readdir;
ops.open = lf_open;
ops.release = lf_release;
ops.read = lf_read;
ops.init = lf_init;
// FIXME: help with options?
paths_t files;
OptData optd = { 0, &files, DefaultBlockFactor, "" };
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
fuse_opt_parse(&args, &optd, lf_opts, lf_opt_proc);
if (optd.nextSource)
fuse_opt_add_arg(&args, optd.nextSource);
OpenParams params(CacheSize, optd.indexRoot, optd.blockFactor);
FileList *flist = new FileList(params);
for (paths_t::const_iterator iter = files.begin(); iter != files.end();
++iter) {
flist->add(*iter);
}
fprintf(stderr, "Ready\n");
return fuse_main(args.argc, args.argv, &ops, flist);
} catch (std::runtime_error& e) {
except(e);
}
}