-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathramdisk.c
573 lines (407 loc) · 10.3 KB
/
ramdisk.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
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
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o hello
*/
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include "ramnode.h"
ramNode *head;
long int ramFSSize = 0;
long int ramAvailableSize = 0;
char *persistentFile;
char *dupPersistentFile;
int isPersistentEnabled= 0;
void initNodes() {
ramNode *n = (ramNode *) malloc(sizeof(ramNode));
struct fuse_context *f = fuse_get_context();
strcpy(n->name, "/");
n->gid = f->gid;
n->uid = f->uid;
n->type = DIR_TYPE;
n->mode = 0777;
n->atime = time(NULL);
n->ctime = time(NULL);
n->mtime = time(NULL);
n->data = NULL;
n->next = NULL;
head = n;
if(access( persistentFile, F_OK ) != -1 ) {
// load the data from here
FILE *fp = fopen(persistentFile, "r");
char *name = (char*)malloc(256);
int type;
mode_t mode;
gid_t gid;
uid_t uid;
size_t size;
char *data;
time_t atime;
time_t mtime;
time_t ctime;
while(!feof(fp)) {
fscanf(fp, "%s", name);
if(searchNode(head, name) != NULL)
break;
ramNode *t = (ramNode *)malloc(sizeof(ramNode));
// Name of the file
strcpy(t->name, name);
// type
char temp[10];
fscanf(fp, "%s", temp);
type = atoi(temp);
t->type = type;
// mode
fscanf(fp, "%s", temp);
mode = atoi(temp);
t->mode = mode;
// gid
fscanf(fp, "%s", temp);
gid = atoi(temp);
t->gid = gid;
//uid
fscanf(fp, "%s", temp);
uid = atoi(temp);
t->uid = uid;
//size
fscanf(fp, "%s", temp);
size = atoi(temp);
t->size = size;
if(type == DIR_TYPE) {
// No need to read data
t->data = NULL;
// a time
fscanf(fp, "%s", temp);
atime = atol(temp);
t->atime = atime;
// m time
fscanf(fp, "%s", temp);
mtime = atol(temp);
t->mtime = mtime;
// c time
fscanf(fp, "%s", temp);
ctime = atol(temp);
t->ctime = ctime;
} else if(type == FILE_TYPE) {
// No need to read data
t->data = (char *) malloc(t->size);
fseek(fp, 1, SEEK_CUR);
fread(t->data, 1, t->size, fp);
// a time
fscanf(fp, "%s", temp);
atime = atol(temp);
t->atime = atime;
// m time
fscanf(fp, "%s", temp);
mtime = atol(temp);
t->mtime = mtime;
// c time
fscanf(fp, "%s", temp);
ctime = atol(temp);
t->ctime = ctime;
}
addNode(head, t);
}
fclose(fp);
}
}
static int ramdisk_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
ramNode *temp = searchNode(head, path);
if(temp == NULL)
return -ENOENT;
stbuf->st_ctime = temp->ctime;
stbuf->st_atime = temp->atime;
stbuf->st_mtime = temp->mtime;
stbuf->st_gid = temp->gid;
stbuf->st_uid = temp->uid;
if (temp != NULL && temp->type == DIR_TYPE) {
stbuf->st_mode = S_IFDIR | temp->mode;
stbuf->st_nlink = 2;
} else if (temp != NULL && temp->type == FILE_TYPE) {
stbuf->st_mode = S_IFREG | temp->mode;
stbuf->st_nlink = 1;
stbuf->st_size = temp->size;
}
return res;
}
static int ramdisk_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
char *tempName;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
ramNode *temp = head;
while (temp != NULL) {
tempName = (char *) malloc(strlen(temp->name));
strcpy(tempName, temp->name);
char *parentName = dirname(tempName);
// Check whether the node and the path are same. It SHOULDN'T be
if(strcmp(temp->name, path) != 0) {
// Now check whether the parent of the file and the path are same.
if(strcmp(parentName, path) == 0) {
strcpy(tempName, temp->name);
filler(buf, basename(tempName), NULL, 0);
}
}
free(tempName);
temp = temp->next;
}
return 0;
}
static int ramdisk_open(const char *path, struct fuse_file_info *fi)
{
int res;
ramNode *temp = searchNode(head, path);
if (temp == NULL)
return -ENOENT;
return 0;
}
static int ramdisk_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
ramNode *temp = searchNode(head, path);
if (temp == NULL)
return -ENOENT;
if(temp->type == DIR_TYPE)
return -ENOENT;
temp->atime = time(NULL);
len = temp->size;
if(len == 0) {
return 0;
}
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, temp->data + offset, size);
} else
size = 0;
return size;
}
static int ramdisk_mkdir(const char *path, mode_t mode) {
int res = 0;
if(ramAvailableSize < sizeof(ramNode)) {
return -ENOSPC;
}
ramNode *n = (ramNode *) malloc(sizeof(ramNode));
ramAvailableSize = ramAvailableSize - sizeof(ramNode);
struct fuse_context *f = fuse_get_context();
strcpy(n->name, path);
n->gid = f->gid;
n->uid = f->uid;
n->mode = mode;
n->type = DIR_TYPE;
n->atime = time(NULL);
n->ctime = time(NULL);
n->mtime = time(NULL);
n->next = NULL;
n->size = 0;
n->data = NULL;
addNode(head, n);
return res;
}
static int ramdisk_rmdir(const char *path) {
int res;
res = deleteNode(head, path);
if(res == 0) {
ramAvailableSize = ramAvailableSize + sizeof(ramNode);
}
return res;
}
static int ramdisk_create(const char * path, mode_t mode, struct fuse_file_info *fi) {
int res = 0;
if(ramAvailableSize < sizeof(ramNode)) {
return -ENOSPC;
}
ramNode *n = (ramNode *) malloc(sizeof(ramNode));
struct fuse_context *f = fuse_get_context();
strcpy(n->name, path);
n->gid = f->gid;
n->uid = f->uid;
n->mode = mode;
n->type = FILE_TYPE;
n->atime = time(NULL);
n->ctime = time(NULL);
n->mtime = time(NULL);
n->next = NULL;
n->size = 0;
n->data = (char *)malloc(sizeof(char));
n->data[0] = '\0';
addNode(head, n);
ramAvailableSize = ramAvailableSize - sizeof(ramNode);
return res;
}
static int ramdisk_rename(const char *source, const char *dest) {
ramNode *temp = searchNode(head, source);
if(temp == NULL)
return -ENOENT;
strcpy(temp->name, dest);
temp->mtime = time(NULL);
return 0;
}
static int ramdisk_utimens(const char* path, const struct timespec tv[2]) {
ramNode *temp = searchNode(head, path);
if(temp == NULL)
return -ENOENT;
temp->atime = tv[0].tv_sec;
temp->mtime = tv[1].tv_sec;
return 0;
}
static int ramdisk_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi){
ramNode *temp = searchNode(head, path);
if (temp == NULL)
return -ENOENT;
if(size != 0) {
if(ramAvailableSize < size) {
return -ENOSPC;
}
temp->data = (char*) realloc(temp->data, offset + size);
memcpy(temp->data+offset, buf, size);
temp->size = offset + size;
ramAvailableSize = ramAvailableSize - size;
}
return size;
}
static int ramdisk_unlink(const char* path) {
int res = 0;
ramNode *temp = searchNode(head, path);
if(temp != NULL) {
ramAvailableSize = ramAvailableSize + sizeof(ramNode) + temp->size;
}
res = deleteFile(head, path);
return res;
}
static int ramdisk_chmod(const char* path, mode_t mode) {
ramNode *n = searchNode(head, path);
if(n == NULL)
return -ENOENT;
n->mode = mode;
return 0;
}
static int ramdisk_chown(const char* path, uid_t uid, gid_t gid) {
ramNode *n = searchNode(head, path);
if(n == NULL)
return -ENOENT;
n->gid = gid;
n->uid = uid;
return 0;
}
static int ramdisk_truncate(const char* path, off_t size) {
ramNode * temp = searchNode(head, path);
if(temp == NULL)
return -ENOENT;
if(size == 0) {
ramAvailableSize = ramAvailableSize + temp->size;
temp->data = realloc(temp->data, size+1);
temp->data[0] = '\0';
} else {
ramAvailableSize = ramAvailableSize + temp->size - size;
temp->data = realloc(temp->data, size+1);
temp->data[size] = '\0';
}
return 0;
}
static int ramdisk_opendir(const char *path, struct fuse_file_info * fi) {
ramNode *n = searchNode(head, path);
if(n == NULL)
return -ENOENT;
if(n->type != DIR_TYPE)
return -ENOTDIR;
return 0;
}
static void ramdisk_destroy(void* private_data) {
if(isPersistentEnabled == 1) {
FILE *fp = fopen(dupPersistentFile, "w");
ramNode *temp = head->next;
while(temp != NULL) {
// Write the file name
fprintf(fp, "%s\n", temp->name);
// type
fprintf(fp, "%d\n", temp->type);
// write(fd, "\n", 1);
// mode
fprintf(fp, "%d\n", temp->mode);
// gid
fprintf(fp, "%d\n", temp->gid);
// uid
fprintf(fp, "%d\n", temp->uid);
// size
fprintf(fp, "%d\n", temp->size);
// data
if(temp->type == FILE_TYPE) {
fwrite(temp->data, sizeof(char), temp->size, fp);
}
// atime
fprintf(fp, "%ld\n", temp->atime);
// mtime
fprintf(fp, "%ld\n", temp->mtime);
// ctime
fprintf(fp, "%ld\n", temp->ctime);
temp = temp->next;
}
fclose(fp);
}
}
static struct fuse_operations hello_oper = {
.getattr = ramdisk_getattr,
.readdir = ramdisk_readdir,
.open = ramdisk_open,
.opendir = ramdisk_opendir,
.read = ramdisk_read,
.mkdir = ramdisk_mkdir,
.rmdir = ramdisk_rmdir,
.create = ramdisk_create,
.rename = ramdisk_rename,
.utimens = ramdisk_utimens,
.write = ramdisk_write,
//.access = ramdisk_access,
.unlink = ramdisk_unlink,
.chmod = ramdisk_chmod,
.chown = ramdisk_chown,
// .flush = ramdisk_flush,
.truncate = ramdisk_truncate,
// .releasedir = ramdisk_releasedir,
.destroy = ramdisk_destroy,
};
int main(int argc, char *argv[])
{
char *newArgs[argc-1]; //Change this to argc-1
int newArgc = 2;
if(argc < 3) {
fprintf(stderr, "Run the program: ./ramdisk <mountpoint> <size in MB> {file Name}\n");
return 1;
} else if(argc == 3) {
newArgs[0] = strdup(argv[0]);
newArgs[1] = strdup(argv[1]);
ramFSSize = atol(argv[2]);
ramFSSize = ramFSSize * 1024 * 1024;
} else if(argc ==4) {
newArgs[0] = strdup(argv[0]);
newArgs[1] = strdup(argv[1]);
ramFSSize = atol(argv[2]);
ramFSSize = ramFSSize * 1024 * 1024;
persistentFile = strdup(argv[3]);
isPersistentEnabled = 1;
dupPersistentFile = (char *)malloc(256);
strcpy(dupPersistentFile, get_current_dir_name());
strcat(dupPersistentFile, "/");
strcat(dupPersistentFile, persistentFile);
}
ramAvailableSize = ramFSSize;
initNodes();
return fuse_main(newArgc, newArgs, &hello_oper, NULL);
}