-
Notifications
You must be signed in to change notification settings - Fork 1
/
microfs.c
295 lines (265 loc) · 8.72 KB
/
microfs.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
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <libgen.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "command.h"
arg_list *root_command;
int get_path(process_info **pi, const char *path, arg_list *root_command);
//Assumes access is always available because we do not implement permissions
static int microfs_access(const char *path, int mask)
{
return 0;
}
// Reads a directory in accordance with the specified script command
int microfs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi) {
process_info *pi = malloc(sizeof(process_info));
//If it is the root directory
if (! strcmp(path, "/")){
arg_list args = { .prev = root_command , .str = ".", .len = strlen(".") };
start_process(&pi, &args);
char *line;
get_line(&line, pi);
if(line == NULL) { return -EACCES; }
assert(!strcmp(line, "!listing"));
int i = 0;
if(i++ >= offset) {
if(filler(buf, ".", NULL, i)) return 0;
}
if(i++ >= offset) {
if(filler(buf, "..", NULL, i)) return 0;
}
for(;;) {
char *name = NULL;
get_line(&name, pi);
if(name == NULL) break;
if(i++ >= offset) {
if(filler(buf, name, NULL, i)) return 0;
}
}
//Otherwise check what kind of operation is being requested
} else {
process_info *pi_parent;
int err = get_path(&pi_parent, path, root_command);
if(err) {
return err;
}
char *line;
get_line(&line, pi_parent);
assert(!strcmp(line, "!subdir_command"));
char *cmd;
get_line(&cmd, pi_parent);
arg_list args = { .prev = word_split(cmd) , .str = ".", .len = strlen(".") };
start_process(&pi, &args);
get_line(&line, pi);
if(line == NULL) { return -EACCES; }
assert(!strcmp(line, "!listing"));
int i = 0;
if(i++ >= offset) {
if(filler(buf, ".", NULL, i)) return 0;
}
if(i++ >= offset) {
if(filler(buf, "..", NULL, i)) return 0;
}
for(;;) {
char *name = NULL;
get_line(&name, pi);
if(name == NULL) break;
if(i++ >= offset) {
if(filler(buf, name, NULL, i)) return 0;
}
}
}
return 0;
}
// Find and run the command for a given path; the command info will be put in *pi.
// See passthrough.sh for the interface that this is using.
// Returns an errno if getting one of the path's ancestors failed.
int get_path_inner(process_info **pi, char *dir, char *base, arg_list *root_command) {
*pi = NULL;
if(!strcmp(dir, "/")) {
arg_list args = { .prev = root_command , .str = base, .len = strlen(base) };
start_process(pi, &args);
return 0;
} else {
process_info *parent;
int parent_err = get_path(&parent, dir, root_command) != 0;
if(parent_err) {
return parent_err;
}
char *line;
get_line(&line, parent);
if(parent->state == ERROR) {
// Parent does not actually exist
return ENOENT;
} else if(line == NULL) {
printf("Got no output when traversing directory: %s/%s\n", dir, base);
fflush(stdout);
return EIO;
} else if (strcmp(line, "!subdir_command")) {
return ENOTDIR;
} else {
char *cmd;
get_line(&cmd, parent);
close_process(parent);
if(parent->state == ERROR) {
return ENOENT;
} else if(cmd == NULL) {
printf("Got no output when traversing directory: %s/%s\n", dir, base);
fflush(stdout);
return EIO;
} else {
arg_list args = { .prev = word_split(cmd) , .str = base, .len = strlen(base) };
start_process(pi, &args);
free_arg_list(args.prev);
return 0;
}
}
}
}
int get_path(process_info **pi, const char *path, arg_list *root_command) {
assert(strcmp(path, "/")); // Path can't just be '/'
char *dir_str = strdup(path);
char *base_str = strdup(path);
char *dir = dirname(dir_str);
char *base = basename(base_str);
int err = get_path_inner(pi, dir, base, root_command);
free(dir_str);
free(base_str);
return err;
}
//Gets the information about the file given the path. Checks whether it is root
//or whether the output is "!file_command" for a file or "!subdir_command" for a
//subdirectory
static int microfs_getattr(const char *path, struct stat *stbuf) {
if(!strcmp(path, "/")) {
stbuf->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
} else {
process_info *pi_parent = malloc(sizeof(process_info));
int err = get_path(&pi_parent, path, root_command);
if(err) { return -ENOENT; }
assert(pi_parent->state != ERROR);
char *line = NULL;
get_line(&line, pi_parent);
if(line == NULL) {
return -ENOENT;
}
if (!strcmp(line,"!file_command")) {
stbuf->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
} else if(!strcmp(line, "!subdir_command")) {
stbuf->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
} else {
return -ENOENT;
}
}
stbuf->st_nlink = 1; // No hard link support
stbuf->st_size = 1; // Seems to work with most programs
stbuf->st_blocks = 20;
stbuf->st_blksize = 20;
return 0;
}
// Opens the file by performing the script specified file_command
static int innermicrofs_open(const char* path, struct fuse_file_info* fi, process_info *pi_parent, process_info *pi) {
int err = get_path(&pi_parent, path, root_command);
char *line; get_line(&line, pi_parent);
if(pi_parent->state == ERROR) { return -ENOENT; }
if(strcmp(line, "!file_command")) {
return -ENOENT;
}
char *cmd; get_line(&cmd, pi_parent);
if(pi_parent->state == ERROR) { return -ENOENT; }
start_process(&pi, word_split(cmd));
if (err)
return -err;
fi->fh = (size_t) pi;
return 0;
}
static int microfs_open(const char* path, struct fuse_file_info* fi) {
process_info *pi_parent = malloc(sizeof(process_info));
process_info *pi = malloc(sizeof(process_info));
int result = innermicrofs_open(path, fi, pi_parent, pi);
close_process(pi_parent);
close_process(pi);
free(pi_parent);
free(pi);
return result;
}
// Reads the contents of a file in accordance with the script file command
static int microfs_read(const char* path, char *buf, size_t size, off_t _off, struct fuse_file_info* fi) {
process_info *pi = (process_info*) fi->fh;
if(_off != pi->offset) {
printf("Seek error: %d %d %lx\n", _off, pi->offset, fi->fh); fflush(stdout);
return -ESPIPE; // test with 'tail' command -- "Illegal seek"
}
assert(pi->state != ERROR);
int len = get_bytes(pi, buf, size);
assert(pi->state != ERROR);
fflush(stdout);
return len;
}
// Both functions should return 0 because nothing needs to be done
static int microfs_flush(const char* path, struct fuse_file_info* fi) { return 0; }
static int microfs_release(const char* path, struct fuse_file_info *fi) { return 0; }
static struct fuse_operations microfs_operations = {
.getattr= microfs_getattr,
.access= microfs_access,
.readdir= microfs_readdir,
.release = microfs_release,
.open= microfs_open,
.read= microfs_read,
.flush = microfs_flush
};
int main(int argc, char **argv) {
save_cwd();
//Look at the command line arguments
//The argument immediately after "--" is the bash script
//All arguments after that are arguments for the script
int commandset=0;
arg_list *command = malloc(sizeof(arg_list));
arg_list *argumentlist = malloc(sizeof(arg_list));
char *mount_dir = NULL;
for (int i=1; i < argc; i++) {
if (!strcmp(argv[i],"--")){
*command = (arg_list) {.len = strlen(argv[i+1]), .str = argv[i+1], .prev = NULL};
i++;
argumentlist = command;
commandset = 1;
} else if (commandset){
arg_list *argument = malloc(sizeof(arg_list));
*argument = (arg_list) {.len = strlen(argv[i]), .str = argv[i], .prev = argumentlist};
argumentlist = argument;
} else if(argv[i][0] == '\0' || argv[i][0] == '-' || mount_dir != NULL) {
printf("Invalid arguments! This program does not accept normal FUSE options.");
exit(1);
} else {
mount_dir = argv[i];
}
}
if(mount_dir == NULL) {
printf("No mount directory specified.");
exit(1);
}
//update the parameters to mount and run
char **new_argv = malloc(sizeof(char*) * 8);
new_argv[0] = argv[0];
new_argv[1] = "-f";
new_argv[2] = "-s";
new_argv[3] = "-o";
// Note: setting max_background = 0 makes things more reliable, for some reason
new_argv[4] = "sync_read,auto_unmount,attr_timeout=0,entry_timeout=0,direct_io,noauto_cache,max_read=512,max_readahead=0,max_background=0";
new_argv[5] = mount_dir;
new_argv[6] = NULL;
int new_argc=6;
root_command = argumentlist;
return fuse_main(new_argc, new_argv, µfs_operations, NULL);
}