-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfusecache.c
More file actions
466 lines (372 loc) · 9.72 KB
/
fusecache.c
File metadata and controls
466 lines (372 loc) · 9.72 KB
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
/*
* Copyright (c) 2024 Nils Zweiling
*
* This file is part of fusecache which is released under the MIT license.
* See file LICENSE or go to https://github.com/zwodev/fusecache/tree/master/LICENSE
* for full license details.
*/
#define FUSE_USE_VERSION 31
#include <sys/types.h>
#include <utime.h>
#include <time.h>
#include <stdbool.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <filesystem>
#include "Helper.h"
#include "Log.h"
#include "CacheManager.h"
static fuse_fill_dir_flags fill_dir_plus = (fuse_fill_dir_flags ) 0;
CacheManager* cache_manager = nullptr;
Log* g_log = nullptr;
static void *fc_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
{
(void) conn;
cfg->use_ino = 1;
cfg->entry_timeout = 0;
cfg->attr_timeout = 0;
cfg->negative_timeout = 0;
return NULL;
}
static int fc_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
//g_log->debug(formatStr("fc_getattr: %s", path));
(void) fi;
int res;
std::string orig_path = cache_manager->origFilePath(path);
res = lstat(orig_path.c_str(), stbuf);
if (res == -1) {
std::string cache_path = cache_manager->writeCacheFilePath(path);
res = lstat(cache_path.c_str(), stbuf);
}
if (res == -1)
return -errno;
return 0;
}
static int fc_access(const char *path, int mask)
{
g_log->debug(formatStr("fc_access: %s", path));
int res;
std::string orig_path = cache_manager->origFilePath(path);
res = access(orig_path.c_str(), mask);
if (res == -1) {
std::string cache_path = cache_manager->writeCacheFilePath(path);
res = access(cache_path.c_str(), mask);
}
if (res == -1)
return -errno;
return 0;
}
static int fc_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
//g_log->debug(formatStr("fc_readdir: %s", path));
DIR *dp;
struct dirent *de;
(void) offset;
(void) fi;
(void) flags;
std::string orig_path = cache_manager->origFilePath(path).c_str();
dp = opendir(orig_path.c_str());
if (dp == NULL)
return -errno;
while ((de = readdir(dp)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0, fill_dir_plus))
break;
}
closedir(dp);
return 0;
}
static int fc_mkdir(const char *path, mode_t mode)
{
g_log->debug(formatStr("fc_mkdir: %s", path));
std::string cache_path = cache_manager->writeCacheFilePath(path);
int res = mkdir(cache_path.c_str(), mode);
if (res == -1)
return -errno;
return 0;
}
static int fc_unlink(const char *path)
{
g_log->debug(formatStr("fc_unlink: %s", path));
int res = unlink(cache_manager->writeCacheFilePath(path).c_str());
if (res == -1)
return -errno;
return 0;
}
static int fc_rmdir(const char *path)
{
g_log->debug(formatStr("fc_rmdir: %s", path));
int res;
std::string orig_path = cache_manager->origFilePath(path);
res = rmdir(orig_path.c_str());
if (res == -1)
return -errno;
return 0;
}
static int fc_rename(const char *from, const char *to, unsigned int flags)
{
if (flags)
return -EINVAL;
std::string cache_path_from = cache_manager->writeCacheFilePath(from);
std::string cache_path_to = cache_manager->writeCacheFilePath(to);
int res = rename(cache_path_from.c_str(), cache_path_to.c_str());
if (res == -1)
return -errno;
return 0;
}
static int fc_chmod(const char *path, mode_t mode,
struct fuse_file_info *fi)
{
(void) fi;
std::string cache_path = cache_manager->writeCacheFilePath(path);
int res = chmod(cache_path.c_str(), mode);
if (res == -1)
return -errno;
return 0;
}
static int fc_chown(const char *path, uid_t uid, gid_t gid, struct fuse_file_info *fi)
{
(void) fi;
std::string orig_path = cache_manager->origFilePath(path);
int res = lchown(orig_path.c_str(), uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int fc_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
g_log->debug(formatStr("fc_create: %s", path));
int res = cache_manager->createFile(path, mode, fi->flags);
if (res < 0) {
return res;
}
fi->fh = res;
return 0;
}
static int fc_open(const char *path, struct fuse_file_info *fi)
{
g_log->debug(formatStr("fc_open: %s", path));
int res = cache_manager->openFile(path, fi->flags);
if (res < 0) {
g_log->debug(formatStr("ERROR OPENING FILE - FLAGS: %d", fi->flags));
return res;
}
fi->fh = res;
return 0;
}
static int fc_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int res = cache_manager->readFile(fi->fh, buf, size, offset);
return res;
}
static int fc_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int res = cache_manager->writeFile(fi->fh, buf, size, offset);
return res;
}
static int fc_statfs(const char *path, struct statvfs *stbuf)
{
g_log->debug(formatStr("fc_statfs: %s", path));
int res;
std::string orig_path = cache_manager->origFilePath(path);
res = statvfs(orig_path.c_str(), stbuf);
if (res == -1)
return -errno;
return 0;
}
static int fc_release(const char *path, struct fuse_file_info *fi)
{
g_log->debug(formatStr("fc_release: %s", path));
(void) path;
int res = cache_manager->closeFile(fi->fh);
return res;
}
static off_t fc_lseek(const char *path, off_t off, int whence, struct fuse_file_info *fi)
{
int fd;
off_t res;
std::string orig_path = cache_manager->origFilePath(path);
if (fi == NULL)
fd = open(orig_path.c_str(), O_RDONLY);
else
fd = fi->fh;
if (fd == -1)
return -errno;
res = lseek(fd, off, whence);
if (res == -1)
res = -errno;
if (fi == NULL)
close(fd);
return res;
}
static int fc_truncate(const char *path, off_t size,
struct fuse_file_info *fi)
{
g_log->debug(formatStr("fc_truncate: %s", path));
std::string cache_path = cache_manager->writeCacheFilePath(path);
int res;
if (fi != NULL)
res = ftruncate(fi->fh, size);
else
res = truncate(cache_path.c_str(), size);
if (res == -1)
return -errno;
return 0;
}
static int fc_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
g_log->debug(formatStr("fc_sync: %s", path));
(void) path;
(void) isdatasync;
(void) fi;
return 0;
}
static void assign_operations(fuse_operations &op) {
op.init = fc_init;
op.getattr = fc_getattr;
op.access = fc_access;
op.readdir = fc_readdir;
op.mkdir = fc_mkdir;
op.unlink = fc_unlink;
op.rmdir = fc_rmdir;
op.rename = fc_rename;
op.chmod = fc_chmod;
op.chown = fc_chown;
op.open = fc_open;
op.create = fc_create;
op.read = fc_read;
op.write = fc_write;
op.statfs = fc_statfs;
op.release = fc_release;
op.lseek = fc_lseek;
op.truncate = fc_truncate;
op.fsync = fc_fsync;
}
int main(int argc, char *argv[])
{
fuse_operations fc_oper {};
assign_operations(fc_oper);
enum { MAX_ARGS = 10 };
int new_argc;
char *new_argv[MAX_ARGS];
umask(0);
std::string name;
bool logToCommandline = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-name") == 0 && (i+1 < argc)) {
try
{
name = std::string(argv[i+1]);
}
catch (...)
{
continue;
}
}
else if (strcmp(argv[i], "-log") == 0 && (i+1 < argc)) {
try
{
logToCommandline = true;
}
catch (...)
{
continue;
}
}
}
char path[512];
getcwd(path, 512);
if (!name.empty()) {
name = "/" + name;
std::string subPath = std::string(path) + name;
std::string dir = std::filesystem::path(subPath).u8string();
std::filesystem::create_directories(dir);
}
std::string rootPath = std::string(path) + name + "/orig";
std::string readCacheDir = std::string(path) + name + "/cache";
std::string writeCacheDir = std::string(path) + name + "/cache";
std::string mountPoint = std::string(path) + name + "/mnt";
std::string dir = std::filesystem::path(rootPath).u8string();
std::filesystem::create_directories(dir);
dir = std::filesystem::path(readCacheDir).u8string();
std::filesystem::create_directories(dir);
dir = std::filesystem::path(writeCacheDir).u8string();
std::filesystem::create_directories(dir);
dir = std::filesystem::path(mountPoint).u8string();
std::filesystem::create_directories(dir);
g_log = new Log(writeCacheDir + "/fusecache.log", logToCommandline);
cache_manager = new CacheManager(g_log);
if (!cache_manager->checkDependencies()) {
delete cache_manager;
return -1;
}
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-readcacheonly") == 0) {
try
{
cache_manager->setReadCacheOnly(true);
}
catch (...)
{
continue;
}
}
else if (strcmp(argv[i], "-ulimit") == 0 && (i+1 < argc)) {
try
{
std::string valueString(argv[i+1]);
float limitInMBPerSec = std::stof(valueString);
cache_manager->setMaxUpBandwidth(limitInMBPerSec);
}
catch (...)
{
continue;
}
}
else if (strcmp(argv[i], "-dlimit") == 0 && (i+1 < argc)) {
try
{
std::string valueString(argv[i+1]);
float limitInMBPerSec = std::stof(valueString);
cache_manager->setMaxDownBandwidth(limitInMBPerSec);
}
catch (...)
{
continue;
}
}
}
cache_manager->setRootPath(rootPath);
cache_manager->setReadCacheDir(readCacheDir);
cache_manager->setWriteCacheDir(writeCacheDir);
cache_manager->setMountPoint(mountPoint);
//cache_manager->createDirectories();
cache_manager->start();
fill_dir_plus = FUSE_FILL_DIR_PLUS;
new_argv[0] = argv[0];
new_argv[1] = (char*)"-f";
new_argv[2] = (char*)"-o";
new_argv[3] = (char*)"allow_other";
new_argv[4] = (char*)mountPoint.c_str();
new_argc = 5;
int ret = fuse_main(new_argc, new_argv, &fc_oper, NULL);
delete cache_manager;
delete g_log;
return ret;
}