-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystem.cpp
1556 lines (1474 loc) · 50 KB
/
FileSystem.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
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 "FileSystem.h"
#include "Inode.h"
#include <vector>
#include <string>
#include <cstddef>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <cstddef>
#include <cmath>
#include <iomanip>
using namespace std;
char buffer[1024];
FileSystem::FileSystem()
{
}
FileSystem::~FileSystem()
{
}
void FileSystem::initial()
{
welcome();
//初始化系统,尝试打开文件
file.open("filesystem.txt", ios::in | ios::out);
//如果文件不存在的话
if (!file.is_open())
{
ofstream fout("filesystem.txt"); //创建文件,并且把文件扩充为16MB
if (!fout) //如果创建失败
{
cout << "Fail to initialize file system..." << endl;
exit(-1);
}
cout << "Initialize the file system successfully!" << endl;
for (int i = 0; i < 16384; i++) //把文件扩充成16MB
{
fout.write(buffer, sizeof(buffer)); //一个buffer1024字节,文件系统一共16MB,所以写16384次
}
//初始化超级块并把超级块写进去
fout.seekp(0, ios::beg);
fout.write((char *)&superBlock, sizeof(superBlock));
//把inode位图写进去(全部初始化为0)
fout.seekp(BLOCK_SIZE, ios::beg);
for (int i = 0; i < INODE_BITMAP_SIZE; i++)
{
char byte;
memset(&byte, 0, 1);
fout.write(&byte, sizeof(char));
}
//把block位图写进去(全部初始化为0)
fout.seekp(BLOCK_SIZE + INODE_BITMAP_SIZE, ios::beg);
for (int i = 0; i < 1024; i++)
{
char byte;
memset(&byte, 0, 1);
fout.write(&byte, sizeof(char));
}
//关掉ofstream,用全局变量fstream来打开文件
fout.close();
file.open("filesystem.txt", ios::in | ios::out);
//把block位图中的前4块分别置为1(superblock, inode bitmap, block bitmap)
//把block位图中的inode table占据的块也设置为1(1024块)
for (int i = 0; i < INODE_TABLE_SIZE / BLOCK_SIZE + 4; i++)
{
modify_block_bitmap(i);
}
superBlock.used_block_count += (4 + 1024); //更新superblock
//设置根目录占据的inode节点(只需要设置id,其他的属性初始化的时候会自动赋值)
root_inode.set_id(0);
modify_inode_bitmap(0);
superBlock.used_inode_count++; //更新superblock
//将inode保存
file.seekg(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE, ios::beg);
file.write((char *)&root_inode, sizeof(root_inode));
//设置当前工作目录
cur_inode = root_inode;
}
//如果文件已存在
else
{
cout << "FileSystem is loading..." << endl;
//把superBlock读出来
file.seekg(0, ios::beg);
file.read((char *)&superBlock, sizeof(superBlock));
//读出文件中的根目录inode
file.seekp(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE, ios::beg);
file.read((char *)&root_inode, sizeof(root_inode));
cur_inode = root_inode;
}
cur_path = "/";
//现在文件已经存在了,开始接收用户输入的指令
handle_command();
}
void FileSystem::welcome()
{
cout << "Welcome to Our File System!" << endl;
cout << "Name StudentID" << endl;
cout << "许晓漫 201930344213" << endl;
cout << "姚禧 201930330247" << endl;
cout << "For more information,please enter help" << endl;
string s;
cin >> s;
if (s == "help")
{
help();
}
}
void FileSystem::help()
{
cout << "--createFile fileName fileSize create a file with specified filesize(in KB) which is smaller than 266KB" << endl;
cout << "--deleteFile fileName delete a file" << endl;
cout << "--createDir dirName create a directory" << endl;
cout << "--deleteDir dirName delete a directory(except the current working directory)" << endl;
cout << "--changeDir path change the working directory" << endl;
cout << "--dir list all the files and sub-directories under current working directory" << endl;
cout << "--cp file1 file2 copy file1 to file2" << endl;
cout << "--sum display the usage of storage space the 16MB" << endl;
cout << "--cat fileName print out the file contents" << endl;
cout << "--exit exit the system" << endl;
cout << "DON'T USE CTRL+C TO EXIT!!!" << endl;
}
void FileSystem::handle_command()
{
//输出当前工作目录
cout << cur_path << "$ ";
string command;
getline(cin, command);
getline(cin, command);
while (1)
{
cur_inode = read_inode(cur_inode.get_id()); //更新cur_inode,防止在上一次操作中改变了
root_inode = read_inode(0); //更新root_inode,防止在上一次操作中改变了
vector<string> commands; //用来存以空格隔开的命令
string::size_type i = command.find(" ", 0); //查找第一个空格所在的地方
while (i != string::npos)
{
commands.push_back(command.substr(0, i));
command = command.substr(i + 1, command.length() - (i + 1));
i = command.find(" ", 0);
}
commands.push_back(command);
//分析命令(检查命令的格式是否正确并调用相应函数
if (commands[0] == "createFile")
{
if (commands.size() != 3)
{
cout << "The number of parameters should be 3!" << endl;
}
else
{
int size;
if (!string_to_int(commands[2], size))
{
cout << "Fail to create! File size should be a positive number!" << endl;
}
else
{
if (size > 266)
{
cout << "Fail to create!Please use a smaller size!" << endl;
}
else
{
//判断文件名长度是否比12字节长
if (commands[1].size() > 12)
cout << "The length of file name is too long" << endl;
else
cout << createFile(commands[1], size) << endl;
}
}
}
}
else if (commands[0] == "deleteFile")
{
//判断参数数目
if (commands.size() != 2)
{
cout << "Lack of parameter! Please try again." << endl;
continue;
}
//判断当前路径是绝对路径还是相对路径,可删除不是当前目录下的文件
// 如果是绝对路径,可从根节点下搜集
string filepath = commands[1];
Inode search_inode;
if (filepath[0] == '/')
{
search_inode = root_inode;
}
else
{
search_inode = cur_inode;
}
//分割字符串
vector<string> spl_path = split_path(filepath);
deleteFile(spl_path, search_inode);
}
else if (commands[0] == "createDir")
{
if (commands.size() != 2)
{
cout << "The number of parameters should be 3!" << endl;
}
else
{
if (commands[1].size() > 12)
cout << "The length of the directory is too long" << endl;
else
cout << createDir(commands[1]) << endl;
}
}
else if (commands[0] == "deleteDir")
{
//判断参数数目
if (commands.size() != 2)
{
cout << "Lack of parameter! Please try again." << endl;
continue;
}
//判断当前路径是绝对路径还是相对路径,可删除不是当前目录下的文件
string filepath = commands[1];
if (filepath == cur_path)
{
cout << "You are not allowed to delete the current working directory! Please try other options.\n";
continue;
}
Inode search_inode;
// 如果是绝对路径,可从根节点下搜集
if (filepath[0] == '/')
{
search_inode = root_inode;
}
else
{
search_inode = cur_inode;
}
//分割字符串
vector<string> spl_path = split_path(filepath);
cout << deleteDir(spl_path, search_inode) << endl;
}
else if (commands[0] == "changeDir")
{
//判断参数数目
if (commands.size() != 2)
{
cout << "Lack of parameter! Please try again." << endl;
continue;
}
changeDir(commands[1]);
}
else if (commands[0] == "dir")
{
if (commands.size() != 1)
{
cout << "The number of parameters should be 1!" << endl;
}
else
{
cout << dir_list() << endl;
}
}
else if (commands[0] == "cp")
{
if (commands.size() != 3)
{
cout << "Something was wrong for the format of the commands. There should be 3 parameters." << endl;
continue;
}
copy_path(commands[1], commands[2]);
}
else if (commands[0] == "sum")
{
if (commands.size() != 1)
{
cout << "The number of parameters should be 1!" << endl;
}
else
{
sum();
}
}
else if (commands[0] == "cat")
{
if (commands.size() != 2)
{
cout << "The number of parameters should be 2!" << endl;
}
else
{
cout << catFile(commands[1]) << endl;
}
}
else if (commands[0] == "help")
{
help();
}
else if (commands[0] == "exit")
{
//把super block写回去
file.seekp(0, ios::beg);
file.write((char *)&superBlock, sizeof(superBlock));
file.close();
exit(0);
}
else
{
cout << "Please input the commands in correct format!" << endl;
}
cout << cur_path << "$ ";
getline(cin, command); //获取下一个指令
}
}
//修改block bitmap中的某一个比特位(参数:哪一位)
void FileSystem::modify_block_bitmap(int position)
{
int byte_position = position / 8; //表示当前这个要修改的位在哪个字节上
int bit_position = position % 8; //表示当前这个要修改的位在字节上的哪个位置
file.seekg(2 * 1024 + byte_position, ios::beg);
char a;
file.read(&a, sizeof(a)); //读出那个需要修改的字节
a = (a ^ (1 << bit_position));
file.seekp(2 * 1024 + byte_position, ios::beg);
file.write(&a, sizeof(a));
}
//修改inode
// bitmap中的某一个比特位(参数:哪一位)
void FileSystem::modify_inode_bitmap(int position)
{
int byte_position = position / 8;
int bit_position = position % 8;
file.seekg(1024 + byte_position, ios::beg);
char a;
file.read(&a, sizeof(a));
a = (a ^ (1 << bit_position));
file.seekp(1024 + byte_position, ios::beg);
file.write(&a, sizeof(a));
}
//根据blockmap找空闲的block,并返回data block id
int FileSystem::find_free_block()
{
file.seekg(2 * BLOCK_SIZE, ios::beg);
char byte;
//遍历整个位图
for (int i = 0; i < BLOCK_BITMAP_SIZE; i++)
{
file.read(&byte, sizeof(char));
//判断
for (int j = 0; j < 8; j++)
{
//如果当前位是0,则返回1,否则返回0
if ((~(byte >> j)) & 1)
{
return i * 8 + j; //返回id
}
}
}
//说明找不到,但一般不会,因为在此之前需要先通过superBlock判断有没有inode可用
return -1;
}
//根据bitmap找空闲的inode,并返回inode id
int FileSystem::find_free_inode()
{
file.seekg(1 * BLOCK_SIZE, ios::beg);
char byte;
//遍历整个位图
for (int i = 0; i < INODE_BITMAP_SIZE; i++)
{
file.read(&byte, sizeof(char));
//判断
for (int j = 0; j < 8; j++)
{
//如果当前位是0,则返回1,否则返回0
if ((~(byte >> j)) & 1)
{
return i * 8 + j; //返回id
}
}
}
//说明找不到,但一般不会,因为在此之前需要先通过superBlock判断有没有inode可用
return -1;
}
//读取inode节点的相关信息
Inode FileSystem::read_inode(int pos)
{
Inode res;
file.seekg(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE + pos * INODE_SIZE, ios::beg);
file.read((char *)&res, sizeof(Inode));
return res;
}
//将inode写回文件
void FileSystem::save_inode(Inode inode)
{
int id = inode.get_id();
file.seekp(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE + id * INODE_SIZE, ios::beg);
file.write((char *)&inode, sizeof(inode));
}
//将string转换为int并判断是不是纯数字
bool FileSystem::string_to_int(string s, int &size)
{
size = 0;
int n = s.length();
bool isD = true;
for (int i = 0; i < n; i++)
{
if (!isdigit(s[i]))
{
isD = false;
break;
}
else
{
int temp = (int)(s[i] - '0');
size += temp * (int)pow(10, n - i - 1);
}
}
return isD;
}
//路径分割
vector<string> FileSystem::split_path(string filepath)
{
vector<string> res_s;
//路径拆分
char *fip = (char *)filepath.c_str();
char *p = strtok(fip, "/");
string tmp;
while (p != NULL)
{
tmp = p;
res_s.push_back(tmp);
p = strtok(NULL, "/");
}
return res_s;
}
//判断并确定当前工作目录对应的inode节点,要求的输入路径
Inode FileSystem::find_inode_from_path(vector<string> res_s, Inode tmp_inode)
{
Inode res_inode;
bool if_not_exist = false;
for (int i = 0; i < res_s.size(); ++i)
{
int d = -1, offset = -1;
vector<File> dirFile = search_file_by_direct_address(tmp_inode, res_s[i], d, offset);
if (dirFile.size() == 0)
{
vector<File> indirFile = search_file_by_indirect_address(tmp_inode, res_s[i], d, offset);
if (indirFile.size() > 0)
{
File tmp = indirFile[0];
tmp_inode = read_inode(tmp.get_InodeID());
}
}
else
{
int p = dirFile[0].get_InodeID();
tmp_inode = read_inode(p);
}
if (d < 0 || offset < 0)
{
if_not_exist = true;
break;
}
}
if (!if_not_exist)
res_inode = tmp_inode;
return res_inode;
}
//通过inode在direct address下找目标File类,如果filenanme为"",则寻找该inode下的所有类,否则返回与filename名字相同的File类
vector<File> FileSystem::search_file_by_direct_address(Inode inode, string filename, int &block_addr, int &offset)
{
block_addr = -1, offset = -1;
vector<File> res_file;
bool tar = true;
if (filename == "")
{
tar = true;
}
else
tar = false;
int addr;
int size = 0; //记录当前在一个data block中已经遍历的File数目
bool haveFile = false;
for (int j = 0; j < 10; ++j)
{
if (inode.get_direct_block_address()[j] == -1)
continue;
addr = inode.get_direct_block_address()[j];
// inode对应的Dir类读取
while (size < 1024)
{
File test_f;
file.seekg(addr * 1024 + size, ios::beg);
file.read((char *)&test_f, sizeof(File));
if (tar)
{
if (test_f.get_InodeID() >= 0)
res_file.push_back(test_f);
}
else
{
//找到对应的文件信息
if (test_f.get_FileName() == filename)
{
haveFile = true;
block_addr = addr * 1024;
offset = size;
res_file.push_back(test_f);
break;
}
}
size += sizeof(File);
}
if (haveFile)
break;
size = 0;
}
return res_file;
}
//通过inode在indirect address下找目标File类,如果filenanme为"",则寻找该inode下的所有类,否则返回与filename名字相同的File类
vector<File> FileSystem::search_file_by_indirect_address(Inode inode, string filename, int &block_addr, int &offset)
{
block_addr = -1, offset = -1;
vector<File> res_file;
bool tar = true, haveFile = false;
int size = 0, dir_tmp;
if (filename == "")
{
tar = true;
}
else
tar = false;
dir_tmp = inode.get_indirect_block_address();
if (dir_tmp != -1)
{
for (int d = 0; d < 256; ++d)
{
file.seekg(dir_tmp * 1024 + d * 4, ios::beg);
file.read((char *)&dir_tmp, sizeof(int));
if (dir_tmp != -1)
{
while (size < 1024)
{
File test_f;
file.seekg(dir_tmp * 1024 + size, ios::beg);
file.read((char *)&test_f, sizeof(File));
// test_f = read_fileinfo(dir_tmp * 1024 + size);
if (tar)
{
if (test_f.get_InodeID() >= 0)
res_file.push_back(test_f);
}
else
{
if (test_f.get_FileName() == filename)
{
haveFile = true;
block_addr = dir_tmp * 1024;
offset = size;
res_file.push_back(test_f);
break;
}
}
size += sizeof(File);
}
}
//跳出256个循环
if (haveFile)
break;
size = 0;
}
}
return res_file;
}
//往新建的文件里写入xx字节的数据
void FileSystem::write_random_string_to_file(int block_id)
{
file.seekp(block_id * BLOCK_SIZE, ios::beg);
file.write((char *)&buffer, sizeof(buffer));
}
//往目录中加入文件名和文件id
void FileSystem::add_file_to_dir(File &file_data, Inode &inode)
{
int byte = inode.get_byte_size();
byte += sizeof(File);
inode.set_byte_size(byte);
int addr[10];
memset(addr, -1, sizeof(addr));
memcpy(addr, inode.get_direct_block_address(), 10 * 4);
int file_count = inode.get_file_count();
//因为删除File类后,原来存储的位置变为空,需要搜索一下空置的部分,以便后面重新利用该部分
File fe;
bool find = false;
for (int a = 0; a < 10; ++a)
{
if (addr[a] == -1)
{
//找到了一个新块,要给这个新块地址
int add = find_free_block();
modify_block_bitmap(add);
superBlock.used_block_count++;
addr[a] = add;
inode.set_direct_block_address(addr);
}
file.seekg(addr[a] * BLOCK_SIZE + 0 * FILE_SIZE, ios::beg);
for (int b = 0; b < FILE_PER_BLOCK; ++b)
{
file.read((char *)&fe, sizeof(File));
//找到空闲的File类
if (fe.get_InodeID() <= 0)
{
file.seekp(addr[a] * BLOCK_SIZE + b * FILE_SIZE, ios::beg);
file.write((char *)&file_data, sizeof(File));
find = true;
break;
}
}
if (find)
break;
}
//如果没找到,说明要去间接地址里放
if (!find)
{
int indirect = inode.get_indirect_block_address();
if (indirect == -1)
{
int add = find_free_block();
modify_block_bitmap(add);
superBlock.used_block_count++;
indirect = add;
inode.set_indirect_block_address(add);
//加入地址块中的第一个地址
file.seekp(indirect * BLOCK_SIZE, ios::beg);
int first_add = find_free_block();
modify_block_bitmap(first_add);
superBlock.used_block_count++;
file.write((char *)&first_add, sizeof(int));
//往第一个地址指向的块中添加File_data
file.seekp(first_add * BLOCK_SIZE, ios::beg);
file.write((char *)&file_data, sizeof(FILE));
}
else
{
int in_addr[256];
file.seekg(indirect * BLOCK_SIZE, ios::beg);
for (int i = 0; i < 256; i++)
{
file.read((char *)&in_addr[i], sizeof(int));
}
bool isFound = false;
for (int i = 0; i < 256; i++)
{
file.seekg(in_addr[i] * BLOCK_SIZE, ios::beg);
for (int j = 0; j < FILE_PER_BLOCK; j++)
{
File fdata;
file.read((char *)&fdata, sizeof(File));
if (fdata.get_InodeID() <= 0)
{
//找到了,加进去
file.seekp(in_addr[i] * BLOCK_SIZE + j * FILE_SIZE, ios::beg);
file.write((char *)&file_data, sizeof(FILE));
isFound = true;
break;
}
}
if (isFound)
break;
}
}
}
file_count++;
inode.set_file_count(file_count);
inode.set_modified_time(time(0));
//把inode写回去
save_inode(inode);
}
//返回成功创建的信息或错误信息
std::string FileSystem::createFile(string filepath, int size)
{
Inode temp_cur_inode;
if (filepath[0] == '/')
{
temp_cur_inode = root_inode;
filepath = filepath.substr(1, filepath.length() - 1);
}
else
{
temp_cur_inode = cur_inode;
}
vector<string> dirs = split_path(filepath); //分割路径
string filename = dirs[dirs.size() - 1];
if (filename == "") //判断是否输入文件名
return "Please input the filename!";
//查找路径是否存在
dirs.erase(dirs.end() - 1);
Inode temp = find_inode_from_path(dirs, temp_cur_inode);
if (temp.get_id() == -1) //路径不存在
return "Path is not exist!";
else if (temp.get_file_type() == FILE_TYPE) //路径中含有文件名
return "There is a file with same name!";
else //路径存在
temp_cur_inode = temp;
//判断文件名存不存在
vector<string> n = {filename};
Inode temp2 = find_inode_from_path(n, temp_cur_inode);
if (temp2.get_id() != -1)
return "File has been exist!";
//判断inode还有没有得用
if (superBlock.inode_count == superBlock.used_inode_count)
{
return "You have created too much file! Please delete some files and try again.";
}
//判断目录下还有没有空间
if (temp_cur_inode.get_file_count() >= MAX_FILE_COUNT_PER_DIRECTORY)
{
return "You have created too much file in current path! Please delete some and retry.";
}
//判断data block里剩余空间还够不够
if (superBlock.block_count == superBlock.used_block_count)
{
return "You have created too much file! Please delete some files and try again.";
}
//创建File对象写进目录里
File file_data;
char filen[MAX_FILENAME_LENGTH];
strcpy(filen, filename.c_str());
file_data.set_FileName(filen);
//根据bitmap寻找空闲的inode
int id = find_free_inode();
file_data.set_InodeID(id);
modify_inode_bitmap(id); //修改bitmap位图
//把文件名和inode写进当前目录(目录也会被更新后写进文件系统)
add_file_to_dir(file_data, temp_cur_inode);
//根据bitmap中block的使用情况来分配block
int addr[10];
memset(addr, -1, sizeof(addr)); //直接地址10个全部初始化为-1
int indirect_addr = -1;
vector<int> indirect;
for (int i = 0; i < size; i++)
{
if (i < 10)
{
addr[i] = find_free_block();
modify_block_bitmap(addr[i]); //修改block bitmap
}
else
{
int temp_block_id = find_free_block();
indirect.push_back(temp_block_id);
modify_block_bitmap(temp_block_id);
}
}
for (int i = 0; i < min(10, size); i++)
{
write_random_string_to_file(addr[i]); //写入随机数据
}
if (!indirect.empty()) //如果间接地址块有被用到
{
int temp_block_id = find_free_block(); //寻找一块空闲block存放间接地址指向的256个地址
indirect_addr = temp_block_id;
modify_block_bitmap(temp_block_id);
//把vector里的地址数据添进indirect_addr所指向的data block
file.seekp(indirect_addr * BLOCK_SIZE, ios::beg);
for (int i = 0; i < indirect.size(); i++)
{
file.write((char *)&indirect[i], sizeof(int));
}
//把地址对应的data block写上随机数据
for (int i = 0; i < indirect.size(); i++)
{
write_random_string_to_file(indirect[i]);
}
}
//创建Inode对象并写进inode table里
Inode finode;
finode.set_id(id);
finode.set_byte_size(size * 1024); //把KB转化成B
finode.set_created_time(time(0));
finode.set_modified_time(time(0));
finode.set_file_type(FILE_TYPE);
finode.set_direct_block_address(addr);
finode.set_indirect_block_address(indirect_addr);
//把文件对应的inode写进inode table里
file.seekp(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE + id * INODE_SIZE, ios::beg);
file.write((char *)&finode, sizeof(Inode));
// superblock更新
superBlock.used_block_count += size;
superBlock.used_inode_count += 1;
return "Create successfully!";
}
//创建新目录
string FileSystem::createDir(string dirPath)
{
Inode temp_cur_inode;
//查找路径存不存在
if (dirPath[0] == '/')
{
temp_cur_inode = root_inode;
dirPath = dirPath.substr(1, dirPath.length() - 1);
}
else
{
temp_cur_inode = cur_inode;
}
//分割路径和目录名
vector<string> dirs = split_path(dirPath);
string dirname = dirs[dirs.size() - 1];
//没有输入目录名
if (dirname == "")
{
return "Please input the directory name!";
}
// cout<<"dirs"<<endl;
// for(int i = 0; i < dirs.size(); i++)
// {
// cout<<dirs[i]<<" ";
// }
// cout<<endl;
//开始查找路径是否存在
dirs.erase(dirs.end() - 1);
Inode temp = find_inode_from_path(dirs, temp_cur_inode);
if (temp.get_id() == -1)
return "Path is not exist!";
else
temp_cur_inode = temp;
//判断要创建的目录名存不存在
vector<string> n = {dirname};
Inode temp2 = find_inode_from_path(n, temp_cur_inode);
if (temp2.get_id() != -1)
if (temp2.get_file_type() == FILE_TYPE)
return "There has been a file with the same name!";
else
return "Directory has been exist!";
//判断inode还有没有得用
if (superBlock.inode_count == superBlock.used_inode_count)
{
return "You have created too many files! Please delete some files and try again.";
}
//判断目录下还有没有空间
if (temp_cur_inode.get_file_count() >= MAX_FILE_COUNT_PER_DIRECTORY)
{
return "You have created too many files in current path! Please delete some and retry.";
}
//判断data block里剩余空间还够不够
if (superBlock.block_count == superBlock.used_block_count)
{
return "You have created too many files! Please delete some files and try again.";
}
//开始创建目录
File file_data;
char dirn[MAX_FILENAME_LENGTH];
strcpy(dirn, dirname.c_str());
file_data.set_FileName(dirn);
//根据bitmap寻找没用到的inode
int id = find_free_inode();
file_data.set_InodeID(id);
modify_inode_bitmap(id);
//把目录名和inode写进当前目录(目录也会被更新后写进文件系统)
add_file_to_dir(file_data, temp_cur_inode);
// superblock更新
superBlock.used_inode_count += 1;
//创建dir node对应的inode
Inode dinode;
dinode.set_file_type(DIRECTORY_TYPE);
dinode.set_id(id);
//把inode写进去
file.seekp(BLOCK_SIZE + INODE_BITMAP_SIZE + BLOCK_BITMAP_SIZE + id * INODE_SIZE, ios::beg);
file.write((char *)&dinode, sizeof(Inode));
return "Create successfully!";
}
//删除关于inode下所有块的相关信息
void FileSystem::delete_blockinfo_for_inode(Inode tar_node)
{
//地址搜寻
int dir_addr;
int indir_addr;
for (int i = 0; i < 10; ++i)
{
//可以判断一下是否有存地址信息
dir_addr = tar_node.get_direct_block_address()[i];
if (dir_addr != -1)
{
//移动到对应的datablock位置,进行清空
file.seekp(dir_addr * BLOCK_SIZE, ios::beg);
file.write(buffer, sizeof(buffer));
// block_bitmap置0
modify_block_bitmap(dir_addr);
}
}
//清空存储间接地址的datablock及其相关内容
indir_addr = tar_node.get_indirect_block_address();
//先清空内容
if (indir_addr != -1 && indir_addr != 0)
{
file.seekg(indir_addr * BLOCK_SIZE, ios::beg);
vector<int> inres;
for (int i = 0; i < 256; ++i)
{
int tmp;
file.read((char *)&tmp, sizeof(int));
if (tmp > 0)
inres.push_back(tmp);
}
for (int j = 0; j < inres.size(); ++j)
{
file.seekp(inres[j] * BLOCK_SIZE, ios::beg);
file.write(buffer, sizeof(buffer));
modify_block_bitmap(inres[j]);
}
//再清空地址
file.seekp(indir_addr * BLOCK_SIZE, ios::beg);
modify_block_bitmap(indir_addr);
file.write(buffer, sizeof(buffer));
}
int id = tar_node.get_id();
file.seekp(1024 + id, ios::beg);
//清空inode table
Inode ino;
file.write((char *)&ino, sizeof(Inode));
modify_inode_bitmap(id);
}
void FileSystem::check_and_modify_inode(bool is_direct, int file_location, Inode &inode)
{
file.seekg(file_location, ios::beg);
bool ifempty = true;
for (int i = 0; i < 1024; i += sizeof(File))
{
//检查读出的文件是否存在
File check;
file.read((char *)&check, sizeof(File));
if (check.get_InodeID() > 0)
{
ifempty = false;
break;
}
}
//此块为空
if (ifempty)
{
//修改此块对应的blcok_bitmap
modify_block_bitmap(file_location / 1024);
superBlock.used_block_count -= 1;
//修改final_inode1对应的地址为-1
//如果是直接地址的话
if (is_direct)
{
int find_direct[10] = {0};
for (int i = 0; i < 10; ++i)
{
find_direct[i] = inode.get_direct_block_address()[i];
if (find_direct[i] == file_location / 1024)
{