-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse_practice.c
296 lines (251 loc) · 7.68 KB
/
fuse_practice.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 31
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stddef.h>
char directories[256][256];
int dir_idx = -1;
char files[256][256];
int file_idx = -1;
char file_contents[256][256];
int content_idx = -1;
static void *prac_init(struct fuse_conn_info *conn, struct fuse_config *cfg);
static int prac_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi);
static int prac_readdir(const char *path, void *buffer, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags);
static int prac_read(const char *path, char *buffer, size_t size, off_t offset,
struct fuse_file_info *fi);
static int prac_truncate(const char *path, off_t size, struct fuse_file_info *fi);
static int prac_write(const char *path, const char *buffer, size_t size, off_t offset,
struct fuse_file_info *fi);
static int prac_mkdir(const char *path, mode_t mode);
static int prac_mknod(const char *path, mode_t mode, dev_t rdev);
static int prac_unlink(const char *path);
static int prac_rmdir(const char *path);
int is_dir(const char *path);
int is_file(const char *path);
int get_file_index(const char *path);
int get_dir_index(const char *path);
static int prac_opt_proc(void *data, const char *arg, int key,
struct fuse_args *outargs);
static struct options {
const char *initfile_name;
const char *initfile_content;
} options;
#define KEY_HELP 1
// t = option flag, a = attribute in options strucct
#define OPTION(t, a) { t, offsetof(struct options, a), 1 }
static const struct fuse_opt option_specs[] = {
OPTION("--init=%s", initfile_name),
OPTION("--content=%s", initfile_content),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_END
};
static const struct fuse_operations prac_ops = {
.init = prac_init,
.getattr = prac_getattr,
.readdir = prac_readdir,
.read = prac_read,
.truncate = prac_truncate,
.write = prac_write,
.mkdir = prac_mkdir,
.mknod = prac_mknod,
.unlink = prac_unlink,
.rmdir = prac_rmdir
};
int main(int argc, char *argv[]) {
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
if (fuse_opt_parse(&args, &options, option_specs, prac_opt_proc) == -1) {
return 1;
}
/* Important: To make command line arg options work,
pass fuse_args#argc and fuse_args#argv to fuse_main().
Pass argc and argv only if no options or fuse_args needed. */
return fuse_main(args.argc, args.argv, &prac_ops, NULL);
}
static void *prac_init(struct fuse_conn_info *conn, struct fuse_config *cfg)
{
if (options.initfile_name != NULL) {
file_idx++;
strcpy(files[file_idx], options.initfile_name);
if (options.initfile_content != NULL) {
content_idx++;
strcpy(file_contents[content_idx], options.initfile_content);
}
}
return NULL;
}
static int prac_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
stbuf->st_uid = getuid(); // Owner id
stbuf->st_gid = getgid(); // Group id
// stbuf->st_atime = time(NULL); // Last access time
// stbuf->st_mtime = time(NULL); // Last modified time
if (strcmp(path, "/") == 0 || is_dir(path) == 1) {
// If root or any directory, set directory mode & permission bits
// 2 hardlinks as directory has links "/." and "/.."
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (is_file(path) == 1) {
// If file, set file mode & permission bits
// Only link to itself
// Set size for all files
stbuf->st_mode = S_IFREG | 0644;
stbuf->st_nlink = 1;
int index = get_file_index(path);
stbuf->st_size = sizeof(file_contents[index]);
} else {
return -ENOENT;
}
return 0;
}
static int prac_readdir(const char *path, void *buffer, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags)
{
filler(buffer, ".", NULL, 0, 0);
filler(buffer, "..", NULL, 0, 0);
if (strcmp(path, "/") == 0) {
for (int i = 0; i <= dir_idx; i++) {
if (strcmp(directories[i], "") != 0)
filler(buffer, directories[i], NULL, 0, 0);
}
for (int j = 0; j <= file_idx; j++) {
if (strcmp(files[j], "") != 0)
filler(buffer, files[j], NULL, 0, 0);
}
}
return 0;
}
static int prac_read(const char *path, char *buffer, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int file_index = get_file_index(path);
if (file_index >= 0) {
// Read content of file starting from offset
char *content = file_contents[file_index];
memcpy(buffer, content + offset, size);
size = strlen(content) - offset;
} else {
size = 0;
}
return size;
}
static int prac_truncate(const char *path, off_t size, struct fuse_file_info *fi)
{
if (size > 1024) {
return -ENOMEM;
}
int index = get_file_index(path);
int curr_content_size = sizeof(file_contents[index]);
if (size > curr_content_size) {
memset(file_contents[index] + curr_content_size, 0, size - curr_content_size);
}
return 0;
}
static int prac_write(const char *path, const char *buffer, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int index = get_file_index(path);
if (file_idx >= 0) {
strcpy(file_contents[index] + offset, buffer);
size = strlen(file_contents[index]) - offset;
} else {
size = 0;
}
return size;
}
static int prac_mkdir(const char *path, mode_t mode)
{
path++;
dir_idx++;
strcpy(directories[dir_idx], path);
return 0;
}
static int prac_mknod(const char *path, mode_t mode, dev_t rdev)
{
path++;
file_idx++;
strcpy(files[file_idx], path);
content_idx++;
strcpy(file_contents[content_idx], "");
return 0;
}
static int prac_unlink(const char *path) {
int file_index = get_file_index(path);
strcpy(files[file_index], "");
strcpy(file_contents[file_index], "");
if (strcmp(files[file_index], "") != 0) {
return -1;
} else {
return 0;
}
}
static int prac_rmdir(const char *path) {
int dir_index = get_dir_index(path);
strcpy(directories[dir_index], "");
if (strcmp(directories[dir_index], "") != 0) {
return -1;
} else {
return 0;
}
}
int is_dir(const char *path)
{
path++; // Keep name without "/"
for (int i = 0; i <= dir_idx; i++) {
if (strcmp(path, directories[i]) == 0 && strcmp(directories[i], "") != 0) {
return 1;
}
}
return 0;
}
int is_file(const char *path)
{
path++;
for (int i = 0; i <= file_idx; i++) {
if (strcmp(path, files[i]) == 0) {
return 1;
}
}
return 0;
}
int get_file_index(const char *path)
{
path++;
for (int i = 0; i <= file_idx; i++) {
if (strcmp(path, files[i]) == 0) {
return i;
}
}
return -1;
}
int get_dir_index(const char *path)
{
path++;
for (int i = 0; i <= dir_idx; i++) {
if (strcmp(path, directories[i]) == 0) {
return i;
}
}
return -1;
}
static int prac_opt_proc(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
if (key == KEY_HELP) {
printf("usage: %s [options] <mountpoint>\n\n", outargs->argv[0]);
printf("File-system specific options:\n"
" --init=<s> Name of the init file\n"
" --content=<s> Content of the init file\n"
"\n");
// May not need to add to arg
fuse_opt_add_arg(outargs, "-h");
}
return 1;
}