-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_system.cpp
1407 lines (1305 loc) · 47.1 KB
/
file_system.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
//
// Created by selman.ozleyen2017 on 17.05.2020.
//
#include <fstream>
#include "file_system.h"
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <utility>
using namespace std;
const char file_system::months[][4]= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* Assumes the parameters are correct */
file_system::file_system(size_t block_size, size_t inode_count) {
inodes.resize(inode_count);
sb.inode_count = (uint16_t)inode_count;
sb.free_inode_count = ((uint16_t)inode_count) - 1;
sb.block_size = block_size;
sb.inode_pos = 1;
block_size_byte = KB * block_size;
node_cap = block_size_byte / 2 - 1;
block_cap = node_cap + 1;
size_t inodes_block_count = ceil(((double)inode_size * inode_count) / ((double)block_size_byte));
size_t inodes_pos_end = sb.inode_pos + inodes_block_count;
sb.root_dir_address = inodes_pos_end;
size_t total_blocks = (KB) / block_size;
// After filling inodes
size_t last_free_block = total_blocks - 2;
// position of the first free block
size_t fb_pos = sb.root_dir_address + 1;
size_t node_address_count = block_size_byte / 2 - 2;
for (size_t i = fb_pos, j = 0; i < last_free_block + 1; ++i, ++j) {
if (j == node_address_count + 1) {
j = 0;
last_free_block--;
}
}
sb.fb_head = total_blocks - 1;
sb.fb_tail = last_free_block + 1;
sb.fb_count = total_blocks - fb_pos;
//fill root dir inode
if (fb_pos > sb.fb_tail || sb.fb_count < 1)
throw invalid_argument("I-node count is too big.");
init_inode(0);
// one for . and one for ..
inodes[0].size = data_block::dir_entry_size*2;
inodes[0].ba[0] = sb.root_dir_address;
inodes[0].type = dir_type;
inodes[0].link_count = 1;
}
void file_system::create_file(const char* filename_arg) {
/* Initial Layout Order: SB -> INODES -> ROOT_DIR -> FREE BLOCKS -> FREE_BLOCKS_LIST*/
ofstream file;
file.open(filename_arg, ios::binary | ios::out);
file.exceptions ( std::ios::failbit | std::ios::badbit );
file.write((char*)&sb, sizeof(superblock));
if (sizeof(superblock) < block_size_byte) {
// Note: Won't work on machines where char is not 1 byte.
size_t remaining = block_size_byte - sizeof(superblock);
char* temp = new char[remaining];
file.write(temp, remaining);
delete[] temp;
}
const inode* iarr = inodes.data();
size_t inode_blks = sb.root_dir_address - sb.inode_pos;
if (sb.root_dir_address < sb.inode_pos)
throw std::length_error("Couldn't calculate inode_blocks.");
file.write((char*)iarr, sizeof(inode) * sb.inode_count);
if (inode_blks * block_size_byte > sizeof(inode) * sb.inode_count) {
size_t remaining = inode_blks * block_size_byte - sizeof(inode) * sb.inode_count;
char* temp = new char[remaining];
file.write(temp, remaining);
delete[] temp;
}
// Going to write block by block the free block nodes
char * zero_chars = new char[block_size_byte];
for (size_t i = 0; i < block_size_byte;i++) {
zero_chars[i] = 0;
}
size_t cap = block_size_byte / 2 - 1;
// Root directory data block currently has . and .. dir entries
data_block temp(zero_chars,0,block_size_byte,sb.root_dir_address);
init_directory(temp,0,0);
file.write((char*)temp.arr, block_size_byte);
data_block zero(zero_chars,0,block_size_byte,0);
// Root directory is written it turn for writing free blocks
for (size_t i = sb.root_dir_address + 1; i < sb.fb_tail; ++i) {
file.write((char*)zero.arr, block_size_byte);
}
// this is the address for the first free block
size_t j = sb.root_dir_address + 1;
// Now writing the free block nodes
for (uint16_t i = sb.fb_tail; i <= sb.fb_head; ++i) {
data_block temp1(zero_chars,0,block_size_byte,0);
for (size_t k = 0; k < cap; k++) {
if (sb.fb_tail > j)
temp1.push_address(j++);
else
break;
}
if (i != sb.fb_head)
temp1.set_address(node_cap,i+1);
file.write(temp1.arr, block_size_byte);
}
delete[] zero_chars;
file.close();
}
void file_system::init_inode(size_t i) {
set_inode_time(i);
inodes[i].type = empty_type;
inodes[i].size = 0;
inodes[i].link_count = 0;
inodes[i].si = 0;
inodes[i].di = 0;
inodes[i].ti = 0;
for (auto & j : inodes[i].ba){
j = 0;
}
}
std::vector<char> file_system::create_dir_entry(uint16_t index,const std::string& dirname) const {
if(dirname.empty())
throw invalid_argument("Please enter a valid file/directory name.");
if (dirname.size() > 6)
throw invalid_argument("Directory/File name cannot be longer than 6 characters.");
if ((dirname.find('/') != string::npos) || (dirname.find(' ') != string::npos))
throw invalid_argument("Directory/File name is invalid.");
vector<char> res{0,0,0,0,0,0,0,0};
res[1] = (uint8_t)(index % data_block::one_byte);
res[0] = (uint8_t)((index >> 8) % data_block::one_byte);
size_t i = 0;
for(char s: dirname)
res[2+(i++)] = s;
for (i = i+2; i < data_block::dir_entry_size; ++i)
res[i] = 0;
return res;
}
//size will be evaluated with its first 24 bits
void file_system::add_inode_size(size_t index, uint32_t size)
{
inode & i = inodes[index];
i.size+=size;
}
file_system::file_system(const char* filename) {
this->filename = filename;
fstream file(filename,ios::binary| ios::out | ios::in);
file.exceptions ( std::ios::failbit | std::ios::badbit );
// reading the superblock
file.read((char*)&sb, sizeof(sb));
block_size_byte = (sb.block_size << 10);
node_cap = block_size_byte / 2 - 1;
block_cap = node_cap + 1;
//reading inodes
file.seekg(sb.inode_pos * block_size_byte);
auto* temp_inodes = new inode[sb.inode_count];
file.read((char*)temp_inodes, ((size_t)sb.inode_count)* ((size_t) inode_size));
for (size_t i = 0; i < sb.inode_count; ++i) {
inodes.emplace_back(temp_inodes[i]);
}
delete[] temp_inodes;
file.close();
}
uint16_t file_system::get_dir_inode(std::string path) {
if (path == "/")
return 0;
// remove the front '/'
path = path.erase(0,1);
return get_dir_inode_helper(path, inodes[0]);
}
uint16_t file_system::get_dir_inode_helper(std::string& path,const inode& i) {
size_t slash_i = path.find('/');
//load the blocks that inode is referring to
//resets inode blocks
inode_blocks.clear();
load_inode_blocks(i);
// if we are on the last level
bool last_level = string::npos == slash_i || path.size() == slash_i + 1;
string searched = path;
if (last_level) {
//if it is a directory remove the last '/'
if (path.size() == slash_i + 1) {
searched.pop_back();
}
}
else {
// select next dir name
searched = string(path, 0, slash_i);
// delete the selected one
path = path.substr(slash_i, path.size() - slash_i);
}
for (auto& in : inode_blocks) {
for (size_t j = 0; j < in.get_dir_entry_count(); ++j) {
if (in.get_entry_name(j) == searched) {
if (last_level) {
//resets inode blocks
size_t res = in.get_entry_inode_no(j);
inode_blocks.clear();
return res;
}
else {
string new_path = string(path,1,path.size());
return get_dir_inode_helper(new_path, inodes[in.get_entry_inode_no(j)]);
}
}
}
}
throw invalid_argument("No such directory.");
}
// changes inode blocks
void file_system::load_inode_blocks(inode i) {
size_t size = get_inode_size(i);
auto block_count = (size_t) ceil((double)size / (double)block_size_byte);
size_t rem_block_count = block_count;
size_t j = 0;
for (j = 0; j < direct_count && rem_block_count > 1; ++j) {
load_by_block_no(i.ba[j], block_size_byte);
inode_blocks.emplace_back(temp_blocks.back());
temp_blocks.pop_back();
size -= block_size_byte;
rem_block_count--;
}
if (rem_block_count == 1 && j < direct_count) {
load_by_block_no(i.ba[j], size);
inode_blocks.emplace_back(temp_blocks.back());
temp_blocks.pop_back();
return;
}
if (rem_block_count == 0)
return;
// if there is still blocks to load use indirect blocks
if (i.si == 0)
throw logic_error("I-node structure and the attributes doesn't match");
load_inode_blocks_helper(i.si, &size, &rem_block_count, 1);
if (rem_block_count == 0)
return;
// If there is still blocks remaining use double indirect blocks
if (i.di == 0)
throw logic_error("I-node structure and the attributes doesn't match");
load_inode_blocks_helper(i.di, &size, &rem_block_count, 2);
if (rem_block_count == 0)
return;
// If there is still blocks remaining use double indirect blocks
if (i.ti == 0)
throw logic_error("I-node structure and the attributes doesn't match");
load_inode_blocks_helper(i.ti, &size, &rem_block_count, 3);
if (rem_block_count == 0)
return;
}
void file_system::load_by_block_no(size_t bno, size_t size = 0) {
ifstream file(filename,ios::binary| ios::in);
file.exceptions ( std::ios::failbit | std::ios::badbit );
file.seekg(bno*block_size_byte);
char* temp = new char[block_size_byte];
file.read(temp, block_size_byte);
file.close();
data_block res(temp, size, block_size_byte, bno);
temp_blocks.emplace_back(res);
delete[] temp;
}
void file_system::write_block(const data_block& b) const
{
ofstream file(filename,ios::binary| ios::out | ios::in);
file.exceptions ( std::ios::failbit | std::ios::badbit );
file.seekp(b.bno*block_size_byte);
file.write((char *)b.arr, block_size_byte);
file.close();
}
void file_system::write_superblock()
{
ofstream file(filename, ios::binary | ios::out | ios::in );
file.exceptions ( std::ios::failbit | std::ios::badbit );
file.write((char *)&sb, sizeof(sb));
file.close();
}
void file_system::write_inode(uint16_t ino)
{
// find which block ino is in
size_t ino_addr = ino*sizeof(inode) + sb.inode_pos * block_size_byte;
fstream file(filename, ios::binary| ios::out | ios::in);
file.exceptions ( std::ios::failbit | std::ios::badbit );
file.seekp(ino_addr);
file.write((char *)&inodes[ino],sizeof(inode));
file.close();
}
uint32_t file_system::get_inode_size(const inode& i) {
return i.size;
}
void file_system::load_inode_blocks_helper(size_t bno, size_t* size, size_t* rem_blocks, size_t level) {
if (*rem_blocks == 0) {
return;
}
if (level == 0) {
if (*rem_blocks == 0) {
load_by_block_no(bno, *size);
inode_blocks.push_back(temp_blocks.back());
temp_blocks.pop_back();
*rem_blocks = 0;
*size = 0;
return;
}
load_by_block_no(bno, block_size_byte);
inode_blocks.push_back(temp_blocks.back());
temp_blocks.pop_back();
*size -= block_size_byte;
*rem_blocks -= 1;
}
else {
load_by_block_no(bno, block_size_byte);
data_block temp = temp_blocks.back();
temp_blocks.pop_back();
for (size_t i = 0; i < block_cap && *rem_blocks > 0; ++i) {
load_inode_blocks_helper(temp.get_address(i), size, rem_blocks, level - 1);
}
}
}
void file_system::mkdir(const std::string& arg) {
size_t parent;
string name,path;
// check the parameter
new_file_args(arg, path, name, parent);
//get free inode
uint16_t newi = get_free_inode();
init_inode(newi);
data_block temp(block_size_byte);
init_directory(temp,newi,parent);
write(newi,0,data_block::dir_entry_size*2,temp.arr);
vector<char> dir_ent = create_dir_entry(newi,name);
//write the data blocks
uint32_t pos = get_inode_size(inodes[parent]);
write(parent,pos,data_block::dir_entry_size,dir_ent.data());
set_inode_time(parent);
add_inode_size(parent,data_block::dir_entry_size);
write_inode(newi);
write_inode(parent);
}
void file_system::write(uint16_t inode_index, uint32_t pos, uint32_t size,const char* buf)
{
//cursor for the buffer
size_t buf_pos = 0;
inode & in = inodes[inode_index];
// store the remaining block to write the remainder to a block specially
size_t cur_block;
size_t file_size = get_inode_size(inodes[inode_index]);
if(file_size >= max_file_size)
throw std::runtime_error("File is too large.");
if(pos > file_size){
throw std::runtime_error("File point cannot be greater than size.");
}
size_t off_di = block_cap*block_size_byte + direct_count*block_size_byte;
size_t off_ti = off_di + block_cap*block_cap*block_size_byte;
if((pos >> (10 + sb.block_size)) < direct_count+1){
cur_block = pos >> (10 + sb.block_size);
}
// if it starts from double indirect
else if( (pos >= off_di) && (pos - off_di) <
(block_cap*block_cap*block_size_byte))
{
cur_block = direct_count;
}
// if it starts from triple indirect
else if((pos >= off_ti) && (pos - off_ti) <
(block_cap*block_cap*block_cap*block_size_byte))
{
cur_block = direct_count+1;
}
else{
throw runtime_error("Position is too large.");
}
// block by block operation
for (; cur_block < direct_count && size > 0; cur_block++){
// if there are no blocks allocated yet
if(in.ba[cur_block] == 0){
//will write the inode later
in.ba[cur_block] = get_free_block();
}
load_by_block_no(in.ba[cur_block]);
data_block & temp = temp_blocks.back();
// assign the buf to the block
for (size_t i = pos % block_size_byte; i < block_size_byte && size > 0 ; ++i, ++pos){
temp.arr[i] = buf[buf_pos++];
size--;
}
// put it to the buffer to write
inode_blocks.emplace_back(temp);
temp_blocks.pop_back();
}
// if the job is done it can exit
if(size == 0){
write_inode_blocks_buffer();
write_inode(inode_index);
write_superblock();
return;
}
// now it is part for the recursive indirect part
// if there is none allocate
size_t off = direct_count*block_size_byte;
size_t wb_size = write_buffer_size;
if(cur_block == direct_count){
if(in.si == 0){
in.si = get_free_block();
data_block zeros(block_size_byte);
zeros.bno = in.si;
write_block(zeros);
}
size_t rel_block = ((pos - off) >> (10 + sb.block_size)) % block_size_byte;
write_helper(in.si,&pos,&size,buf,rel_block,1,off,&wb_size,&buf_pos);
cur_block++;
}
// if the job is done it can exit
if(size == 0){
write_inode_blocks_buffer();
write_inode(inode_index);
write_superblock();
return;
}
if(cur_block == direct_count+1){
if(in.di == 0){
in.di = get_free_block();
}
off += block_size_byte*block_size_byte/2;
size_t rel_block = ((pos - off) >> 2*(10 + sb.block_size)) % block_size_byte;
if(pos < off)
throw overflow_error("Overflow happened probably file is too big.");
write_helper(in.di,&pos,&size,buf,rel_block,2,off,&wb_size,&buf_pos);
cur_block++;
}
// if the job is done it can exit
if(size == 0){
write_inode_blocks_buffer();
write_inode(inode_index);
write_superblock();
return;
}
if(cur_block == 9){
if(in.ti == 0){
in.ti = get_free_block();
}
off += block_size_byte*block_size_byte*block_size_byte/4;
size_t rel_block = ((pos - off) >> 3*(10 + sb.block_size)) % block_size_byte;
if(pos < off)
throw overflow_error("Overflow happened probably file is too big.");
write_helper(in.ti,&pos,&size,buf,rel_block,3,off,&wb_size,&buf_pos);
}
write_superblock();
write_inode(inode_index);
}
void file_system::write_helper(uint16_t address, uint32_t * pos, uint32_t * size,
const char * buf, size_t rel_block, size_t level,size_t off,size_t* wb_size,size_t * buf_pos)
{
if(*size == 0)
return;
if(level == 0){
load_by_block_no(address);
data_block &temp = temp_blocks.back();
// assign the buf to the block
for (size_t i = *pos % block_size_byte; i < block_size_byte && *size > 0 ; ++i){
temp.arr[i] = buf[(*buf_pos)++];
++(*pos);
--(*size);
}
// put it to the buffer to write
inode_blocks.emplace_back(temp);
temp_blocks.pop_back();
--(*wb_size);
// if it is time write the buffer
if(*wb_size == 0){
write_inode_blocks_buffer();
*wb_size = write_buffer_size;
}
return;
}
else{
load_by_block_no(address);
data_block temp = temp_blocks.back();
temp_blocks.pop_back();
size_t block_count = block_size_byte >> 1;
uint16_t next_add = 0;
size_t new_rel = 0;
for (; rel_block < block_count; rel_block++)
{
next_add = temp.get_address(rel_block);
if(!next_add){
next_add = get_free_block();
temp.set_address(rel_block, next_add);
inode_blocks.emplace_back(temp);
++(*wb_size);
}
if(level > 1){
new_rel = ((*pos - off) >> (level-1)*sb.block_size) % block_size_byte;
}
write_helper(next_add,pos,size,buf,new_rel,level-1,off,wb_size,buf_pos);
if(*size == 0)
return;
}
}
}
void file_system::write_inode_blocks_buffer()
{
for (const auto& in : inode_blocks)
write_block(in);
inode_blocks.clear();
}
uint16_t file_system::get_free_inode() {
size_t i = 0;
for (i = 0; i < inodes.size(); ++i) {
if(inodes[i].type == empty_type){
sb.free_inode_count--;
init_inode(i);
inodes[i].type = file_type;
write_inode(i);
write_superblock();
return i;
}
}
throw underflow_error("No empty inode left.");
}
void file_system::put_free_inode(uint16_t index)
{
// get the tail block
inodes[index].type = empty_type;
sb.free_inode_count++;
write_superblock();
write_inode(index);
}
uint16_t file_system::get_free_block()
{
if (sb.fb_head == 0 || sb.fb_count == 0) {
write_superblock();
throw length_error("No more free blocks left.");
}
uint16_t res = 0;
load_by_block_no(sb.fb_tail);
data_block& fb = temp_blocks.back();
size_t free_blocks = fb.get_fb_size();
fb.size = free_blocks*2;
sb.fb_count--;
if (free_blocks == 0) {
if(sb.fb_tail == sb.fb_head){
sb.fb_tail = 0;
sb.fb_head = 0;
}
else{
sb.fb_tail = fb.get_address(node_cap);
}
res = fb.get_bno();
}
else {
res = fb.pop_address();
write_block(fb);
}
temp_blocks.pop_back();
write_superblock();
if(res > KB/sb.block_size)
throw logic_error("Error returning a free block no.");
return res;
}
void file_system::put_free_block(uint16_t bno)
{
if(bno > KB/sb.block_size)
throw invalid_argument("Given free block no is invalid.");
// if there is no free block make that the new free block list
if(sb.fb_count == 0){
data_block zeros(block_size_byte);
zeros.bno = bno;
sb.fb_tail = bno;
sb.fb_head = bno;
sb.fb_count++;
write_block(zeros);
write_superblock();
return;
}
load_by_block_no(sb.fb_tail);
data_block& fb = temp_blocks.back();
size_t fb_size = fb.get_fb_size();
fb.size = fb_size*2;
sb.fb_count++;
// If the tail is full then the
// block we are trying to put
// will be our new tail node
if(node_cap == fb_size){
temp_blocks.pop_back();
// change the tail to block bno
uint16_t temp = sb.fb_tail;
sb.fb_tail = bno;
load_by_block_no(bno);
fb = temp_blocks.back();
fb.clear_block();
fb.set_address(node_cap,temp);
write_block(fb);
}
else{
fb.push_address(bno);
write_block(fb);
}
write_superblock();
temp_blocks.pop_back();
}
void file_system::init_directory(data_block & db,uint16_t index, uint16_t parent){
// modifying the block
inodes[index].link_count = 1;
inodes[index].type = dir_type;
inodes[index].size = 2*data_block::dir_entry_size;
vector<char> dir_ent = create_dir_entry(index,".");
vector<char> dir_ent2 = create_dir_entry(parent,"..");
for (size_t l = 0; l < data_block::dir_entry_size; ++l) {
db.arr[l] = dir_ent[l];
db.arr[l+data_block::dir_entry_size] = dir_ent2[l];
}
}
// given path point to a folder
void file_system::list_folders(const std::string &path) {
inode_blocks.clear();
uint16_t path_inode = get_dir_inode(path);
// checking if it is a folder or not
if(inodes[path_inode].type != dir_type && inodes[path_inode].type != sym_dir)
throw std::invalid_argument("Given path doesn't point to a listable object.");
load_inode_blocks(inodes[path_inode]);
vector<string> names;
vector<size_t > inode_nos;
for(auto &in: inode_blocks){
size_t dir_count = in.get_dir_entry_count();
for (size_t j = 2; j < dir_count; ++j) {
names.push_back(in.get_entry_name(j));
inode_nos.push_back(in.get_entry_inode_no(j));
}
}
size_t i = 0;
inode * temp = nullptr;
const char * tempstr = nullptr;
for(const auto& line : names){
temp = &inodes[inode_nos[i]];
tempstr = string(line.data(),dir_name_size).append("\0").data();
printf("%7u %u %s %2u %.2d:%.2d:%.2d %s\n",temp->size,temp->year,
months[temp->month],temp->day,temp->hour,temp->min,temp->sec,tempstr);
i++;
}
fflush(stdout);
inode_blocks.clear();
}
void file_system::dumpe2fs() {
// block count
size_t block_count = KB/sb.block_size;
// inode count sb.inode_count
// fb count sb.free_blocks
// sb.block size
map<size_t,set<string>> nm;
// inode and the blocks it occupies
map<size_t,vector<size_t>> blk_map;
get_all_occupied_names_blocks(nm,blk_map);
// getting all free blocks
vector<size_t> fblocks;
get_all_free_blocks(fblocks,sb.fb_tail);
// getting all free inodes
vector<size_t> finodes;
size_t dir_count = 0;
get_all_free_inodes(finodes,&dir_count);
cout << GREEN << "Block Count: " << RESET << block_count << endl;
cout << GREEN << "Inode Count: " << RESET << sb.inode_count << endl;
cout << GREEN << "Free Block Count: " << RESET << sb.fb_count << endl;
cout << GREEN << "Free Inode Count: " << RESET << sb.free_inode_count << endl;
cout << GREEN "Number Of Files: " RESET<< blk_map.size() - dir_count << endl;
cout << GREEN "Number Of Directories: " RESET<< dir_count << endl;
cout << GREEN "Block Size (KB): " RESET<< sb.block_size << endl;
size_t j = 0;
cout << endl <<GREEN "Free Blocks: " RESET << " (" <<fblocks.size() << "): ";
for(auto i: fblocks){
cout << i << ", ";
j++;
if(j == 30){
cout << endl;
j = 0;
}
}
cout << endl << endl;
cout <<GREEN "Free Inodes:" RESET << " (" <<finodes.size() << "): ";
j = 0;
for(auto i: finodes){
cout << i << ", ";
j++;
if(j == 30){
cout << endl;
j = 0;
}
}
cout << endl << endl;
cout << GREEN "Occupied Inodes List: " RESET<< endl;
for(auto& in : blk_map){
cout << GREEN"-----------------------------" RESET << endl;
cout << "Inode: " << in.first << endl;
cout << "Occupied Blocks: ";
j = 0;
for(auto &ob: in.second){
cout << ob << ", ";
j++;
if(j == 30){
cout << endl;
j = 0;
}
}
cout << endl << "Occupied Names: ";
const char * tempstr = nullptr;
for(auto &on: nm[in.first]){
tempstr = string(on.data(),dir_name_size).append("\0").data();
cout << tempstr << ", ";
j++;
if(j == 30){
cout << endl;
j = 0;
}
}
cout << endl;
cout <<GREEN "-----------------------------" RESET<< endl;
}
}
void file_system::get_all_occupied_names_blocks(map<size_t, set<string>> &name_map,
map<size_t, vector<size_t>> &blk_map) {
// listing all occupied inodes
vector<size_t> all_dirs;
for (size_t i = 0; i < inodes.size(); ++i) {
if (inodes[i].type != empty_type){
if(inodes[i].type == file_system::dir_type)
all_dirs.push_back(i);
name_map[i] = set<string>();
blk_map[i] = vector<size_t>();
}
}
for (auto& dir: all_dirs){
//clears the inode blocks
inode_blocks.clear();
load_inode_blocks(inodes[dir]);
//iterates through all blocks
for (auto& in : inode_blocks) {
size_t dir_count = in.get_dir_entry_count();
for (size_t j = 2; j < dir_count; ++j) {
name_map[in.get_entry_inode_no(j)].insert(in.get_entry_name(j));
}
}
}
name_map[0].insert("/");
for(auto& pair: blk_map){
load_occupied_inode_blocks(pair.first,pair.second);
}
}
void file_system::get_all_free_blocks(std::vector<size_t> &res, size_t pos) {
if(pos == 0)
return;
res.push_back(pos);
load_by_block_no(pos);
data_block& temp = temp_blocks.back();
size_t address_count = temp.get_fb_size();
if(address_count == 0 && sb.fb_tail == pos){
if(sb.fb_head == sb.fb_tail){
temp_blocks.pop_back();
return;
}
size_t next = temp.get_address(node_cap);
res.push_back(pos);
temp_blocks.pop_back();
get_all_free_blocks(res,next);
}
size_t next = temp.get_address(node_cap);
for (size_t i = 0; i < address_count; ++i) {
res.push_back(temp.get_address(i));
}
temp_blocks.pop_back();
if(pos != sb.fb_head)
get_all_free_blocks(res,next);
}
void file_system::get_all_free_inodes(std::vector<size_t> &res,size_t * dir_count) {
for (size_t i = 0; i < inodes.size(); ++i) {
if (inodes[i].type == empty_type)
res.push_back(i);
else if(inodes[i].type == dir_type || inodes[i].type == sym_dir)
++(*dir_count);
}
}
void file_system::load_occupied_inode_blocks(size_t index, vector<size_t> &res) {
inode in = inodes[index];
for (uint16_t i : in.ba) {
if(i != 0)
res.push_back(i);
else
return;
}
load_occupied_inode_blocks_helper(index,res,in.si,1);
load_occupied_inode_blocks_helper(index,res,in.di,2);
load_occupied_inode_blocks_helper(index,res,in.ti,3);
}
void file_system::load_occupied_inode_blocks_helper(size_t index, vector<size_t> &res, size_t address, size_t level) {
if(address == 0)
return;
if(level == 0){
res.push_back(address);
}
else{
load_by_block_no(address);
res.push_back(address);
data_block blk = temp_blocks.back();
temp_blocks.pop_back();
for (size_t i = 0; i < block_cap; ++i) {
if(blk.get_address(i) != 0)
load_occupied_inode_blocks_helper(index,res,blk.get_address(i),level-1);
}
}
}
void file_system::copy_file(const std::string& path, const char * fname) {
ifstream file(fname, ios::binary| ios::in | ios::ate);
file.exceptions(std::ios::failbit | std::ios::badbit);
auto fsize = file.tellg();
if(fsize > max_file_size)
throw invalid_argument("Given file exceeds the size of the file system disk.");
vector<char> buf(fsize);
file.seekg(0);
file.read(buf.data(),fsize);
file.close();
// write this buffer to the given path
write_str_to_file(path, buf, false);
}
void file_system::write_str_to_file(const string &arg, std::vector<char> &buf, bool error_when_exist) {
string path,name;
size_t parent;
size_t block_needed = ceil(((double) buf.size())/((double) block_size_byte));
size_t si_block_needed = 0,di_block_needed = 0,ti_block_needed = 0;
if(block_needed > direct_count)
si_block_needed = ceil(((double)(block_needed - direct_count))/((double)block_cap));
if(si_block_needed > 1)
di_block_needed = ceil(((double)(si_block_needed - 1))/((double)block_cap));
if(di_block_needed > 1)
ti_block_needed = ceil(((double)(di_block_needed - 1))/((double)block_cap));
if(block_needed + si_block_needed + di_block_needed + ti_block_needed > sb.fb_count)
throw length_error("Given file is too big for the system.");
// sets these values
bool file_exists = new_file_args(arg, path, name, parent, error_when_exist);
if(file_exists){
// get the existing file
size_t child = get_dir_inode(arg);
size_t to_write = child;
if(inodes[child].type == sym_file){
vector<char>path_to_link(inodes[child].size);
copy_system_file_to_buf(child,path_to_link.data(),inodes[child].size);
to_write = get_dir_inode(path_to_link.data());
}
// set time for parent and child
set_inode_time(parent);
set_inode_time(to_write);
// update child size
empty_inode_blocks(to_write);
inodes[to_write].size = buf.size();
write(to_write,0,buf.size(),buf.data());
write_inode(parent);
write_inode(to_write);
}
else{
// now allocate inode and call write function
uint16_t newi = get_free_inode();
// set time for parent and child
set_inode_time(parent);
init_inode(newi);
//init file attributes
init_file(newi,buf.size());
write(newi,0,buf.size(),buf.data());
vector<char> dir_ent = create_dir_entry(newi,name);
//write the data blocks
uint32_t pos = get_inode_size(inodes[parent]);
write(parent,pos,data_block::dir_entry_size,dir_ent.data());
add_inode_size(parent,data_block::dir_entry_size);
write_inode(newi);
write_inode(parent);
}
}
bool file_system::new_file_args(const std::string &arg, std::string &path, std::string &name, size_t &parent,
bool error_when_exists) {
size_t last_slash = arg.find_last_of('/');
if (last_slash == string::npos)
throw invalid_argument("Please enter a full directory.");
path = string(arg, 0, last_slash + 1);
name = string(arg, last_slash + 1, arg.size() - last_slash);
if(name.empty())
throw invalid_argument("Please enter a valid file/directory name.");
if (name.size() > dir_name_size)
throw invalid_argument("File/Directory names should be at most 6 characters.");
if ((name.find('/') != string::npos) || (name.find(' ') != string::npos))
throw invalid_argument("File/Directory name is invalid.");
if (path.find(' ') != string::npos)
throw invalid_argument("Given path is invalid");
//first go to the location
if(last_slash != 0 && last_slash == (path.size() - 1) )
path.pop_back();
parent = get_dir_inode(path);
inode i = inodes[parent];
if (i.type != dir_type && i.type != sym_dir)
throw invalid_argument("File or directory doesn't exist.");
//clears the inode blocks
inode_blocks.clear();
load_inode_blocks(i);
//check if file name is valid
for (auto &in : inode_blocks) {
size_t dir_count = in.get_dir_entry_count();
for (size_t j = 0; j < dir_count; ++j) {
if (in.get_entry_name(j) == name) {
if(error_when_exists)
throw invalid_argument("File or directory name already exists.");
else
return true;
}
}
}
inode_blocks.clear();
return false;
}
void file_system::init_file(uint16_t index, size_t fsize) {
// modifying the block
inodes[index].link_count = 1;
inodes[index].type = file_type;
inodes[index].size = fsize;
}
void file_system::read_file(std::string path, const char *fname) {
size_t file_inode = get_dir_inode(std::move(path));
if(inodes[file_inode].type == sym_file){
vector<char>buf(inodes[file_inode].size);
copy_system_file_to_buf(file_inode,buf.data(),buf.size());
file_inode = get_dir_inode(buf.data());
}
size_t file_size = inodes[file_inode].size;
vector<char> buf(file_size);
copy_system_file_to_buf(file_inode,buf.data(),file_size);
ofstream file(fname,ios::out | ios::binary);
file.exceptions(std::ios::failbit | std::ios::badbit);
file.write(buf.data(),file_size);