-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1172 lines (1076 loc) · 32.9 KB
/
main.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
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 <stdlib.h>
#include <string.h>
#include <malloc.h>
//定义的常量
#define SIZE 1024000 //虚拟磁盘空间大小
#define END 65535 //FAT中文件结束标志
#define FREE 0 //FAT中盘块空闲标志
#define ROOTBLOCKNUM 2 //根目录初始所占的盘块总数
#define MAXOPENFILE 10 //最多同时打开文件个数
#define DIRLEN 80 //文件路径长度
#define max(X, Y) (((X) > (Y)) ? (X) : (Y))
#define min(X, Y) (((X) < (Y)) ? (X) : (Y))
//定义文件控制块FCB
typedef struct FCB {
char filename[DIRLEN]; //文件名
char exname[3]; //文件扩展名
unsigned char attribute; //文件属性字段:0:目录文件,1:数据文件。
unsigned short time; //文件创建时间
unsigned short data; //文件创建日期
unsigned short first; //文件起始盘块号
unsigned long length; //文件长度
char free; //目录项是否为空,0:空,1:已分配
}fcb;
//定义文件分配表FAT
typedef struct FAT {
unsigned short id; //下一块的ID
}fat;
//定义用户打开文件表USEROPEN
typedef struct USEROPEN {
fcb open_fcb; //文件的FCB中的内容
char dir[DIRLEN]; //打开文件所在的路径,方便快速检查出指定文件是否已经打开
int count; //读写指针在文件中的位置
char fcbstate; //是否修改了文件的FCB的内容,如果修改了置为 1,否则为0
char topenfile; //表示该用户打开表项是否为空,若值为0,表示为空,否则表示已被某打开文件占据
int dirno; //相应打开文件的目录项在父目录文件中的盘块号
int diroff; //相应打开文件的目录项在父目录文件的dirno盘块中的起始位置
}useropen;
//定义引导块BLOCK0
typedef struct BLOCK0 {
int blocksize;
int blocknum;
unsigned short root; //根目录文件的起始盘块号
unsigned char *startblock; //虚拟磁盘上数据区开始位置
} block0;
//-------------------------------------函数声明--------------------------------------------------------
//初始化文件控制块FCB
void fcb_init(fcb *new_fcb, const char* filename, unsigned short first, unsigned char attribute);
//初始化用户打开文件表USEROPEN
void useropen_init(useropen *openfile, int dirno, int diroff, const char* dir);
//释放FAT表中记录的磁盘块号
void fatFree(int id);
//得到一个空闲的FAT块
int getFreeFatid();
//得到一个空闲的打开文件表项
int getFreeOpenlist();
//得到一个FAT表项的下一个FAT表项,如果没有则创建
int getNextFat(int id);
//检查一个打开文件表下表是否合法
int check_fd(int fd);
//把一个路径按'/'分割
int spiltDir(char dirs[DIRLEN][DIRLEN], char *filename);
//把一个路径的最后一个目录从字符串中删去
void popLastDir(char *dir);
//把一个路径的最后一个目录从字符串中分割出
void splitLastDir(char *dir, char new_dir[2][DIRLEN]);
// 得到某个长度在某个FAT中的对应的盘块号和偏移量,用来记录一个打开文件项在其父目录对应FAT的位置
void getPos(int *id, int *offset, unsigned short first, int length);
//把路径规范化并检查
int rewrite_dir(char *dir);
//根据盘块号和偏移量,直接从FAT上读取指定长度的信息
int fat_read(unsigned short id, unsigned char *text, int offset, int len);
//读取某个已打开文件的指定长度信息
int do_read(int fd, unsigned char *text, int len);
//根据盘块号和偏移量,直接从FAT上写入指定长度的信息
int fat_write(unsigned short id, unsigned char *text, int blockoffset, int len);
//某个已打开文件写入指定长度信息
int do_write(int fd, unsigned char *text, int len);
//从一个已打开目录文件找到对应名称的文件夹FCB,用于一些不断递归打开文件夹的函数中
int getFcb(fcb* fcbp, int *dirno, int *diroff, int fd, const char *dir);
//在一个已打开目录文件下打开某个文件
int getOpenlist(int fd, const char *org_dir);
//打开文件
int my_open(char *filename);
//读取一个文件夹下的FCB信息
int read_ls(int fd, unsigned char *text, int len);
//把一个文件夹下的FCB信息打印出来
void my_ls();
//把一个打开文件的内容根据文件指针打印出来
int my_read(int fd, int pos);
//重新从磁盘中读取一个打开文件的FCB内容
void my_reload(int fd);
//把键盘输入的信息写入一个打开文件
int my_write(int fd);
//把一个指定目录的FCB的free置为1
void my_rmdir(char *dirname);
//把一个指定文件的fcb的free置为1
void my_rm(char *filename);
//初始化
void my_format();
//在指定目录下创建一个文件或文件夹
int my_touch(char *filename, int attribute, int *rpafd);
//调用touch创建出一个文件
int my_create(char *filename);
//调用touch创建出一个文件夹
void my_mkdir(char *dirname);
//启动系统,做好相关的初始化
void startsys();
//退出系统,做好相应备份工作
void my_exitsys();
//将一个打开文件的FAT信息储存下来
void my_save(int fd);
//关闭一个打开文件
void my_close(int fd);
//利用my_open把当前目录切换到指定目录下
void my_cd(char *dirname);
//按16进制显示FAT数据
void show_fat(void);
//改变磁盘大小
void change_block_size(void);
//-------------------------------------函数声明---------------------------------------
//定义全局变量
unsigned char *myvhard; //指向虚拟磁盘的起始地址
useropen openfilelist[MAXOPENFILE]; //用户打开文件表数组
int curdirid; //当前目录的文件描述符fd
unsigned char *blockaddr[1000]; //磁盘块地址
block0 initblock; //声明引导块
fat fat1[1000], fat2[1000]; //声明FAT表
const char USERNAME[] = "root"; //用户名
int BLOCKSIZE=1024; //磁盘块大小
int BLOCKNUM=1000; //磁盘块总数
//-------------------------------------函数实现---------------------------------------
//szj
int main(){
int fd;
int pos;
char command[DIRLEN];
startsys();
printf("%s %s: ", USERNAME, openfilelist[curdirid].dir);
while(~scanf("%s", command)){
if(strcmp(command, "exit") == 0){
break;
}
else if(strcmp(command, "ls") == 0){
my_ls();
}
else if(strcmp(command, "mkdir") == 0){
scanf("%s", command);
if (rewrite_dir(command)){
my_mkdir(command);
}
}
else if(strcmp(command, "close") == 0){
scanf("%d", &fd);
my_close(fd);
}
else if(strcmp(command, "open") == 0){
scanf("%s", command);
if(rewrite_dir(command) == 0){
continue;
}
fd = my_open(command);
if(0 <= fd && fd < MAXOPENFILE){
if(openfilelist[fd].open_fcb.attribute == 0){
my_close(fd);
printf("%s is dirictory, please use cd command\n", command);
}
else{
printf("%s is open, it\'s id is %d\n", openfilelist[fd].dir, fd);
}
}
}
else if(strcmp(command, "cd") == 0){
scanf("%s", command);
if(rewrite_dir(command)){
my_cd(command);
}
}
else if(strcmp(command, "create") == 0){
scanf("%s", command);
if(rewrite_dir(command) == 0){
continue;
}
fd = my_create(command);
if(0 <= fd && fd < MAXOPENFILE) {
printf("%s is created, it\'s id is %d\n", openfilelist[fd].dir, fd);
}
}
else if(strcmp(command, "rm") == 0){
scanf("%s", command);
if(rewrite_dir(command)){
my_rm(command);
}
}
else if(strcmp(command, "rmdir") == 0){
scanf("%s", command);
if(rewrite_dir(command)){
my_rmdir(command);
}
}
else if(strcmp(command, "read") == 0){
scanf("%d %d", &fd, &pos);
if(pos >= 0){
my_read(fd, pos);
}
else{
my_read(fd,-1);
}
}
else if(strcmp(command, "write") == 0){
scanf("%d %d", &fd, &pos);
if(pos >= 0){
my_write(fd);
}
}
/*else if(strcmp(command, "sf") == 0){
for(int i = 0; i < MAXOPENFILE; i++) {
if (openfilelist[i].topenfile){
printf(" %d : %s\n", i, openfilelist[i].dir);
}
}
}*/
else if(strcmp(command, "format") == 0){
scanf("%s", command);
my_format();
}
else if(strcmp(command, "showfat") == 0){
show_fat();
}
/*
else if(strcmp(command,"changesize") == 0){
change_block_size();
}*/
else{
printf("command %s : no such command\n", command);
}
my_reload(curdirid);
printf("%s %s: ", USERNAME, openfilelist[curdirid].dir);
}
my_exitsys();
return 0;
}
void startsys(){
// 各种变量初始化
myvhard = (unsigned char*)malloc(SIZE);
// 准备读入 myfsys 文件信息
FILE *fp = fopen("myfsys", "rb");
char need_format = 0;
// 判断是否需要格式化
if(fp != NULL){
unsigned char *buf = (unsigned char*)malloc(SIZE);
fread(buf, 1, SIZE, fp);
memcpy(myvhard, buf, SIZE);
memcpy(&initblock, myvhard, sizeof(block0));
BLOCKNUM = initblock.blocknum;
BLOCKSIZE = initblock.blocksize;
char choose;
printf("初始化虚拟文件系统...\n");
printf("您保存的文件系统空间大小为: %d\n磁盘块大小为: %d\n磁盘块数量为: %d\n", SIZE, BLOCKSIZE, BLOCKNUM);
printf("您是否需要更改磁盘块大小?(y/n):\n");
printf("\033[31m更改磁盘块大小会格式化磁盘空间,请谨慎考虑\033[0m\n");
scanf("%c",&choose);
if(choose == 'y'){
need_format = 1;
}
free(buf);
fclose(fp);
}
else {
need_format = 1;
}
// 不需要格式化的话接着读入fat信息
if(need_format == 0){
for(int i = 0; i < BLOCKNUM; i++){
blockaddr[i] = i * BLOCKSIZE + myvhard;
}
for(int i = 0; i < MAXOPENFILE; i++){
openfilelist[i].topenfile = 0;
}
memcpy(fat1, blockaddr[1], BLOCKSIZE*sizeof(fat));
memcpy(fat2, blockaddr[3], BLOCKSIZE*sizeof(fat));
printf("载入文件系统成功...\n");
}
else {
printf("创建文件系统...\n");
my_format();
}
// 把根目录fcb放入打开文件表中,设定当前目录为根目录
curdirid = 0;
memcpy(&openfilelist[curdirid].open_fcb, blockaddr[5], sizeof(fcb));
useropen_init(&openfilelist[curdirid], 5, 0, "~/");
}
void my_reload(int fd){
if(check_fd(fd) == 0){
return ;
}
fat_read(openfilelist[fd].dirno, (unsigned char*)&openfilelist[fd].open_fcb, openfilelist[fd].diroff, sizeof(fcb));
return;
}
void my_exitsys(){
//关闭所有打开文件项
for(int i = 0; i < MAXOPENFILE; i++){
my_close(i);
}
memcpy(blockaddr[0], &initblock, sizeof(initblock));
memcpy(blockaddr[1], fat1, BLOCKNUM*sizeof(fat));
memcpy(blockaddr[3], fat1, BLOCKNUM*sizeof(fat));
fcb *testfcb;
//将内存中的磁盘块信息写入文件"myfsys"中保存。
FILE *fp = fopen("myfsys", "wb");
fwrite(myvhard, BLOCKSIZE, BLOCKNUM, fp);
free(myvhard);
fclose(fp);
}
void my_format(){
printf("请输入磁盘块大小,和数量:\n");
scanf("%d %d",&BLOCKSIZE,&BLOCKNUM);
for(int i = 0; i < BLOCKNUM; i++){
blockaddr[i] = i * BLOCKSIZE + myvhard;
}
for(int i = 0; i < MAXOPENFILE; i++){
openfilelist[i].topenfile = 0;
}
initblock.root = 5;
initblock.startblock = blockaddr[5];
initblock.blocksize = BLOCKSIZE;
initblock.blocknum = BLOCKNUM;
for(int i = 0; i < 5; i++){
fat1[i].id = END;
fat2[i].id = END;
}
for(int i = 5; i < BLOCKNUM; i++){
fat1[i].id = FREE;
fat2[i].id = FREE;
}
fat1[5].id = END;
fcb root;
fcb_init(&root, ".", 5, 0);
memcpy(blockaddr[5], &root, sizeof(fcb));
strcpy(root.filename, "..");
memcpy(blockaddr[5] + sizeof(fcb), &root, sizeof(fcb));
printf("初始化完成\n");
}
void fcb_init(fcb *new_fcb, const char* filename, unsigned short first, unsigned char attribute){
strcpy(new_fcb->filename, filename);
new_fcb->first = first;
new_fcb->attribute = attribute;
new_fcb->free = 0;
if(attribute == 1){
//如果类型是数据文件,则它的初始化长度为0
new_fcb->length = 0;
}
else{
//如果是目录文件,则初始长度为2个FCB大小(初始化要建立'.'和'..'两个特殊的目录项)
new_fcb->length = 2 * sizeof(fcb);
}
}
void useropen_init(useropen *openfile, int dirno, int diroff, const char* dir){
openfile->dirno = dirno;
openfile->diroff = diroff;
strcpy(openfile->dir, dir);
openfile->fcbstate = 0;
openfile->topenfile = 1;
openfile->count = openfile->open_fcb.length;
}
void change_block_size(){
// 各种变量初始化
myvhard = (unsigned char*)malloc(SIZE);
printf("\033[31m更改磁盘块大小会格式化磁盘空间,请谨慎考虑\033[0m\n");
printf("您是否需要更改磁盘块大小?(y/n):\n");
char choose;
getchar();
scanf("%c",&choose);
if(choose != 'y'){
return ;
}
my_format();
// 把根目录fcb放入打开文件表中,设定当前目录为根目录
curdirid = 0;
memcpy(&openfilelist[curdirid].open_fcb, blockaddr[5], sizeof(fcb));
useropen_init(&openfilelist[curdirid], 5, 0, "~/");
}
void show_fat(){
for(int i = 0 ; i < BLOCKNUM ; i += 16){
for(int j = 0 ; j < 16 ; j++){
if(i + j < BLOCKNUM){
printf("%.4x ",fat1[i+j].id);
}
}
printf("\n");
}
}
//smh
void fatFree(int id){
if(id == END){
return;
}
if(fat1[id].id != END){
fatFree(fat1[id].id);
}
fat1[id].id = FREE;
}
int getFreeFatid(){
for(int i = 5; i < BLOCKNUM; i++){
if(fat1[i].id == FREE){
fat1[i].id = END;
return i;
}
}
return END;
}
int getFreeOpenlist(){
for(int i = 0; i < MAXOPENFILE; i++){
if(openfilelist[i].topenfile == 0){
return i;
}
}
return -1;
}
int getNextFat(int id){
if(fat1[id].id == END){
fat1[id].id = getFreeFatid();
}
return fat1[id].id;
}
int fat_read(unsigned short id, unsigned char *text, int offset, int len){
int ret = 0;
unsigned char *buf = (unsigned char*)malloc(BLOCKSIZE);
int count = 0;
while(len){
memcpy(buf, blockaddr[id], BLOCKSIZE);
count = min(len, BLOCKSIZE - offset);
memcpy(text + ret, buf + offset, count);
len -= count;
ret += count;
offset = 0;
id = fat1[id].id;
}
free(buf);
return ret;
}
int do_read(int fd, unsigned char *text, int len){
int blockorder = openfilelist[fd].count / BLOCKSIZE;
int blockoffset = openfilelist[fd].count % BLOCKSIZE;
unsigned short id = openfilelist[fd].open_fcb.first;
while(blockorder){
blockorder--;
id = fat1[id].id;
}
int ret = fat_read(id, text, blockoffset, len);
return ret;
}
int my_read(int fd, int pos){
if((fd < 0 || fd >= MAXOPENFILE) || (openfilelist[fd].topenfile == 0) || (openfilelist[fd].open_fcb.attribute == 0)){
printf("my_read: fd invaild\n");
return -1;
}
unsigned char *buf = (unsigned char *)malloc(SIZE);
if(pos >= 0){
if(pos <= openfilelist[fd].open_fcb.length){
openfilelist[fd].count = pos;
}
else{
printf("ERROR:\n");
printf("my_read: pos is invalid!\n");
return -1;
}
}
else{
openfilelist[fd].count = 0;
}
int len = openfilelist[fd].open_fcb.length - openfilelist[fd].count;
int ret = do_read(fd, buf, len);
if(ret == -1){
free(buf);
printf("my_read: do_read error\n");
return -1;
}
buf[ret] = '\0';
printf("%s\n", buf);
return ret;
}
int fat_write(unsigned short id, unsigned char *text, int blockoffset, int len){
int ret = 0;
char *buf = (char*)malloc(BLOCKSIZE);
if(buf == NULL){
printf("ERROR:\n");
printf("fat_write: malloc error\n");
return -1;
}
// 写之前先把磁盘长度扩充到所需大小
int tlen = len;
int toffset = blockoffset;
unsigned short tid = id;
while(tlen){
if(tlen <= BLOCKSIZE - toffset){
break;
}
tlen -= (BLOCKSIZE - toffset);
toffset = 0;
id = getNextFat(id);
if(id == END){
printf("ERROR:\n");
printf("fat_write: no next fat\n");
return -1;
}
}
id = tid;
int count = 0;
while(len){
memcpy(buf, blockaddr[id], BLOCKSIZE);
count = min(len, BLOCKSIZE - blockoffset);
memcpy(buf + blockoffset, text + ret, count);
memset(blockaddr[id],'0',BLOCKSIZE);
memcpy(blockaddr[id], buf, BLOCKSIZE);
len -= count;
ret += count;
blockoffset = 0;
id = fat1[id].id;
}
free(buf);
return ret;
}
int do_write(int fd, unsigned char *text, int len){
fcb *fcbp = &openfilelist[fd].open_fcb;
int blockorder = openfilelist[fd].count / BLOCKSIZE;
int blockoffset = openfilelist[fd].count % BLOCKSIZE;
unsigned short id = openfilelist[fd].open_fcb.first;
while(blockorder){
blockorder--;
id = fat1[id].id;
}
int ret;
ret = fat_write(id, text, blockoffset, len);
fcbp->length +=ret;
openfilelist[fd].fcbstate = 1;
return ret;
}
int my_write(int fd){
char content[SIZE];
if((fd < 0 || fd >= MAXOPENFILE) || (openfilelist[fd].topenfile == 0) ||(openfilelist[fd].open_fcb.attribute == 0)){
printf("ERROR:\n");
printf("my_write: fd invaild\n");
return -1;
}
useropen *file = &openfilelist[fd];
printf("please input me the write style\n");
printf(" a : append write\n");
printf(" w : truncate write\n");
printf(" o : overwrite write\n");
char op;
scanf("%s", &op);
getchar();
if(op == 'a'){
file->count = file->open_fcb.length;
}
else if(op == 'w'){
file->count = 0;
file->open_fcb.length = 0;
fatFree(fat1[file->open_fcb.first].id);
}
else if(op=='o'){
file->count=0;
}
else{
printf("ERROR:\n");
printf("my_write: invaild write style!\n");
return -1;
}
int ret = 0;
int tmp;
printf("input content,end with input 'wq!'\n");
while(fgets(content,SIZE*sizeof(char),stdin)){
if(strncmp(content,"wq!",3) == 0){
break;
}
int len = strlen(content);
//content[len] = '\n';
int tem_length=file->open_fcb.length;
tmp = do_write(fd, (unsigned char*)content, len-1);
if(tmp == -1){
printf("ERROR:\n");
printf("my_write: do_write error\n");
return -1;
}
//file->count += tmp;
ret += tmp;
if(op=='o'){
file->open_fcb.length=tem_length>ret ? tem_length :ret;
}
}
my_close(fd);
return ret;
}
//chq
int my_open(char *filename){
char dirs[DIRLEN][DIRLEN];
int count = spiltDir(dirs, filename);
char realdirs[DIRLEN][DIRLEN];
int tot = 0;
for(int i = 1; i < count; i++){
if(strcmp(dirs[i], ".") == 0){
continue;
}
if(strcmp(dirs[i], "..") == 0){
if (tot){
tot--;
}
continue;
}
strcpy(realdirs[tot], dirs[i]);
tot++;
}
// 生成根目录的副本
int fd = getOpenlist(-1, "");
// 利用当前目录的副本不断找到下一个目录
int flag = 0;
for(int i = 0; i < tot; ++i){
int newfd = getOpenlist(fd, realdirs[i]);
if(newfd == -1){
flag = 1;
break;
}
my_close(fd);
fd = newfd;
}
if(flag == 1){
printf("ERROR:\n");
printf("my_open: %s no such file or directory\n", filename);
openfilelist[fd].topenfile = 0;
return -1;
}
//如果当前打开的文件打开两次,则关闭后打开的一次,返回之前打开的fd
for(int i = 0 ; i < MAXOPENFILE ; i++){
if(openfilelist[i].topenfile == 1 && strcmp(openfilelist[i].dir, openfilelist[fd].dir) == 0 && fd != i ){
my_close(fd);
return i;
}
}
if(openfilelist[fd].open_fcb.attribute == 1){
openfilelist[fd].count = 0;
}
else{
openfilelist[fd].count = openfilelist[fd].open_fcb.length;
}
return fd;
}
void splitLastDir(char *dir, char new_dir[2][DIRLEN]){
int len = strlen(dir) - 1;
int flag = -1;
for(int i = len; i >= 0; i--){
if(dir[i] == '/'){
flag = i;
break;
}
}
if(flag == -1){
printf("ERROR:\n");
printf("splitLastDir: can\'t split %s\n", dir);
return;
}
int tlen = 0;
for(int i = 0; i < flag; i++){
new_dir[0][tlen] = dir[i];
tlen++;
}
new_dir[0][tlen] = '\0';
tlen = 0;
for(int i = flag + 1; i <= len; i++){
new_dir[1][tlen] = dir[i];
tlen++;
}
new_dir[1][tlen] = '\0';
}
void my_rmdir(char *dirname){
int fd = my_open(dirname);
if(fd < 0 || fd >= MAXOPENFILE){
printf("ERROR:\n");
printf("my_rmdir: my_open error!");
my_close(fd);
return ;
}
if(openfilelist[fd].open_fcb.attribute){
printf("my_rmdir: %s is a file, please use rm command\n", dirname);
my_close(fd);
return;
}
if(strcmp(openfilelist[fd].dir, openfilelist[curdirid].dir)==0){
printf("my_rmdir: can not remove the current directory!\n");
my_close(fd);
return;
}
// 从磁盘中读出当前目录的信息
int cnt = 0;
unsigned char *buf = (unsigned char*)malloc(SIZE);
int read_size = read_ls(fd, buf,openfilelist[fd].open_fcb.length);
if(read_size == -1){
my_close(fd);
free(buf);
printf("ERROR:\n");
printf("my_rmdir: read_ls error\n");
return;
}
fcb dirfcb;
int flag = -1;
for(int i = 0; i < read_size; i += sizeof(fcb)){
memcpy(&dirfcb, buf + i, sizeof(fcb));
if(dirfcb.free){
continue;
}
cnt++;
}
if(cnt > 2){
printf("ERROR:\n");
printf("my_rmdir: %s is not empty\n", dirname);
my_close(fd);
return;
}
openfilelist[fd].open_fcb.free = 1;
fatFree(openfilelist[fd].open_fcb.first);
openfilelist[fd].fcbstate = 1;
my_close(fd);
}
void my_rm(char *filename){
int fd = my_open(filename);
if(fd < 0 || fd >= MAXOPENFILE){
printf("ERROR:\n");
printf("my_rm: my_open error!\n");
my_close(fd);
return ;
}
if(openfilelist[fd].open_fcb.attribute == 0) {
printf("ERROR:\n");
printf("my_rm: %s is a directory, please use rmdir command\n", filename);
my_close(fd);
return;
}
openfilelist[fd].open_fcb.free = 1;
fatFree(openfilelist[fd].open_fcb.first);
openfilelist[fd].fcbstate = 1;
my_close(fd);
}
int my_touch(char *filename, int attribute, int *rpafd){
// 先打开file的上级目录,如果上级目录不存在就报错
char split_dir[2][DIRLEN];
splitLastDir(filename, split_dir);
int pafd = my_open(split_dir[0]);
if(pafd < 0 || pafd >= MAXOPENFILE){
printf("ERROR\n");
printf("my_creat: my_open error\n");
return -1;
}
// 从磁盘中读出当前目录的信息,进行检查
unsigned char *buf = (unsigned char*)malloc(SIZE);
int read_size = read_ls(pafd, buf, openfilelist[pafd].open_fcb.length);
if(read_size == -1){
printf("ERROR:\n");
printf("my_touch: read_ls error\n");
return -1;
}
fcb dirfcb;
for(int i = 0; i < read_size; i += sizeof(fcb)){
memcpy(&dirfcb, buf + i, sizeof(fcb));
if(dirfcb.free){
continue;
}
if(!strcmp(dirfcb.filename, split_dir[1])){
printf("%s is already exit\n", split_dir[1]);
return -1;
}
}
// 利用空闲磁盘块创建文件
int fatid = getFreeFatid();
if(fatid == -1){
printf("ERROR:\n");
printf("my_touch: no free fat\n");
return -1;
}
fat1[fatid].id = END;
fcb_init(&dirfcb, split_dir[1], fatid, attribute);
// 写入父亲目录内存
memcpy(buf, &dirfcb, sizeof(fcb));
int write_size = do_write(pafd, buf, sizeof(fcb));
if(write_size == -1){
printf("ERROR:\n");
printf("my_touch: do_write error\n");
return -1;
}
openfilelist[pafd].count += write_size;
// 创建自己的打开文件项
int fd = getFreeOpenlist();
if(fd < 0 || fd > MAXOPENFILE){
printf("ERROR:\n");
printf("my_touch: no free fat\n");
return -1;
}
getPos(&openfilelist[fd].dirno, &openfilelist[fd].diroff, openfilelist[pafd].open_fcb.first, openfilelist[pafd].count - write_size);
memcpy(&openfilelist[fd].open_fcb, &dirfcb, sizeof(fcb));
if(attribute==1){
openfilelist[fd].count = 0;
}
else{
openfilelist[fd].count = openfilelist[fd].open_fcb.length;
}
openfilelist[fd].fcbstate = 1;
openfilelist[fd].topenfile = 1;
strcpy(openfilelist[fd].dir, openfilelist[pafd].dir);
strcat(openfilelist[fd].dir, split_dir[1]);
free(buf);
*rpafd = pafd;
return fd;
}
int my_create(char *filename){
int pafd;
int fd = my_touch(filename, 1, &pafd);
if (check_fd(fd) == 0){
printf("ERROR:\n");
printf("my_create: check_fd error!\n");
}
my_close(pafd);
return fd;
}
void my_mkdir(char *dirname){
int pafd;
int fd = my_touch(dirname, 0, &pafd);
if(check_fd(fd) == 0){
printf("ERROR:\n");
printf("my_mkdir: check_fd error!\n");
return;
}
unsigned char *buf = (unsigned char*)malloc(SIZE);
// 把"."和".."装入自己的磁盘
fcb dirfcb;
memcpy(&dirfcb, &openfilelist[fd].open_fcb, sizeof(fcb));
int fatid = dirfcb.first;
strcpy(dirfcb.filename, ".");
memcpy(blockaddr[fatid], &dirfcb, sizeof(fcb));
memcpy(&dirfcb, &openfilelist[pafd].open_fcb, sizeof(fcb));
strcpy(dirfcb.filename, "..");
memcpy(blockaddr[fatid] + sizeof(fcb), &dirfcb, sizeof(fcb));
my_close(pafd);
my_close(fd);
free(buf);
}
void my_save(int fd){
if(check_fd(fd) == 0){
printf("ERROR:\n");
printf("my_save: check_fd error!\n");
return;
}
useropen *file = &openfilelist[fd];
if(file->fcbstate == 1){
fat_write(file->dirno, (unsigned char *)&file->open_fcb, file->diroff, sizeof(fcb));
}
file->fcbstate = 0;
return;
}
void my_close(int fd){
if(check_fd(fd) == 0){
printf("ERROR:\n");
printf("my_close: check_fd error!\n");
return;
}
if(openfilelist[fd].topenfile == 0){
return;
}
// 若内容有改变,把fcb内容写回父亲的磁盘块中
if(openfilelist[fd].fcbstate == 1){
my_save(fd);
}
openfilelist[fd].topenfile = 0;
return;
}
void my_cd(char *dirname){
int fd = my_open(dirname);
if(check_fd(fd) == 0){
printf("ERROR:\n");
printf("my_cd: check_fd error!\n");
return;
}
if(openfilelist[fd].open_fcb.attribute == 1){
printf("ERROR:\n");
printf("%s is a file, please use open command\n", openfilelist[fd].dir);
my_close(fd);
return;
}
my_close(curdirid);
curdirid = fd;
}
//lsx
int check_fd(int fd){
if((fd < 0 || fd >= MAXOPENFILE)){
printf("ERROR:\n");
printf("check_fd: %d is invaild index\n", fd);
return 0;
}
return 1;
}
int spiltDir(char dirs[DIRLEN][DIRLEN], char *filename){
int bg = 0;
int ed = strlen(filename);
if(filename[0] == '/'){
bg++;
}
if(filename[ed - 1] == '/'){
ed--;
}
int ret = 0;
int tlen = 0;
for(int i = bg; i < ed; i++){
if(filename[i] == '/'){
dirs[ret][tlen] = '\0';
tlen = 0;