-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
814 lines (781 loc) · 23.5 KB
/
main.cpp
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
#include "head.h"
string cmd1, cmd2;
int cur_loc; // 当前目录
char tmp[2 * BLKSIZE]; // 缓冲区
User user; // 当前的用户
char bitmap[BLKNUM]; // 位图数组
Inode inode_array[INODENUM]; // i节点数组
File_table file_table[FILENUM]; // 打开文件表数组
char image_name[10] = "image.dat"; // 文件系统名称
FILE *fp; // 文件指针
const string Commands[] = {"help", "cd", "ls", "mkdir", "touch", "cat", "vi", "rm", "clear", "format", "exit", "rmdir",
"df"};
int readby() { //根据当前目录和第二个参数, 返回目标目录的inode号
if (cmd2[cmd2.length() - 1] != '/')
cmd2 += '/';
int result_cur = 0;
int tmp_cur = cur_loc;
vector <string> v; // 存储路径
while (cmd2.find('/') != -1) {
v.push_back(cmd2.substr(0, cmd2.find_first_of('/')));
cmd2 = cmd2.substr(cmd2.find_first_of('/') + 1);
}
if (v.size() == 0) {
return cur_loc;
}
if (v[0].length() == 0) {
tmp_cur = 0;
} else if (v[0] == "..") {
if (inode_array[cur_loc].inum > 0) {
tmp_cur = inode_array[cur_loc].iparent;
}
} else {
int i;
for (i = 0; i < INODENUM; i++) {
if ((inode_array[i].inum > 0) &&
(inode_array[i].iparent == cur_loc) &&
(inode_array[i].type == 'd') &&
inode_array[i].file_name == v[0]) {
break;
}
}
if (i == INODENUM) {
return -1;
} else {
tmp_cur = i;
}
}
int i;
for (unsigned int count = 1; count < v.size(); count++) {
for (i = 0; i < INODENUM; i++) {
if ((inode_array[i].inum > 0) && //是否为空
(inode_array[i].iparent == tmp_cur) &&
(inode_array[i].type == 'd') &&
inode_array[i].file_name == v[count]) {
break;
}
}
if (i == INODENUM) {
return -1;
} else {
tmp_cur = i;
}
}
result_cur = tmp_cur;
return result_cur;
}
//创建磁盘映像,并将所有用户和文件清除
void format(void) {
int i;
printf("Formating filesystem...\n");
printf("All the old data will lost!!!\n");
printf("Are you sure format the fileSystem?(Y/N)?");
char choice;
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) {
// 打开映像文件
fp = fopen(image_name, "w+b");
if (fp == NULL) {
printf("Can't open file %s\n", image_name);
getchar();
exit(-1);
}
//成功打开后,位图数组置0
for (i = 0; i < BLKNUM; i++)
fputc('0', fp);
Inode inode;
inode.inum = 0;
inode.iparent = 0;
inode.length = 0;
inode.address[0] = -1;
inode.address[1] = -1;
inode.type = 'd';
strcpy(inode.file_name, "/");
strcpy(inode.user_name, "all");
fwrite(&inode, sizeof(Inode), 1, fp);
inode.inum = -1;
//对32个节点进行格式化
for (i = 0; i < 31; i++)
fwrite(&inode, sizeof(Inode), 1, fp);
//数据块进行格式化
for (i = 0; i < BLKNUM * BLKSIZE; i++)
fputc('\0', fp);
fclose(fp);
//初始化用户文件
fp = fopen("user.txt", "w+");
if (fp == NULL) {
printf("Can't create file %s\n", "user.txt");
exit(-1);
}
fclose(fp);
printf("Filesystem created successful!\n");
} else
quit();
return;
}
/*功能: 用户登陆,如果是新用户则创建用户*/
void login() {
// 先检查文件系统是否存在
if ((fp = fopen("user.txt", "r+")) == NULL) {
printf("Unable to load filesystem, trying to repair now...\n");
format();
login();
}
int flag = 0;
char user_name[10];
char password[10];
do {
// 输入用户名、密码
printf("Please Login First!\n");
printf("username:");
gets(user_name);
printf("password:");
gets(password);
// 校验用户名、密码
while (1) {
size_t n = fread(&user, sizeof(User), 1, fp);
// 已经存在的用户, 且密码正确
if (!strcmp(user.user_name, user_name) &&
!strcmp(user.password, password)) {
fclose(fp);
printf("\n");
return; //校验成功,直接跳出登陆函数
}
// 已经存在的用户, 但密码错误
else if (!strcmp(user.user_name, user_name)) {
printf("\nPassword incorrect!\n");
flag = 1; //密码错误,重新登陆
fclose(fp);
break;
}
// 不存在的用户名
if(n == 0) {
printf("\nThis user doesn't exist.\n");
break; //用户不存在,先跳出循环创建新用户
}
}
} while (flag);
// 创建新用户
if (flag == 0) {
printf("\nDo you want to creat a new user?(y/n):");
char choice; //选择是否(y/n)
cin >> choice; getchar();
if ((choice == 'y') || (choice == 'Y')) {
strcpy(user.user_name, user_name);
strcpy(user.password, password);
fwrite(&user, sizeof(User), 1, fp);
fclose(fp);
return;
} else
quit();
}
}
// 功能: 将所有i节点读入内存
void init(void) {
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s.\n", image_name);
exit(-1);
}
// 读入位图
for (int i = 0; i < BLKNUM; i++)
bitmap[i] = fgetc(fp);
// 显示位图
// 读入i节点信息
for (int i = 0; i < INODENUM; i++)
fread(&inode_array[i], sizeof(Inode), 1, fp);
// 显示i节点
// 当前目录为根目录
cur_loc = 0;
// 初始化打开文件表
for (int i = 0; i < FILENUM; i++)
file_table[i].inum = -1;
}
void df() {
int cnt_inode = 0, cnt_mem = 0;
for (int i = 0; i < INODENUM; i++) {
if (inode_array[i].inum > 0)
cnt_inode++;
}
for (int i = 0; i < BLKNUM; i++) {
if (bitmap[i] == '1')
cnt_mem++;
}
cout << "Inode use:" << cnt_inode << "/" << INODENUM << endl;
cout << "Store use:" << cnt_mem << "/" << BLKNUM << endl;
}
// 功能: 将num号i节点保存到磁盘
void save_inode(int num) {
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s\n", image_name);
exit(-1);
}
fseek(fp, BLKNUM + num * sizeof(Inode), SEEK_SET);
fwrite(&inode_array[num], sizeof(Inode), 1, fp);
fclose(fp);
}
// 功能: 申请一个数据块
int get_blknum(void) {
int i;
for (i = 0; i < BLKNUM; i++)
if (bitmap[i] == '0')
break;
// 未找到空闲数据块
if (i == BLKNUM) {
printf("Data area is full.\n");
exit(-1);
}
bitmap[i] = '1';
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s\n", image_name);
exit(-1);
}
fseek(fp, i, SEEK_SET);
fputc('1', fp);
fclose(fp);
return i;
}
// 功能: 将i节点号为num的文件读入tmp
void read_blk(int num) {
int i, len;
char ch;
int add0, add1;
len = inode_array[num].length;
add0 = inode_array[num].address[0];
if (len > BLKSIZE)
add1 = inode_array[num].address[1];
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s.\n", image_name);
exit(-1);
}
fseek(fp, BLKOFFSET + add0 * BLKSIZE, SEEK_SET); // 前1536是
ch = fgetc(fp);
for (i = 0; (i < len) && (ch != '\0') && (i < BLKSIZE); i++) {
tmp[i] = ch;
ch = fgetc(fp);
}
if (i >= BLKSIZE) {
fseek(fp, BLKOFFSET + add1 * BLKSIZE, SEEK_SET);
ch = fgetc(fp);
for (; (i < len) && (ch != '\0'); i++) {
tmp[i] = ch;
ch = fgetc(fp);
}
}
tmp[i] = '\0';
fclose(fp);
}
// 功能: 将tmp的内容输入磁盘的数据区
void write_blk(int num) {
int i, len;
int add0, add1;
add0 = inode_array[num].address[0];
len = inode_array[num].length;
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s.\n", image_name);
exit(-1);
}
fseek(fp, BLKOFFSET + add0 * BLKSIZE, SEEK_SET);
for (i = 0; (i < len) && (tmp[i] != '\0') && (i < BLKSIZE); i++)
fputc(tmp[i], fp);
if (i == BLKSIZE) {
add1 = inode_array[num].address[1];
fseek(fp, BLKOFFSET + add1 * BLKSIZE, SEEK_SET);
for (; (i < len) && (tmp[i] != '\0'); i++)
fputc(tmp[i], fp);
}
fputc('\0', fp);
fclose(fp);
}
// 功能: 释放文件块号为num的文件占用的空间
void release_blk(int num) {
FILE *fp;
if ((fp = fopen(image_name, "r+b")) == NULL) {
printf("Can't open file %s\n", image_name);
exit(-1);
}
bitmap[num] = '0';
fseek(fp, num, SEEK_SET);
fputc('0', fp);
fclose(fp);
}
//设置文件路径
void pathset() {
string s;
if (inode_array[cur_loc].inum == 0)
s = "";
else {
int tmp = cur_loc;
while (inode_array[tmp].inum != 0) {
s = inode_array[tmp].file_name + s;
s = '/' + s;
tmp = inode_array[tmp].iparent;
}
}
cout << user.user_name << "@" << HOSTNAME << ":~" << s << "# ";
}
// 功能: 切换目录(cd .. 或者 cd dir1)
void cd() {
int tmp_cur;
if (cmd2.length() == 0) { // 如果无参数,则去根目录
tmp_cur = 0;
} else { // 否则去目标目录
tmp_cur = readby();
}
if (tmp_cur != -1) {
cur_loc = tmp_cur;
} else {
cout << "No Such Directory" << endl;
}
}
// 功能: 显示当前目录下的子目录和文件(dir)
void dir(void) {
int tmp_cur;
int i = 0;
if (cmd2.length() == 0) {
tmp_cur = cur_loc;
} else {
tmp_cur = readby();
if (tmp_cur == -1 || inode_array[tmp_cur].type != 'd') {
cout << "No Such Directory" << endl;
return;
}
}
printf("Name\t\tType\t\tUseage\t\tOwner\n", inode_array[i].file_name, inode_array[i].user_name);
for (i = 0; i < INODENUM; i++) {
if ((inode_array[i].inum > 0) && // 有效节点
(inode_array[i].iparent == tmp_cur) // 当前目录下
// && !strcmp(inode_array[i].user_name, user.user_name)) // 属于当前用户
){
if (inode_array[i].type == 'd')
printf("%s\t\t<DIR>\t\t---\t\t%s\n", inode_array[i].file_name, inode_array[i].user_name);
if (inode_array[i].type == '-')
printf("%s\t\t<FILL>\t\t%2d bytes\t%s\n", inode_array[i].file_name, inode_array[i].length, inode_array[i].user_name);
}
}
}
void rm(int inode) {
int i;
for (i = 0; i < INODENUM; i++) {
if (inode_array[i].iparent == inode && !strcmp(inode_array[i].user_name, user.user_name)) {
if (inode_array[i].type == 'd')
rm(i);
else
delet(i);
}
}
delet(inode);
}
void rmdir() {
int tmp_cur = readby();
if (tmp_cur == -1 || tmp_cur == cur_loc)
printf("No Such Directory\n");
else {
if (inode_array[tmp_cur].type != 'd') {
printf("That's A File! (Try rm command ?)\n");
} else {
int tmp = inode_array[cur_loc].iparent;
while (tmp != 0) { // 检测要删除的是否为父级目录,若是,则不能删除
if (tmp == tmp_cur || tmp_cur == 0) {
printf("Can't delete your father directory\n");
return;
}
tmp = inode_array[tmp].iparent;
}
rm(tmp_cur);
}
}
return;
}
// 功能: 在当前目录下创建子目录(mkdir dir1)
void mkdir(void) {
int i;
if (cmd2.length() == 0) {
cout << "Please input name" << endl;
return;
}
for (i = 0; i < INODENUM; i++) { // 遍历i节点数组, 查找未用的i节点
if (inode_array[i].iparent == cur_loc && inode_array[i].type == 'd' && inode_array[i].file_name == cmd2 &&
inode_array[i].inum > 0) {
break;
}
}
if (i != INODENUM) {
printf("There is directory having same name.\n");
return;
}
for (i = 0; i < INODENUM; i++)
if (inode_array[i].inum < 0)
break;
if (i == INODENUM) {
printf("Inode is full.\n");
exit(-1);
}
inode_array[i].inum = i;
strcpy(inode_array[i].file_name, cmd2.data());
inode_array[i].type = 'd';
strcpy(inode_array[i].user_name, user.user_name);
inode_array[i].iparent = cur_loc;
inode_array[i].length = 0;
save_inode(i);
}
// 功能: 在指定目录下创建空文件
void touch(void) {
if (cmd2.length() == 0) {
printf("Please input filename!\n");
return;
}
int i, tmp_cur;
string fileLoc, fileName;
if (cmd2.find('/') != -1) {
fileLoc = cmd2.substr(0, cmd2.find_last_of('/') + 1);
fileName = cmd2.substr(cmd2.find_last_of('/') + 1);
cmd2 = fileLoc;
tmp_cur = readby();
if (tmp_cur == -1) {
printf("No Such Directory\n");
}
} else {
fileName = cmd2;
tmp_cur = cur_loc;
}
// 检测文件名是否存在
for (i = 0; i < INODENUM; i++)
if ((inode_array[i].inum > 0) && // 有效节点
(inode_array[i].type == '-') && // 文件类型
fileName == inode_array[i].file_name && // 文件名相同
inode_array[i].iparent == tmp_cur) // 目录符合
break;
if (i != INODENUM) {
printf("The file name already exists\n");
return;
}
// 检测Inode是否已经存满
for (i = 0; i < INODENUM; i++)
if (inode_array[i].inum < 0)
break;
if (i == INODENUM) {
printf("Inode is full!\n");
exit(-1);
}
// 创建新的空文件
inode_array[i].inum = i;
strcpy(inode_array[i].file_name, fileName.data());
inode_array[i].type = '-';
strcpy(inode_array[i].user_name, user.user_name);
inode_array[i].iparent = tmp_cur;
inode_array[i].length = 0; // 文件初始大小为0
save_inode(i);
}
//检查当前I节点的文件是否属于当前用户
int check(int i) {
int j;
char *uuser, *fuser;
if(strcmp(user.user_name, inode_array[i].user_name))
return 0;
else
return 1;
}
/* 功能: 打开当前目录下的文件; 目前mode仅作为一个字段,无其他含义 */
int open(int mode) {
int i, inum, filenum, chk;
if (cmd2.length() == 0) {
printf("This file doesn't exist.\n");
return -1;
}
int tmp_cur;
string fileLoc, fileName;
if (cmd2.find('/') != -1) {
fileLoc = cmd2.substr(0, cmd2.find_last_of('/') + 1);
fileName = cmd2.substr(cmd2.find_last_of('/') + 1);
string tmps = cmd2;
cmd2 = fileLoc;
tmp_cur = readby();
cmd2 = tmps;
if (tmp_cur == -1) {
cout << "No Such Directory" << endl;
}
} else {
fileName = cmd2;
tmp_cur = cur_loc;
}
for (i = 0; i < INODENUM; i++){
if ((inode_array[i].inum > 0) &&
(inode_array[i].type == '-') &&
fileName == inode_array[i].file_name &&
inode_array[i].iparent == tmp_cur) //判断是否在当前目录下
break;
}
if (i == INODENUM) {
cout << "This is no \"" + fileName + "\" file...\n";
return -1;
}
inum = i;
chk = check(i); //检查该文件是否为当前用户的文件
if (chk != 1) {
printf("Permission denied! This file is owned by %s!\n", inode_array[i].user_name);
return -1;
}
if ((mode < 1) || (mode > 3)) {
printf("Open mode is wrong.\n");
return -1;
}
filenum = i;
file_table[filenum].inum = inum;
strcpy(file_table[filenum].file_name, inode_array[inum].file_name);
file_table[filenum].mode = mode;
file_table[filenum].offset = 0;
return 0;
}
/*功能: 关闭已经打开的文件 */
void close() {
int i;
int tmp_cur;
string fileLoc, fileName;
if (cmd2.find('/') != -1) {
fileLoc = cmd2.substr(0, cmd2.find_last_of('/') + 1);
fileName = cmd2.substr(cmd2.find_last_of('/') + 1);
string tmps = cmd2;
cmd2 = fileLoc;
tmp_cur = readby();
cmd2 = tmps;
} else {
fileName = cmd2;
tmp_cur = cur_loc;
}
for (i = 0; i < FILENUM; i++)
if ((file_table[i].inum > 0) &&
(fileName == file_table[i].file_name))
break;
if (i == FILENUM) {
printf("This file didn't be opened.\n");
return;
} else
file_table[i].inum = -1;
}
void cat() {
if(open(1) != 0) {
cout << "error occurs!" << endl;
return;
}
string fileLoc, fileName;
int tmp_cur;
if (cmd2.find('/') != -1) {
fileLoc = cmd2.substr(0, cmd2.find_last_of('/') + 1);
fileName = cmd2.substr(cmd2.find_last_of('/') + 1);
string tmps = cmd2;
cmd2 = fileLoc;
tmp_cur = readby();
cmd2 = tmps;
} else {
fileName = cmd2;
tmp_cur = cur_loc;
}
int i;
for (i = 0; i < FILENUM; i++)
if ((file_table[i].inum > 0) &&
(fileName == file_table[i].file_name))
break;
int inum = file_table[i].inum;
if (inode_array[inum].length > 0) {
read_blk(inum);
for (i = 0; tmp[i] != '\0'; i++)
printf("%c", tmp[i]);
printf("\n");
}
close();
}
/*功能: 向文件中写入字符 */
void vi() {
if(open(3) != 0) {
cout << "error occurs!" << endl;
return;
}
int i, inum;
for (i = 0; i < FILENUM; i++)
if ((file_table[i].inum > 0) &&
cmd2 == file_table[i].file_name)
break;
if (i == FILENUM) {
cout << "Open " << cmd2 << " false.\n";
return;
}
inum = file_table[i].inum;
if (inode_array[inum].length == 0) {
printf("Input the data(Enter to end):\n");
gets(tmp);
if (strlen(tmp) > BLKSIZE * 2) {
printf("You can't creat a file which data more than %d bytes.'.\n", BLKSIZE * 2);
return;
}
inode_array[inum].length = strlen(tmp);
inode_array[inum].address[0] = get_blknum();
if (strlen(tmp) > BLKSIZE)
inode_array[inum].address[1] = get_blknum();
save_inode(inum);
write_blk(inum);
} else {
printf("Are you sure to write this file? Data in it will all be deleted.(y/n):");
char choice;
scanf("%c", &choice);
if ((choice == 'y') || (choice == 'Y')) {
printf("Input the data(Enter to end):\n");
gets(tmp);
if (strlen(tmp) > BLKSIZE * 2) {
printf("You can't creat a file which data more than %d bytes.'.\n", BLKSIZE * 2);
return;
}
inode_array[inum].length = strlen(tmp);
inode_array[inum].address[0] = get_blknum();
if (strlen(tmp) > BLKSIZE)
inode_array[inum].address[1] = get_blknum();
save_inode(inum);
write_blk(inum);
}
}
close();
int tmp_cur = cur_loc;
init(); //需要初始化一下,否则文件有一个文件名会出错
cur_loc = tmp_cur;
}
//根据Inode节点号删存储
void delet(int innum) {
inode_array[innum].inum = -1;
if (inode_array[innum].length >= 0) {
release_blk(inode_array[innum].address[0]);
if (inode_array[innum].length >= BLKSIZE)
release_blk(inode_array[innum].address[1]);
}
save_inode(innum);
}
void rmfile(void) {
if (cmd2.length() == 0) {
printf("This file doesn't exist.\n");
return;
}
int i, tmp_cur;
string fileLoc, fileName;
if (cmd2.find('/') != -1) {
fileLoc = cmd2.substr(0, cmd2.find_last_of('/') + 1);
fileName = cmd2.substr(cmd2.find_last_of('/') + 1);
cmd2 = fileLoc;
tmp_cur = readby();
} else {
fileName = cmd2;
tmp_cur = cur_loc;
}
for (i = 0; i < INODENUM; i++)
if ((inode_array[i].inum > 0) &&
(inode_array[i].type == '-') && //文件类型
fileName == inode_array[i].file_name && //文件名符合
inode_array[i].iparent == tmp_cur && // 父目录符合
!strcmp(inode_array[i].user_name, user.user_name)) // 权限检查
break;
if (i == INODENUM) {
printf("This file doesn't exist.\n");
return;
}
delet(i);
}
// 功能: 退出文件系统(quit)
void quit() {
printf("Bye~\n");
exit(0);
}
void help() {
printf("command: \n\
help --- show help menu \n\
clear --- clear the screen \n\
vi --- open and write something to a particular file \n\
rm --- delete a exist file \n\
ls --- show all the files and directories in particular directory \n\
cd --- change directory \n\
mkdir --- make a new directory \n\
rmdir --- delete a directory \n\
touch --- create a new file \n\
cat --- open and read an exist file \n\
format --- format a exist filesystem \n\
df --- show the useage of storage \n\
exit --- exit this system \n");
}
// 结果: 0-12为系统命令, 13为命令错误
int cmd_analyze() {
string s = "";
getline(cin, s);
if (s.find(' ') == -1) { // 如果不含空格,则是无参数的命令
cmd1 = s;
cmd2 = "";
} else { //否则是有参数的命令
cmd1 = s.substr(0, s.find_first_of(' '));
cmd2 = s.substr(s.find_first_of(' ') + 1);
}
int choice;
for (choice = 0; choice < 13; choice++) {
if (cmd1 == Commands[choice])
break;
}
return choice;
}
// 功能: 循环执行用户输入的命令
void command(void) {
//system("cls");
system("clear");
printf("Login success!\n");
printf("Type \"help\" for more info.\n\n");
do {
pathset();
switch (cmd_analyze()) {
case 0:
help();
break;
case 1:
cd();
break;
case 2:
dir();
break;
case 3:
mkdir();
break;
case 4:
touch();
break;
case 5:
cat();
break;
case 6:
vi();
break;
case 7:
rmfile();
break;
case 8:
system("clear");
break;
case 9:
format();
printf("Format success, please reopen the program!\n");
quit();
case 10:
quit();
break;
case 11:
rmdir();
break;
case 12:
df();
break;
case 13:
printf("Command Error!!!\n");
break;
default:
break;
}
} while (1);
}
// 主函数
int main(void) {
login();
init();
command();
return 0;
}