forked from swapnilbansal1/Modified-Unix-File-System-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsaccess.c
1502 lines (1262 loc) · 37.5 KB
/
fsaccess.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
// ***********************************************************************
// File Name: fsaccess.c
///**** Modified Version of V6 Unix File System ****/
// Program written by :
// Amith Kumar Matapady (axm180029)
// Swapnil Bansal (sxb180020)
// Surajit Baitalik (sxb180026)
//
// Objective: To design a modified version of Unix V6 file-system with
// 1024 bytes of block size and 4 GB max file size.
//
// To execute program, the following commands can be used on Linux server:
// cc fsaccess.c
// ./a.out
//
// ***********************************************************************
//#include "stdafx.h" // for windows support
//#include "pch.h" // for windows support
//header files
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include <stdbool.h>
//inode flags - extra 0 has been added in the beginning to indicate that these are positive numbers
const unsigned short directory = 040000;
const unsigned short plainfile = 000000;
const unsigned short largefile = 010000;
const unsigned short allocatedinode = 0100000;
//Structure of superblock (total size - 1022 Bytes)
typedef struct
{
unsigned int isize;
unsigned int fsize;
unsigned int nfree;
unsigned int free[148];
unsigned int ninode;
unsigned int inode[100];
unsigned short flock;
unsigned short ilock;
unsigned short fmod;
unsigned int time[2];
}super_Block;
//Structure of inode (total size - 128 Bytes) //8 inodes per block
typedef struct
{
unsigned short flags;
unsigned int nlinks;
unsigned short uid;
unsigned short gid;
unsigned int size1; // restricts max size to 2^32 = 4 GB
unsigned int addr[27];
unsigned short actime[1];
unsigned short modtime[2];
}inode_Struct;
//Structure of directory (total size - 16 Bytes)
typedef struct
{
unsigned int inode;
char filename[12];
}dirStruct;
//Constants
#define BLOCK_SIZE 1024
#define INODES_PER_BLOCK 8
const unsigned short INODE_SIZE = 128; //size of each INODE (not the value of isize on superblock)
//global instances
super_Block super_Block_inst;
inode_Struct inoderef;
dirStruct root_dir;
dirStruct new_dir;
//Global Variables
int fd; //file descriptor
unsigned int last_free_block[256]; //array used to fill free[0] with the link to next set of free array
//function to read character array from the required block
void blockreaderchar(char *chararray, unsigned int blocknum)
{
if (blocknum >= super_Block_inst.fsize)
{
printf(" Block number is greater than max file system block size for reading\n");
}
else
{
lseek(fd, blocknum*BLOCK_SIZE, 0);
read(fd, chararray, BLOCK_SIZE);
}
}
//function to write character array to the required block
void blockwriterchar(char *chararray, unsigned int blocknum)
{
int bytes_written;
if (blocknum >= super_Block_inst.fsize)
printf(" Block number is greater than max file system block size for writing\n");
else {
lseek(fd, blocknum*BLOCK_SIZE, 0);
if ((bytes_written = write(fd, chararray, BLOCK_SIZE)) < BLOCK_SIZE)
printf("\n Error in writing block number : %d\n", blocknum);
}
}
//need to add inodes for > 100
unsigned int GetFreeInode()
{
super_Block_inst.ninode--;
unsigned int ReturnInodeNumber = super_Block_inst.inode[super_Block_inst.ninode];
return ReturnInodeNumber;
}
//function to write integer array to the required block
void writeinttoblock(unsigned int *source, unsigned int blocknum)
{
int bytes_written;
if (blocknum >= super_Block_inst.fsize)
printf(" Block number is greater than max file system block size for writing\n");
else
{
lseek(fd, blocknum*BLOCK_SIZE, 0);
if ((bytes_written = write(fd, source, BLOCK_SIZE)) < BLOCK_SIZE)
printf("\n Error in writing block number : %d", blocknum);
}
}
// Data blocks are linked in sets of 148 so as to access all free blocks
void set_up_freeblock_links(unsigned int total_no_blocks)
{
int i = 0; //index to be used
for (i = 0; i < 256; i++)
last_free_block[i] = 0; //clearing last_free_block array (to 0)
unsigned int emptybuffer[256]; // buffer to fill an entire block with zeros. Since integer size is 4 bytes, 256 * 4 = 1024 Bytes.
unsigned int no_of_sets = (total_no_blocks - (2 + super_Block_inst.isize)) / 148; //splitting into blocks of 148 as free array can only store 148 logical addresses
unsigned int remainingblocks = (total_no_blocks - (2 + super_Block_inst.isize)) % 148; //remaining blocks
//fill the emptybuffer[256] with zeroes
for (i = 0; i < 256; i++)
emptybuffer[i] = 0; //initializing emptybuffer array to 0 - may be redundant
unsigned int set_no = 0;
//setting up linked sets of 148 blocks at a time
for (set_no = 0; set_no < no_of_sets; set_no++)
{
if (set_no != (no_of_sets - 1))
{
last_free_block[0] = 148;
}
else
{
last_free_block[0] = remainingblocks; //last set only has 'remainingblocks' no of elements
}
for (i = 0; i < 148; i++)
{
if (set_no == (no_of_sets - 1) && remainingblocks == 0 && i == 0)
{
last_free_block[i + 1] = 0;
break;
}
else
{
int temp_Block_no = 2 + super_Block_inst.isize + (148 * (set_no + 1)) + i;
if (set_no == (no_of_sets - 1) && remainingblocks > 0 && temp_Block_no >= total_no_blocks)
{
last_free_block[i + 1] = 0;
}
else
{
last_free_block[i + 1] = temp_Block_no;
}
}
}
writeinttoblock(last_free_block, 2 + super_Block_inst.isize + (148 * set_no));
for (i = 1; i < 148; i++)
writeinttoblock(emptybuffer, 2 + super_Block_inst.isize + i + 148 * set_no);
}
for (i = 0; i < 256; i++)
last_free_block[i] = 0;
writeinttoblock(last_free_block, 2 + super_Block_inst.isize + (148 * set_no)); //write into the '0th' block of last set
//clear all the memory blocks indicated in the last set
for (i = 1; i < remainingblocks; i++)
writeinttoblock(emptybuffer, 2 + super_Block_inst.isize + i + (148 * set_no));
}
//free data blocks and initialize free array
int add_freeblock(unsigned int blockNum)
{
if (super_Block_inst.nfree < 148)
{
super_Block_inst.free[super_Block_inst.nfree] = blockNum;
++super_Block_inst.nfree;
}
else
{
int i = 0;
for (i = 0; i < 256; i++)
last_free_block[i] = 0;
last_free_block[0] = super_Block_inst.nfree;
for (i = 0; i < 148; i++)
last_free_block[i + 1] = super_Block_inst.free[i];
for (i = 0; i < 148; i++)
super_Block_inst.free[i] = 0;
super_Block_inst.nfree = 0;
writeinttoblock(last_free_block, blockNum);
super_Block_inst.free[super_Block_inst.nfree] = blockNum;
++super_Block_inst.nfree;
for (i = 0; i < 256; i++)
last_free_block[i] = 0;
lseek(fd, BLOCK_SIZE, 0);
int bytes_written;
if ((bytes_written = write(fd, &super_Block_inst, BLOCK_SIZE)) < BLOCK_SIZE)
{
printf("\nERROR : error in writing the super block after re-writing free block");
return 1;
}
}
return 0;
}
//function to read integer array from the required block
void read_int_from_block(unsigned int *dest, unsigned int blocknum)
{
if (blocknum >= super_Block_inst.fsize)
printf(" Block number is greater than max file system block size for reading\n");
else
{
lseek(fd, blocknum*BLOCK_SIZE, 0);
read(fd, dest, BLOCK_SIZE);
}
}
//Get a free data block.
unsigned int allocatedatablock()
{
unsigned int free_block_no;
super_Block_inst.nfree--;
free_block_no = super_Block_inst.free[super_Block_inst.nfree];
super_Block_inst.free[super_Block_inst.nfree] = 0;
if (super_Block_inst.nfree == 0)
{
int n = 0;
read_int_from_block(last_free_block, free_block_no);
super_Block_inst.nfree = last_free_block[0];
for (n = 0; n < 148; n++)
super_Block_inst.free[n] = last_free_block[n + 1];
}
return free_block_no;
}
//function to write to an inode given the inode number
//here inodenumber = (actual_inode - 1)
void write_to_inode(inode_Struct inodeinstance, unsigned int inodenumberMinusOne)
{
int bytes_written;
lseek(fd, 2 * BLOCK_SIZE + inodenumberMinusOne * INODE_SIZE, 0);
if ((bytes_written = write(fd, &inodeinstance, INODE_SIZE)) < INODE_SIZE)
printf("\n Error in writing inode number : %d", (inodenumberMinusOne + 1));
}
inode_Struct read_from_inode(unsigned int inodeNumber)
{
inode_Struct returnStruct;
lseek(fd, 2 * BLOCK_SIZE + (inodeNumber - 1) * INODE_SIZE, 0);
read(fd, &returnStruct, sizeof(inode_Struct));
return returnStruct;
}
//function to create root directory and its corresponding inode.
void create_root()
{
unsigned int i = 0;
unsigned short bytes_written;
unsigned int datablock = allocatedatablock();
root_dir.filename[0] = '.'; //root directory's file name is .
root_dir.filename[1] = '\0';
root_dir.inode = 1; // root directory's inode number is 1.
inoderef.flags = allocatedinode | directory | 000077; // flag for root directory
inoderef.nlinks = 2;
inoderef.uid = 0;
inoderef.gid = 0;
inoderef.size1 = 0;
inoderef.addr[0] = datablock;
for (i = 1; i < 28; i++)
inoderef.addr[i] = 0;
inoderef.actime[0] = 0;
inoderef.modtime[0] = 0;
inoderef.modtime[1] = 0;
write_to_inode(inoderef, 0);
lseek(fd, datablock*BLOCK_SIZE, 0);
//fill first 16 Bytes with .
if ((bytes_written = write(fd, &root_dir, 16)) < 16)
printf("\n Error in writing root directory '.' \n ");
root_dir.filename[1] = '.';
root_dir.filename[2] = '\0';
// fill .. in next 16 Bytes of data block.
if ((bytes_written = write(fd, &root_dir, 16)) < 16)
printf("\n Error in writing root directory '..' \n");
}
//function to write to directory's data block
//gets inode(always root directory's inode from mkdir) and directory (struct's) reference as inputs.
int write_to_directory(inode_Struct rootinode, dirStruct dir)
{
int duplicate = 0; //to find duplicate named directories.
unsigned short addrcount = 0;
char dirbuf[BLOCK_SIZE]; //array to
int i = 0;
for (addrcount = 0; addrcount <= 27; addrcount++)
{
lseek(fd, rootinode.addr[addrcount] * BLOCK_SIZE, 0);
for (i = 0; i < 128; i++)
{
/*char *FN1;
strcpy(FN1, new_dir.filename);
char *FN2;
strcpy(FN2, dir.filename);*/
read(fd, &new_dir, 16);
if (strcmp(new_dir.filename, dir.filename) == 0) //check for duplicate named directories
{
printf("Cannot create directory.The directory name already exists.\n");
duplicate = 1;
break;
}
}
}
if (duplicate != 1)
{
for (addrcount = 0; addrcount <= 27; addrcount++) //for each of the address elements ( addr[0],addr[1] till addr[27]), check which inode is not allocated
{
blockreaderchar(dirbuf, rootinode.addr[addrcount]);
for (i = 0; i < 128; i++) //Looping for each directory entry (2048/16 = 128 entries in total, where 2048 is block size and 16 bytes is directory entry size)
{
if (dirbuf[16 * i] == 0) // if inode is not allocated
{
memcpy(dirbuf + 16 * i, &dir.inode, sizeof(dir.inode));
memcpy(dirbuf + 16 * i + sizeof(dir.inode), &dir.filename, sizeof(dir.filename));
//using memcpy function to copy contents of filename and inode number, to store it in directory entry.
blockwriterchar(dirbuf, rootinode.addr[addrcount]);
return duplicate;
}
}
}
}
return duplicate;
}
//function initializes the file system:
//parameters - path, total number of blocks and total number of inodes as input.
//Command : initfs <file_name> <total_number_of_blocks_in_disk> <total_number_of_inodes>
//The given path directory is where the file system begins.
int initfs(char* path, unsigned int total_no_blocks, unsigned int total_inodes)
{
char buffer[BLOCK_SIZE];
int bytes_written;
if ((total_inodes % INODES_PER_BLOCK) == 0)
super_Block_inst.isize = total_inodes / INODES_PER_BLOCK;
else
super_Block_inst.isize = (total_inodes / INODES_PER_BLOCK) + 1;
super_Block_inst.fsize = total_no_blocks;
unsigned int i = 0;
if ((fd = open(path, O_RDWR | O_CREAT, 0600)) == -1) //0600 -> owner can read and write
{
printf("\n open() call for the path [%s] failed with error [%s]\n", path, strerror(errno));
return 1;
}
for (i = 0; i < 148; i++)
super_Block_inst.free[i] = 0; //initializing free array to 0 to remove junk data. free array will be stored with data block numbers shortly.
super_Block_inst.nfree = 0;
super_Block_inst.ninode = 100;
if (total_inodes < 100)
super_Block_inst.ninode = total_inodes;
for (i = 0; i < super_Block_inst.ninode; i++)
super_Block_inst.inode[i] = i; //initializing inode array to store inumbers.
super_Block_inst.flock = '1'; //flock,ilock and fmode are not used.
super_Block_inst.ilock = '1'; //initializing to fill up block
super_Block_inst.fmod = '1';
super_Block_inst.time[0] = 0;
super_Block_inst.time[1] = 0;
lseek(fd, BLOCK_SIZE, 0);
// Writing super block
if ((bytes_written = write(fd, &super_Block_inst, sizeof(super_Block)) < sizeof(super_Block)))
{
printf("\nERROR : error in writing the super block");
return 0;
}
lseek(fd, 2 * BLOCK_SIZE, 0);
// writing zeroes to all inodes in ilist
for (i = 0; i < BLOCK_SIZE; i++)
buffer[i] = 0;
for (i = 0; i < super_Block_inst.isize; i++)
write(fd, buffer, BLOCK_SIZE);
// calling chaining data blocks procedure
set_up_freeblock_links(total_no_blocks);
//filling free array to first 148 data blocks
for (i = 0; i < 148; i++)
add_freeblock(i + 2 + super_Block_inst.isize);
// Make root directory
create_root();
return 1;
}
//remove a directory of file
void RemoveDirectory(char* DirectoryName)
{
char *newPartialPath = (char*)malloc(strlen(DirectoryName));
strcpy(newPartialPath, DirectoryName);
char* partialPath = strtok(newPartialPath, "/");
char* FinalFileName = (char *)malloc(sizeof(char) * 12);
while (partialPath)
{
if (strlen(partialPath) < 11)
strcpy(FinalFileName, partialPath);
else
strncpy(FinalFileName, partialPath, 11);
partialPath = strtok(NULL, "/");
}
partialPath = strtok(DirectoryName, "/");
int ParentInodeNumber = 1;
bool skippedRoot = true;
inode_Struct TempStruct;
while (partialPath)
{
bool foundParent = false;
if (skippedRoot)
{
if (strcmp(FinalFileName, partialPath) != 0)
{
TempStruct = read_from_inode(ParentInodeNumber);
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
lseek(fd, dataBlock * BLOCK_SIZE, 0);
int index = 0;
for (; index < 16; index++)
{
dirStruct TempReadDir;
read(fd, &TempReadDir, sizeof(dirStruct));
if (strcmp(TempReadDir.filename, partialPath) == 0)
{
ParentInodeNumber = TempReadDir.inode;
foundParent = true;
strcpy(TempReadDir.filename, "");
TempReadDir.inode = 0;
lseek(fd, (dataBlock * BLOCK_SIZE) + (index * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
//AddressArrayindex = 27; //done to break from all the outer loop
//break;
}
else if (strcmp(TempReadDir.filename, "") == 0)
{
//AddressArrayindex = 27; //done to break from all the outer loop
//break;
}
else if (foundParent)
{
lseek(fd, (dataBlock * BLOCK_SIZE) + ((index-1) * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
}
}
if (foundParent)
break;
}
if (!foundParent)
{
printf("\n The given Hierarchy of directory doesn't exist. Create all the parent directories using \"mkdir\" function before trying to create sub directories \n");
return;
}
}
else
{
//this is the actual filename at the destination which conatins the contents of the external file
TempStruct = read_from_inode(ParentInodeNumber);
//unsigned short LFile = largefile | allocatedinode | plainfile | 000777;
//unsigned short SFile = allocatedinode | plainfile | 000777;
//unsigned short Directory = directory | 000777;
unsigned short check = TempStruct.flags | directory;
if (TempStruct.flags == check) //directory
{
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
add_freeblock(dataBlock);
}
}
else //file
{
int index = 0;
for (; index < 27; index++)
{
add_freeblock(TempStruct.addr[index]);
}
int finish = 0;
unsigned short filecheck = TempStruct.flags | largefile;
if (TempStruct.flags == filecheck) //large file
{
int x, y, z = 0;
unsigned int array1[256];
unsigned int array2[256];
unsigned int array3[256];
for (x = 0; x < 256; x++)
{
array1[x] = 0;
array2[x] = 0;
array3[x] = 0;
}
lseek(fd, TempStruct.addr[26] * BLOCK_SIZE, 0);
read(fd, array1, BLOCK_SIZE);
for (x = 0; x < 256; x++)
{
if (array1[x] < 2)
{
finish = 1;
break;
}
lseek(fd, array1[x] * BLOCK_SIZE, 0);
read(fd, array2, BLOCK_SIZE);
for (y = 0; y < 256; y++)
{
if (array2[y] < 2)
{
finish = 1;
break;
}
lseek(fd, array2[y] * BLOCK_SIZE, 0);
read(fd, array3, BLOCK_SIZE);
for (z = 0; z < 256; z++)
{
if (array3[z] < 2)
{
finish = 1;
break;
}
add_freeblock(array3[z]);
}
add_freeblock(array2[y]);
if (finish)
break;
}
add_freeblock(array1[x]);
if (finish)
break;
}
}
add_freeblock(TempStruct.addr[26]);
}
TempStruct.flags = 0;
int i = 0;
for (; i < 27; i++)
TempStruct.addr[i] = 0;
write_to_inode(TempStruct, (ParentInodeNumber - 1));
if (super_Block_inst.ninode < 100)
{
super_Block_inst.inode[super_Block_inst.ninode] = ParentInodeNumber;
super_Block_inst.ninode++;
}
break;
}
}
skippedRoot = true;
partialPath = strtok(NULL, "/");
}
printf("\nDirectory Deleted Successfully\n\n");
}
void CreateDirectory(char* DirectoryName, unsigned int NewInodeNumber)
{
//char* partialPath = strtok(DirectoryName, "/");
//char *FinalFileName = GetFinalFolderOrPath(DirectoryName);
char *newPartialPath = (char*)malloc(strlen(DirectoryName));
strcpy(newPartialPath, DirectoryName);
char* partialPath = strtok(newPartialPath, "/");
char* FinalFileName = (char *)malloc(sizeof(char) * 12);
while (partialPath)
{
if (strlen(partialPath) < 11)
strcpy(FinalFileName, partialPath);
else
strncpy(FinalFileName, partialPath, 11);
partialPath = strtok(NULL, "/");
}
//printf("file name is %s\n", FinalFileName);
partialPath = strtok(DirectoryName, "/");
int ParentInodeNumber = 1;
bool skippedRoot = true;
inode_Struct TempStruct;
while (partialPath)
{
bool foundParent = false;
if (skippedRoot)
{
if (strcmp(FinalFileName, partialPath) != 0)
{
TempStruct = read_from_inode(ParentInodeNumber);
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
lseek(fd, dataBlock * BLOCK_SIZE, 0);
int index = 0;
for (; index < 16; index++)
{
dirStruct TempReadDir;
read(fd, &TempReadDir, sizeof(dirStruct));
if (strcmp(TempReadDir.filename, partialPath) == 0)
{
ParentInodeNumber = TempReadDir.inode;
foundParent = true;
//AddressArrayindex = 27; //done to break from all the outer loop
break;
}
else if (strcmp(TempReadDir.filename, "") == 0)
{
//AddressArrayindex = 27; //done to break from all the outer loop
break;
}
}
}
if (!foundParent)
{
printf("\n The given Hierarchy of directory doesn't exist. Create all the parent directories using \"mkdir\" function before trying to create sub directories \n");
return;
}
}
else
{
//this is the actual filename at the destination which conatins the contents of the external file
TempStruct = read_from_inode(ParentInodeNumber);
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
lseek(fd, dataBlock * BLOCK_SIZE, 0);
int index = 0;
for (; index < 16; index++)
{
dirStruct TempReadDir;
read(fd, &TempReadDir, sizeof(dirStruct));
if (strcmp(TempReadDir.filename, FinalFileName) == 0)
{
printf("Cannot create the given directory as a directory with the same name already exists.\n");
AddressArrayindex = 27; //done to break from all the outer loop
break;
}
else if (strcmp(TempReadDir.filename, "") == 0)
{
unsigned int block_num = allocatedatablock();
strcpy(TempReadDir.filename, FinalFileName);
TempReadDir.inode = NewInodeNumber;
lseek(fd, (dataBlock * BLOCK_SIZE) + (index * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
// set up this directory's inode and store at inode number - NewInodeNumber
inoderef.flags = allocatedinode | directory | 000777;
inoderef.nlinks = 2;
inoderef.uid = '0';
inoderef.gid = '0';
inoderef.size1 = 128;
int i = 0;
for (i = 1; i < 28; i++)
inoderef.addr[i] = 0;
inoderef.addr[0] = block_num;
inoderef.actime[0] = 0;
inoderef.modtime[0] = 0;
inoderef.modtime[1] = 0;
write_to_inode(inoderef, (NewInodeNumber - 1));
strcpy(TempReadDir.filename, ".");
TempReadDir.inode = NewInodeNumber;
lseek(fd, (block_num * BLOCK_SIZE) + (0 * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
strcpy(TempReadDir.filename, "..");
TempReadDir.inode = ParentInodeNumber;
lseek(fd, (block_num * BLOCK_SIZE) + (1 * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
printf("\nDirectory created \n\n");
AddressArrayindex = 27; //done to break from all the outer loop
break;
}
}
}
break;
}
}
skippedRoot = true;
partialPath = strtok(NULL, "/");
}
}
int CheckParentExists(char* path, int iNumber)
{
//char* partialPath = strtok(path, "/");
//char *FinalFileName = GetFinalFolderOrPath(path);
char* partialPath = strtok(path, "/");
char* FinalFileName = (char *)malloc(sizeof(char) * 12);
while (partialPath)
{
if (strlen(partialPath) < 11)
strcpy(FinalFileName, partialPath);
else
strncpy(FinalFileName, partialPath, 11);
partialPath = strtok(NULL, "/");
}
//printf("file name is %s\n", FinalFileName);
partialPath = strtok(path, "/");
int ParentInodeNumber = 1;
bool skippedRoot = true;
inode_Struct TempStruct;
while (partialPath)
{
bool foundParent = false;
if (skippedRoot)
{
if (strcmp(FinalFileName, partialPath) != 0)
{
TempStruct = read_from_inode(ParentInodeNumber);
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
lseek(fd, dataBlock * BLOCK_SIZE, 0);
int index = 0;
for (; index < 16; index++)
{
dirStruct TempReadDir;
read(fd, &TempReadDir, sizeof(dirStruct));
if (strcmp(TempReadDir.filename, partialPath) == 0)
{
ParentInodeNumber = TempReadDir.inode;
foundParent = true;
break;
}
else if (strcmp(TempReadDir.filename, "") == 0)
{
break;
}
}
}
if (!foundParent)
{
printf("\nThe given Hierarchy of directory doesn't exist. Create all the parent directories using \"mkdir\" function before trying to copy a file to that location \n");
return;
}
}
else
{
//this is the actual filename at the destination which conatins the contents of the external file
TempStruct = read_from_inode(ParentInodeNumber);
int AddressArrayindex = 0;
for (; AddressArrayindex < 27; AddressArrayindex++)
{
unsigned int dataBlock = TempStruct.addr[AddressArrayindex];
lseek(fd, dataBlock * BLOCK_SIZE, 0);
int index = 0;
for (; index < 16; index++)
{
dirStruct TempReadDir;
read(fd, &TempReadDir, sizeof(dirStruct));
if (strcmp(TempReadDir.filename, "") == 0)
{
strcpy(TempReadDir.filename, FinalFileName);
TempReadDir.inode = iNumber;
lseek(fd, (dataBlock * BLOCK_SIZE) + (index * (sizeof(TempReadDir))), 0);
write(fd, &TempReadDir, sizeof(TempReadDir));
break;
}
}
}
break;
}
}
skippedRoot = true;
partialPath = strtok(NULL, "/");
}
}
int cpin(char* source, char* dest)
{
//printf("cpin start\n");
char ReadBuffer[BLOCK_SIZE];
int sfd;
unsigned long sfileSize = 0;
unsigned long TotalBytesRead = 0;
int CurrentBytesRead = 0;
FILE *fp = fopen(source, "r");
fseek(fp, 0, SEEK_END);
sfileSize = ftell(fp);
fclose(fp);
//printf("\nfile size is %ld\n", sfileSize);
//open external file
if ((sfd = open(source, O_RDONLY)) == -1)
{
printf("\nCan't open Source file %s. Permission Issue \n", source);
return -1;
}
//printf("\nSource file open success %s\n", source);
unsigned int inumber = GetFreeInode();
//printf("inode no %d\n", inumber);
if (inumber < 2) // 1 for root
{
printf("Error : ran out of inodes \n");
return;
}
/*if (!CheckParentExists(dest, inumber))
{
printf("\n Invalid file-System File Name \n");
return -1;
}*/
char* partialPath = strtok(dest, "/");
char* FinalFileName = (char *)malloc(sizeof(char) * 12);
while (partialPath)
{
if (strlen(partialPath) < 11)
strcpy(FinalFileName, partialPath);
else
strncpy(FinalFileName, partialPath, 11);
partialPath = strtok(NULL, "/");
}
//printf("file name is %s\n", FinalFileName);
//new file - modified V6 file system
new_dir.inode = inumber;
if (strlen(FinalFileName) < 12)
{
memcpy(new_dir.filename, FinalFileName, strlen(FinalFileName));
}
else
{
memcpy(new_dir.filename, dest, 11);
}
//New file - inode struct
inoderef.flags = plainfile | allocatedinode | 000777;
inoderef.nlinks = 1;
inoderef.uid = '0';
inoderef.gid = '0';
inoderef.size1 = 0;
if (sfileSize <= 27648)
{
//small file
int index = 0;
while (1)
{
memset(ReadBuffer, 0x00, BLOCK_SIZE);
if ((CurrentBytesRead = read(sfd, ReadBuffer, BLOCK_SIZE)) != 0 && index < 27)
{
int NewBlockNum = allocatedatablock();
//printf("Blk_no is %d\n", NewBlockNum);