-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdir.c
More file actions
1378 lines (1120 loc) · 34.8 KB
/
Copy pathdir.c
File metadata and controls
1378 lines (1120 loc) · 34.8 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
#include <dir.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "dolphinfs.h"
struct fs_dir_open open_dirs[MAX_OPEN_DIR] = {0};
struct fs_dir_open *alloc_open_dir(void)
{
int i;
struct fs_dir_open *od;
for (i = 0; i < MAX_OPEN_DIR; i++) {
od = &open_dirs[i];
if (od->sb == NULL) {
return od;
}
}
return NULL;
}
int free_open_dir(struct fs_dir_open *od)
{
if (!od) {
return -1;
}
od->sb = NULL;
return 0;
}
#define odir_idx(od) ((od) - &open_dirs[0])
#define idx_odir(idx) (&open_dirs[(idx)])
/**
* @brief 清理路径,移除其中的 "."、".." 和多余的分隔符。
* @param input_path 待清洗的原始路径字符串
* @param clean_result 存储清洗后结果的字符数组
* @param result_size clean_result数组的大小
*/
void clean_path(const char* input_path, char* clean_result, size_t result_size) {
char* components[256]; // 栈,用于存储路径组件
int depth = -1; // 栈深度指针
// 复制路径,因为strtok会修改原字符串
char* path_copy = strdup(input_path);
char* token = strtok(path_copy, "/");
// 1. 词法分析:分解路径并处理特殊组件
while (token != NULL) {
if (strcmp(token, ".") == 0 || strlen(token) == 0) {
// 忽略当前目录标记和空字符串
} else if (strcmp(token, "..") == 0) {
// 处理上级目录:如果栈内有组件,则弹出(回退)
if (depth >= 0) {
depth--;
}
// 如果栈为空,在根目录下忽略".."
} else {
// 普通路径组件,压入栈
if (depth < 255) {
components[++depth] = token;
}
}
token = strtok(NULL, "/");
}
// 2. 构建标准化路径
clean_result[0] = '\0';
// 处理绝对路径:如果原路径以'/'开始
if (input_path[0] == '/') {
strncat(clean_result, "/", result_size - 1);
}
// 连接栈中的组件
for (int i = 0; i <= depth; i++) {
strncat(clean_result, components[i], result_size - strlen(clean_result) - 1);
if (i < depth) {
strncat(clean_result, "/", result_size - strlen(clean_result) - 1);
}
}
// 如果结果为空,设为当前目录
if (strlen(clean_result) == 0) {
strncpy(clean_result, ".", result_size);
}
clean_result[result_size - 1] = '\0'; // 确保终止
free(path_copy); // 释放复制的字符串
}
/**
* @brief 从路径中提取下一个组件
* @param path 输入路径
* @param component 输出参数,存储提取的组件
* @param max_len 组件缓冲区最大长度
* @return 返回下一个组件的起始位置,NULL表示结束
*/
const char* extract_next_component(const char* path, char* component, size_t max_len) {
static const char* current_pos = NULL;
const char* start;
size_t len = 0;
// 初始化或继续上次的位置
if (path != NULL) {
current_pos = path;
}
if (current_pos == NULL || *current_pos == '\0') {
return NULL;
}
// 跳过开头的分隔符
while (*current_pos == '/') {
current_pos++;
}
if (*current_pos == '\0') {
return NULL;
}
// 找到组件的开始和结束
start = current_pos;
while (*current_pos != '\0' && *current_pos != '/') {
len++;
current_pos++;
}
// 复制组件(确保不溢出)
if (len >= max_len) {
len = max_len - 1;
}
strncpy(component, start, len);
component[len] = '\0';
return current_pos;
}
/**
* @brief 检查路径组件是否存在且为目录
* @param base_path 基础路径
* @param component 要检查的组件名
* @param full_path 输出参数,存储完整路径
* @param max_path_len 路径缓冲区大小
* @return 0:存在且为目录, -1:不存在, -2:存在但不是目录
*/
int check_component_exists(struct super_block *sb, const char* base_path, const char* component,
char* full_path, size_t max_path_len) {
struct file_stat stat_buf;
// 构建完整路径
if (strcmp(base_path, "/") == 0) {
snprintf(full_path, max_path_len, "/%s", component);
} else {
snprintf(full_path, max_path_len, "%s/%s", base_path, component);
}
// 检查路径是否存在
// printf("check_component_exists: %s\n", full_path);
if (stat_file(sb, full_path, &stat_buf) != 0) {
return -1; // 路径不存在
}
// 检查是否为目录
if (!(stat_buf.mode & FF_DIR)) {
return -2; // 存在但不是目录
}
return 0; // 存在且是目录
}
static int load_dir_meta(struct fs_dir_meta *meta, int fd)
{
int bytes_of_index;
/* seek to head */
if (seek_file(fd, 0, FP_SET) != 0) {
return -1;
}
if (read_file(fd, &meta->head, sizeof(struct fs_dir_head)) != sizeof(struct fs_dir_head)) {
return -1;
}
if (meta->head.max_index > 0) {
bytes_of_index = DIV_ROUND_UP(meta->head.max_index, DIR_BITMAP_ITEM_SIZE) * sizeof(unsigned int);
if (read_file(fd, &meta->body, bytes_of_index) != bytes_of_index) {
return -1;
}
}
return 0;
}
static int sync_dir_meta(struct fs_dir_meta *meta, int fd, char is_full)
{
int bytes_of_index;
/* seek to head */
if (seek_file(fd, 0, FP_SET) != 0) {
return -1;
}
if (!is_full) {
if (write_file(fd, &meta->head, sizeof(struct fs_dir_head)) != sizeof(struct fs_dir_head)) {
return -1;
}
if (meta->head.max_index > 0) {
bytes_of_index = DIV_ROUND_UP(meta->head.max_index, DIR_BITMAP_ITEM_SIZE) * sizeof(unsigned int);
if (write_file(fd, &meta->body, bytes_of_index) != bytes_of_index) {
return -1;
}
}
} else {
if (write_file(fd, meta, sizeof(struct fs_dir_meta)) != sizeof(struct fs_dir_meta)) {
return -1;
}
}
return 0;
}
static int touch_dir_file(struct super_block *sb, const char *path)
{
int fd;
int fill_head = 0;
struct file_stat st;
/* fill dir head */
if (stat_file(sb, path, &st)) {
fill_head = 1;
}
/* create file with dir attr on system */
fd = open_file(sb, path, FF_DIR | FF_CRATE | FF_RDWR);
if (fd == -1) {
return -1;
}
// fill head info if file not exist
if (fill_head) {
struct fs_dir_meta meta;
// printf("[INFO] touch dir, fill head info of %s\n", path);
memset(&meta, 0, sizeof(meta));
if (sync_dir_meta(&meta, fd, 1)) {
close_file(fd);
return -1;
}
}
close_file(fd);
return 0;
}
static int touch_normal_file(struct super_block *sb, const char *path, int mode)
{
int fd;
/* create file with dir attr on system */
fd = open_file(sb, path, FF_CRATE | mode);
if (fd == -1) {
return -1;
}
close_file(fd);
return 0;
}
static int rm_dir_file(struct super_block *sb, const char *path)
{
return delete_file(sb, path);
}
static int alloc_dir_bitmap(struct fs_dir_meta *meta)
{
int i, j;
unsigned int *bit;
int idx;
for (i = 0; i < DIR_BITMAP_SIZE; i++) {
bit = &meta->body.bitmap[i];
for (j = 0; j < DIR_BITMAP_ITEM_SIZE; j++) {
if (!(*bit & (1 << j))) {
*bit |= (1 << j);
idx = i * DIR_BITMAP_ITEM_SIZE + j;
if (idx >= meta->head.max_index) {
meta->head.max_index = idx + 1;
}
return idx;
}
}
}
return -1;
}
static int find_dir_bitmap_next(struct fs_dir_meta *meta, int start)
{
int i, j;
unsigned int *bit;
int idx;
i = start / DIR_BITMAP_ITEM_SIZE;
j = start % DIR_BITMAP_ITEM_SIZE;
for (; i < DIR_BITMAP_SIZE; i++) {
bit = &meta->body.bitmap[i];
for (; j < DIR_BITMAP_ITEM_SIZE; j++) {
if ((*bit & (1 << j)) != 0) {
idx = i * DIR_BITMAP_ITEM_SIZE + j;
return (idx < meta->head.max_index) ? idx : -1;
}
}
j = 0;
}
return -1;
}
static int find_dir_bitmap_by_name(struct fs_dir_meta *meta, char *name, int fd)
{
struct fs_dir_entry de;
int idx;
// seek to head
if (seek_file(fd, sizeof(struct fs_dir_meta), FP_SET) != sizeof(struct fs_dir_meta)) {
return -1;
}
idx = 0;
while (read_file(fd, &de, sizeof(struct fs_dir_entry)) == sizeof(struct fs_dir_entry)) {
if (!strcmp(de.fname.buf, name)) {
return idx;
}
idx++;
}
return -1;
}
static int free_dir_bitmap(struct fs_dir_meta *meta, int idx)
{
int i, j;
if (idx >= meta->head.max_index) {
return -1;
}
i = idx / DIR_BITMAP_ITEM_SIZE;
j = idx % DIR_BITMAP_ITEM_SIZE;
meta->body.bitmap[i] &= ~(1 << j);
return 0;
}
static int test_dir_bitmap(struct fs_dir_meta *meta, int idx)
{
int i, j;
i = idx / DIR_BITMAP_ITEM_SIZE;
j = idx % DIR_BITMAP_ITEM_SIZE;
return (meta->body.bitmap[i] & (1 << j)) != 0;
}
static int mod_dir_entry(struct fs_dir_meta *meta, int fd, struct fs_dir_entry *de, int bit)
{
long off;
// calc file offset
off = sizeof(struct fs_dir_meta) + bit * sizeof(struct fs_dir_entry);
if (seek_file(fd, off, FP_SET) != off) {
return -1;
}
if (write_file(fd, de, sizeof(struct fs_dir_entry)) != sizeof(struct fs_dir_entry)) {
return -1;
}
return 0;
}
static int add_dir_entry(struct fs_dir_meta *meta, int fd, struct fs_dir_entry *de)
{
int bit;
long off;
// find a free dir entry to store
bit = alloc_dir_bitmap(meta);
if (bit == -1) {
return -1;
}
if (mod_dir_entry(meta, fd, de, bit)) {
free_dir_bitmap(meta, bit);
return -1;
}
meta->head.count++;
if (sync_dir_meta(meta, fd, 0)) {
free_dir_bitmap(meta, bit);
return -1;
}
return 0;
}
static int del_dir_entry(struct fs_dir_meta *meta, int fd, const char *dir_name)
{
int bit;
long off;
struct fs_dir_entry de;
/* 遍历读取所有文件,匹配文件名,找对匹配的,并给出索引 */
bit = find_dir_bitmap_by_name(meta, dir_name, fd);
if (bit == -1) {
return -1;
}
/* 清理目录项 */
memset(&de, 0, sizeof(de));
if (mod_dir_entry(meta, fd, &de, bit)) {
return -1;
}
/* 释放位图 */
if (free_dir_bitmap(meta, bit)) {
return -1;
}
meta->head.count--;
if (sync_dir_meta(meta, fd, 0)) {
return -1;
}
return 0;
}
static int create_dir_entry(struct super_block *sb, const char *path, const char *current_path, const char *name, int type)
{
int fd;
struct fs_dir_entry de;
struct file_stat st;
struct fs_dir_meta meta;
if (!strlen(name)) {
return 0; // if no name, just return
}
/* write dir entry info into parent dir */
if (stat_file(sb, path, &st)) {
return -1;
}
/* fill dir entry */
strcpy(de.fname.buf, name);
de.num = st.num;
de.type = type;
/* write dir entry into parent dir */
fd = open_file(sb, current_path, FF_RDWR);
if (fd == -1) {
return -1;
}
memset(&meta, 0, sizeof(meta));
if (load_dir_meta(&meta, fd)) {
close_file(fd);
return -1;
}
if (add_dir_entry(&meta, fd, &de)) {
close_file(fd);
return -1;
}
close_file(fd);
return 0;
}
static int create_new_dir(struct super_block *sb, const char *path, const char *current_path, const char *name)
{
/* create dir entry file */
if (touch_dir_file(sb, path)) {
return -1;
}
if (create_dir_entry(sb, path, current_path, name, 1)) {
rm_dir_file(sb, path);
return -1;
}
return 0;
}
static int create_new_file(struct super_block *sb, const char *path, const char *current_path, const char *name, int mode)
{
/* create normal file */
if (touch_normal_file(sb, path, mode)) {
return -1;
}
if (create_dir_entry(sb, path, current_path, name, 0)) {
delete_file(sb, path);
return -1;
}
return 0;
}
static struct fs_dir_open *load_dir_info(struct super_block *sb, const char *path)
{
struct fs_dir_open *od;
od = alloc_open_dir();
if (!od) {
return NULL;
}
od->sb = sb;
od->fd = open_file(sb, path, FF_RDWR);
if (od->fd == -1) {
free_open_dir(od);
return NULL;
}
od->offset = sizeof(struct fs_dir_meta);
return od;
}
static int release_dir_info(struct fs_dir_open *od)
{
if (!od) {
return -1;
}
if (od->fd == -1) {
return -1;
}
od->offset = 0;
if (close_file(od->fd)) {
return -1;
}
od->fd = -1;
return free_open_dir(od);
}
#define DIR_WALK_CREATE_NOT_EXIST 0x01
#define DIR_WALK_LOAD_INFO 0x02
#define DIR_WALK_CREATE_FILE_NOT_EXIST 0x04
/**
* @brief 逐级解析并检查路径组件
* @param full_path 完整路径(如 "/abc/def/123")
* @return 0:所有组件都存在, -1:路径格式错误, -2:某个组件不存在
*/
int walk_dir(struct super_block *sb, const char *path, int flags, int mode, struct fs_dir_open **od)
{
char current_path[1024] = "/"; // 从根目录开始构建
char full_path[1024] = {0}; // 从根目录开始构建
char component[256] = {0};
const char* pos;
int level = 0;
char new_path[1024] = {0};
if (!strlen(path)) {
return -1;
}
// printf("开始解析路径: %s\n", path);
clean_path(path, full_path, sizeof(full_path));
// printf("clean path: %s\n", full_path);
pos = full_path;
// 初始化组件提取
pos = extract_next_component(full_path, component, sizeof(component));
// printf("pos: %p, component:%s\n", pos, component);
if (pos == NULL && component[0] != '\0') {
// 只有一个组件的情况
pos = extract_next_component(NULL, component, sizeof(component));
// printf("pos: %p, component:%s\n", pos, component);
}
/* 检查是根目录的情况 */
if (pos == NULL && full_path[0] == '/' && full_path[1] == '\0') {
component[0] = '\0';
}
do {
int result;
if (pos != NULL && pos[0] != '\0') {
// printf("解析下一个组件,相对路径:%s\n", pos);
}
// printf("第%d级 - 检查组件: '%s'\n", level, component);
// printf("当前路径: %s\n", current_path);
// 检查当前组件是否存在
result = check_component_exists(sb, current_path, component, new_path, sizeof(new_path));
switch (result) {
case 0:
// printf(" ✓ 组件存在且是目录: %s\n", new_path);
strcpy(current_path, new_path); // 更新当前路径
// 如果是最后一个组件,并且是创建目录,说明已经存在,不能再创建了,就返回报错。
if ((flags & DIR_WALK_CREATE_NOT_EXIST) && !(pos != NULL && pos[0] != '\0')) {
// printf("[ERROR] 创建目录,但已经存在,不允许创建: %s\n", new_path);
return -1;
}
if ((flags & DIR_WALK_CREATE_FILE_NOT_EXIST) && !(pos != NULL && pos[0] != '\0')) {
// printf("[ERROR] 创建文件,但已经存在,不允许创建: %s\n", new_path);
return -1;
}
break;
case -1:
// printf(" ✗ 组件不存在: %s, 全路径:%s\n", component, new_path);
// printf("在层级 %d 处停止: %s 不存在\n", level, component);
// CREATE DIR hear
if (flags & DIR_WALK_CREATE_NOT_EXIST) {
// printf("---> 打开目录文件:%s,写入组件信息:%s\n", current_path, component);
if (create_new_dir(sb, new_path, current_path, component)) {
return -1;
}
// printf(" ✓ 组件不存在但创建了新目录: %s\n", new_path);
strcpy(current_path, new_path); // 更新当前路径
} else if ((flags & DIR_WALK_CREATE_FILE_NOT_EXIST) && !(pos != NULL && pos[0] != '\0')) {
// printf("---> 文件不存在创建新文件:%s\n", new_path);
struct file *f;
if (create_new_file(sb, new_path, current_path, component, mode)) {
return -1;
}
} else {
return -2;
}
break;
case -2:
// printf(" ✗ 路径存在但不是目录: %s\n", new_path);
// printf("在层级 %d 处停止: %s 不是目录\n", level, component);
return -2;
}
level++;
// 获取下一个组件
if (pos != NULL && pos[0] != '\0') {
// printf("解析下一个组件,相对路径:%s\n", pos);
pos = extract_next_component(pos, component, sizeof(component));
} else {
// 加载目录信息
if ((DIR_WALK_LOAD_INFO & flags) && od != NULL) {
*od = load_dir_info(sb, new_path);
if (*od == NULL) {
return -1;
}
}
component[0] = '\0'; // 结束循环
}
// printf("---\n");
} while (component[0] != '\0');
// printf("✓ 路径解析完成: 所有 %d 个组件都存在\n", level);
return 0;
}
int create_dir(struct super_block *sb, const char *path)
{
return walk_dir(sb, path, DIR_WALK_CREATE_NOT_EXIST, 0, NULL);
}
int new_file(struct super_block *sb, const char *path, int mode)
{
return walk_dir(sb, path, DIR_WALK_CREATE_FILE_NOT_EXIST, mode, NULL);
}
static int split_dir_path(const char *path, char *parent_name, char *dir_name)
{
char *p;
int len;
p = strrchr(path, '/');
if (p == NULL) {
return -1;
}
len = p - path;
if (!len) {
strcpy(parent_name, "/");
} else {
strncpy(parent_name, path, len);
}
strcpy(dir_name, (const char *)(p + 1));
return 0;
}
static int remove_parent_dir_entry(struct super_block *sb, const char *path)
{
int fd;
struct fs_dir_meta meta;
char parent_dir[FILE_NAME_LEN] = {0};
char dir_name[FILE_NAME_LEN] = {0};
if (split_dir_path(path, parent_dir, dir_name)) {
return -1;
}
fd = open_file(sb, parent_dir, FF_RDWR);
if (fd == -1) {
return -1;
}
memset(&meta, 0, sizeof(meta));
if (load_dir_meta(&meta, fd)) {
close_file(fd);
return -1;
}
if (del_dir_entry(&meta, fd, dir_name)) {
close_file(fd);
return -1;
}
close_file(fd);
return 0;
}
static int check_dir_empty(struct super_block *sb, const char *path)
{
int fd;
struct fs_dir_meta meta;
char parent_dir[FILE_NAME_LEN] = {0};
char dir_name[FILE_NAME_LEN] = {0};
fd = open_file(sb, path, FF_RDWR);
if (fd == -1) {
return -1;
}
memset(&meta, 0, sizeof(meta));
if (load_dir_meta(&meta, fd)) {
close_file(fd);
return -1;
}
close_file(fd);
return !meta.head.count;
}
int delete_dir(struct super_block *sb, const char *path)
{
// check dir empty
struct file_stat stat_buf;
int fd;
if (stat_file(sb, path, &stat_buf) != 0) {
return -1;
}
if (!(stat_buf.mode & FF_DIR)) {
return -1;
}
// can't delete root dir
if (!strcmp(path, "/")) {
printf("[ERROR] can not delete root dir!\n");
return -1;
}
/* check dir is empty */
if (!check_dir_empty(sb, path)) {
printf("[ERROR] dir %s not empty!\n", path);
return -1;
}
printf("[INFO] ready delete dir %s.\n", path);
/* remove dir entry from parent dir */
if (remove_parent_dir_entry(sb, path)) {
return -1;
}
return delete_file(sb, path);
}
int del_file(struct super_block *sb, const char *path)
{
// check dir empty
struct file_stat stat_buf;
int fd;
if (stat_file(sb, path, &stat_buf) != 0) {
return -1;
}
if ((stat_buf.mode & FF_DIR)) {
return -1;
}
// can't delete root dir
if (!strcmp(path, "/")) {
printf("[ERROR] can not delete root dir!\n");
return -1;
}
printf("[ERROR] ready delete file %s.\n", path);
/* remove dir entry from parent dir */
if (remove_parent_dir_entry(sb, path)) {
return -1;
}
return delete_file(sb, path);
}
int open_dir(struct super_block *sb, const char *path)
{
struct fs_dir_open *od = NULL;
// 先遍历目录,确认文件存在。
if (walk_dir(sb, path, DIR_WALK_LOAD_INFO, 0, &od)) {
return -1;
}
return odir_idx(od);
}
int close_dir(int dir)
{
struct fs_dir_open *od;
od = idx_odir(dir);
return release_dir_info(od);
}
int read_dir(int dir, struct fs_dir_entry *de)
{
struct fs_dir_open *od;
struct fs_dir_meta meta;
int idx;
od = idx_odir(dir);
long len;
struct fs_dir_entry tmp;
/* get head info */
memset(&meta, 0, sizeof(meta));
if (load_dir_meta(&meta, od->fd)) {
return -1;
}
if (!meta.head.count) {
return -1;
}
/* check offset has entry */
idx = (od->offset - sizeof(struct fs_dir_meta)) / sizeof(struct fs_dir_entry);
if (!test_dir_bitmap(&meta, idx)) {
// find next bitmap
idx = find_dir_bitmap_next(&meta, idx);
if (idx == -1) {
return -1; // no direnty
}
od->offset = sizeof(struct fs_dir_meta) + idx * sizeof(struct fs_dir_entry);
}
seek_file(od->fd, od->offset, FP_SET);
len = read_file(od->fd, &tmp, sizeof(tmp));
if (len <= 0) {
return -1;
}
*de = tmp;
od->offset += sizeof(tmp);
return 0;
}
int seek_dir(int dir, int pos)
{
struct fs_dir_open *od;
od = idx_odir(dir);
long offset;
offset = sizeof(struct fs_dir_meta) + pos * sizeof(struct fs_dir_entry);
if (offset < 0) {
offset = 0;
}
od->offset = offset;
return 0;
}
int list_dir(struct super_block *sb, const char *path)
{
int dir = open_dir(sb, path);
if (dir < 0)
{
printf("open dir %s failed!\n", path);
return -1;
}
struct fs_dir_entry de;
printf("PATH: %s\n", path);
printf("NUM TYPE NAME\n", de.fname.buf, de.type, de.num);
while (read_dir(dir, &de) != -1)
{
printf("%8d %8d %s\n", de.num, de.type, de.fname.buf);
}
close_dir(dir);
return 0;
}
/* ==================== 新增目录功能 ==================== */
int mkdir_p(struct super_block *sb, const char *path)
{
if (!sb || !path) {
return DIR_INVALID_PATH;
}
char current_path[1024] = "/";
char component[256] = {0};
char temp_path[1024] = {0};
const char *pos = path;
clean_path(path, temp_path, sizeof(temp_path));
pos = extract_next_component(temp_path, component, sizeof(component));
do {
if (strlen(component) > 0) {
if (strcmp(current_path, "/") == 0) {
snprintf(temp_path, sizeof(temp_path), "/%s", component);
} else {
snprintf(temp_path, sizeof(temp_path), "%s/%s", current_path, component);
}
/* 检查目录是否存在 */
struct file_stat stat;
if (stat_file(sb, temp_path, &stat) != 0) {
/* 目录不存在,创建它 */
if (create_dir(sb, temp_path) != 0) {
printf("[MKDIR_P] Failed to create directory: %s\n", temp_path);
return DIR_ERROR;
}
}
strcpy(current_path, temp_path);
}
pos = extract_next_component(NULL, component, sizeof(component));
} while (pos != NULL);
printf("[MKDIR_P] Created directory tree: %s\n", path);
return DIR_SUCCESS;
}
int rmdir_r(struct super_block *sb, const char *path)
{
if (!sb || !path) {
return DIR_INVALID_PATH;
}
/* 不能删除根目录 */
if (strcmp(path, "/") == 0) {
printf("[RMDIR_R] Cannot delete root directory\n");
return DIR_ERROR;
}
/* 检查路径是否存在且为目录 */
struct file_stat stat;
if (stat_file(sb, path, &stat) != 0) {
printf("[RMDIR_R] Directory not found: %s\n", path);
return DIR_NOT_FOUND;
}
if (!(stat.mode & FF_DIR)) {
printf("[RMDIR_R] Path is not a directory: %s\n", path);
return DIR_ERROR;
}
/* 递归删除目录内容 */
int dir = open_dir(sb, path);
if (dir < 0) {