-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileSystem.c
1764 lines (1553 loc) · 56 KB
/
fileSystem.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
//Filename: fileSystem.c
//Team Members: Divya Machenahalli Lokesh and Siddrameshwar Kadagad
//UTD_ID: 2021496022 and 2021491485
//NetID: dxm190018 and sxk190071
//Class: CS 5348.001
//Project: project3
/***********************************************************************
This program allows user to do two things:
1. initfs: Initilizes the file system and redesigning the Unix file system to accept large
files of up tp 4GB, expands the free array to 152 elements, expands the i-node array to
200 elemnts, doubles the i-node size to 64 bytes and other new features as well.
2. Quit: save all work and exit the program.
User Input:
- initfs (file path) (# of total system blocks) (# of System i-nodes)
- q
File name is limited to 14 characters.
***********************************************************************/
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define FREE_SIZE 152
#define I_SIZE 200
#define BLOCK_SIZE 1024
#define ADDR_SIZE 11
#define INPUT_SIZE 256
// Superblock Structure
typedef struct {
unsigned short isize;
unsigned short fsize;
unsigned short nfree;
unsigned short ninode;
unsigned short inode[I_SIZE];
char flock;
char ilock;
unsigned short fmod;
unsigned short time[2];
unsigned int free[FREE_SIZE]; // to make size 1024.
} superblock_type;
// Declaring superblock
superblock_type superBlock;
// I-Node Structure
//Do we really use size in inode??
//What should we do about modtime and actime?
//How are they initializing root_inode?
typedef struct {
unsigned short flags;
unsigned short nlinks;
unsigned short uid;
unsigned short gid;
unsigned int size;
unsigned int addr[ADDR_SIZE];
unsigned short actime[2];
unsigned short modtime[2];
} inode_type;
// Declaring I-Node
inode_type inode;
//this inode is inode number or inode address?
typedef struct {
unsigned short inode;
unsigned char filename[14];
} dir_type;
// Declaring root
dir_type root; //each entry in root directory will hold max of 14characters long file name and 2byes inode address
int fileDescriptor ; //file descriptor
const unsigned short inode_alloc_flag = 0100000;
const unsigned short dir_flag = 040000;
const unsigned short dir_large_file = 010000;
const unsigned short dir_small_file = 010000;
const unsigned short dir_access_rights = 000777; // User, Group, & World have all access privileges
const unsigned short file_flag = 000000;
const unsigned short small_file_flag = 000000;
const unsigned short large_file_flag = 010000;
const unsigned short file_access_rights = 000777;
const unsigned short INODE_SIZE = 64; // inode has been doubled
int cur_dir_inode_num; //Hopefully keeps track of current directory.
char cur_dir[1000];
int dir_index=0;
int initfs(char* path, unsigned int total_blcks,unsigned int total_inodes);
void add_block_to_free_list( int blocknumber , unsigned int *empty_buffer );
void create_root();
int getInode();
inode_type addFreeBlockToAddr(inode_type inode, int no_free_blocks);
int preInitialization();
int getFreeBlock();
void printInode(inode_type inode_);
void printSuperBlock();
inode_type getInodeWithNum(int num);
void findLocWhereCurrDirFileShouldGo(int cur_dir_inode_num);
void printFirst5RootDirectories();
void ls();
void mkdir();
int inodeOfFile(char *file_name);
void printContentOfFile(int inode_num);
void cd();
void rmFile();
void rmDir();
void addToFreeBlockArrayWithoutBuffer(int block_number);
void freeInode(int inode_num);
void addInodeToFreeArray(int inode_num);
int getinodeNumberAfterDeleteFile();
void printBlock(int blockNum);
void printPwd();
int main() {
char input[INPUT_SIZE];
char *splitter;
unsigned int numBlocks = 0, numInodes = 0;
char *filepath;
printf("Size of super block = %d , size of i-node = %d\n",(int)sizeof(superBlock), (int)sizeof(inode));
printf("Enter command:\n");
// while adding a new file or dir check if the file/dir exist
// check if the inode val is 0. Then set that val to inode number.
// Method that we can use -> ftell -> current position of pointer
// If open function has not been used it will mess things up. Catch that exception. Have a method or if else condition to take care of it.
//Change all the address types to long or better
//Check the flags. Set the flags.
//We have to keep track of current directory. How are we doing it?
//Inode address will be Inode_num-1
//Fix the compiler errors first of all.
//Can we copy one Struct into another just by Assigning?
//Need a fuction to print the contents of super block
//Also need a function to print any block for that matter
//fsize if shot but we are not using it anyway
//In large file how many data_block number can be fit in a 1 data_block?
//We can print the number of blocks were used during an operation. also when we add it back to free array we can keep track of the numbers.
//We shouldn't fill the addr array with free block. If the directory or the file requires it only then we should add the free blocks.
//When we deallocate an inode we need to set the first bit to 0
//Questions
//Will the cursor move after the read command.
//
//if ninode is full we will add the free inodes to the queue.
//If at any time, fileDescriptor < 3 then it should print out that scenario.
// Check if Dir/file/special_file
/*
if((flags & 060000) == 040000){
printf("flags- inode is a dir_access_rights\n");
} else if((flags & 060000) == 0) {
printf("flags- inode is a file\n");
} else {
printf("flags -inode is a special file\n");
}
// Checks if large or small
if((flags & 010000) == 010000) {
printf("inode points to large file\n");
} else {
printf("inode points to small file\n");
}
*/
while(1) {
// Take commands from user till the input is 'q'
//Takes input as string until it encounters a new line character.
scanf(" %[^\n]s", input);
// Split the user input string into words which are seperated by space ' '
splitter = strtok(input," ");
// if the first word of the input command is initfs, execute this if block
if(strcmp(splitter, "initfs") == 0){
preInitialization();
splitter = NULL;
} else if (strcmp(splitter, "q") == 0) {
//Exit the program when the input command is 'q'
//Before quitting the program setting the cursor offset by BLOCK_SIZE to 1st Block which is super block
lseek(fileDescriptor, BLOCK_SIZE, 0);
// saving the contents of superBlock
write(fileDescriptor, &superBlock, BLOCK_SIZE);
//Should we close the file here? Because it wasn't closed in the given code
close(fileDescriptor);
return 0;
} else if(strcmp(splitter, "cpin") == 0) {
char *externalFile = strtok(NULL, " ");
char *v6_file = strtok(NULL, " ");
char *dirpathcpin = v6_file;
int index1=0;
char cpinfileName[100];
inode_type inode_of_dir;
int inode_num_of_dir;
int dir_error_flag=0;
if (*dirpathcpin++=='/')
{
int temp=cur_dir_inode_num;
cur_dir_inode_num=1;
while(*dirpathcpin){
cpinfileName[index1]=*dirpathcpin++;
index1++;
if(cpinfileName[index1-1]=='/')
{
cpinfileName[index1-1]='\0';
inode_num_of_dir = inodeOfFile(cpinfileName);
inode_of_dir = getInodeWithNum(inode_num_of_dir);
if((inode_of_dir.flags & 060000) == 040000) {
cur_dir_inode_num = inode_num_of_dir;
}
else {
printf("invalid direcotry name\n");
cur_dir_inode_num=temp;
dir_error_flag=1;
break;
}
while(index1>=0)
{
cpinfileName[index1]='\0';
index1--;
}
index1=0;
}
}
cur_dir_inode_num=temp;
}
else{
inode_num_of_dir=cur_dir_inode_num;
*dirpathcpin--;
while(*dirpathcpin){
cpinfileName[index1]=*dirpathcpin++;
index1++;
}
cpinfileName[index1]='\0';
}
if(dir_error_flag==0)
{
FILE* fin=fopen(externalFile,"r");
//Check if the given file already exists// If so we would have to over lap the
// Also check if inode corresponding to filename is 0 or not. If it is zero then replace that with new inode.
if (fin==NULL)
{
printf("File not found.");
} else {
printf("copying data...\n");
fseek(fin, 0L, SEEK_END);
long int filesize=ftell(fin);
//printf("filesize is %ld", filesize);
fclose(fin);
// get an inode
// consier the scenario when inode array is empty
int cur_file_inode_num = getInode();
//printf("cur_file_inode_num is %d\n", cur_file_inode_num);
int inode_address = (cur_file_inode_num-1)* INODE_SIZE + 2 * BLOCK_SIZE;
//printf("inode_address is %d\n", inode_address);
//We will have to write the inode not get one!. We only need the inode number to which
//we want to write the contents to.
inode_type new_file_inode;
new_file_inode.size = filesize;
int number_of_free_block_req;
if(filesize % BLOCK_SIZE == 0){
number_of_free_block_req = filesize/BLOCK_SIZE;
} else {
number_of_free_block_req = filesize/BLOCK_SIZE + 1;
}
//printf("number_of_free_block_req is %d\n", number_of_free_block_req);
new_file_inode = addFreeBlockToAddr(new_file_inode, number_of_free_block_req);
FILE* fin=fopen(externalFile,"r");
int index = 0;
int num_of_char_debug = 0;
//printf(" writing the contents to blocks\n");
if(number_of_free_block_req > ADDR_SIZE) {
//big file
int count = 0;
int count2 = 0;
int block_num1;
int currBlock;
new_file_inode.flags = inode_alloc_flag | file_flag | large_file_flag | file_access_rights;
//printf("*************Big file*****************\n");
//printf("Number of free block required is -> %d\n", number_of_free_block_req);
while(count < number_of_free_block_req && count < ((ADDR_SIZE-1) * (BLOCK_SIZE/sizeof(int)))){
if(count % (BLOCK_SIZE/sizeof(int)) == 0) {
block_num1 = new_file_inode.addr[index];
//printf("\tBlock_num1 is %d and block number is %d indes is %d\n", count, block_num1, index);
count2 = 0;
index++;
}
//Change data type of everything
//printf("block_num - %d, BLOCK_SIZE - %d, count2 - %d, sizeof(int) -%d, ", block_num1, BLOCK_SIZE, count2, sizeof(int));
//printf("block address is %ld ", ((block_num1 * BLOCK_SIZE) + (count2 * sizeof(int))));
//What is the input type for lseek.
lseek(fileDescriptor, ((block_num1 * BLOCK_SIZE) + (count2 * sizeof(int))), 0);
read(fileDescriptor, &currBlock, sizeof(int));
lseek(fileDescriptor, currBlock * BLOCK_SIZE, 0);
//printf("Block is %d and block number is %d\n", count, currBlock);
int byteNum = 0;
char c = getc(fin);
while(byteNum < 1023 && c!=EOF) {
write(fileDescriptor, &c, 1);
c = getc(fin);
//printf("%c", c);
num_of_char_debug++;
byteNum++;
}
if(c!=EOF)
write(fileDescriptor, &c, 1);
count++;
count2++;
}
if(count < number_of_free_block_req) {
number_of_free_block_req -= count;
count = 0;
count2 = 0;
int count3 = 0;
int count4 = 0;
int block_num = new_file_inode.addr[index];
int cur_block;
int block_write;
int data_block;
//printf("Beginning of extra large part. And block num is %d\n", block_num);
while(count < number_of_free_block_req){
if(count % ((BLOCK_SIZE/sizeof(int)) * (BLOCK_SIZE/sizeof(int))) == 0) {
lseek(fileDescriptor, (block_num * BLOCK_SIZE) + (count2 * sizeof(int)), 0);
read(fileDescriptor, &cur_block, sizeof(int));
//lseek(fileDescriptor, cur_block * BLOCK_SIZE, 0);
count2++;
count3 = 0;
//printf("copying data...\t");
}
if(count % (BLOCK_SIZE/sizeof(int)) == 0){
lseek(fileDescriptor, ((cur_block * BLOCK_SIZE) + (count3 * sizeof(int))), 0);
read(fileDescriptor, &block_write, sizeof(int));
count3++;
count4 = 0;
}
lseek(fileDescriptor, ((block_write * BLOCK_SIZE) + (count4 * sizeof(int))), 0);
read(fileDescriptor, &data_block, sizeof(int));
lseek(fileDescriptor, data_block * BLOCK_SIZE, 0);
//printf("Count is %d and block number is %d\n", count, data_block);
int byteNum = 0;
char c = getc(fin);
while(byteNum < 1023 && c!=EOF) {
write(fileDescriptor, &c, 1);
c = getc(fin);
//printf("%c", c);
num_of_char_debug++;
byteNum++;
}
if(c!=EOF)
write(fileDescriptor, &c, 1);
count++;
count4++;
}
}
} else {
//small file
new_file_inode.flags = inode_alloc_flag | file_flag | small_file_flag | file_access_rights;
//read(fileDescriptor, &new_file_inode, INODE_SIZE);
//We need to add this inode to the current directory.
//write this inode
while(index < number_of_free_block_req ) {
int currBlock = new_file_inode.addr[index];
lseek(fileDescriptor, currBlock * BLOCK_SIZE, SEEK_SET);
//printf("block number - %d. contents of this block are below", currBlock);
//We can split this into last block and other blocks.
int byteNum = 0;
char c = getc(fin);
while(byteNum < 1023 && c!=EOF) {
write(fileDescriptor, &c, 1);
c = getc(fin);
//printf("%c", c);
num_of_char_debug++;
byteNum++;
}
if(c!=EOF)
write(fileDescriptor, &c, 1);
//write(fileDescriptor, &c, 1);
index++;
}
}
fclose(fin);
//printf("num_of_char_debug is %d\n", num_of_char_debug);
lseek(fileDescriptor, inode_address, SEEK_SET);
write(fileDescriptor, &new_file_inode, INODE_SIZE);
//have to add this inode to current directory
//long int address = findLocWhereCurrDirFileShouldGo(cur_dir_inode_num);
findLocWhereCurrDirFileShouldGo(inode_num_of_dir);
//printf("+1\t");
dir_type dir_file;
//printf("+2\t");
dir_file.inode = cur_file_inode_num;
//printf("+3\t");
int i =0;
index1=0;
while(cpinfileName[index1] != '\0') {
dir_file.filename[i++] = cpinfileName[index1];
index1+=1;
}
//printf("\n i is %d", i);
//printf("+4\t");
dir_file.filename[i] = '\0';
//lseek(fileDescriptor, address, 0);
//printf("+5\t");
//printf("inode number of file is %d. fileName is %s", dir_file.inode, dir_file.filename);
write(fileDescriptor, &dir_file, 16);
printf("cpin has been executed\n");
/*
printf("addr array of the file is \n");
i =0;
//printf("curent index is %d\n", addr_index);
while(i < ADDR_SIZE){
printf("addr[%d] - %d\t",i,new_file_inode.addr[i]);
i++;
} */
//printFirst5RootDirectories();
// find the number of free blocks that you need
// if the free array gets empty write code for that
// ADDR array will point to the free block that we have got
// copy the the contents from the file 1block at a time to freeblocks
// Add F_END at the end of last free block.
//First find in which directory we are currently in
// we will assume this is the first command and root will our directory
//Get an inode for
//Find the empty directory/file-name place
//Get an inode for the file
//keep writing to free blocks and keep the address of the free blocks in an array
//
//things to keep in mind is we have to free blocks using super blocks. Once the first free array is empty we have to write code to get next set of free blocks list
}
}
} else if(strcmp(splitter, "cpout") == 0) {
char *v6_file = strtok(NULL, " ");
char *externalFile = strtok(NULL, " ");
int temp=cur_dir_inode_num;
char *dirpathcpin = v6_file;
int index1=0;
char cpinfileName[100];
inode_type inode_of_dir;
int inode_num_of_dir;
int dir_error_flag=0;
if (*dirpathcpin++=='/')
{
cur_dir_inode_num=1;
while(*dirpathcpin){
cpinfileName[index1]=*dirpathcpin++;
index1++;
if(cpinfileName[index1-1]=='/')
{
cpinfileName[index1-1]='\0';
inode_num_of_dir = inodeOfFile(cpinfileName);
inode_of_dir = getInodeWithNum(inode_num_of_dir);
if((inode_of_dir.flags & 060000) == 040000) {
cur_dir_inode_num = inode_num_of_dir;
}
else {
printf("invalid direcotry name\n");
cur_dir_inode_num=temp;
dir_error_flag=1;
break;
}
while(index1>=0)
{
cpinfileName[index1]='\0';
index1--;
}
index1=0;
}
}
}
else{
*dirpathcpin--;
while(*dirpathcpin){
cpinfileName[index1]=*dirpathcpin++;
index1++;
}
cpinfileName[index1]='\0';
}
if(dir_error_flag==0)
{
//Will fopen read a file with a file? Or just file name
int fd_external = open(externalFile, O_RDWR | O_CREAT, 0700);
//FILE* fout=fopen(externalFile,"w");
//v6_file - Will it have a file path from the
int inode_num_of_file = inodeOfFile(cpinfileName);
//Also check inode_num_of_file is a file
if(inode_num_of_file > 0 ) {
printf("copying data...\n");
inode_type inode_of_file = getInodeWithNum(inode_num_of_file);
if((inode_of_file.flags & 060000) != dir_flag ) {
//looping through addr array
//Get the inode of the given file name. What if the v-6 file doesn't exist? print that file doesn't exist.
//Assuming small size for the directory
//looping through addr array //looping through dir_data blocks
//Use the existing code
//Need to change inode name below
//data block of current file
int num_of_data_block;
if(inode_of_file.size % BLOCK_SIZE == 0) {
num_of_data_block = inode_of_file.size/BLOCK_SIZE;
} else {
num_of_data_block = inode_of_file.size/BLOCK_SIZE + 1;
}
int addr_index = 0;
//again assuming small file
//printf("\n inode_of_file.size is %d\n", inode_of_file.size);
//printf("printing the contents while copying\n");
char copy_char;
if((inode_of_file.flags & large_file_flag) == large_file_flag) {
//large file
int count = 0;
int count2 = 0;
int block_num;
int curr_block;
while(count < num_of_data_block && count < ((ADDR_SIZE-1) * (BLOCK_SIZE/sizeof(int)))) {
if(count % (BLOCK_SIZE/sizeof(int)) == 0) {
//debugging
block_num = inode_of_file.addr[addr_index];
if(block_num == 0) {
int i =0;
printf("curent index is %d\n", addr_index);
while(i < ADDR_SIZE){
printf("addr[%d] - %d\t",i,inode_of_file.addr[i]);
i++;
}
printf("size in the file inode is not correct"); // Change the print statement in the future
break;
}
addr_index++;
count2 =0;
}
lseek(fileDescriptor, (block_num * BLOCK_SIZE) + (count2 * sizeof(int)), 0);
read(fileDescriptor, &curr_block, sizeof(int));
if(curr_block == 0) {
//printf("It was here\n");
printf("size in the file inode is not correct"); // Change the print statement in the future
break;
}
int i=0;
//printf("Block %d and block num is %d\n", count, curr_block);
lseek(fileDescriptor, curr_block * BLOCK_SIZE, 0);
while(i< BLOCK_SIZE) {
read(fileDescriptor, ©_char, 1);
if(copy_char == '\0') {
//write(fd_external, ©_char, 1);
break;
}
//printf("%c", copy_char);
write(fd_external, ©_char, 1);
i++;
}
count++;
count2++;
}
if(count < num_of_data_block) {
num_of_data_block -= count;
count = 0;
count2 = 0;
int count3 = 0;
int count4 = 0;
int block_num = inode_of_file.addr[addr_index];
int cur_block;
int block_write;
int dataBlock;
//printf("reading extra large part of file. block_num is %d\n", block_num);
while(count < num_of_data_block) {
if(count % ((BLOCK_SIZE/sizeof(int)) * (BLOCK_SIZE/sizeof(int))) == 0) {
lseek(fileDescriptor, (block_num * BLOCK_SIZE) + (count2 * sizeof(int)), 0);
read(fileDescriptor, &cur_block, sizeof(int));
//lseek(fileDescriptor, cur_block * BLOCK_SIZE, 0);
count2++;
count3 = 0;
//printf("copying data blocks...\t");
}
if(count % (BLOCK_SIZE/sizeof(int)) == 0) {
lseek(fileDescriptor, (cur_block * BLOCK_SIZE) + (count3 * sizeof(int)), 0);
read(fileDescriptor, &block_write, sizeof(int));
count3++;
count4 = 0;
}
lseek(fileDescriptor, (block_write * BLOCK_SIZE) + (count4 * sizeof(int)), 0);
read(fileDescriptor, &dataBlock, sizeof(int));
lseek(fileDescriptor, dataBlock * BLOCK_SIZE, 0);
int i=0;
//printf("Block %d and block num is %d\n", count, dataBlock);
while(i< BLOCK_SIZE) {
read(fileDescriptor, ©_char, 1);
if(copy_char == '\0') {
//write(fd_external, ©_char, 1);
break;
}
//printf("%c", copy_char);
write(fd_external, ©_char, 1);
i++;
}
count++;
count4++;
}
}
} else {
//small file
while(addr_index < num_of_data_block) {
//check till the empty block
int dir_block_num = inode_of_file.addr[addr_index];
lseek(fileDescriptor, dir_block_num * BLOCK_SIZE, 0);
if(dir_block_num == 0) {
printf("size in the file inode is not correct"); // Change the print statement in the future
break;
}
int i=0;
//printf("Block %d and block num is %d\n", addr_index, dir_block_num);
while(i< BLOCK_SIZE) {
read(fileDescriptor, ©_char, 1);
if(copy_char == '\0') {
//write(fd_external, ©_char, 1);
break;
}
//printf("%c", copy_char);
write(fd_external, ©_char, 1);
i++;
}
addr_index++;
//loop through the directories in the dir_block_num
/**
while(false) {
//dir_type cur_loop_dir =
//see if this directory has the file that you looking for.
}
**/
}
}
} else {
printf("not a valid file name\n");
}
//loop through all the directories and search for the file that you are looking for
//How do we read data from file system?
// May be we can have a pointer to the address of the starting address (like FILE* or char*) and then use read command to get the data from the block. Or We can character(character pointer) to point to it and read the content interatively
}
close(fd_external);
printf("cpout has been executed\n");
cur_dir_inode_num=temp;
}
} else if(strcmp(splitter, "mkdir") == 0) {
mkdir();
} else if(strcmp(splitter, "rm") == 0) {
rmFile();
} else if(strcmp(splitter, "ls") == 0) {
ls();
} else if(strcmp(splitter, "pwd") == 0) {
printPwd();
} else if(strcmp(splitter, "cd") == 0) {
cd();
} else if(strcmp(splitter, "rmdir") == 0) {
rmDir();
//go to all subdirectory and delete all the files and directories
//Can not delete root
//delete dir/files you have set all the blocks free and then to free list.
// Free inodes and if the ilist size changes- update that in the super block
} else if(strcmp(splitter, "open") == 0) {
char *filepath = strtok(NULL, " ");
//fileDescriptor = open(fileName, O_RDWR);
if(access(filepath, F_OK) != -1) {
//Check if the filepath exists
//Check if the read/write access for the filepath for the current user does not exist
fileDescriptor = open(filepath, O_RDWR, 0600);
if( fileDescriptor == -1){
printf("\n filesystem already exists but open() failed with error [%s]\n", strerror(errno));
return 1;
}
//read/write access for filepath is available for the current user
// One the following 3 methods is getting stuck? What could be the reason for that?
// Initialise superBlock
//printf("fd is %d\n", fileDescriptor);
lseek(fileDescriptor, BLOCK_SIZE, 0);
read(fileDescriptor, &superBlock, BLOCK_SIZE);
//printSuperBlock();
cur_dir_inode_num = 1;
cur_dir[dir_index]='/';
//Set current directory.
printf("filesystem %s open\n", filepath);
//printf("****\n");
// Initialise rootDir
//printFirst5RootDirectories();
} else {
printf("Given FileSystem doesn't exist");
}
} else if(strcmp(splitter, "print") == 0) {
char *blockNum = strtok(NULL, " ");
int block_num = atoi(blockNum);
printBlock(block_num);
} else {
printf("incorrect command. Try again\n");
}
}
}
void rmFile(){
int inode_num_of_file = getinodeNumberAfterDeleteFile();
if(inode_num_of_file != 0) {
freeInode(inode_num_of_file);
}
}
void printPwd()
{
printf("%s\n",cur_dir);
}
void rmDir() {
int inode_num_of_file = getinodeNumberAfterDeleteDir();
if(inode_num_of_file != 0) {
freeInode(inode_num_of_file);
}
}
void freeInode(int inode_num) {
if(inode_num == 0)
return;
//printf("Adding to free list -> %d inode\n", inode_num);
//Go through addr array. Free a block. Add the block to free list. set the addr[index] val to zero
inode_type inode_ = getInodeWithNum(inode_num);
int i =0;
int block_num;
if((inode_.flags & large_file_flag) == small_file_flag) {
//small file
for(i = 0; i < ADDR_SIZE; i++) {
if(inode_.addr[i] == 0){
break;
}
block_num = inode_.addr[i];
addToFreeBlockArrayWithoutBuffer(block_num);
inode_.addr[i] = 0;
//No need to have buffer right?
}
} else {
//large file
int j = 0;
int curr_block;
for(i = 0; i < ADDR_SIZE-1; i++) {
if(inode_.addr[i] == 0){
break;
}
block_num = inode_.addr[i];
for(j = 0; j < BLOCK_SIZE/sizeof(int); j++){
lseek(fileDescriptor, ((block_num * BLOCK_SIZE) + (j * sizeof(int))), 0);
read(fileDescriptor, &curr_block, sizeof(int));
if(curr_block == 0){
break;
}
//printf("\t i is %d, j is %d, block num %d\n", i, j, curr_block);
addToFreeBlockArrayWithoutBuffer(curr_block);
}
//printf("i is %d, block num %d\n", i, block_num);
addToFreeBlockArrayWithoutBuffer(block_num);
inode_.addr[i] = 0;
}
if(inode_.addr[ADDR_SIZE-1] != 0) {
int data_block;
int dataBlock2;
int k;
block_num = inode_.addr[ADDR_SIZE-1];
for(i = 0; i < BLOCK_SIZE/sizeof(int); i++) {
lseek(fileDescriptor, ((block_num * BLOCK_SIZE) + (i * sizeof(int))), 0);
read(fileDescriptor, &curr_block, sizeof(int));
//printf("deallocating data blocks...\t");
if(curr_block == 0){
break;
}
for(j = 0; j < BLOCK_SIZE/sizeof(int); j++){
lseek(fileDescriptor, ((curr_block * BLOCK_SIZE) + (j * sizeof(int))), 0);
read(fileDescriptor, &data_block, sizeof(int));
if(data_block == 0){
break;
}
for(k = 0; k < BLOCK_SIZE/sizeof(int); k++){
lseek(fileDescriptor, ((data_block * BLOCK_SIZE) + (k * sizeof(int))), 0);
read(fileDescriptor, &dataBlock2, sizeof(int));
if(dataBlock2 == 0){
break;
}
addToFreeBlockArrayWithoutBuffer(dataBlock2);
}
//printf("\t\t i is %d, j is %d, block num %d\n", i, j, data_block);
addToFreeBlockArrayWithoutBuffer(data_block);
}
//printf("\t i is %d, block num %d\n", i, curr_block);
addToFreeBlockArrayWithoutBuffer(curr_block);
}
//printf("block num %d\n", block_num);
addToFreeBlockArrayWithoutBuffer(block_num);
inode_.addr[ADDR_SIZE-1] = 0;
}
}
//Set inode allocate bit to zero. Add it the inode array if possible.
inode_.flags = inode_.flags & 011111;
//Need to write this inode contents to its location.
lseek(fileDescriptor, 2 * BLOCK_SIZE + (inode_num - 1)*INODE_SIZE, 0);
write(fileDescriptor, &inode_, INODE_SIZE);
//add inode to the free list if possible
addInodeToFreeArray(inode_num);
printf(" was successfully deleted\n");
}
void addInodeToFreeArray(int inode_num){
if(superBlock.ninode < I_SIZE) {
superBlock.inode[superBlock.ninode] = inode_num;
superBlock.ninode++;
}
}
int getinodeNumberAfterDeleteFile(){
char *v6_file = strtok(NULL, " ");
inode_type current_dir_inode = getInodeWithNum(cur_dir_inode_num);
int i = 0;
printf("%s", v6_file);
while(i < ADDR_SIZE) {
int block_number = current_dir_inode.addr[i];
lseek(fileDescriptor, block_number * BLOCK_SIZE, 0);
int j = 0;
dir_type iter_dir_cur;
while(j < BLOCK_SIZE/16){
read(fileDescriptor, &iter_dir_cur, 16);
if(iter_dir_cur.filename[0] == '\0') {
printf(" rm: %s file not found in the directory\n", v6_file);
return 0;
} else if(strcmp(iter_dir_cur.filename, v6_file) == 0) {
if(iter_dir_cur.inode == 0) {
printf(" rm: %s file not found in the directory\n", v6_file);
return 0;
}
//Make sure while adding a file or folder if the file previously existed, then use the same dir and set the new inode number for that.
//Check if inode of the v6_file is a file or directory.
inode_type file_or_dir = getInodeWithNum(iter_dir_cur.inode);
if((file_or_dir.flags & 060000) == dir_flag ) {
printf(" rm: cannot remove %s: Is a directory\n", v6_file);
return 0;
}
//printf("\n %s found in the current dir. inode number is %d\n", v6_file, iter_dir_cur.inode);
int inode_num = iter_dir_cur.inode;
iter_dir_cur.inode = 0;
lseek(fileDescriptor, block_number * BLOCK_SIZE + j * 16, 0);
write(fileDescriptor, &iter_dir_cur, 16);
return inode_num;
}
j++;
}
i++;
}
//Find dir in the current directory which has this file name
//get inode number. Set the val in the dir.inode to zero
//Go through all the addr array and free the block and set the addr[i] val to 0;
//once all the blocks are freed, set the allocate bit to zero. add the node to inode array if it is not full;
}
//Combine getinodeNumberAfterDeleteFile and getinodeNumberAfterDeleteDir
int getinodeNumberAfterDeleteDir(){
char *v6_file = strtok(NULL, " ");
inode_type current_dir_inode = getInodeWithNum(cur_dir_inode_num);
int i = 0;
printf("%s", v6_file);
while(i < ADDR_SIZE) {
int block_number = current_dir_inode.addr[i];
lseek(fileDescriptor, block_number * BLOCK_SIZE, 0);
int j = 0;
dir_type iter_dir_cur;
while(j < BLOCK_SIZE/16){
read(fileDescriptor, &iter_dir_cur, 16);
if(iter_dir_cur.filename[0] == '\0') {
printf(" rm: %s not found in the directory\n", v6_file);
return 0;
} else if(strcmp(iter_dir_cur.filename, v6_file) == 0) {
if(iter_dir_cur.inode == 0) {
printf("rm: %s not found in the directory\n", v6_file);
return 0;
}
//Make sure while adding a file or folder if the file previously existed, then use the same dir and set the new inode number for that.
//Check if inode of the v6_file is a file or directory.
inode_type file_or_dir = getInodeWithNum(iter_dir_cur.inode);
if((file_or_dir.flags & 060000) == file_flag ) {
printf(" rm: cannot remove %s: Is a file\n", v6_file);
return 0;
}
//printf("\n %s found in the current dir. inode number is %d\n", v6_file, iter_dir_cur.inode);
int inode_num = iter_dir_cur.inode;
iter_dir_cur.inode = 0;
lseek(fileDescriptor, block_number * BLOCK_SIZE + j * 16, 0);
write(fileDescriptor, &iter_dir_cur, 16);
return inode_num;
}
j++;
}
i++;
}
}
void printContentOfFile(int inode_num) {
inode_type current_dir_inode = getInodeWithNum(inode_num);
}
//setting the pointer to the location where the dir should go
//change method name.
void findLocWhereCurrDirFileShouldGo(int cur_dir_inode_num){
inode_type cur_dir_inode = getInodeWithNum(cur_dir_inode_num);
int i = 0;
while( i < ADDR_SIZE) {
int block_number = cur_dir_inode.addr[i];
//printf("block_number is %d, inode num %d \n", block_number, cur_dir_inode_num);
lseek(fileDescriptor, block_number * BLOCK_SIZE, 0);
int j = 0;
dir_type iter_dir_cur;