-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathfs.c
140 lines (124 loc) · 3.17 KB
/
mathfs.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
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include "mathfs.h"
Entry * entries[NUM_DIRECTS];
char * tokens[3];
char output[BUFF_SIZE];
int toks, choice;
// Fill the stbuf with attributes of the directory or file
// Return result of attempt to get attributes
static int mathfs_getattributes(const char *path, struct stat *stbuf)
{
// Fill the entirety of stbuf with 0s
memset(stbuf, 0, sizeof(struct stat));
// ENOENT means no such file or directory
int res = -ENOENT;
// if path is '/', return a new directory
// TODO: What does this mean
if (strcmp(path, "/") == 0) {
return new_dir(stbuf);
}
choice = -1;
toks = tokenize(tokens, path);
for(int i = 0 ; i < NUM_DIRECTS ; ++i){
if(strcmp(tokens[0], entries[i]->dirName+1) == 0) {
choice = i;
if(toks == 1) {
res = new_dir(stbuf);
}
else if(toks == 2){
if(strcmp(tokens[1], "doc") == 0) {
res = new_file(stbuf, strlen(documents[i]));
}
else if(is_number(tokens[1]) == 0){
if(entries[choice]->argCount == 2) {
res = new_dir(stbuf);
}
else {
res = new_file(stbuf, BUFF_SIZE);
}
}
}
else if(toks == 3) {
if(is_number(tokens[1]) == 0 && is_number(tokens[2]) == 0) {
res = new_file(stbuf, BUFF_SIZE);
}
}
break;
}
}
return res;
}
// Fill buffer with contents of given directory path
// Return result of attempt to read dir
static int mathfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
if(strcmp(path, "/") == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for(int i = 0 ; i < NUM_DIRECTS ; ++i)
filler(buf, entries[i]->dirName+1, NULL, 0);
return 0;
}
for(int i = 0 ; i < NUM_DIRECTS ; ++i) {
if(strcmp(path, entries[i]->dirName) == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "doc", NULL, 0);
return 0;
}
}
return -ENOENT;
}
// Reads file into content_buffer
static int mathfs_read(const char *path, char *content_buffer, size_t size, off_t offset, struct fuse_file_info *fi)
{
size_t length;
(void) fi;
char * res;
double x, y = 0;
toks = tokenize(tokens, path);
length = 0;
memset(&output[0], 0, BUFF_SIZE);
if(strcmp(tokens[1], "doc") == 0){
res = documents[choice];
}else{
sscanf(tokens[1], "%lf", &x);
sscanf(tokens[2], "%lf", &y);
res = (*entries[choice]->funcPtr)(&output[0], x, y);
}
if((length = strlen(res)) > 0 && res != 0){
if (offset < length) {
if (offset + size > length){
size = length - offset;
memcpy(content_buffer, res + offset, size);
} else {
size = 0;
}
return size;
}
}
return -ENOENT;
}
static struct fuse_operations mathfs_oper = {
.getattr = mathfs_getattributes,
.readdir = mathfs_readdir,
.read = mathfs_read,
};
// driver
int main(int argc, char *argv[])
{
tokens[2] = &output[0];
fill_table(entries);
fuse_main(argc, argv, &mathfs_oper, NULL);
cleanup(entries);
return 0;
}