-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext2.c
1616 lines (1506 loc) · 50.5 KB
/
ext2.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 <time.h>
#include <string.h>
#define block_size 1024 //data block 1024 bytes
#define s_block_size 64 //superblock 64 bytes
#define group_desc_size 32 //group description 32 bytes
#define inode_size 64 //inode 64 bytes
#define inode_table_size (1024 * 8 * 64) //inode table
#define inode_counts (1024 * 8) //inodes in total
#define block_counts (4 + 8 * 64 + 1024 * 8) //blocks in total
#define block_counts_per_block 256 //indirect data block
#define dir_counts_per_block 32 //dir counts
#define s_block_start 0 //superblock address
#define group_desc_start 1024 //group description address
#define block_bitmap_start 2048 //block bitmap address
#define inode_bitmap_start 3072 //inode bitmap address
#define inode_table_start 4096 //inode table address
#define data_block_start (1024 * (8 * 64 + 4)) //data block address
#define volume_name "lips apple"
#define passwd "lips"
#define u_8 unsigned char
#define u_16 unsigned short
#define u_32 unsigned int
typedef struct super_block //64 bytes
{
u_32 s_inodes_count; //inodes in total
u_32 s_blocks_count; //blocks in total
u_32 s_free_inodes_count; //free inodes in total
u_32 s_free_blocks_count; //free blocks in total
u_32 s_wtime; //last write access to the file system
char s_volume_name[16]; //volume name
u_8 s_pad[28];
} Super_Block;
typedef struct group_desc //32 bytes
{
u_32 bg_block_bitmap; //block bitmap address
u_32 bg_inode_bitmap; //inode bitmap address
u_32 bg_inode_table; //inode table address
u_32 bg_free_blocks_count; //free blocks in total
u_32 bg_free_inodes_count; //free inodes in total
u_32 bg_used_dirs_count; //dirctories used in total
u_8 bg_pad[8];
} Group_Desc;
typedef struct inode //64 bytes
{
u_16 i_mode; //indicate the access rights
u_32 i_size; //number of dir_entry * dir_entry size
u_32 i_atime; //access time
u_32 i_ctime; //create time
u_32 i_mtime; //modify time
u_32 i_dtime; //delete time
u_16 i_blocks; //data blocks
u_32 i_block[9];
//1~8 contains direct block number
//9 an indirect data block which contains direct block number
char i_pad[4];
} Inode;
typedef struct dir_entry //32 bytes each block contains 32 dir_entry
{
u_32 inode; //inode number
u_16 rec_len;
u_16 name_len; //file name lenth
u_8 file_type; //1 : file 2 : directory
char name[23]; //filename
} Dir_Entry;
u_32 fopen_table[16]; //contains inode number
char file_open_table[16][256]; //contasns file absolute path
char file_absolute_path[256]; //file absolute path
char current_path[256]; //current directory
char current_ttime[32]; //current time
u_32 last_alloc_inode = 1; //last allocated inode number "/" took the first one
u_32 last_alloc_block = 0; //last allocated block number
u_32 current_dir_inode; //current directory inode number
u_8 current_dirlen; //cureent directory path lenth
Super_Block s_block_buff[1]; //superblock buffer
Group_Desc gdt_buff[1]; //group description buffer
Inode inode_buff[1]; //inode buffer
Dir_Entry dir[32]; //directory enrty buffer
u_8 block_bitbuff[1024]; //block bitmap buffer
u_8 inode_bitbuff[1024]; //inode bitmap buffer
u_32 indirect_block_buff[256]; //indireact block buffer
char block_buff[1024]; //block buffer
char file_buff[264 * 1024]; //file buffer
FILE *fp; //a virtul disk pointer
void upload_super_block(); //upload super block
void load_super_block(); //load super block
void upload_group_desc(); //upload group description
void load_group_desc(); //load group description
void upload_block_bitmap(); //upload block bitmap
void load_block_bitmap(); //load block bitmap
void upload_inode_bitmap(); //upload inode bitmap
void load_inode_bitmap(); //load inode bitmap
void upload_dir(u_32 block_num); //upload data block
void load_dir(u_32 block_num); //load data block
void upload_block(u_32 block_num); //upload data block
void load_block(u_32 block_num); //load data block
void upload_inode(u_32 inode_num); //upload inode
void load_inode(u_32 inode_num); //load inode
u_32 alloc_block(); //allocate a block, return block number
u_32 alloc_inode(); //allocate an inode, return inode number
void remove_block(u_32 block_num); //remove a block
void remove_inode(u_32 inode_num); //remove an inode
u_32 current_time(); //set current time
char *time_now(u_32 t); //change time to string
void init_disk(); //initiate a disk
//arg=1:find an empty directory, arg=2:this block is empty?
int search_dir_entry(u_32 block_num, int arg);
//help fucntion search to finish seaching
int search_help(char filename[23], u_8 *type, u_32 tblock_num,
u_32 *dir_num, u_32 *inode_num);
//search a file or directory,return file type,block number,directory enrty number,inode number
int search(char filename[23], u_8 *type, u_32 *block_num,
u_32 *dir_num, u_32 *inode_num);
u_32 file_is_open(char filename[23]); //find a file in file open table
void init_dir(u_32 dir_inode, u_8 dir_len); //initiate a directory
void help(); //show help
void ls_help(u_32 block_num); //help function ls
void ls(); //show name,user...in current directory
void mkdir(char filename[23]); //make a directory
void rmdir(char filename[23]); //delete a directory
void cd(char filename[23]); //change to a directory
void touch(char filename[23]); //make a file
void rm(char filename[23]); //delete a file
void cat(char filename[23]); //show file content
void open(char filename[23]); //open file, add to file open table
void close(char filename[23]); //close file, delete from file open table
void read(char filename[23]); //read file
void write(char filename[23]); //write file
void re_name(char oldname[23], char newname[23]); //rename a file
void chmod(char filename[23], u_16 mode); //change a file's or a directory's mode
void login(); //login
void check(); //show disk infomation
void format(); //format the disk
void upload_super_block() //upload super block
{
fseek(fp, s_block_start, SEEK_SET); //set file pointer to super block address
fwrite(s_block_buff, s_block_size, 1, fp);
fflush(fp);
}
void load_super_block() //load super block
{
fseek(fp, s_block_start, SEEK_SET);
fread(s_block_buff, s_block_size, 1, fp);
}
void upload_group_desc() //upload group description
{
fseek(fp, group_desc_start, SEEK_SET);
fwrite(gdt_buff, group_desc_size, 1, fp);
fflush(fp);
}
void load_group_desc() //load group description
{
fseek(fp, group_desc_start, SEEK_SET);
fread(gdt_buff, group_desc_size, 1, fp);
}
void upload_block_bitmap() //upload block bitmap
{
fseek(fp, block_bitmap_start, SEEK_SET);
fwrite(block_bitbuff, block_size, 1, fp);
fflush(fp);
}
void load_block_bitmap() //load block bitmap
{
fseek(fp, block_bitmap_start, SEEK_SET);
fread(block_bitbuff, block_size, 1, fp);
}
void upload_inode_bitmap() //upload inode bitmap
{
fseek(fp, inode_bitmap_start, SEEK_SET);
fwrite(inode_bitbuff, block_size, 1, fp);
fflush(fp);
}
void load_inode_bitmap() //load inode bitmap
{
fseek(fp, inode_bitmap_start, SEEK_SET);
fread(inode_bitbuff, block_size, 1, fp);
}
void upload_dir(u_32 block_num) //upload data block
{
fseek(fp, data_block_start + block_num * block_size, SEEK_SET);
fwrite(dir, block_size, 1, fp);
fflush(fp);
}
void load_dir(u_32 block_num) //load data block
{
fseek(fp, data_block_start + block_num * block_size, SEEK_SET);
fread(dir, block_size, 1, fp);
}
void upload_block(u_32 block_num) //upload data block
{
fseek(fp, data_block_start + block_num * block_size, SEEK_SET);
fwrite(block_buff, block_size, 1, fp);
fflush(fp);
}
void load_block(u_32 block_num) //load data block
{
fseek(fp, data_block_start + block_num * block_size, SEEK_SET);
fread(block_buff, block_size, 1, fp);
}
void upload_inode(u_32 inode_num) //upload inode
{
fseek(fp, inode_table_start + (inode_num - 1) * inode_size, SEEK_SET);
fwrite(inode_buff, inode_size, 1, fp);
fflush(fp);
}
void load_inode(u_32 inode_num) //load inode
{
fseek(fp, inode_table_start + (inode_num - 1) * inode_size, SEEK_SET);
fread(inode_buff, inode_size, 1, fp);
}
//byte cur cur+1
//bitbuff 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
//con 0 0 1 0 0 0 0 0
//flag = 2
//data block number begin with 0
u_32 alloc_block() //allocate a block, return block number
{
load_group_desc();
if (gdt_buff->bg_free_blocks_count == 0)
{
printf("\033[1;31mThere is no block to be alloced!\n\033[m");
return 0;
}
u_16 cur = last_alloc_block / 8;
u_8 con = 128; // 1 0 0 0 0 0 0 0 0
char flag = 0;
load_block_bitmap();
while (block_bitbuff[cur] == 255)
{
if (cur == 1023)
cur = 0;
else
cur++;
}
while (block_bitbuff[cur] & con) //find an empty
{
con >>= 1;
flag++;
}
block_bitbuff[cur] = block_bitbuff[cur] + con; //set 0 to 1
last_alloc_block = cur * 8 + flag;
upload_block_bitmap();
load_super_block();
s_block_buff->s_free_blocks_count--;
upload_super_block();
gdt_buff->bg_free_blocks_count--;
upload_group_desc();
return last_alloc_block;
}
//byte cur cur+1
//bitbuff 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
//con 0 0 0 1 0 0 0 0
//flag = 3
//inode number begin with 1
u_32 alloc_inode() //allocate an inode, return inode number
{
load_group_desc();
if (gdt_buff->bg_free_inodes_count == 0)
{
printf("\033[1;31mThere is no inode to be alloced!\n\033[m");
return 0;
}
u_16 cur = (last_alloc_inode - 1) / 8;
u_8 con = 128;
char flag = 0;
load_inode_bitmap();
while (inode_bitbuff[cur] == 255)
{
if (cur == 1023)
cur = 0;
else
cur++;
}
while (inode_bitbuff[cur] & con)
{
con >>= 1;
flag++;
}
inode_bitbuff[cur] = inode_bitbuff[cur] + con; //set 0 to 1
last_alloc_inode = cur * 8 + flag + 1;
upload_inode_bitmap();
load_super_block();
s_block_buff->s_free_inodes_count--;
upload_super_block();
gdt_buff->bg_free_inodes_count--;
upload_group_desc();
return last_alloc_inode;
}
//0 0 0 0 1 1 1 1 & 1 1 1 1 1 0 0 0 ~ = 1 1 1 1 0 1 1 1
void remove_block(u_32 block_num) //remove a block
{
u_16 cur = block_num / 8;
u_8 temp = block_num % 8;
load_block_bitmap();
block_bitbuff[cur] &= ~(255 >> temp) & (255 << (7 - temp));
upload_block_bitmap();
load_group_desc();
gdt_buff->bg_free_blocks_count++;
upload_group_desc();
load_super_block();
s_block_buff->s_free_blocks_count++;
upload_super_block();
}
void remove_inode(u_32 inode_num) //remove an inode
{
u_16 cur = (inode_num - 1) / 8;
u_8 temp = (inode_num - 1) % 8;
load_inode_bitmap();
inode_bitbuff[cur] &= ~(255 >> temp) & (255 << (7 - temp));
upload_inode_bitmap();
load_group_desc();
gdt_buff->bg_free_inodes_count++;
upload_group_desc();
load_super_block();
s_block_buff->s_free_inodes_count++;
upload_super_block();
}
u_32 current_time() //set current time
{
time_t t = time(0);
return (u_32)t;
}
char *time_now(u_32 t) //change time to string
{
time_t ti = t;
strftime(current_ttime, sizeof(current_ttime), "%Y-%m-%d %H:%M:%S", localtime(&ti));
return current_ttime;
}
void init_disk() //initiate a disk
{
printf("\033[1;37mWait...\033[m");
if ((fp = fopen("./data.img", "w+b")) == NULL) //open disk
{
printf("\033[1;31mFail to open disk!\n\033[m");
exit(0);
}
last_alloc_inode = 1; //reset last allocated inode number
last_alloc_block = 0; //reset last allocated block bumber
int i;
for (i = 0; i < 16; i++) //reset file open table
{
fopen_table[i] = 0;
memset(file_open_table[i], 0, 256);
}
memset(block_buff, 0, block_size); //clear block buffer
fseek(fp, 0, SEEK_SET); //use empty block buffer to format the disk
for (i = 0; i < block_counts; i++)
fwrite(block_buff, block_size, 1, fp);
fflush(fp);
//initiate super block
load_super_block();
s_block_buff->s_inodes_count = inode_counts;
s_block_buff->s_blocks_count = block_counts;
s_block_buff->s_free_inodes_count = inode_counts - 1;
s_block_buff->s_free_blocks_count = block_counts;
s_block_buff->s_wtime = current_time();
strcpy(s_block_buff->s_volume_name, volume_name);
upload_super_block();
//initiate group descripion
load_group_desc();
gdt_buff->bg_block_bitmap = block_bitmap_start;
gdt_buff->bg_inode_bitmap = inode_bitmap_start;
gdt_buff->bg_inode_table = inode_table_start;
gdt_buff->bg_free_inodes_count = inode_counts - 1;
gdt_buff->bg_free_blocks_count = block_counts - 512 - 4;
gdt_buff->bg_used_dirs_count = 0;
upload_group_desc();
load_block_bitmap();
load_inode_bitmap();
//initiate "/"
load_inode(current_dir_inode);
inode_buff->i_mode = 0x0007; //rwx
inode_buff->i_size = 32 * 2;
inode_buff->i_atime = current_time();
inode_buff->i_ctime = current_time();
inode_buff->i_mtime = current_time();
inode_buff->i_dtime = 0;
inode_buff->i_blocks = 1;
inode_buff->i_block[0] = alloc_block();
current_dir_inode = alloc_inode();
current_dirlen = 1;
upload_inode(current_dir_inode);
//initiate "." ".."
load_dir(0);
dir[0].inode = dir[1].inode = current_dir_inode;
dir[0].name_len = dir[1].name_len = 1;
dir[0].file_type = dir[1].file_type = 2;
strcpy(dir[0].name, ".");
strcpy(dir[1].name, "..");
for (i = 2; i < 32; i++)
dir[i].inode = 0;
upload_dir(inode_buff->i_block[0]);
fclose(fp);
}
//arg=1:find an empty directory, arg=2:this block is empty?
int search_dir_entry(u_32 block_num, int arg)
{
int i;
load_dir(block_num);
for (i = 0; i < 32; i++)
{
if (!dir[i].inode)
{
if (arg == 1)
return i;
else if (arg == 2)
break;
}
}
if (i == 32)
return 1;
return -1;
}
//help fucntion search to finish seaching
int search_help(char filename[23], u_8 *type, u_32 tblock_num, u_32 *dir_num, u_32 *inode_num)
{
int i;
load_dir(tblock_num);
for (i = 0; i < dir_counts_per_block; i++)
{
if (dir[i].inode && !strcmp(dir[i].name, filename))
{
*type = dir[i].file_type;
*inode_num = dir[i].inode;
*dir_num = i;
return 1;
}
}
return 0;
}
//search a file or directory, return file type, block number, directory enrty number, inode number
int search(char filename[23], u_8 *type, u_32 *block_num, u_32 *dir_num, u_32 *inode_num)
{
int i, j, flag;
inode_buff->i_atime = current_time();
for (i = 0; i < inode_buff->i_blocks; i++)
{
if (i < 8) //direct block
{
flag = search_help(filename, &(*type), inode_buff->i_block[i], dir_num, inode_num);
if (flag)
{
*block_num = inode_buff->i_block[i];
return 1;
}
}
else //indirect block
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
for (j = 0; j < t; j++)
{
flag = search_help(filename, type, indirect_block_buff[j], dir_num, inode_num);
if (flag)
{
*block_num = indirect_block_buff[j];
return 1;
}
}
break;
}
}
return 0;
}
u_32 file_is_open(char filename[23]) //find a file in file open table
{
int i;
sprintf(file_absolute_path, "%s/%s", current_path, filename);
for (i = 0; i < 16; i++)
if (!strcmp(file_open_table[i], file_absolute_path))
return fopen_table[i];
return 0;
}
void init_dir(u_32 dir_inode, u_8 dir_len) //initiate a directory
{
load_inode(dir_inode);
inode_buff->i_mode = 0x0007; //rwx
inode_buff->i_size = 32 * 2;
inode_buff->i_atime = current_time();
inode_buff->i_ctime = current_time();
inode_buff->i_mtime = current_time();
inode_buff->i_blocks = 1;
inode_buff->i_block[0] = alloc_block();
//initiate "." ".."
load_dir(inode_buff->i_block[0]);
dir[0].inode = dir_inode;
dir[0].name_len = dir_len;
dir[1].inode = current_dir_inode;
dir[1].name_len = current_dirlen;
dir[0].file_type = dir[1].file_type = 2;
int i;
for (i = 2; i < 32; i++) //clear
dir[i].inode = 0;
strcpy(dir[0].name, ".");
strcpy(dir[1].name, "..");
upload_dir(inode_buff->i_block[0]);
upload_inode(dir_inode);
load_group_desc();
gdt_buff->bg_used_dirs_count++;
upload_group_desc();
}
void help() //show help
{
printf("\033[1;37m ----------------------------------------\n\033[m");
printf("\033[1;37m | Ext2 file system |\n\033[m");
printf("\033[1;37m | 0. help : help |\n\033[m");
printf("\033[1;37m | 1. change dir : cd dirname |\n\033[m");
printf("\033[1;37m | 2. list file : ls |\n\033[m");
printf("\033[1;37m | 3. show file : cat filename |\n\033[m");
printf("\033[1;37m | 4. create dir : mkdir dirname |\n\033[m");
printf("\033[1;37m | 5. delete dir : rmdir dirname |\n\033[m");
printf("\033[1;37m | 6. create file : touch filename |\n\033[m");
printf("\033[1;37m | 7. delete file : rm filename |\n\033[m");
printf("\033[1;37m | 8. rename file : rename filename |\n\033[m");
printf("\033[1;37m | 9. open file : open filename |\n\033[m");
printf("\033[1;37m | 10. close file : close filename |\n\033[m");
printf("\033[1;37m | 11. read file : read filename |\n\033[m");
printf("\033[1;37m | 12. write file : write filename |\n\033[m");
printf("\033[1;37m | 13. change mode : chmod filename |\n\033[m");
printf("\033[1;37m | 14. log out : quit |\n\033[m");
printf("\033[1;37m | 15. format disk : format |\n\033[m");
printf("\033[1;37m | 16. check disk : check |\n\033[m");
printf("\033[1;37m ----------------------------------------\n\033[m");
}
void ls_help(u_32 block_num) //help ls function
{
int j, k, t;
char full_mode[] = "rwx";
load_dir(block_num);
for (j = 0; j < 32; j++)
{
if (dir[j].inode)
{
if (dir[j].file_type == 1)
{
printf("\033[1;33m%-15s\033[m", dir[j].name);
printf("\033[1;33m%-12s\033[m", " File ");
}
else if (dir[j].file_type == 2)
{
printf("\033[1;36m%-15s\033[m", dir[j].name);
printf("\033[1;36m%-12s\033[m", " Dir ");
}
printf("\033[1;37m%-12s\033[m", "lips");
load_inode(dir[j].inode);
inode_buff->i_atime = current_time();
upload_inode(dir[j].inode);
printf("\033[1;37m%-25s\033[m", time_now(inode_buff->i_atime));
printf("\033[1;37m%-25s\033[m", time_now(inode_buff->i_ctime));
printf("\033[1;37m%-25s\033[m", time_now(inode_buff->i_mtime));
for (k = 0, t = 4; k < 3; k++, t >>= 1)
if (!(inode_buff->i_mode & t))
full_mode[k] = '-';
printf("\033[1;37m%-14s\033[m", full_mode);
printf("\033[1;37m%d\n\033[m", inode_buff->i_size);
}
strcpy(full_mode, "rwx");
}
load_inode(current_dir_inode); //re load current directory inode
}
void ls() //show name,user...in current directory
{
printf("\033[1;37m%-15s %-10s %-15s %-24s %-24s %-19s %-13s %-10s\n\033[m", "name", "type",
"user", "access time", "create time", "modify time", "mode", "size");
load_inode(current_dir_inode);
inode_buff->i_atime = current_time();
int i, j;
for (i = 0; i < inode_buff->i_blocks; i++)
{
if (i < 8)
ls_help(inode_buff->i_block[i]);
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
for (j = 0; j < t; j++)
ls_help(indirect_block_buff[j]);
break;
}
}
}
void mkdir(char filename[23]) //make a directory
{
load_inode(current_dir_inode);
if (!(inode_buff->i_mode & 0x0001)) //no execute righst
{
printf("\033[1;31mMkdir denied!\n\033[m");
return;
}
if (inode_buff->i_size == block_size * 264) //directory is full
{
printf("\033[1;31mThere is no room to be alloced!\n\033[m");
return;
}
u_32 block_num, dir_num, inode_num, dir_inode;
u_8 type;
int i, j, k, flag;
flag = search(filename, &type, &block_num, &dir_num, &inode_num);
if (!flag)
{
inode_buff->i_atime = current_time();
if (inode_buff->i_size != block_size * inode_buff->i_blocks) //free block
{
for (i = 0, flag = 0; !flag && i < inode_buff->i_blocks; i++)
{
if (i < 8)
{
j = search_dir_entry(inode_buff->i_block[i], 1);
break;
}
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
for (k = 0; k < t; k++)
{
j = search_dir_entry(indirect_block_buff[t], 1);
flag = 1; //use flag to jump out the loop
break;
}
}
}
dir_inode = alloc_inode();
dir[j].inode = dir_inode;
dir[j].name_len = strlen(filename);
dir[j].file_type = 2;
strcpy(dir[j].name, filename);
if (i <= 8)
upload_dir(inode_buff->i_block[i]);
else
upload_dir(indirect_block_buff[k]);
}
else //all blocks are full
{
if (inode_buff->i_blocks < 8)
{
inode_buff->i_block[inode_buff->i_blocks] = alloc_block();
inode_buff->i_blocks++;
load_dir(inode_buff->i_block[inode_buff->i_blocks - 1]);
dir_inode = alloc_inode();
dir[0].inode = dir_inode;
dir[0].name_len = strlen(filename);
dir[0].file_type = 2;
strcpy(dir[0].name, filename);
for (j = 1; j < 32; j++)
dir[j].inode = 0;
upload_dir(inode_buff->i_block[inode_buff->i_blocks - 1]);
}
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
indirect_block_buff[t] = alloc_block();
memcpy(block_buff, indirect_block_buff, block_size);
upload_block(inode_buff->i_block[8]);
inode_buff->i_blocks++;
load_dir(indirect_block_buff[t]);
dir_inode = alloc_inode();
dir[0].inode = dir_inode;
dir[0].name_len = strlen(filename);
dir[0].file_type = 2;
strcpy(dir[0].name, filename);
for (j = 1; j < 32; j++)
dir[j].inode = 0;
upload_dir(indirect_block_buff[t]);
}
}
inode_buff->i_mtime = current_time();
inode_buff->i_size += 32;
upload_inode(current_dir_inode);
init_dir(dir_inode, strlen(filename));
}
else if (type == 1)
printf("\033[1;31mU can not mkdir a dir same to a file!\n\033[m");
else
printf("\033[1;31mU can not mkdir two same dirs!\n\033[m");
}
void rmdir(char filename[23]) //delete a directory
{
load_inode(current_dir_inode);
if (!strcmp(filename, ".") || !strcmp(filename, ".."))
{
printf("\033[1;31mU can not delete this dir!\n\033[m");
return;
}
u_32 block_num, dir_num, inode_num;
u_8 type;
int i, j, flag;
flag = search(filename, &type, &block_num, &dir_num, &inode_num);
if (flag && type == 2)
{
load_inode(inode_num);
if (!(inode_buff->i_mode & 0x0001)) //no execute right
{
printf("\033[1;31mRmdir denied!\n\033[m");
return;
}
inode_buff->i_atime = current_time();
if (inode_buff->i_size == 64)
{
inode_buff->i_mode = 0;
inode_buff->i_size = 0;
inode_buff->i_blocks = 0;
//delete "." ".."
load_dir(inode_buff->i_block[0]);
dir[0].inode = 0;
dir[1].inode = 0;
upload_dir(inode_buff->i_block[0]);
remove_block(inode_buff->i_block[0]);
remove_inode(inode_num);
load_inode(current_dir_inode);
inode_buff->i_atime = current_time();
load_dir(block_num);
dir[dir_num].inode = 0;
upload_dir(block_num);
inode_buff->i_size -= 32;
for (i = inode_buff->i_blocks; i > 1;) //delete block form the last one
{
if (i <= 8)
{
if (!search_dir_entry(inode_buff->i_block[i - 1], 2))
{
remove_block(inode_buff->i_block[i - 1]);
inode_buff->i_blocks--;
i--;
}
}
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
for (j = 0; j < t; j++)
{
if (!search_dir_entry(indirect_block_buff[j], 2))
{
remove_block(indirect_block_buff[j]);
inode_buff->i_blocks--;
i--;
}
}
}
}
inode_buff->i_mtime = current_time();
upload_inode(current_dir_inode);
}
else
printf("\033[1;31mDir is not empty!\n\033[m");
}
else if (type == 1)
printf("\033[1;31mU can not rmdir a file! Use rm instead!\n\033[m");
else
printf("\033[1;31mCan not find the dir!\n\033[m");
}
void cd(char filename[23]) //change to a directory
{
load_inode(current_dir_inode);
if (!strcmp(filename, "."))
return;
else if (!strcmp(current_path, "/") && !strcmp(filename, ".."))
return;
u_32 block_num, dir_num, inode_num;
u_8 type;
int flag;
flag = search(filename, &type, &block_num, &dir_num, &inode_num);
if (flag && type == 2)
{
load_inode(inode_num);
if (!(inode_buff->i_mode & 0x0004)) //no read right
{
printf("\033[1;31mCd denied!\n\033[m");
return;
}
inode_buff->i_atime = current_time();
if (!strcmp(filename, ".."))
{
current_dir_inode = inode_num;
//for example /123/abc/
// /123/
//set 'a' to '\0'
current_path[strlen(current_path) - dir[dir_num - 1].name_len - 1] = '\0';
current_dirlen = dir[dir_num].name_len;
}
else
{
current_dir_inode = inode_num;
strcat(current_path, filename);
strcat(current_path, "/");
current_dirlen = dir[dir_num].name_len;
}
}
else if (type == 1)
printf("\033[1;31mU can not cd a file!\n\033[m");
else
printf("\033[1;31mCan not find the dir!\n\033[m");
}
void touch(char filename[23]) //make a file
{
load_dir(current_dir_inode);
if (!(inode_buff->i_mode & 0x0001)) //no execute right
{
printf("\033[1;31mTouch denied!\n\033[m");
return;
}
if (inode_buff->i_size == block_size * 264) //current directory is full
{
printf("\033[1;31mThere is no room to be alloced!\n\033[m");
return;
}
u_32 block_num, dir_num, inode_num, file_inode;
u_8 type;
int i, j, k, flag;
flag = search(filename, &type, &block_num, &dir_num, &inode_num);
if (!flag)
{
inode_buff->i_atime = current_time();
if (inode_buff->i_size != block_size * inode_buff->i_blocks)
{
for (i = 0, flag = 0; !flag && i < inode_buff->i_blocks; i++)
{
if (i < 8)
{
j = search_dir_entry(inode_buff->i_block[i], 1);
break;
}
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
for (k = 0; k < t; k++)
{
j = search_dir_entry(indirect_block_buff[t], 1);
flag = 1;
break;
}
}
}
file_inode = alloc_inode();
dir[j].inode = file_inode;
dir[j].name_len = strlen(filename);
dir[j].file_type = 1;
strcpy(dir[j].name, filename);
if (i <= 8)
upload_dir(inode_buff->i_block[i]);
else
upload_dir(indirect_block_buff[k]);
}
else
{
if (inode_buff->i_blocks < 8)
{
inode_buff->i_block[inode_buff->i_blocks] = alloc_block();
inode_buff->i_blocks++;
load_dir(inode_buff->i_block[inode_buff->i_blocks - 1]);
file_inode = alloc_inode();
dir[0].inode = file_inode;
dir[0].name_len = strlen(filename);
dir[0].file_type = 2;
strcpy(dir[0].name, filename);
for (j = 1; j < 32; j++)
dir[j].inode = 0;
upload_dir(inode_buff->i_block[inode_buff->i_blocks - 1]);
}
else
{
int t = inode_buff->i_blocks - 8;
load_block(inode_buff->i_block[8]);
memcpy(indirect_block_buff, block_buff, block_size);
indirect_block_buff[t] = alloc_block();
memcpy(block_buff, indirect_block_buff, block_size);
upload_block(inode_buff->i_block[8]);
inode_buff->i_blocks++;
load_dir(indirect_block_buff[t]);
file_inode = alloc_inode();
dir[0].inode = file_inode;
dir[0].name_len = strlen(filename);
dir[0].file_type = 2;
strcpy(dir[0].name, filename);
for (j = 1; j < 32; j++)
dir[j].inode = 0;
upload_dir(indirect_block_buff[t]);
}
}
inode_buff->i_size += 32;
upload_inode(current_dir_inode);
load_inode(file_inode);
inode_buff->i_mode = 0x0007; //rwx
inode_buff->i_size = 0;
inode_buff->i_atime = current_time();
inode_buff->i_ctime = current_time();
inode_buff->i_mtime = current_time();
inode_buff->i_blocks = 0;
upload_inode(file_inode);
}
else if (type == 2)
printf("\033[1;31mU can not touch a file same to a dir!\n\033[m");
else
printf("\033[1;31mU can not touch two same files!\n\033[m");
}
void rm(char filename[23]) //delete a file
{
load_dir(current_dir_inode);
u_32 block_num, dir_num, inode_num;
u_8 type;
int i, j, k, flag;
flag = search(filename, &type, &block_num, &dir_num, &inode_num);
if (flag && type == 1)
{
load_inode(inode_num);
if (!(inode_buff->i_mode & 0x0001)) //no execute right
{
printf("\033[1;31mRm denied!\n\033[m");
return;
}
if (file_is_open(filename))
close(filename);
load_dir(block_num);