forked from bradfield-csi-2/alexbozhenko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayfs.c
304 lines (279 loc) · 6.17 KB
/
arrayfs.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
#define FUSE_USE_VERSION 31
#define ROOTDIR_LENGTH 10
#define DIR_LENGTH 10
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>
char *array[ROOTDIR_LENGTH][DIR_LENGTH];
static void *arrayfs_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
{
(void)conn;
cfg->kernel_cache = 1;
// TODO: I was not able to figure out proper way to initialize
// array of arrays of strings. What is the right way?
// Also, instead of using global array, we want to return from this
// function, so we can access it as private_data field of fuse_context
array[0][0] = "Hi.";
array[0][1] = NULL;
array[0][2] = NULL;
array[0][3] = NULL;
array[0][4] = NULL;
array[0][5] = "this";
array[0][6] = NULL;
array[0][7] = NULL;
array[0][8] = NULL;
array[0][9] = NULL;
array[1][0] = NULL;
array[1][1] = "is";
array[1][2] = NULL;
array[1][3] = NULL;
array[1][4] = NULL;
array[1][5] = NULL;
array[1][6] = NULL;
array[1][7] = NULL;
array[1][8] = NULL;
array[1][9] = NULL;
array[2][0] = NULL;
array[2][1] = "only";
array[2][2] = NULL;
array[2][3] = NULL;
array[2][4] = NULL;
array[2][5] = NULL;
array[2][6] = NULL;
array[2][7] = NULL;
array[2][8] = NULL;
array[2][9] = NULL;
array[3][0] = NULL;
array[3][1] = NULL;
array[3][2] = NULL;
array[3][3] = NULL;
array[3][4] = NULL;
array[3][5] = NULL;
array[3][6] = NULL;
array[3][7] = NULL;
array[3][8] = NULL;
array[3][9] = "a";
array[4][0] = NULL;
array[4][1] = NULL;
array[4][2] = NULL;
array[4][3] = NULL;
array[4][4] = NULL;
array[4][5] = NULL;
array[4][6] = NULL;
array[4][7] = "test";
array[4][8] = NULL;
array[4][9] = NULL;
array[5][0] = NULL;
array[5][1] = NULL;
array[5][2] = NULL;
array[5][3] = NULL;
array[5][4] = NULL;
array[5][5] = NULL;
array[5][6] = NULL;
array[5][7] = NULL;
array[5][8] = NULL;
array[5][9] = NULL;
array[6][0] = NULL;
array[6][1] = NULL;
array[6][2] = NULL;
array[6][3] = NULL;
array[6][4] = NULL;
array[6][5] = NULL;
array[6][6] = NULL;
array[6][7] = NULL;
array[6][8] = NULL;
array[6][9] = NULL;
array[7][0] = NULL;
array[7][1] = NULL;
array[7][2] = NULL;
array[7][3] = NULL;
array[7][4] = NULL;
array[7][5] = NULL;
array[7][6] = NULL;
array[7][7] = NULL;
array[7][8] = NULL;
array[7][9] = NULL;
array[8][0] = NULL;
array[8][1] = "!";
array[8][2] = NULL;
array[8][3] = NULL;
array[8][4] = NULL;
array[8][5] = NULL;
array[8][6] = NULL;
array[8][7] = NULL;
array[8][8] = NULL;
array[8][9] = NULL;
array[9][0] = NULL;
array[9][1] = NULL;
array[9][2] = NULL;
array[9][3] = NULL;
array[9][4] = NULL;
array[9][5] = NULL;
array[9][6] = NULL;
array[9][7] = NULL;
array[9][8] = NULL;
array[9][9] = NULL;
return array;
}
int parse_dir(const char *path)
{
if (strlen(path) == 1)
{
return -1;
}
return path[1] - '0';
}
int parse_file(const char *path)
{
if (strlen(path) < 4)
{
return -1;
}
return path[3] - '0';
}
static int arrayfs_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
(void)fi;
int res = 0;
struct fuse_context fc = *fuse_get_context();
//char *arr[ROOTDIR_LENGTH][DIR_LENGTH] = fc.private_data;
memset(stbuf, 0, sizeof(struct stat));
int dir = parse_dir(path);
int file = parse_file(path);
printf("dir=%d files=%d\n", dir, file);
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2 + ROOTDIR_LENGTH;
}
else if (strchr(path + 1, '/') == NULL &&
dir != -1 && array[dir] != NULL) // no more slashes in the path,
// so it is a directory
{
printf("entered nested dir path of getattr.");
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else if (((int)(strchr(path + 1, '/') - path) == 2) &&
(file != -1) &&
(array[dir][file] != NULL)) // found second slash, so it is a file
{
printf("entered file path of getattr.");
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(array[dir][file]);
}
else
res = -ENOENT;
return res;
}
static int arrayfs_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;
int dir = parse_dir(path);
int file = parse_file(path);
printf("dir=%d files=%d\n", dir, file);
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
if (strcmp(path, "/") == 0)
{
for (int i = 0; i < ROOTDIR_LENGTH; i++)
{
for (int j = 0; j < DIR_LENGTH; j++)
{
if (array[i][j] != NULL)
{
char s[2];
snprintf(s, 2, "%d", i);
filler(buf, s, NULL, 0, 0);
break;
}
}
}
}
else if ((dir != -1) && (file == -1)) // nested dir
{
for (int j = 0; j < DIR_LENGTH; j++)
{
if (array[dir][j] != NULL)
{
char s[2];
snprintf(s, 2, "%d", j);
filler(buf, s, NULL, 0, 0);
continue;
}
}
}
else
{
return -ENOENT;
}
return 0;
}
static int arrayfs_open(const char *path, struct fuse_file_info *fi)
{
int dir = parse_dir(path);
int file = parse_file(path);
if (file == -1)
return -ENOENT;
if (array[dir][file] == NULL)
return -ENOENT;
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
return 0;
}
static int arrayfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void)fi;
int dir = parse_dir(path);
int file = parse_file(path);
if (dir == -1 || file == -1)
return -ENOENT;
if (array[dir][file] == NULL)
return -ENOENT;
len = strlen(array[dir][file]);
if (offset < len)
{
if (offset + size > len)
size = len - offset;
memcpy(buf, array[dir][file] + offset, size);
}
else
size = 0;
return size;
}
static const struct fuse_operations arrayfs_oper = {
.init = arrayfs_init,
.getattr = arrayfs_getattr,
.readdir = arrayfs_readdir,
.open = arrayfs_open,
.read = arrayfs_read,
};
int main(int argc, char *argv[])
{
//struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
printf("argc! %d\n", argc);
//printf("argv! %s\n", argv[3]) ;
printf("here!\n");
printf("here!\n");
printf("!!!%p\n", &arrayfs_oper);
printf("there!\n");
return fuse_main(argc,
argv,
&arrayfs_oper,
NULL);
//fuse_opt_free_args(&args);
}