forked from Aveek-Saha/FUSE-Filesystem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFS.c
More file actions
1674 lines (1510 loc) · 44.6 KB
/
FS.c
File metadata and controls
1674 lines (1510 loc) · 44.6 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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 <sys/stat.h>
#include <errno.h>
/*
* 编译和挂载文件系统说明
*
* 1. 编译文件系统:
* 使用以下命令编译文件系统代码:
* gcc FS.c -o FS `pkg-config fuse --cflags --libs`
*
* 参数说明:
* - gcc FS.c: 编译 FS.c 文件。
* - -o FS: 指定输出文件名为 FS。
* - `pkg-config fuse --cflags --libs`: 自动获取 FUSE 库的编译和链接参数。
*
* 2. 挂载文件系统:
* 使用以下命令挂载文件系统:
* ./FS -f Desktop/OS/mountpoint4
*
* 参数说明:
* - ./FS: 运行编译生成的文件系统程序。
* - -f: 在前台运行文件系统(调试模式),程序会输出日志信息。
* - Desktop/OS/mountpoint4: 挂载点路径,文件系统将挂载到该目录。
*
* 3. 挂载后的操作:
* 挂载成功后,可以通过挂载点路径(如 Desktop/OS/mountpoint4)访问文件系统:
* - 创建文件: touch Desktop/OS/mountpoint4/test.txt
* - 读取文件: cat Desktop/OS/mountpoint4/test.txt
* - 删除文件: rm Desktop/OS/mountpoint4/test.txt
*
* 4. 卸载文件系统:
* 使用以下命令卸载文件系统:
* fusermount -u Desktop/OS/mountpoint4
*
* 参数说明:
* - fusermount -u: 卸载文件系统。
* - Desktop/OS/mountpoint4: 挂载点路径。
*
* 5. 调试模式:
* 如果使用 -f 参数在前台运行文件系统,程序会输出日志信息,方便调试。例如:
* OPEN /test.txt
* READ /test.txt
* CLOSE /test.txt
*
* 注意事项:
* 1. 确保挂载点路径存在且为空目录。
* 2. 如果挂载失败,检查是否有权限问题或路径错误。
* 3. 调试完成后,可以去掉 -f 参数,让文件系统在后台运行。
*/
#define block_size 1024
/*
* superblock - 文件系统超级块结构
*
* 功能:
* 1. 存储文件系统的全局元数据信息。
* 2. 管理数据块和 inode 的分配状态。
*
* 字段说明:
* - datablocks: 数据块数组,存储文件系统的所有数据块。
* - data_bitmap: 数据块位图,标识哪些数据块已被占用('1')或空闲('0')。
* - inode_bitmap: inode 位图,标识哪些 inode 已被占用('1')或空闲('0')。
*
* 示例:
* 假设文件系统刚初始化,调用 initialize_superblock() 后:
* - spblock.data_bitmap 的所有位均为 '0',表示所有数据块空闲。
* - spblock.inode_bitmap 的所有位均为 '0',表示所有 inode 空闲。
*
* 注意:
* - 超级块是文件系统的核心数据结构,必须在文件系统初始化时调用 initialize_superblock() 方法。
* - 数据块位图和 inode 位图的大小固定为 100 个字节。
*/
typedef struct superblock
{
char datablocks[block_size * 100]; // 数据块数组,存储文件系统的所有数据块
char data_bitmap[105]; // 数据块位图,标识数据块的占用状态
char inode_bitmap[105]; // inode 位图,标识 inode 的占用状态
} superblock;
/*
* inode - 文件系统索引节点结构
*
* 功能:
* 1. 存储文件或目录的元数据信息。
* 2. 管理文件数据块的分配和引用。
*
* 字段说明:
* - datablocks: 数据块编号数组,存储文件数据所在的数据块。
* - number: inode 的唯一标识符。
* - blocks: 文件占用的数据块数量。
* - size: 文件或目录的大小(以字节为单位)。
*
* 示例:
* 假设文件系统结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* inode 示例:
* inode (test.txt):
* - datablocks: [5, 6, 7] // 文件数据存储在数据块 5、6、7 中
* - number: 3 // inode 编号为 3
* - blocks: 3 // 文件占用 3 个数据块
* - size: 3072 // 文件大小为 3072 字节
*
* 注意:
* - inode 是文件系统的核心数据结构,用于管理文件的元数据和数据块。
* - 每个文件或目录都有一个唯一的 inode。
*/
typedef struct inode
{
int datablocks[16]; // 数据块编号数组,存储文件数据所在的数据块
int number; // inode 的唯一标识符
int blocks; // 文件占用的数据块数量
// int link; //==number of links
int size; // 文件或目录的大小(以字节为单位)
} inode;
/*
* filetype - 文件/目录元数据结构
*
* 功能:
* 1. 存储文件或目录的元数据信息。
* 2. 用于管理文件系统的树形结构。
*
* 字段说明:
* - valid: 标识节点是否有效(1 表示有效,0 表示无效)。
* - test: 保留字段,未使用。
* - path: 文件或目录的完整路径。
* - name: 文件或目录的名称。
* - inum: 指向关联的 inode 结构。
* - children: 子节点指针数组,用于存储目录的子文件或子目录。
* - num_children: 子节点数量。
* - num_links: 硬链接数。
* - parent: 指向父目录的指针。
* - type: 文件类型(如 "file" 或 "directory")。
* - permissions: 文件或目录的权限模式(如 0777)。
* - user_id: 文件或目录的用户 ID。
* - group_id: 文件或目录的组 ID。
* - a_time: 最后访问时间。
* - m_time: 最后修改时间。
* - c_time: 最后状态更改时间。
* - b_time: 创建时间。
* - size: 文件或目录的大小(以字节为单位)。
* - datablocks: 数据块编号数组,存储文件数据所在的数据块。
* - number: 文件或目录的编号(唯一标识)。
* - blocks: 文件占用的数据块数量。
*
* 示例:
* 假设文件系统结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* 文件类型示例:
* Filetype (Root):
* - valid: 1
* - path: "/"
* - name: "/"
* - type: "directory"
* - permissions: 0777
* - user_id: 1000
* - group_id: 1000
* - a_time: 1698765432
* - m_time: 1698765432
* - c_time: 1698765432
* - b_time: 1698765432
* - size: 4096
* - number: 1
* - blocks: 1
* - children: [Filetype (home), Filetype (test.txt)]
* - num_children: 2
* - parent: NULL
* - num_links: 2
*
* 注意:
* - 文件类型和目录类型使用相同的结构体。
* - 目录的 size 字段通常表示目录元数据的大小。
*/
typedef struct filetype
{
int valid; // 标识节点是否有效
char test[10]; // 保留字段,未使用
char path[100]; // 文件或目录的完整路径
char name[100]; // 文件或目录的名称
inode *inum; // 指向关联的 inode 结构
struct filetype **children; // 子节点指针数组
int num_children; // 子节点数量
int num_links; // 硬链接数
struct filetype *parent; // 指向父目录的指针
char type[20]; // 文件类型(如 "file" 或 "directory")
mode_t permissions; // 文件或目录的权限模式
uid_t user_id; // 用户 ID
gid_t group_id; // 组 ID
time_t a_time; // 最后访问时间
time_t m_time; // 最后修改时间
time_t c_time; // 最后状态更改时间
time_t b_time; // 创建时间
off_t size; // 文件或目录的大小
int datablocks[16]; // 数据块编号数组
int number; // 文件或目录的编号
int blocks; // 文件占用的数据块数量
} filetype;
superblock spblock;
/*
* initialize_superblock - 初始化超级块
*
* 功能:
* 1. 初始化文件系统的超级块结构。
* 2. 将数据块位图和 inode 位图初始化为全 0,表示所有数据块和 inode 均为空闲状态。
*
* 参数:
* - 无。
*
* 返回值:
* - 无。
*
* 实现逻辑:
* 1. 使用 memset 将数据块位图(spblock.data_bitmap)初始化为全 0。
* 2. 使用 memset 将 inode 位图(spblock.inode_bitmap)初始化为全 0。
*
* 示例:
* 假设文件系统刚创建,调用 initialize_superblock() 后:
* - spblock.data_bitmap 的所有位均为 0,表示所有数据块空闲。
* - spblock.inode_bitmap 的所有位均为 0,表示所有 inode 空闲。
*
* 注意:
* - 超级块是文件系统的核心数据结构,必须在文件系统初始化时调用此方法。
* - 数据块位图和 inode 位图的大小固定为 100 个字节。
*/
void initialize_superblock()
{
memset(spblock.data_bitmap, '0', 100 * sizeof(char));
memset(spblock.inode_bitmap, '0', 100 * sizeof(char));
}
filetype *root;
filetype file_array[50];
/*
* tree_to_array - 将文件树结构序列化为数组
*
* 功能:
* 1. 通过广度优先遍历(BFS)将文件树结构扁平化为数组。
* 2. 将树中的每个节点按遍历顺序存储到数组中。
* 3. 使用无效节点(valid = 0)填充空位,确保数组长度固定。
*
* 参数:
* - queue: 用于广度优先遍历的队列,存储待处理的节点。
* - front: 队列的起始索引,指向当前处理的节点。
* - rear: 队列的结束索引,指向下一个可插入的位置。
* - index: 当前数组的索引,指向下一个可存储的位置。
*
* 实现逻辑:
* 1. 从队列中取出当前节点(queue[*front]),并将其存储到数组(file_array[*index])中。
* 2. 如果当前节点有效(valid = 1),将其子节点加入队列。
* 3. 如果当前节点无效或子节点不足 5 个,用无效节点填充队列。
* 4. 递归处理队列中的下一个节点,直到队列为空或数组已满。
*
* 示例:
* 假设文件树结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* 序列化后的数组布局如下:
* Filetype 0 (Root):
* - valid: 1
* - path: "/"
* - name: "/"
* - type: "directory"
* - children: [Filetype 1, Filetype 2]
*
* Filetype 1 (home):
* - valid: 1
* - path: "/home"
* - name: "home"
* - type: "directory"
* - children: [Filetype 3]
*
* Filetype 2 (test.txt):
* - valid: 1
* - path: "/test.txt"
* - name: "test.txt"
* - type: "file"
* - children: []
*
* Filetype 3 (user):
* - valid: 1
* - path: "/home/user"
* - name: "user"
* - type: "directory"
* - children: []
*
* Filetype 4-30:
* - valid: 0 (无效节点,用于占位)
*
* 注意:
* - 数组长度固定为 31,包括无效节点。
* - 每个节点最多有 5 个子节点。
*/
void tree_to_array(filetype *queue, int *front, int *rear, int *index)
{
if (rear < front)
return;
if (*index > 30)
return;
filetype curr_node = queue[*front];
*front += 1;
file_array[*index] = curr_node;
*index += 1;
if (*index < 6)
{
if (curr_node.valid)
{
int n = 0;
int i;
for (i = 0; i < curr_node.num_children; i++)
{
if (*rear < *front)
*rear = *front;
queue[*rear] = *(curr_node.children[i]);
*rear += 1;
}
while (i < 5)
{
filetype waste_node;
waste_node.valid = 0;
queue[*rear] = waste_node;
*rear += 1;
i++;
}
}
else
{
int i = 0;
while (i < 5)
{
filetype waste_node;
waste_node.valid = 0;
queue[*rear] = waste_node;
*rear += 1;
i++;
}
}
}
tree_to_array(queue, front, rear, index);
}
/*
* save_contents - 保存文件系统内容到磁盘
*
* 功能:
* 1. 将内存中的文件树结构通过广度优先遍历序列化为数组。
* 2. 将序列化后的数组和超级块分别保存到 `file_structure.bin` 和 `super.bin` 文件中。
*
* 文件布局:
* - `file_structure.bin` 存储文件树的所有节点信息,按广度优先遍历顺序扁平化为数组。
* - 每个节点包含文件/目录的元数据,如路径、名称、类型、权限、时间戳等。
* - 无效节点(valid = 0)用于占位,确保数组长度固定。
* - 在内存中,通过 children 指针重建树结构。
* - `super.bin` 存储超级块信息,包括数据块和 inode 的位图。
*
* 示例:
* 假设文件系统结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* `file_structure.bin` 的布局可能如下:
* Filetype 0 (Root):
* - valid: 1
* - path: "/"
* - name: "/"
* - type: "directory"
* - children: [Filetype 1, Filetype 2]
*
* Filetype 1 (home):
* - valid: 1
* - path: "/home"
* - name: "home"
* - type: "directory"
* - children: [Filetype 3]
*
* Filetype 2 (test.txt):
* - valid: 1
* - path: "/test.txt"
* - name: "test.txt"
* - type: "file"
* - children: []
*
* Filetype 3 (user):
* - valid: 1
* - path: "/home/user"
* - name: "user"
* - type: "directory"
* - children: []
*
* Filetype 4-30:
* - valid: 0 (无效节点,用于占位)
*
* 流程:
* 1. 初始化队列,将根节点加入队列。
* 2. 通过广度优先遍历将文件树序列化为数组。
* 3. 将序列化后的数组写入 `file_structure.bin`。
* 4. 将超级块写入 `super.bin`。
*
* 注意:
* - 文件树最多支持 31 个节点(包括无效节点)。
* - 每个节点最多有 5 个子节点。
*/
int save_contents()
{
printf("SAVING\n");
filetype *queue = malloc(sizeof(filetype) * 60);
int front = 0;
int rear = 0;
queue[0] = *root;
int index = 0;
tree_to_array(queue, &front, &rear, &index);
for (int i = 0; i < 31; i++)
{
printf("%d", file_array[i].valid);
}
FILE *fd = fopen("file_structure.bin", "wb");
FILE *fd1 = fopen("super.bin", "wb");
fwrite(file_array, sizeof(filetype) * 31, 1, fd);
fwrite(&spblock, sizeof(superblock), 1, fd1);
fclose(fd);
fclose(fd1);
printf("\n");
}
/*
* initialize_root_directory - 初始化根目录
*
* 功能:
* 1. 创建并初始化文件系统的根目录。
* 2. 设置根目录的元数据,包括路径、名称、类型、权限、时间戳等。
* 3. 标记根目录的 inode 为已使用。
* 4. 调用 save_contents 方法将初始化后的文件系统保存到磁盘。
*
* 实现逻辑:
* 1. 在 inode 位图中标记根目录的 inode 为已使用(spblock.inode_bitmap[1] = 1)。
* 2. 分配内存并初始化根目录结构(filetype)。
* 3. 设置根目录的路径为 "/",名称为 "/"。
* 4. 设置根目录的类型为 "directory",权限为 0777。
* 5. 初始化时间戳(创建时间、访问时间、修改时间等)。
* 6. 设置根目录的 inode 编号为 2。
* 7. 调用 save_contents 方法保存文件系统。
*
* 初始化后的根目录结构示例:
* Filetype (Root):
* - valid: 1
* - path: "/"
* - name: "/"
* - type: "directory"
* - permissions: 0777
* - user_id: 当前用户ID
* - group_id: 当前组ID
* - a_time: 当前时间
* - m_time: 当前时间
* - c_time: 当前时间
* - b_time: 当前时间
* - size: 0
* - number: 2 (inode 编号)
* - blocks: 0
* - children: NULL
* - num_children: 0
* - parent: NULL
* - num_links: 2
*
* 注意:
* - 根目录的 inode 编号固定为 2。
* - 根目录是文件系统的起点,所有其他文件和目录都是其子节点。
*/
void initialize_root_directory()
{
spblock.inode_bitmap[1] = 1; // marking it with 0
root = (filetype *)malloc(sizeof(filetype));
strcpy(root->path, "/");
strcpy(root->name, "/");
root->children = NULL;
root->num_children = 0;
root->parent = NULL;
root->num_links = 2;
root->valid = 1;
strcpy(root->test, "test");
// root -> type = malloc(10);
strcpy(root->type, "directory");
root->c_time = time(NULL);
root->a_time = time(NULL);
root->m_time = time(NULL);
root->b_time = time(NULL);
root->permissions = S_IFDIR | 0777;
root->size = 0;
root->group_id = getgid();
root->user_id = getuid();
root->number = 2;
// root -> size = 0;
root->blocks = 0;
save_contents();
}
/*
* filetype_from_path - 根据路径查找对应的文件节点
*
* 功能:
* 1. 根据给定的路径,在文件树中查找对应的文件或目录节点。
* 2. 支持绝对路径(以 "/" 开头)的解析。
* 3. 如果路径不存在,返回 NULL。
*
* 参数:
* - path: 要查找的路径字符串(必须以 "/" 开头)。
*
* 返回值:
* - 成功时返回对应的文件节点指针(filetype *)。
* - 如果路径不存在或路径格式错误,返回 NULL。
*
* 实现逻辑:
* 1. 检查路径是否以 "/" 开头,如果不是则报错并退出。
* 2. 从根节点开始,逐级解析路径中的目录名。
* 3. 在当前节点的子节点中查找匹配的目录或文件。
* 4. 如果找到匹配的节点,返回该节点;否则返回 NULL。
*
* 示例:
* 假设文件树结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* 调用示例:
* 1. filetype_from_path("/") -> 返回根节点
* 2. filetype_from_path("/home") -> 返回 home 目录节点
* 3. filetype_from_path("/home/user") -> 返回 user 目录节点
* 4. filetype_from_path("/test.txt") -> 返回 test.txt 文件节点
* 5. filetype_from_path("/invalid") -> 返回 NULL
*
* 注意:
* - 路径必须以 "/" 开头。
* - 路径末尾的 "/" 会被自动去除。
* - 如果路径不存在,返回 NULL。
*/
filetype *filetype_from_path(char *path)
{
char curr_folder[100];
char *path_name = malloc(strlen(path) + 2);
strcpy(path_name, path);
filetype *curr_node = root;
fflush(stdin);
if (strcmp(path_name, "/") == 0)
return curr_node;
if (path_name[0] != '/')
{
printf("INCORRECT PATH\n");
exit(1);
}
else
{
path_name++;
}
if (path_name[strlen(path_name) - 1] == '/')
{
path_name[strlen(path_name) - 1] = '\0';
}
char *index;
int flag = 0;
while (strlen(path_name) != 0)
{
index = strchr(path_name, '/');
if (index != NULL)
{
strncpy(curr_folder, path_name, index - path_name);
curr_folder[index - path_name] = '\0';
flag = 0;
for (int i = 0; i < curr_node->num_children; i++)
{
if (strcmp((curr_node->children)[i]->name, curr_folder) == 0)
{
curr_node = (curr_node->children)[i];
flag = 1;
break;
}
}
if (flag == 0)
return NULL;
}
else
{
strcpy(curr_folder, path_name);
flag = 0;
for (int i = 0; i < curr_node->num_children; i++)
{
if (strcmp((curr_node->children)[i]->name, curr_folder) == 0)
{
curr_node = (curr_node->children)[i];
return curr_node;
}
}
return NULL;
}
path_name = index + 1;
}
}
/*
* find_free_inode - 查找空闲的 inode
*
* 功能:
* 1. 查找当前文件系统中空闲的 inode 编号。
* 2. 用于创建新文件或目录时分配 inode。
*
* 参数:
* - 无。
*
* 返回值:
* - 成功时返回空闲的 inode 编号。
* - 如果没有空闲 inode,返回 -1。
*
* 实现逻辑:
* 1. 遍历 inode 表,查找第一个空闲的 inode。
* 2. 返回找到的 inode 编号。
*
* 注意:
* - 如果 inode 表已满,需扩展文件系统。
*/
int find_free_inode()
{
for (int i = 2; i < 100; i++)
{
if (spblock.inode_bitmap[i] == '0')
{
spblock.inode_bitmap[i] = '1';
}
return i;
}
}
/*
* find_free_db - 查找空闲的数据块
*
* 功能:
* 1. 查找当前文件系统中空闲的数据块编号。
* 2. 用于存储文件数据时分配数据块。
*
* 参数:
* - 无。
*
* 返回值:
* - 成功时返回空闲的数据块编号。
* - 如果没有空闲数据块,返回 -1。
*
* 实现逻辑:
* 1. 遍历数据块位图,查找第一个空闲的数据块。
* 2. 返回找到的数据块编号。
*
* 注意:
* - 如果数据块已满,需扩展文件系统。
*/
int find_free_db()
{
for (int i = 1; i < 100; i++)
{
if (spblock.inode_bitmap[i] == '0')
{
spblock.inode_bitmap[i] = '1';
}
return i;
}
}
/*
* add_child - 添加子节点
*
* 功能:
* 1. 将指定节点添加到父目录的子节点列表中。
* 2. 更新父目录的子节点数量和列表。
*
* 参数:
* - parent: 父目录节点。
* - child: 要添加的子节点。
*
* 返回值:
* - 无。
*
* 实现逻辑:
* 1. 扩展父目录的子节点列表(如果需要)。
* 2. 将子节点添加到列表中。
* 3. 更新父目录的子节点数量。
*
* 注意:
* - 确保父目录是目录类型。
*/
void add_child(filetype *parent, filetype *child)
{
(parent->num_children)++;
parent->children = realloc(parent->children, (parent->num_children) * sizeof(filetype *));
(parent->children)[parent->num_children - 1] = child;
}
/*
* mymkdir - 创建新目录
*
* 功能:
* 1. 在指定路径下创建一个新目录。
* 2. 分配并初始化新目录的元数据,包括路径、名称、类型、权限、时间戳等。
* 3. 将新目录添加到父目录的子节点列表中。
* 4. 调用 save_contents 方法将更新后的文件系统保存到磁盘。
*
* 参数:
* - path: 新目录的完整路径(必须以 "/" 开头)。
* - mode: 目录的权限模式(未直接使用,固定为 0777)。
*
* 返回值:
* - 成功时返回 0。
* - 如果父目录不存在,返回 -ENOENT。
*
* 实现逻辑:
* 1. 查找一个空闲的 inode 编号。
* 2. 解析路径,获取新目录的名称和父目录路径。
* 3. 分配内存并初始化新目录结构(filetype)。
* 4. 设置新目录的元数据,包括路径、名称、类型、权限、时间戳等。
* 5. 将新目录添加到父目录的子节点列表中。
* 6. 调用 save_contents 方法保存文件系统。
*
* 示例:
* 假设文件系统结构如下:
* /
* └── home
*
* 调用示例:
* 1. mymkdir("/home/user", 0777) -> 在 /home 下创建 user 目录
* - 新目录路径: "/home/user"
* - 新目录名称: "user"
* - 父目录: "/home"
* 2. mymkdir("/invalid/path", 0777) -> 返回 -ENOENT(父目录不存在)
*
* 注意:
* - 路径必须以 "/" 开头。
* - 新目录的权限固定为 0777。
* - 如果父目录不存在,操作失败。
*/
static int mymkdir(const char *path, mode_t mode)
{
printf("MKDIR\n");
int index = find_free_inode();
filetype *new_folder = malloc(sizeof(filetype));
char *pathname = malloc(strlen(path) + 2);
strcpy(pathname, path);
char *rindex = strrchr(pathname, '/');
// new_folder -> name = malloc(strlen(pathname)+2);
strcpy(new_folder->name, rindex + 1);
// new_folder -> path = malloc(strlen(pathname)+2);
strcpy(new_folder->path, pathname);
*rindex = '\0';
if (strlen(pathname) == 0)
strcpy(pathname, "/");
new_folder->children = NULL;
new_folder->num_children = 0;
new_folder->parent = filetype_from_path(pathname);
new_folder->num_links = 2;
new_folder->valid = 1;
strcpy(new_folder->test, "test");
if (new_folder->parent == NULL)
return -ENOENT;
// printf(";;;;%p;;;;\n", new_folder);
add_child(new_folder->parent, new_folder);
// new_folder -> type = malloc(10);
strcpy(new_folder->type, "directory");
new_folder->c_time = time(NULL);
new_folder->a_time = time(NULL);
new_folder->m_time = time(NULL);
new_folder->b_time = time(NULL);
new_folder->permissions = S_IFDIR | 0777;
new_folder->size = 0;
new_folder->group_id = getgid();
new_folder->user_id = getuid();
new_folder->number = index;
new_folder->blocks = 0;
save_contents();
return 0;
}
/*
* myreaddir - 读取目录内容
*
* 功能:
* 1. 读取指定目录下的所有文件和子目录。
* 2. 使用 FUSE 提供的 filler 函数将目录项添加到缓冲区。
* 3. 更新目录的访问时间。
*
* 参数:
* - path: 要读取的目录路径。
* - buffer: 用于存储目录项的缓冲区。
* - filler: FUSE 提供的回调函数,用于将目录项添加到缓冲区。
* - offset: 读取偏移量(未使用)。
* - fi: 文件信息结构(未使用)。
*
* 返回值:
* - 成功时返回 0。
* - 如果目录不存在,返回 -ENOENT。
*
* 实现逻辑:
* 1. 使用 filler 函数添加 "." 和 ".." 目录项。
* 2. 根据路径查找对应的目录节点。
* 3. 如果目录存在,遍历其子节点,使用 filler 函数将每个子节点添加到缓冲区。
* 4. 更新目录的访问时间。
*
* 示例:
* 假设文件系统结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* 调用示例:
* 1. myreaddir("/", buffer, filler, 0, fi) -> 缓冲区包含 ["home", "test.txt"]
* 2. myreaddir("/home", buffer, filler, 0, fi) -> 缓冲区包含 ["user"]
* 3. myreaddir("/invalid", buffer, filler, 0, fi) -> 返回 -ENOENT
*
* 注意:
* - 每次读取目录时,都会更新目录的访问时间。
* - 如果目录不存在,返回 -ENOENT。
*/
int myreaddir(const char *path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
printf("READDIR\n");
filler(buffer, ".", NULL, 0);
filler(buffer, "..", NULL, 0);
char *pathname = malloc(strlen(path) + 2);
strcpy(pathname, path);
filetype *dir_node = filetype_from_path(pathname);
if (dir_node == NULL)
{
return -ENOENT;
}
else
{
dir_node->a_time = time(NULL);
for (int i = 0; i < dir_node->num_children; i++)
{
printf(":%s:\n", dir_node->children[i]->name);
filler(buffer, dir_node->children[i]->name, NULL, 0);
}
}
return 0;
}
/*
* mygetattr - 获取文件或目录属性
*
* 功能:
* 1. 获取指定路径的文件或目录的属性信息。
* 2. 将属性信息填充到 stat 结构中。
* 3. 支持文件和目录的属性查询。
*
* 参数:
* - path: 要查询的文件或目录的完整路径。
* - statit: 用于存储属性信息的 stat 结构指针。
*
* 返回值:
* - 成功时返回 0。
* - 如果文件或目录不存在,返回 -ENOENT。
*
* 实现逻辑:
* 1. 根据路径查找对应的文件或目录节点。
* 2. 如果节点存在,将属性信息填充到 stat 结构中,包括:
* - 用户 ID (st_uid)
* - 组 ID (st_gid)
* - 访问时间 (st_atime)
* - 修改时间 (st_mtime)
* - 创建时间 (st_ctime)
* - 权限模式 (st_mode)
* - 硬链接数 (st_nlink)
* - 文件大小 (st_size)
* - 数据块数 (st_blocks)
* 3. 如果节点不存在,返回错误码。
*
* 示例:
* 假设文件系统结构如下:
* /
* ├── home
* │ └── user
* └── test.txt
*
* 调用示例:
* 1. mygetattr("/test.txt", statit) -> 获取 test.txt 文件的属性
* 2. mygetattr("/home", statit) -> 获取 home 目录的属性
* 3. mygetattr("/invalid", statit) -> 返回 -ENOENT(文件或目录不存在)
*
* 注意:
* - 必须正确处理文件和目录的属性查询。
*/
static int mygetattr(const char *path, struct stat *statit)
{
char *pathname;
pathname = (char *)malloc(strlen(path) + 2);
strcpy(pathname, path);
printf("GETATTR %s\n", pathname);
filetype *file_node = filetype_from_path(pathname);
if (file_node == NULL)
return -ENOENT;
statit->st_uid = file_node->user_id; // The owner of the file/directory is the user who mounted the filesystem
statit->st_gid = file_node->group_id; // The group of the file/directory is the same as the group of the user who mounted the filesystem
statit->st_atime = file_node->a_time; // The last "a"ccess of the file/directory is right now
statit->st_mtime = file_node->m_time; // The last "m"odification of the file/directory is right now
statit->st_ctime = file_node->c_time;
statit->st_mode = file_node->permissions;
statit->st_nlink = file_node->num_links + file_node->num_children;
statit->st_size = file_node->size;
statit->st_blocks = file_node->blocks;
return 0;
}
/*
* myrmdir - 删除目录
*
* 功能:
* 1. 删除指定路径的目录。
* 2. 从父目录的子节点列表中移除该目录。
* 3. 调用 save_contents 方法将更新后的文件系统保存到磁盘。
*
* 参数:
* - path: 要删除的目录的完整路径。
*
* 返回值: