forked from MaaSTaaR/LSYSFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsysfs.c
485 lines (414 loc) · 12.4 KB
/
lsysfs.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
/**
* Less Simple, Yet Stupid Filesystem.
*
* Mohammed Q. Hussain - http://www.maastaar.net
*
* This is an example of using FUSE to build a simple filesystem. It is a part of a tutorial in MQH Blog with the title "Writing Less Simple, Yet Stupid Filesystem Using FUSE in C": http://maastaar.net/fuse/linux/filesystem/c/2019/09/28/writing-less-simple-yet-stupid-filesystem-using-FUSE-in-C/
*
* License: GNU GPL
*/
#define FUSE_USE_VERSION 30
#include <fuse.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
// 加入aes演算法
#include "aes/aes.h"
int id_count = 0;
// 模擬inode的結構
struct file{
int idx;
char name[ 256 ];
char content[ 256 ];// 檔案內容
struct file* next_file;
};
struct dir{
char name[ 256 ];
struct inode* content_inode;
struct dir* next_dir;
};
// link list紀錄filelist, dir list
struct inode{
struct file* files_list; // files link list for this layer
struct dir* dir_list; // directory link list for this layer
};
// 初始化根目錄
struct inode temp_node= {.files_list = NULL, .dir_list = NULL};
struct inode * root_inode = &temp_node;
/*分析path
將一個char* 的path字串根據"/"拆成
ex: "dir1/dir2" => ["dir1", "dir2"]
*/
#define MAX_DIRS 100
char** split_path(const char *path, char delimiter, int *count) {
char *temp_path;
char delim[2] = {delimiter, '\0'};
char *token;
char **output;
int dir_count = 0;
// 拷貝原始字符串
temp_path = strdup(path);
if (temp_path == NULL) {
perror("strdup failed");
exit(EXIT_FAILURE);
}
// 分配指針數組的內存
output = (char **)malloc(MAX_DIRS * sizeof(char *));
if (output == NULL) {
perror("malloc failed");
free(temp_path);
exit(EXIT_FAILURE);
}
// 使用strtok拆分字符串
token = strtok(temp_path, delim);
while (token != NULL && dir_count < MAX_DIRS) {
output[dir_count] = strdup(token);
if (output[dir_count] == NULL) {
perror("strdup failed");
// 釋放已分配的內存
for (int i = 0; i < dir_count; i++) {
free(output[i]);
}
free(output);
free(temp_path);
exit(EXIT_FAILURE);
}
dir_count++;
token = strtok(NULL, delim);
}
free(temp_path); // 釋放臨時字符串的內存
*count = dir_count;
return output;
}
struct inode* trace_inode(char** path_list, int count){
struct inode* current_inode = (struct inode*)malloc(sizeof(struct inode*));
current_inode = root_inode;
for(int i=0; i<count-1; i++){ // 留下path中的最後一組
// 從當層的inode找dir
struct dir* dir_ptr = current_inode->dir_list;
while(dir_ptr!= NULL){
if( strcmp( path_list[i], dir_ptr->name ) == 0 ){
current_inode = dir_ptr->content_inode;
break;
}
dir_ptr = dir_ptr->next_dir;
}
}
return current_inode;
}
//
// 初始化函數
struct inode* create_inode() {
struct inode* new_inode = (struct inode*)malloc(sizeof(struct inode));
if (new_inode == NULL) {
perror("Failed to allocate memory");
exit(EXIT_FAILURE);
}
new_inode->files_list = NULL;
new_inode->dir_list = NULL;
return new_inode;
}
// create_file funciton
struct file* create_file(){
struct file* new_file = (struct file*)malloc(sizeof(struct file));
memset(new_file->content, 0, sizeof(sizeof(char*)*256));
memset(new_file->name, 0, sizeof(sizeof(char*)*256));
new_file->idx = id_count++;
return new_file;
}
void removeFile(struct file * prev_ptr, struct file * target_ptr){
prev_ptr->next_file = target_ptr->next_file;
free(target_ptr);
}
struct dir* create_dir(){
struct dir* new_dir = (struct dir*)malloc(sizeof(struct dir));
memset(new_dir->name, 0, sizeof(sizeof(char*)*256));
new_dir->next_dir = NULL;
return new_dir;
}
void add_dir( const char *dir_name )
{
// 實體劃一個inode structure(必須分配記憶體空間),要連在正確的inode下
int path_len;
char** path_list = split_path(dir_name, '/', &path_len);
struct inode* current_inode = trace_inode(path_list, path_len);
if(current_inode == NULL)
printf("error in add_dir when trace inode!\n");
struct inode * new_inode = create_inode();
struct dir * new_dir = create_dir();
strcpy(new_dir->name, path_list[path_len-1]);
new_dir->content_inode = new_inode;
// 插入到dir list中
new_dir->next_dir = current_inode->dir_list;
current_inode->dir_list = new_dir;
}
int is_dir( const char *path )
{
path++; // Eliminating "/" in the path
// 這邊要分析path,追著inode去判斷最後是不是對應到dir
int path_len;
char** path_list = split_path(path, '/', &path_len);
struct inode* current_inode = trace_inode(path_list, path_len);
int check = 0;
struct dir* dir_ptr = current_inode->dir_list;
while(dir_ptr && path_len){
if( strcmp( path_list[path_len-1], dir_ptr->name ) == 0 ){
check = 1;
break;
}
dir_ptr = dir_ptr->next_dir;
}
if(!check){
return 0;
}
return 1;
}
void add_file( const char *path )
{
int count;
char** path_list = split_path(path, '/', &count);
struct inode * current_inode = trace_inode(path_list, count);
struct file* new_file = create_file();
strcpy( new_file->name, path_list[count-1] );
// 插入到file list中
new_file->next_file = current_inode->files_list;
current_inode->files_list = new_file;
}
int is_file( const char *path )
{
//先找出inode
int count;
char** path_list = split_path(path, '/', &count);
struct inode * current_inode = trace_inode(path_list, count);
// 找到file
int check = 0;
struct file* file_ptr = current_inode->files_list;
while(file_ptr && count){
if( strcmp( path_list[count-1], file_ptr->name ) == 0 ){
check = 1;
break;
}
file_ptr = file_ptr->next_file;
}
if(check)
return 1;
return 0;
}
void write_to_file( const char *path, const char *new_content )
{
// write file
//先找出inode
int count;
char** path_list = split_path(path, '/', &count);
struct inode * current_inode = trace_inode(path_list, count);
// 找到file
int check = 0;
struct file* file_ptr = current_inode->files_list;
while(file_ptr && count){
if( strcmp( path_list[count-1], file_ptr->name ) == 0 ){
check = 1;
break;
}
file_ptr = file_ptr->next_file;
}
struct file* target_file = file_ptr;
unsigned char* encrypted_data = encrypt(new_content, target_file->idx);
//strcpy 內容
strcpy( target_file->content, encrypted_data );
}
// ... //
static int do_getattr( const char *path, struct stat *st )
{
st->st_uid = getuid(); // The owner of the file/directory is the user who mounted the filesystem
st->st_gid = getgid(); // The group of the file/directory is the same as the group of the user who mounted the filesystem
st->st_atime = time( NULL ); // The last "a"ccess of the file/directory is right now
st->st_mtime = time( NULL ); // The last "m"odification of the file/directory is right now
if ( strcmp( path, "/" ) == 0 || is_dir( path ) == 1 )
{
st->st_mode = S_IFDIR | 0755;
st->st_nlink = 2; // Why "two" hardlinks instead of "one"? The answer is here: http://unix.stackexchange.com/a/101536
}
else if ( is_file( path ) == 1 )
{
st->st_mode = S_IFREG | 0644;
st->st_nlink = 1;
st->st_size = 1024;
}
else
{
return -ENOENT;
}
return 0;
}
static int do_readdir( const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi )
{
printf("Read dir: %s\n", path);
// 用inode tree找到當下路徑的inode並用該inode列出所有file和dir
//這兩行不知道是什麼
filler( buffer, ".", NULL, 0 ); // Current Directory
filler( buffer, "..", NULL, 0 ); // Parent Directory
// 跳到指定的路徑
int count;
char** path_list = split_path(path, '/', &count);
struct inode * current_inode = trace_inode(path_list, count);
struct dir* dir_ptr = current_inode->dir_list;
while(dir_ptr && count){
if( strcmp( path_list[count-1], dir_ptr->name ) == 0 ){
current_inode = dir_ptr->content_inode;
break;
}
dir_ptr = dir_ptr->next_dir;
}
struct dir* dirlistptr = current_inode->dir_list;
while(dirlistptr != NULL){
filler( buffer, dirlistptr->name, NULL, 0 );
dirlistptr = dirlistptr->next_dir;
}
struct file* filelistptr = current_inode->files_list;
while(filelistptr){
filler( buffer, filelistptr->name, NULL, 0 );
filelistptr = filelistptr->next_file;
}
return 0;
}
static int do_read( const char *path, char *buffer, size_t size, off_t offset, struct fuse_file_info *fi )
{
printf("Read file: %s\n", path);
int path_len;
char** path_list = split_path(path, '/', &path_len);
struct inode * current_inode = trace_inode(path_list, path_len);
// 找到file
int check = 0;
struct file* file_ptr = current_inode->files_list;
while(file_ptr && path_len){
if( strcmp( path_list[path_len-1], file_ptr->name ) == 0 )
break;
file_ptr = file_ptr->next_file;
}
char *content = file_ptr->content;
content = decrypt(content, file_ptr->idx);
memcpy( buffer, content + offset, sizeof(char*)*strlen(content) );
return strlen( content ) - offset;
}
static int do_mkdir( const char *path, mode_t mode )
{
printf("Remove file: %s\n", path);
path++;
add_dir( path );
return 0;
}
static int do_mknod( const char *path, mode_t mode, dev_t rdev )
{
printf("Make Node : %s\n", path);
path++;
add_file( path );
return 0;
}
static int do_write( const char *path, const char *buffer, size_t size, off_t offset, struct fuse_file_info *info )
{
printf("Write file: %s\n", path);
write_to_file( path, buffer );
return size;
}
// TODO: rmdir
static int do_rmdir(const char * path){
printf("Remove dir: %s\n", path);
int path_len;
char** path_list = split_path(path, '/', &path_len);
struct inode* current_inode = trace_inode(path_list, path_len);
struct dir* prev_dir = current_inode->dir_list;
struct dir* dir_ptr = current_inode->dir_list;
while(dir_ptr && path_len){
if( strcmp( path_list[path_len-1], dir_ptr->name ) == 0 ){
break;
}
prev_dir = dir_ptr;
dir_ptr = dir_ptr->next_dir;
}
if(dir_ptr == current_inode->dir_list){
current_inode->dir_list = dir_ptr->next_dir;
free(dir_ptr);
}
else{
prev_dir->next_dir = dir_ptr->next_dir;
free(dir_ptr);
}
}
static int do_rm(const char * path){
printf("Remove file(unlink): %s\n", path);
// rm file
int path_len;
char** path_list = split_path(path, '/', &path_len);
struct inode* current_inode = trace_inode(path_list, path_len);
struct file* prev_file = current_inode->files_list;
struct file* file_ptr = current_inode->files_list;
while(file_ptr && path_len){
if( strcmp( path_list[path_len-1], file_ptr->name ) == 0 ){
break;
}
prev_file = file_ptr;
file_ptr = file_ptr->next_file;
}
if(file_ptr == current_inode->files_list){
current_inode->files_list = file_ptr->next_file;
free(file_ptr);
}
else{
prev_file->next_file = file_ptr->next_file;
free(file_ptr);
}
}
struct my_file_info {
int fd; // 文件描述符
};
// TODO: open file
static int do_open(const char *path, struct fuse_file_info *fi) {
printf("Opening file: %s\n", path);
return 0;
}
// TODO: create file
static int do_create(const char *path, const struct timespec tv[2], struct fuse_file_info *fi) {
path++;
printf("Create file : %s\n", path);
int count;
char** path_list = split_path(path, '/', &count);
struct inode * current_inode = trace_inode(path_list, count);
struct file* new_file = create_file();
strcpy( new_file->name, path_list[count-1] );
// 插入到file list中
new_file->next_file = current_inode->files_list;
current_inode->files_list = new_file;
unsigned char* aes_key = malloc(sizeof(unsigned char)*AES_KEY_SIZE);
unsigned char* aes_iv = malloc(sizeof(unsigned char)*AES_KEY_SIZE);
genKey(&aes_key);
genKey(&aes_iv);
memcpy(map_key[new_file->idx], aes_key, AES_KEY_SIZE);
memcpy(map_iv[new_file->idx], aes_iv, AES_KEY_SIZE);
return 0;
}
// TODO: close file
static int do_close(const char * path, struct fuse_file_info * fi){
printf("Close file: %s\n", path);
return 0;
}
static struct fuse_operations operations = {
.getattr = do_getattr,
.readdir = do_readdir,
.read = do_read,
.mkdir = do_mkdir,
.mknod = do_create,
.write = do_write,
.rmdir = do_rmdir,
.unlink = do_rm, // 接收rm指令fuse_operations
.create = do_create, // 接收touch指令的fuse_operations
.release = do_close,
.open = do_open
};
int main( int argc, char *argv[] )
{
return fuse_main( argc, argv, &operations, NULL );
}