-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.c
More file actions
2028 lines (1957 loc) · 55.4 KB
/
Copy pathFileSystem.c
File metadata and controls
2028 lines (1957 loc) · 55.4 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 <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define NONE "\033[m"
#define RED "\033[0;32;31m"
#define LIGHT_RED "\033[1;31m"
#define GREEN "\033[0;32;32m"
#define LIGHT_GREEN "\033[1;32m"
#define BLUE "\033[0;32;34m"
#define LIGHT_BLUE "\033[1;34m"
#define DARY_GRAY "\033[1;30m"
#define CYAN "\033[0;36m"
#define LIGHT_CYAN "\033[1;36m"
#define PURPLE "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN "\033[0;33m"
#define YELLOW "\033[1;33m"
#define LIGHT_GRAY "\033[0;37m"
#define WHITE "\033[1;37m"
// FAT16
#define BLOCKSIZE 1024
#define SIZE 1024 * 1000
#define END 65535
#define FREE 0
#define ROOTBLOCKNUM 2
#define MAXOPENFILE 10
#define ITSELF "."
#define PARENT ".."
#define DIR 0
#define NORMALFILE 1
#define FILENAME "/root/fileSystem/myVfile.VF"
/*
*/
typedef struct FCB
{
char filename[8]; // 文件名
char exname[4]; // 拓展名
unsigned char attribute; // 属性 0为目录,1为普通文件
char time[9]; // 创建时间
char date[11]; // 创建日期
unsigned short first; // 起始盘块号
unsigned long length; // 文件长度(字节数)
char free; // 目录项:0为空,1为已分配
} fcb;
typedef struct FAT
{
unsigned short id;
} fat;
typedef struct USEROPEN
{
char filename[8];
char exname[4];
char time[9]; // 创建时间
char date[11]; // 创建日期
unsigned short first; // 起始盘块号
unsigned long length; // 文件长度
char dir[80]; // 文件路径
int count; // 读写指针的位置
char fcbstate; // 文件的FCB是否被修改,是则为1,否为0
int topenfile; // 打开表项是否为空。0为空,否为已占用
int fd; // 文件描述符
struct USEROPEN *next;
struct USEROPEN *header; // 永远指向头部
} useropen;
typedef struct BLOCK0
{
char infomation[200]; // 存储描述信息,磁盘块大小,磁盘块数量等
unsigned short root; // 根目录的起始盘块号
unsigned char *startblock; // 虚拟磁盘上数据区开始的位置
} block0;
unsigned char *myvhard; // 指向虚拟磁盘的起始地址
int curdir; // 当前文件描述符
char currentdir[80] = ""; // 当前目录
unsigned char *startp; // 数据区开始的位置
unsigned char *currentBlock; // 当前处于的盘块地址
unsigned char *startFAT; // FAT表开始地址
useropen *openFileList; // 打开的文件单项链表 采用先入先出
int split(char *pathname, char result[][8], char delimiter); // 根据符号切割字符串
int spliteDot(char *filename, char result[2][8]); // 按点切割获取文件名和后缀名
int splitSpaceKey(char *str, char result[3][80]); // 按空格切割
int isCurrentDir(char *str); // 路径是否是当前路径下
int getFreeBlock(struct FCB *F); // 获取到空闲的盘块的块号
int my_getline(char *line, int max_size); // 读取一行输入
void getFileInfo(struct FCB *F, char *name, char *type, char *space); // 文件信息拼接
void initFCB(struct FCB *F, int first, struct FCB *parent); // 初始化 . 和 .. FCB
void setFileTime(struct FCB *F, struct FCB *obj); // 初始化FCB时间或者是复制FCB时间
void recombination(char *pathname, char result[][8], int length, char *newPath); // 从文件路径中解析出路径
void initNormalFCB(struct FCB *F, int first, char *name, char *exname); // 初始化普通文件的FCB
void initUserOpenList(); //初始化用户打开表
void startsys();
void my_format();
void ls();
int cd(char *str, int mode);
int mkdir(char *str);
int rmdir(char *str, int mode);
void rmAll(struct FCB *F);
int openFile(char *str);
int createFile(char *str);
int removeFile(char *str);
int writeFile(char *str, int mode);
int readFile(char *str, int len);
// 返回值是切片个数 如果返回值是0,则出错
int split(char *str, char result[][8], char delimiter)
{
if (strlen(str) == 0)
{
return 0;
}
char *filename = NULL, *ptr = NULL;
filename = strtok(str, "/");
if (strlen(filename) > 7)
{
return 0;
}
int length = 0;
while (filename != NULL)
{
strcpy(result[length], filename);
length++;
if (strlen(filename) > 7)
{
return 0;
}
filename = strtok(NULL, "/");
}
return length;
}
void recombination(char *pathname, char result[][8], int length, char *newPath)
{ // 从文件路径中解析出路径
char a[80] = "";
if (pathname[0] == '/')
{
for (int i = 0; i < length - 1; i++)
{
strcat(a, "/");
strcat(a, result[i]);
}
strcpy(newPath, a);
return;
}
// 跑到这里说明不是以/开头的
strcat(a, result[0]);
for (int i = 1; i < length - 1; i++)
{
strcat(a, "/");
strcat(a, result[i]);
}
strcpy(newPath, a);
}
void getFileInfo(struct FCB *F, char *name, char *type, char *space)
{
char unit[8] = "";
float length = F->length;
if (length > 1024)
{
length = length / 1024;
strcat(unit, "KB");
}
else
{
strcat(unit, "B");
}
sprintf(space, "%.2lf", length);
strcat(space, unit);
// printf("%s\n",space);
if (F->attribute == 0)
{
strcat(type, "DIR");
}
else
{
strcat(type, "FILE");
}
if (strlen(F->exname) != 0)
{
strcat(name, F->filename);
strcat(name, ".");
strcat(name, F->exname);
}
else
{
strcat(name, F->filename);
}
// printf("dasdas");
// printf("%s <%s> %s\n", name, type, space);
}
int getFreeBlock(struct FCB *F)
{
if (F != NULL)
{
fat *p = (fat *)startFAT;
// printf("myhard2:%d\n",myvhard);
// printf("start:%d\n",p);
for (int i = 0; i < 2 * BLOCKSIZE / sizeof(fat); i++)
{
// printf("@@%d\n",i);
if (p->id == FREE)
{
// printf("free:%d\n",p);
F->first = i;
p->id = END;
return 1;
}
p++;
}
// 如果走到这,说明没空间分配了
return 0;
}
else
{
fat *p = (fat *)startFAT;
for (int i = 0; i < 2 * BLOCKSIZE / sizeof(fat); i++)
{
if (p->id == FREE)
{
// printf("\n$$%d$$\n",i);
return i;
}
p++;
}
return 0;
}
}
int isCurrentDir(char *str)
{
if (strlen(str) == 0)
{
return 1;
}
char result[10][8];
int num = split(str, result, '/');
if (num == 0)
{
printf(RED "文件路径有误\n" NONE);
return 0;
}
if (num > 2)
{
printf(RED "只允许对当前目录下子文件操作\n" NONE);
return 0;
}
if (num == 2 && (str[0] != '.' || str[1] != '/'))
{
printf(RED "只允许对当前目录下子文件操\n" NONE);
return 0;
}
if (num == 1 && str[0] == '/')
{
printf(RED "只允许对当前目录下子文件操作\n" NONE);
return 0;
}
return 1;
}
void initFCB(struct FCB *F, int first, struct FCB *parent)
{ // 如果parent给了,那就是设置父元素的FCB
// 这里的parent我认为是当前所在目录的那个 . 的first
F->free = 1;
F->length = 0;
F->attribute = DIR;
memset(F->exname, 0, sizeof(F->exname));
strcat(F->exname, "");
if (parent == NULL)
{
strcat(F->filename, ITSELF);
F->first = first;
setFileTime(F, NULL);
return;
}
strcat(F->filename, PARENT);
F->first = parent->first;
setFileTime(F, parent);
}
void setFileTime(struct FCB *F, struct FCB *obj)
{ // 第二个参数如果不是null就复制第二个参数的时间
memset(F->date, 0, sizeof(F->date));
memset(F->time, 0, sizeof(F->time));
if (obj != NULL)
{
strcpy(F->date, obj->date);
strcpy(F->time, obj->time);
return;
}
time_t *now;
now = (time_t *)malloc(sizeof(time_t));
time(now);
struct tm *nowtime = localtime(now);
char hour[3] = "";
sprintf(hour, "%d", nowtime->tm_hour);
if (nowtime->tm_hour < 10)
{
strcat(F->time, "0");
}
strcat(F->time, hour);
strcat(F->time, ":");
char min[3] = "";
sprintf(min, "%d", nowtime->tm_min);
if (nowtime->tm_min < 10)
{
strcat(F->time, "0");
}
strcat(F->time, min);
strcat(F->time, ":");
char sec[3] = "";
sprintf(sec, "%d", nowtime->tm_sec);
if (nowtime->tm_sec < 10)
{
strcat(F->time, "0");
}
strcat(F->time, sec);
char year[5] = "";
sprintf(year, "%lu", nowtime->tm_year + 1900);
strcat(F->date, year);
strcat(F->date, "-");
char month[3] = "";
sprintf(month, "%d", nowtime->tm_mon + 1);
if (nowtime->tm_mon + 1 < 10)
{
strcat(F->date, "0");
}
strcat(F->date, month);
strcat(F->date, "-");
char day[3] = "";
sprintf(day, "%d", nowtime->tm_mday);
if (nowtime->tm_mday < 10)
{
strcat(F->date, "0");
}
strcat(F->date, day);
free(now);
}
void setUserOpen(struct FCB *F, struct USEROPEN *open)
{ // 将fcb的内容复制给useropen
memset(open->filename, 0, sizeof(open->filename));
memset(open->exname, 0, sizeof(open->exname));
memset(open->time, 0, sizeof(open->time));
memset(open->date, 0, sizeof(open->date));
strcpy(open->filename, F->filename);
strcpy(open->exname, F->exname);
strcpy(open->time, F->time);
strcpy(open->date, F->date);
open->first = F->first;
open->length = F->length;
open->count = 0;
open->fcbstate = 0;
open->topenfile = 1;
}
void initNormalFCB(struct FCB *F, int first, char *name, char *exname)
{
F->free = 1;
F->length = 0;
F->attribute = NORMALFILE;
// printf("%s %s",name,exname);
memset(F->exname, 0, sizeof(F->exname));
strcpy(F->exname, exname);
memset(F->filename, 0, sizeof(F->filename));
strcpy(F->filename, name);
F->first = first;
fat *f = (fat *)startFAT;
f += first;
f->id = END;
setFileTime(F, NULL);
}
int spliteDot(char *filename, char result[2][8])
{
// int index=0;
// // printf("%d\n",strlen(filename));
// while(filename[index]!='.' && filename[index]!='\0'){
// index++;
// }
// // printf("%d\n",index);
// if(index==strlen(filename)){
// return -1;
// }
int i = 0;
for (; i < strlen(filename); i++)
{
if (filename[i] == '.')
{
break;
}
result[0][i] = filename[i];
}
result[0][i] = '\0';
i++;
int j = 0;
for (; i < strlen(filename); i++, j++)
{
// printf("%c ",filename[i]);
result[1][j] = filename[i];
}
result[1][j] = '\0';
if (strlen(result[1]) > 3)
{
return 0;
}
if (strlen(result[1]) == 0)
{
return 1;
}
return 2;
}
int splitSpaceKey(char *str, char result[3][80]) // 按空格切割
{
char *filename = NULL, *ptr = NULL;
filename = strtok(str, " ");
int length = 0;
while (filename != NULL)
{
strcpy(result[length], filename);
length++;
if (length > 3)
{
return 0;
}
filename = strtok(NULL, " ");
}
return length;
}
int my_getline(char *line, int max_size)
{
int c;
int len = 0;
while ((c = getchar()) != EOF && len < max_size)
{
line[len++] = c;
if ('\n' == c)
break;
}
line[len - 1] = '\0';
return len;
}
void initUserOpenList()
{
// 初始化链表
openFileList = (useropen *)malloc(sizeof(useropen));
openFileList->topenfile = 0;
openFileList->header = openFileList;
useropen *header = openFileList, *s = openFileList;
header->fd = 0;
int x = 1;
for (int i = 1; i < MAXOPENFILE; i++)
{
useropen *p = (useropen *)malloc(sizeof(useropen));
p->header = header;
p->topenfile = 0;
p->fd = x;
x++;
s->next = p;
s = p;
}
s->next = NULL;
}
// 上面都是辅助函数,下面是操作函数
void startsys()
{
myvhard = (unsigned char *)malloc(SIZE);
memset(myvhard, 0, SIZE);
FILE *fp;
if ((fp = fopen(FILENAME, "r")) != NULL)
{
fread(myvhard, SIZE, 1, fp);
fclose(fp);
}
else
{
printf("未发现保存的数据,将重新格式化\n");
// 将磁盘进行格式化
my_format();
return;
}
startFAT = myvhard + BLOCKSIZE;
startp = myvhard + (1 + 2 * ROOTBLOCKNUM) * BLOCKSIZE;
currentBlock = startp;
initUserOpenList();
strcpy(currentdir, "/root");
printf(YELLOW "文件系统初始化完毕\n" NONE);
}
void my_format()
{
// printf("myvhard:%d\n",myvhard);
unsigned char *p = myvhard;
block0 *b0 = (block0 *)(p);
strcat(b0->infomation, "磁盘块共1000个,每个1024个字节\n");
p += BLOCKSIZE; // 跑到fat表初始地址
// printf("p:%d\n",p);
fat *fat1 = (fat *)(p);
fat *fat2 = (fat *)(p + ROOTBLOCKNUM * BLOCKSIZE);
fat1->id = 1;
// printf("fat1:%d\n",fat1);
fat2->id = 1;
fat1++;
fat2++;
fat1->id = END;
fat2->id = END;
fat1++;
fat2++;
for (int i = ROOTBLOCKNUM; i < 2 * BLOCKSIZE / sizeof(fat) - 1; i++)
{
fat1->id = FREE;
fat2->id = FREE;
fat1++;
fat2++;
}
fat1->id = END;
fat2->id = END;
fcb *root = (fcb *)(p + 4 * BLOCKSIZE);
initFCB(root, 0, NULL);
fcb *q = root;
q++;
for (int i = 1; i < 2 * BLOCKSIZE / sizeof(fcb); i++)
{
q->free = FREE;
q++;
}
b0->startblock = (p + 4 * BLOCKSIZE);
startp = (p + 4 * BLOCKSIZE);
currentBlock = (p + 4 * BLOCKSIZE);
startFAT = p;
strcat(currentdir, "/root");
initUserOpenList();
// printf("%d\n",currentBlock);
printf(YELLOW "bi~bi~bi~bi~······初始化完毕······\n" NONE);
// printf("/root>");
};
// 允许跨越展示
void ls(char *str)
{
char result[10][8];
int length;
length = split(str, result, '/');
if (length == 0 && strlen(str) != 0 && str[0] != '.')
{
printf("路径不存在");
return;
}
if (strlen(str) != 0)
{
// printf("%d,%d",length,strlen(str) );
// 说明现在不是直接展示当前目录的文件
// 思路是跑到目标目录下,然后输出,然后再跑回来
char saveDir[80] = "";
strcpy(saveDir, currentdir);
// printf("@%s\n",saveDir);
// printf("???%s\n",currentdir);
int flag = cd(str, 0);
if (flag == 0)
{
printf("请输入正确目录\n");
// printf("%s>",currentdir);
return;
}
fcb *p = (fcb *)currentBlock;
printf("目录 文件类型 文件大小 创建时间 创建日期\n");
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
if (p->free == 1)
{
char name[8] = "", type[5] = "", space[12] = "";
getFileInfo(p, name, type, space);
if (p->attribute == 1)
{
printf("%-8s <%s> %-12s%s %s\n", name, type, space, p->time, p->date);
}
else
printf("%-8s <%s> %-12s%s %s\n", name, type, space, p->time, p->date);
}
p++;
}
// printf("save:%s\n", saveDir);
if (cd(saveDir, 0) == 0)
{
printf("出错了\n");
}
// printf("@@%s\n",currentdir);
return;
}
if (strlen(str) == 0)
{
// printf("走到这了吗?");
// 说明要展示当前目录下的文件
fcb *p = (fcb *)(char *)currentBlock;
printf("目录 文件类型 文件大小 创建时间 创建日期\n");
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
// printf("%d\n",p);
if (p->free == 1)
{
char name[8] = "", type[5] = "", space[12] = "";
getFileInfo(p, name, type, space);
// printf("%d\n",i);
if (p->attribute == 1)
{
printf("%-8s <%s> %-12s%s %s\n", name, type, space, p->time, p->date);
}
else
printf("%-8s <%s> %-12s%s %s\n", name, type, space, p->time, p->date);
}
p++;
}
}
}
// 允许跨越当前目录rmdir
int cd(char *str, int mode) // mode=1说明开启路径打印,mode=0说明不开启路径打印 返回值为0说明错误
{
if (strlen(str) == 0)
{
return 1;
}
char backupDir[80] = "";
strcpy(backupDir, currentdir);
// printf("@@%s\n",currentdir);
unsigned char *backupBlock = currentBlock; // 备份一下当前的状态
char result[10][8];
// printf("xxx%s\n",str);
int length = split(str, result, '/');
// printf("%s %s\n",result[1],result[2]);
unsigned char *q = startp; // 拿到数据区开始地址,也就是root的开始地址
// 没有 / 或者 有/ 但不是以/开头的
if ((length == 1 || length > 1) && str[0] != '/')
{
fcb *p = (fcb *)currentBlock;
int flag = 0;
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{ // 如果循环内找到需要的fcb,那么会return出去
if (p->free == 0)
continue;
if (strcmp(p->filename, result[0]) == 0 && p->attribute == 0)
{
// printf("%s\n",result[0]);
q = BLOCKSIZE * (p->first) + startp; // 找到目标盘块起始地址
currentBlock = q; // 将当前盘块地址切换为目标盘块起始地址
if (strcmp(result[0], "..") == 0)
{
char a[10][8];
int b = split(backupDir, a, '/');
memset(currentdir, 0, 80);
for (int x = 0; x < b - 1; x++)
{
strcat(currentdir, "/");
strcat(currentdir, a[x]);
}
p = (fcb *)currentBlock;
}
else if (strcmp(result[0], ".") != 0)
{
strcat(currentdir, "/");
strcat(currentdir, result[0]); // 路径更新
}
if (length == 1 && mode == 1)
{
// printf("%s>", currentdir);
return 1;
}
flag = 1;
break;
}
p++;
}
if (flag == 0)
{
// 说明没找到
currentBlock = backupBlock;
memset(currentdir, 0, sizeof(currentdir));
strcat(currentdir, backupDir);
return 0;
}
// 如果跑到这 length至少为2
for (int i = 1; i < length; i++)
{
// printf("%s\n",result[1]);
flag = 0;
for (int j = 0; j < BLOCKSIZE / sizeof(fcb); j++)
{
if (p->free == 0)
continue;
if (strcmp(p->filename, result[i]) == 0 && p->attribute == 0)
{
q = BLOCKSIZE * (p->first) + startp;
currentBlock = q;
strcat(currentdir, "/");
strcat(currentdir, result[i]);
flag = 1;
break;
}
p++;
}
// 如果跑到这里flag还为0,说明上面没找到
if (flag == 0)
{
currentBlock = backupBlock;
memset(currentdir, 0, sizeof(currentdir));
strcat(currentdir, backupDir);
return 0;
}
}
// 跑到这说明都找到了
if (mode == 1)
{
// printf("%s>", currentdir);
}
return 1;
}
// 如果执行到这,说明给的str里面以 / 开头 ,就说明路径可能需要多次跳转
if (str[0] == '/' && strcmp(result[0], "root") == 0)
{
// 以 / 开头 并且第一个切出来root 说明要根据根目录跳转
memset(currentdir, 0, sizeof(currentdir));
strcat(currentdir, "/root");
currentBlock = q;
if (length == 1)
{ // 说明只有一个/root 或/root/
currentBlock = q;
if (mode == 1)
{
// printf("/root>");
}
return 1;
}
// 跑到这里说明不止有/root
fcb *p = (fcb *)q;
for (int i = 0; i < 2 * BLOCKSIZE / sizeof(fcb); i++)
{
if (p->free == 0)
continue;
if (strcmp(p->filename, result[1]) == 0 && p->attribute == 0)
{
q = BLOCKSIZE * (p->first) + startp; // 找到目标盘块起始地址
currentBlock = q; // 将当前盘块地址切换为目标盘块起始地址
strcat(currentdir, "/");
strcat(currentdir, result[1]);
if (length == 2)
{ // 长度等于2 说明只有一个 / 只需要在root查找一次就可以了
// strcat(currentdir, "/");
// strcat(currentdir, result[1]);
// printf("%s>", currentdir);
return 1;
}
// 如果跑到这,说明长度不止为2,那么需要循环了
fcb *x = (fcb *)currentBlock;
for (int j = 2; j < length; j++)
{
int flag = 0;
for (int k = 0; k < BLOCKSIZE / sizeof(fcb); k++)
{
if (x->free == 0)
continue;
if (strcmp(x->filename, result[j]) == 0 && x->attribute == 0)
{
q = BLOCKSIZE * (x->first) + startp;
currentBlock = q;
strcat(currentdir, "/");
strcat(currentdir, result[j]);
flag = 1;
break;
}
x++;
}
if (flag == 0)
{ // 说明有没找到的
currentBlock = backupBlock;
memset(currentdir, 0, sizeof(currentdir));
strcat(currentdir, backupDir);
return 0;
}
}
// 如果执行到这,说明都找到了
if (mode == 1)
{
// printf("%s>", currentdir);
}
return 1;
}
p++;
}
// 如果跑到这,说明没找到
memset(currentdir, 0, sizeof(currentdir));
strcat(currentdir, backupDir);
currentBlock = backupBlock;
return 0;
}
if (str[0] == '/' && strcmp(result[0], "root") != 0)
{
// 说明以/开头但是后面跟的不是root 直接错误
return 0;
}
return 0;
}
// 仅支持当前目录下创建
int mkdir(char *str)
{
char result[10][8];
if (isCurrentDir(str) == 0)
{
return 0;
}
if (spliteDot(str, result) != 1)
{
printf("文件夹名称不允许包含“.” \n");
return 0;
}
fcb *p = (fcb *)(char *)currentBlock;
fcb *parent = p;
fcb *x = p;
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
if (x->attribute == 0 && strcmp(x->filename, str) == 0 && x->free == 1)
{
printf("文件夹已存在\n");
return 0;
}
x++;
}
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
if (p->free == 0)
{
p->free = 1;
memset(p->filename, 0, 8);
strcpy(p->filename, str);
p->attribute = 0;
p->length = 0;
setFileTime(p, NULL);
if (getFreeBlock(p) == 0)
{
printf("抱歉,磁盘空间不足,请扩容\n");
return 0;
}
// printf("%d\n",p->first);
fcb *q = (fcb *)(startp + BLOCKSIZE * (p->first));
initFCB(q, p->first, NULL);
initFCB(++q, 0, parent);
return 1;
}
p++;
}
// 如果跑到这里了说明该文件目录下没fcb可以分配
// 仅支持当前目录下创建
return 0;
}
// 支持当前目录递归删除,但仅仅是当前目录
int rmdir(char *str, int mode)
{ // mode=1为递归删除,mode=0为普通删除 返回值为0说明失败
if (str == NULL || str[0] == '\0')
{
printf("请输入要删除的文件夹名称\n");
return 0;
}
if (isCurrentDir(str) == 0)
{
return 0;
}
fcb *p = (fcb *)(char *)currentBlock;
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
if (p->attribute == 0 && strcmp(p->filename, str) == 0 && p->free == 1)
{
fcb *q = (fcb *)(startp + BLOCKSIZE * (p->first));
if (mode == 0)
{
for (int j = 0; j < BLOCKSIZE / sizeof(fcb); j++)
{
if (q->free == 1 && strcmp(q->filename, ".") != 0 && strcmp(q->filename, "..") != 0)
{
printf("%s\n", q->filename);
printf("%s下不为空,请清空后再操作\n", p->filename);
return 0;
}
q++;
}
// 如果跑到这里,说明对应文件夹目录下是空的,接下来就置为空标志符
for (int j = 0; j < BLOCKSIZE / sizeof(fcb); j++)
{
if (q->free == 1)
{
q->free = 0;
}
q++;
}
p->free = 0;
// unsigned char* x=startp+BLOCKSIZE*p->first;
// memset(x,0,BLOCKSIZE);
}
else if (mode == 1)
{
p->free = 0;
rmAll(q);
}
break;
}
// p->free = 0;
p++;
}
if (mode == 0)
{
printf("没找到 %s \n", str);
return 0;
}
// mode=1为递归删除,mode=0为普通删除 返回值为0说明失败
return 1;
}
void rmAll(struct FCB *F)
{ // 递归删除 有bug,不会删除打开文件的打开列表
if (F == NULL)
{
return;
}
for (int i = 0; i < BLOCKSIZE / sizeof(fcb); i++)
{
if (F->free == 1 && F->attribute == 0 && strcmp(F->filename, ".") != 0 && strcmp(F->filename, "..") != 0)
{
F->free == 0;
fcb *p = (fcb *)(startp + BLOCKSIZE * (F->first));
rmAll(p);
}
else if (F->free == 1 && F->attribute == 1)
{
F->free = 0;
fat *start = (fat *)startFAT, *p = (fat *)startFAT, *next = (fat *)startFAT;
p += F->first;
next = p;
unsigned char *y = startp + BLOCKSIZE * F->first;
memset(y, 0, BLOCKSIZE);
while (p->id != END)
{
next = start + p->id;
y = startp + BLOCKSIZE * p->id;
memset(y, 0, BLOCKSIZE);
p->id = FREE;
p = next;
}
p->id = FREE;
}
else if (F->free == 1 && F->attribute == 0 && (strcmp(F->filename, ".") == 0 || strcmp(F->filename, "..") == 0))
{
F->free = 0;
}
F->free = 0;
F++;
}
return;
}
// 允许跨越目录打开文件
int openFile(char *str)
{ // 返回值0说明发生了错误
char result[10][8];
int length = split(str, result, '/');
if (length == 0)
{
printf("@路径错误\n");