-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
574 lines (517 loc) · 15.6 KB
/
filesystem.cpp
File metadata and controls
574 lines (517 loc) · 15.6 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
Sample filesystem in user space and in memory using BTree and custom memory allocator.
Lisovskiy Stanislav Stanislav.Lisovskiy@gmail.com
*/
#define FUSE_USE_VERSION 30
#include <config.h>
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include "btree.h"
#include "mem_allocator.h"
#include "coder.h"
CharBTree<5> *tree = NULL;
MemoryAllocator2 *mem = NULL;
Coder *coder = NULL;
#define MAX_CHANGES 50
#define BUFSIZE 50000000
int max_backup_changes = MAX_CHANGES;
int max_write_changes = MAX_CHANGES;
int backup_changes_counter = MAX_CHANGES;
int changes_counter = MAX_CHANGES;
unsigned char buffer[BUFSIZE];
char *memory_file = "/memory_file";
char *btree_file = "/btree_file";
char *backup_memory_file = NULL;
char *backup_btree_file = NULL;
CharBTree<5>::CharBNode *root_node = NULL;
CharBTree<5>::CharBNode *remove_poison_node = (CharBTree<5>::CharBNode *)0xffffffff;
bool flat_view = false;
static void sync_changes(CharBTree<5> *tree, MemoryAllocator2 *mem)
{
mem->syncToDisk(memory_file);
tree->syncToDisk(btree_file);
return;
}
static void check_init_backup_names(void)
{
if(backup_memory_file == NULL || backup_btree_file == NULL)
{
backup_memory_file = (char*)malloc(strlen(memory_file)+strlen("_backup")+1);
backup_btree_file = (char*)malloc(strlen(btree_file)+strlen("_backup")+1);
strcpy(backup_memory_file, memory_file);
strcat(backup_memory_file, "_backup");
strcpy(backup_btree_file, btree_file);
strcat(backup_btree_file, "_backup");
}
return;
}
static void sync_backup_changes(CharBTree<5> *tree, MemoryAllocator2 *mem)
{
check_init_backup_names();
mem->syncToDisk(backup_memory_file);
tree->syncToDisk(backup_btree_file);
return;
}
static void check_changes_for_backup(CharBTree<5> *tree, MemoryAllocator2 *mem)
{
backup_changes_counter--;
if(backup_changes_counter==0)
{
sync_backup_changes(tree, mem);
backup_changes_counter = max_backup_changes;
}
return;
}
static void check_write_changes(CharBTree<5> *tree, MemoryAllocator2 *mem)
{
changes_counter--;
if(changes_counter==0)
{
sync_changes(tree, mem);
changes_counter = max_write_changes;
}
return;
}
static int filesystem_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1;
}
else if (strstr(path, "/memory") != NULL) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = sizeof(buffer);
}
else {
char *_str = NULL;
CharBTree<5>::CharBNode *node = tree->FindNode(path);
tree->Print();
if(!node)
return -ENOENT;
_str = node->value;
stbuf->st_mode = S_IFREG | 0444;
if(node->attr.is_dir)
{
stbuf->st_mode = S_IFDIR | 0755;
}
stbuf->st_nlink = node->filesystem_nodes->size();
stbuf->st_size = node->size;
}
return res;
}
static int filesystem_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
(void) offset;
(void) fi;
(void) flags;
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if (strcmp(path, "/") != 0)
{
node = tree->FindNode(path);
}
else
{
node = root_node;
}
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
#ifdef MEMORY_FILE
filler(buf, "memory", NULL, 0, 0);
#endif
vector< CharBTree<5>::CharBNode * > vec;
if(flat_view)
{
tree->TraverseTree(vec, node, false);
tree->Print();
for(int i=0; i<vec.size(); i++)
filler(buf, vec[i]->data+1, NULL, 0, 0);
}
else
{
for(int i=0;i<node->filesystem_nodes->size();i++)
{
if((*(node->filesystem_nodes))[i] != remove_poison_node)
{
DEBUG("Displaying path %s node %s\n", path, ((*(node->filesystem_nodes))[i])->data);
char *s = strrchr(((*(node->filesystem_nodes))[i])->data, '/');
if(s)
{
filler(buf, s+1, NULL, 0, 0);
}
}
}
}
return 0;
}
static int filesystem_mkdir(const char *path, mode_t mode)
{
if (strcmp(path, "/memory") == 0) {
return 0;
}
CharBTree<5>::CharBNode *node = tree->FindNode(path);
int last_slash = strrchr(path, '/')-path;
char *parent_path = (char*)mem->Allocate(last_slash+1);
strncpy(parent_path, path, last_slash);
parent_path[last_slash] = '\0';
CharBTree<5>::CharBNode *parent_node = tree->FindNode(parent_path);
if(parent_node == NULL)
{
DEBUG("Could not find parent for node path %s, considering root\n", path);
parent_node = root_node;
}
mem->Free(parent_path);
if(!node)
{
char *_path = (char*)mem->Allocate(strlen(path)+1);
memcpy(_path, path, strlen(path)+1);
char *str = (char*)mem->Allocate(2);
strcpy(str,"");
node = tree->AddNode(_path, str);
node->attr.is_dir = true;
node->filesystem_parent = parent_node;
bool added = false;
for(int i=0;i<parent_node->filesystem_nodes->size();i++)
{
if((*(parent_node->filesystem_nodes))[i] == remove_poison_node)
{
(*parent_node->filesystem_nodes)[i] = node;
added = true;
break;
}
}
if(!added)
parent_node->filesystem_nodes->push_back(node);
}
check_write_changes(tree, mem);
check_changes_for_backup(tree, mem);
return 0;
}
static int filesystem_open(const char *path, struct fuse_file_info *fi)
{
if (strcmp(path, "/memory") == 0) {
return 0;
}
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node)
{
char *_path = (char*)mem->Allocate(strlen(path)+1);
memcpy(_path, path, strlen(path)+1);
char *str = (char*)mem->Allocate(2);
strcpy(str,"");
tree->AddNode(_path, str);
}
check_changes_for_backup(tree, mem);
check_write_changes(tree, mem);
return 0;
}
static int filesystem_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
char *_str = NULL;
if (strcmp(path, "/memory") == 0) {
if (offset < sizeof(buffer)) {
if (offset + size > sizeof(buffer))
size = sizeof(buffer) - offset;
memcpy(buf, buffer + offset, size);
} else
size = 0;
return size;
}
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node)
{
return -ENOENT;
}
_str = node->value;
len = node->size;
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, _str + offset, size);
coder->decode(buf,buf,size);
}
else
{
size = 0;
}
return size;
}
static int filesystem_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node)
{
return -ENOENT;
}
char *new_val = node->value;
if((offset + size) > node->size) {
new_val = (char*)mem->Allocate((offset+size));
memcpy(new_val, node->value, node->size);
if(node->value)
{
mem->Free(node->value);
}
node->value = new_val;
node->size = (offset + size);
}
memcpy(&new_val[offset], buf, size);
coder->encode(&new_val[offset], &new_val[offset], size+1);
check_write_changes(tree, mem);
check_changes_for_backup(tree, mem);
return size;
}
static int filesystem_unlink(const char *path)
{
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node) return -ENOENT;
int last_slash = strrchr(path, '/') - path;
char *parent_path = (char*)mem->Allocate(last_slash+1);
strncpy(parent_path, path, last_slash);
parent_path[last_slash] = '\0';
CharBTree<5>::CharBNode *parent_node = tree->FindNode(parent_path);
if(parent_node == NULL)
{
DEBUG("Could not find parent for node path %s, considering root\n", path);
parent_node = root_node;
}
mem->Free(parent_path);
for(int i=0;i<parent_node->filesystem_nodes->size();i++)
{
if((*(parent_node->filesystem_nodes))[i] == node)
{
(*(parent_node->filesystem_nodes))[i] = remove_poison_node;
break;
}
}
tree->RemoveNode(node, false);
check_write_changes(tree, mem);
check_changes_for_backup(tree, mem);
return 0;
}
static int filesystem_rmdir(const char *path)
{
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node) return -ENOENT;
int last_slash = strrchr(path, '/') - path;
char *parent_path = (char*)mem->Allocate(last_slash+1);
strncpy(parent_path, path, last_slash);
parent_path[last_slash] = '\0';
CharBTree<5>::CharBNode *parent_node = tree->FindNode(parent_path);
if(parent_node == NULL)
{
DEBUG("Could not find parent for node path %s, considering root\n", path);
parent_node = root_node;
}
mem->Free(parent_path);
for(int i=0;i<parent_node->filesystem_nodes->size();i++)
{
if((*(parent_node->filesystem_nodes))[i] == node)
{
(*(parent_node->filesystem_nodes))[i] = remove_poison_node;
break;
}
}
tree->RemoveNode(node, false);
check_write_changes(tree, mem);
check_changes_for_backup(tree, mem);
return 0;
}
static int filesystem_mknod(const char *path, mode_t mode, dev_t rdev)
{
if (strstr(path, "/memory") != NULL) {
return 0;
}
CharBTree<5>::CharBNode *node = tree->FindNode(path);
if(!node)
{
int last_slash = strrchr(path, '/') - path;
char *parent_path = (char*)mem->Allocate(last_slash+1);
strncpy(parent_path, path, last_slash);
parent_path[last_slash] = '\0';
CharBTree<5>::CharBNode *parent_node = tree->FindNode(parent_path);
if(parent_node == NULL)
{
DEBUG("Could not find parent for node path %s, considering root\n", path);
parent_node = root_node;
}
mem->Free(parent_path);
char *_path = (char*)mem->Allocate(strlen(path)+1);
memcpy(_path, path, strlen(path)+1);
char *str = (char*)mem->Allocate(2);
strcpy(str,"");
CharBTree<5>::CharBNode *node = tree->AddNode(_path, str);
node->attr.is_dir = false;
node->filesystem_parent = parent_node;
bool added = false;
for(int i=0;i<parent_node->filesystem_nodes->size();i++)
{
if((*(parent_node->filesystem_nodes))[i] == remove_poison_node)
{
(*(parent_node->filesystem_nodes))[i] = node;
added = true;
break;
}
}
if(!added)
parent_node->filesystem_nodes->push_back(node);
DEBUG("Added node %s to parent node %s\n", node->data, parent_node->data);
}
check_write_changes(tree, mem);
check_changes_for_backup(tree, mem);
return 0;
}
static int filesystem_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
sync_changes(tree, mem);
return 0;
}
static int filesystem_chown(const char *path, uid_t uid, gid_t gid)
{
return 0;
}
static int filesystem_chmod(const char *path, mode_t mode)
{
return 0;
}
static struct fuse_operations filesystem_oper;
extern int opterr;
#define FILESYSTEM_NUMBUCKETS 5
int main(int argc, char *argv[])
{
int argc_fuse = 2;
int opt;
opterr = 0;
int num_blocks_each_size = 32;
int start_block_size = 8192;
char *pass = "";
char **argv_fuse = new char *[argc+1];
argv_fuse[0] = argv[0];
if(argc < 2)
{
printf("No mount point specified!\n");
return -1;
}
argv_fuse[1] = argv[1];
argv_fuse[2] = "-d";
int n = 2;
while (n < argc) {
opt = getopt(argc, argv, "m:t:e:s:p:b:w:");
if(opt != -1)
{
switch (opt) {
case 'm':
memory_file = optarg;
break;
case 't':
btree_file = optarg;
break;
case 'e':
num_blocks_each_size = atoi(optarg);
break;
case 'w':
max_write_changes = atoi(optarg);
break;
case 'b':
max_backup_changes = atoi(optarg);
break;
case 's':
start_block_size = atoi(optarg);
break;
case 'p':
pass = optarg;
break;
default:
char *s = new char[3];
s[0] = '-';
s[1] = optopt;
s[2] = '\0';
argv_fuse[argc_fuse] = s;
argc_fuse++;
}
}
n++;
}
printf("Memory file %s btree_file %s num_blocks_each_size %d start %d pass %s max_write_changes %d max_backup_changes %d\n",
memory_file, btree_file, num_blocks_each_size, start_block_size, pass, max_write_changes, max_backup_changes);
printf("fuse_argc %d fuse args:\n", argc_fuse);
for(int i=0; i<argc_fuse; i++)
{
printf("%s\n", argv_fuse[i]);
}
mem = new MemoryAllocator2(buffer, sizeof(buffer), num_blocks_each_size, start_block_size);
coder = new Coder(pass);
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
mem->PrintBlocksUsage();
if(mem->syncFromDisk(memory_file) != 0)
{
check_init_backup_names();
delete mem;
printf("Could not open memory file %s, trying _backup..\n", memory_file);
mem = new MemoryAllocator2(buffer, sizeof(buffer), num_blocks_each_size, start_block_size);
delete tree;
printf("Could not open btree file %s, trying _backup..\n", btree_file);
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
if(tree->syncFromDisk(backup_btree_file) != 0)
{
printf("Could not open backup btree file %s\n", backup_btree_file);
delete tree;
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
}
if(mem->syncFromDisk(backup_memory_file) != 0)
{
printf("Could not open backup memory file %s\n", backup_memory_file);
delete mem;
mem = new MemoryAllocator2(buffer, sizeof(buffer), num_blocks_each_size, start_block_size);
delete tree;
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
}
}
if(tree->syncFromDisk(btree_file) != 0)
{
check_init_backup_names();
delete tree;
printf("Could not open btree file %s, trying _backup..\n", btree_file);
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
if(tree->syncFromDisk(backup_btree_file) != 0)
{
printf("Could not open backup btree file %s\n", backup_btree_file);
delete tree;
tree = new CharBTree<FILESYSTEM_NUMBUCKETS>(mem, coder);
}
}
root_node = tree->getRoot();
if(!root_node)
{
char *root_path =(char *) mem->Allocate(strlen("/")+1);
root_path[0] = '/';
root_path[1] = '\0';
tree->AddNode(root_path, "");
root_node = tree->getRoot();
}
filesystem_oper.getattr = filesystem_getattr;
filesystem_oper.readdir = filesystem_readdir;
filesystem_oper.mkdir = filesystem_mkdir;
filesystem_oper.rmdir = filesystem_rmdir;
filesystem_oper.open = filesystem_open;
filesystem_oper.read = filesystem_read;
filesystem_oper.write = filesystem_write;
filesystem_oper.mknod = filesystem_mknod;
filesystem_oper.unlink = filesystem_unlink;
filesystem_oper.chown = filesystem_chown;
filesystem_oper.chmod = filesystem_chmod;
filesystem_oper.fsync = filesystem_fsync;
return fuse_main(argc_fuse, argv_fuse, &filesystem_oper, NULL);
}