-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsaccess.c
More file actions
935 lines (809 loc) · 26.5 KB
/
fsaccess.c
File metadata and controls
935 lines (809 loc) · 26.5 KB
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
// ***********************************************************************
// File Name: fsaccess.c
// Program written by : Harish Srinivasa(hxs134430) and Pradeep Anantharaman(pxa130130)
// Objective: To re design UNIX V6 file system to remove 16 MB limitation
// on file size using given guidelines.
// Description: This program creates an environment in which required
// features(Project 2 requirements) are provided to user to access the
// modified Unix Version 6 file system. It reads series of commands
// from the user and executes the same.
// Supported commands: initfs,cpin,cpout,mkdir and q.
// To execute program, the following commands can be used on Linux server:
// gcc -o fsaccess fsaccess.c
// ./fsaccess
// ***********************************************************************
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
//structure definition for directory
typedef struct
{
unsigned short inode;
char filename[13];
}dir;
dir newdir;
dir dir1; //second reference used while comparing directories for duplicates
//structure definition for superblock (reference: Unix V6 file system manual)
typedef struct {
unsigned short isize;
unsigned short fsize;
unsigned short nfree;
unsigned short free[100];
unsigned short ninode;
unsigned short inode[100];
char flock;
char ilock;
char fmod;
unsigned short time[2];
} fs_super;
fs_super super;
//structure definition for inode (reference: Unix V6 file system manual)
typedef struct {
unsigned short flags;
char nlinks;
char uid;
char gid;
char size0;
unsigned int size1;
unsigned int addr[28]; // since word size = 4
unsigned short actime[1];
unsigned short modtime[2];
} fs_inode;
fs_inode inoderef;
//constants globally defined. (reference: Unix V6 file system manual)
const unsigned short BLOCK_SIZE = 2048;
const unsigned short ISIZE = 128;
const unsigned short inode_alloc = 0100000;
const unsigned short plainfile = 000000;
const unsigned short largefile = 010000;
const unsigned short directory = 040000;
//global variables
int fd ; //file descriptor
unsigned int chainarray[256]; //array used while chaining data blocks.
//functions used:
int initialize_fs(char* path, unsigned short total_blcks,unsigned short total_inodes);
void create_root();
void blockreaderchar(char *target, unsigned int blocknum);
void blockreaderint(int *target, unsigned int blocknum);
void blockwriterint(unsigned int *target, unsigned int blocknum);
void blockwriterchar(char *target, unsigned int blocknum);
void inodewriter(fs_inode inodeinstance, unsigned int inodenumber);
void freeblock(unsigned int block);
void chaindatablocks(unsigned short total_blcks);
unsigned int allocatedatablock();
unsigned short allocateinode();
void mkdirectory(char* filename, unsigned int newinode);
int directorywriter(fs_inode rootinode, dir dir);
int makeindirectblocks(int fd,int block_num);
void cpin(char* src, char* targ);
void cpout(char* src, char* targ);
int main()
{
int fsinit = 0;
char input[256];
char *parser;
unsigned short n = 0;
char dirbuf[BLOCK_SIZE];
int i =0;
unsigned short bytes_written;
unsigned int number_of_blocks =0, number_of_inodes=0;
printf("Enter command:\n");
while(1)
{
scanf(" %[^\n]s", input);
parser = strtok(input," ");
if(strcmp(parser, "initfs")==0)
{
char *filepath;
char *num1, *num2;
filepath = strtok(NULL, " ");
num1 = strtok(NULL, " ");
num2 = strtok(NULL, " ");
if(access(filepath, F_OK) != -1)
{
if((fd = open(filepath,O_RDWR,0600))== -1)
{
printf("\n filesystem already exists but open() failed with error [%s]\n",strerror(errno));
return 1;
}
printf("filesystem already exists and the same will be used.\n");
fsinit=1;
}
else
{
if (!num1 || !num2)
printf(" All arguments(path, number of inodes and total number of blocks) have not been entered\n");
else
{
number_of_blocks = atoi(num1);
number_of_inodes = atoi(num2);
if(initialize_fs(filepath,number_of_blocks, number_of_inodes))
{
printf("The file system is initialized\n");
fsinit = 1;
}
else
{
printf("Error initializing file system. Exiting... \n");
return 1;
}
}
}
parser = NULL;
}
else if(strcmp(parser, "mkdir")==0)
{
char *dirname;
if(fsinit == 0)
printf("The file system is not initialized. Please retry after initializing file system\n");
else
{
dirname = strtok(NULL, " ");
if(!dirname)
printf("No directory name entered. Please retry\n");
else
{
unsigned int dirinum = allocateinode();
if(dirinum < 0)
{
printf("Error : ran out of inodes \n");
return 1;
}
mkdirectory(dirname,dirinum);
}
}
parser = NULL;
}
else if(strcmp(parser, "cpin")==0)
{
char *targname;
char *srcname;
if(fsinit == 0)
printf("The file system is not initialized. Please retry after initializing file system\n");
else
{
srcname = strtok(NULL, " ");
targname = strtok(NULL, " ");
if(!srcname || !targname )
printf("Required file names(source and target file names) have not been entered. Please retry\n");
else
{
cpin(srcname,targname);
}
}
parser = NULL;
}
else if(strcmp(parser, "cpout")==0)
{
char *targnameout;
char *srcnameout;
if(fsinit == 0)
printf("The file system is not initialized. Please retry after initializing file system\n");
else
{
srcnameout = strtok(NULL, " ");
targnameout = strtok(NULL, " ");
if(!srcnameout || !srcnameout )
printf("Required file names(source and target file names) have not been entered. Please retry\n");
else
{
cpout(srcnameout,targnameout);
}
}
parser = NULL;
}
else if(strcmp(parser, "q")==0)
{
lseek(fd,BLOCK_SIZE,0);
if((bytes_written =write(fd,&super,BLOCK_SIZE)) < BLOCK_SIZE)
{
printf("\nERROR : error in writing the super block");
return 1;
}
return 0;
}
else
{
printf("\nInvalid command\n ");
}
}
}
//function initializes the file system: takes path, total number of blocks and total number of
//inodes as input. Command : initfs <directory_name_in_foreign_operating_system> <total_number_of_blocks> <total_number_of_inodes>
//The given path directory is where the file system begins.
int initialize_fs(char* path, unsigned short total_blcks,unsigned short total_inodes )
{
char buffer[BLOCK_SIZE];
int bytes_written;
if((total_inodes%16) == 0)
super.isize = total_inodes/16;
else
super.isize = (total_inodes/16) + 1;
super.fsize = total_blcks;
unsigned short i = 0;
if((fd = open(path,O_RDWR|O_CREAT,0600))== -1)
{
printf("\n open() failed with error [%s]\n",strerror(errno));
return 1;
}
for (i = 0; i<100; i++)
super.free[i] = 0; //initializing free array to 0 to remove junk data. free array will be stored with data block numbers shortly.
super.nfree = 0;
super.ninode = 100;
for (i=0; i < 100; i++)
super.inode[i] = i; //initializing inode array to store inumbers.
super.flock = 'f'; //flock,ilock and fmode are not used.
super.ilock = 'i'; //initializing to fill up block
super.fmod = 'f';
super.time[0] = 0;
super.time[1] = 0;
lseek(fd,BLOCK_SIZE,0);
// Writing to super block
if((bytes_written =write(fd,&super,BLOCK_SIZE)) < BLOCK_SIZE)
{
printf("\nERROR : error in writing the super block");
return 0;
}
// writing zeroes to all inodes in ilist
for (i=0; i<BLOCK_SIZE; i++)
buffer[i] = 0;
for (i=0; i < super.isize; i++)
write(fd,buffer,BLOCK_SIZE);
// calling chaining data blocks procedure
chaindatablocks(total_blcks);
//filling free array to first 100 data blocks
for (i=0; i<100; i++)
freeblock(i+2+super.isize);
// Make root directory
create_root();
return 1;
}
//function to read character array from the required block
void blockreaderchar(char *target, unsigned int blocknum)
{
if (blocknum > super.isize + super.fsize + 2)
printf(" Block number is greater than max file system block size for reading\n"); */
else{
lseek(fd,blocknum*BLOCK_SIZE,0);
read(fd, target, BLOCK_SIZE);
}
}
//function to read integer array from the required block
void blockreaderint(int *target, unsigned int blocknum)
{
if (blocknum > super.isize + super.fsize + 2)
printf(" Block number is greater than max file system block size for reading\n"); */
else{
lseek(fd,blocknum*BLOCK_SIZE,0);
read(fd, target, BLOCK_SIZE);
}
}
//function to write integer array to the required block
void blockwriterint(unsigned int *target, unsigned int blocknum)
{
int bytes_written;
if (blocknum > super.isize + super.fsize + 2)
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, target, BLOCK_SIZE)) < BLOCK_SIZE)
printf("\n Error in writing block number : %d", blocknum);
}
}
//function to write character array to the required block
void blockwriterchar(char *target, unsigned int blocknum)
{
int bytes_written;
if (blocknum > super.isize + super.fsize + 2)
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, target, BLOCK_SIZE)) < BLOCK_SIZE)
printf("\n Error in writing block number : %d", blocknum);
}
}
// Data blocks chaining procedure
void chaindatablocks(unsigned short total_blcks)
{
unsigned int emptybuffer[512]; // buffer to fill with zeros to entire blocks. Since integer size is 4 bytes, 512 * 4 = 2048 bytes.
unsigned int blockcounter;
unsigned int no_chunks = total_blcks/100; //splitting into blocks of 100
unsigned int remainingblocks = total_blcks%100; //getting remaining/left over blocks
unsigned int i = 0;
for (i=0; i<512; i++)
emptybuffer[i] = 0; //setting character array to 0 to remove any bad/junk data
for (i=0; i<512; i++)
chainarray[i] = 0; //setting integer array to 0 to remove any bad/junk data
//chaining for chunks of blocks 100 blocks at a time
for (blockcounter=0; blockcounter < no_chunks; blockcounter++)
{
chainarray[0] = 100;
for (i=0;i<100;i++)
{
if(blockcounter == (no_chunks - 1) && remainingblocks == 0 && i==0)
{
chainarray[i+1] = 0;
continue;
}
chainarray[i+1] = 2+super.isize+i+100*(blockcounter+1);
}
blockwriterint(chainarray, 2+super.isize+100*blockcounter);
for (i=1; i<=100;i++)
blockwriterint(emptybuffer, 2+super.isize+i+ 100*blockcounter);
}
//chaining for remaining blocks
chainarray[0] = remainingblocks;
chainarray[1] = 0;
for (i=1;i<=remainingblocks;i++)
chainarray[i+1] = 2+super.isize+i+(100*blockcounter);
blockwriterint(chainarray, 2+super.isize+(100*blockcounter));
for (i=1; i<=remainingblocks;i++)
blockwriterint(chainarray, 2+super.isize+1+i+(100*blockcounter));
for (i=0; i<512; i++)
chainarray[i] = 0;
}
//function to write to an inode given the inode number
void inodewriter(fs_inode inodeinstance, unsigned int inodenumber)
{
int bytes_written;
lseek(fd,2*BLOCK_SIZE+inodenumber*ISIZE,0);
if((bytes_written=write(fd,&inodeinstance,ISIZE)) < ISIZE)
printf("\n Error in writing inode number : %d", inodenumber);
}
//function to create root directory and its corresponding inode.
void create_root()
{
unsigned int i = 0;
unsigned short bytes_written;
unsigned int datablock = allocatedatablock();
for (i=0;i<14;i++)
newdir.filename[i] = 0;
newdir.filename[0] = '.'; //root directory's file name is .
newdir.filename[1] = '\0';
newdir.inode = 1; // root directory's inode number is 1.
inoderef.flags = inode_alloc | directory | 000077; // flag for root directory
inoderef.nlinks = 2;
inoderef.uid = '0';
inoderef.gid = '0';
inoderef.size0 = '0';
inoderef.size1 = ISIZE;
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;
inodewriter(inoderef, 0);
lseek(fd, datablock*BLOCK_SIZE, 0);
//filling 1st entry with .
if((bytes_written = write(fd, &newdir, 16)) < 16)
printf("\n Error in writing root directory \n ");
newdir.filename[1] = '.';
newdir.filename[2] = '\0';
// filling with .. in next entry(16 bytes) in data block.
if((bytes_written = write(fd, &newdir, 16)) < 16)
printf("\n Error in writing root directory ");
}
//free data blocks and initialize free array
void freeblock(unsigned int block)
{
super.free[super.nfree] = block;
++super.nfree;
}
//function to get a free data block. Also decrements nfree for each pass
unsigned int allocatedatablock()
{
unsigned int block;
super.nfree--;
block = super.free[super.nfree];
super.free[super.nfree] = 0;
if (super.nfree == 0)
{
int n=0;
blockreaderint(chainarray, block);
super.nfree = chainarray[0];
for(n=0; n<100; n++)
super.free[n] = chainarray[n+1];
}
return block;
}
//getting free inode
//only allocation performed
//if inode reaches 0, error caught but cannot proceed from that point.
unsigned short allocateinode()
{
unsigned short inumber;
unsigned int i = 0;
super.ninode--;
inumber = super.inode[super.ninode];
return inumber;
}
//create a new directory with the given name
//creates all directories under root directory only.
void mkdirectory(char* filename, unsigned int newinode)
{
int blocks_read;
unsigned int parentinum = 1; //since parent is always root directory for this project, inumber is 1.
char buffertemp[BLOCK_SIZE];
int i =0;
unsigned int block_num = allocatedatablock();
strncpy(newdir.filename,filename,14); //string copy filename contents to directory structure's field
newdir.inode = newinode;
lseek(fd,2*BLOCK_SIZE,0);
inoderef.nlinks++;
// set up this directory's inode
inoderef.flags = inode_alloc | directory | 000777;
inoderef.nlinks = 2;
inoderef.uid = '0';
inoderef.gid = '0';
inoderef.size0 = '0';
inoderef.size1 = 128;
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;
inodewriter(inoderef, newinode);
lseek(fd,2*BLOCK_SIZE,0);
blocks_read = read(fd,&inoderef,128);
inoderef.nlinks++;
if(directorywriter(inoderef, newdir))
return;
for (i=0;i<BLOCK_SIZE ;i++)
buffertemp[i] = 0;
// copying to inode numbers and filenames to directory's data block for ".".
memcpy(buffertemp, &newinode, sizeof(newinode)); //memcpy(used for fixed width character array inbuilt function copies n bytes from memory area newinode to memory area buffertemp
buffertemp[2] = '.';
buffertemp[3] = '\0';
// copying to inode numbers and filenames to directory's data block for ".."
memcpy(buffertemp+16, &parentinum, sizeof(parentinum)); //memcpy(used for fixed width character array inbuilt function copies n bytes from memory area newinode to memory area buffertemp
buffertemp[18] = '.';
buffertemp[19] = '.';
buffertemp[20] = '\0';
blockwriterchar(buffertemp, block_num); //writing character array to newly allocated block
printf("\n Directory created \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 directorywriter(fs_inode rootinode, dir 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++)
{
read(fd, &dir1, 16);
if(strcmp(dir1.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;
}
//cpin or copy in function :
//command : cpin <source_file_path_in_foreign_operating_system> <destination_filename_in_v6>
//performs of copy of contents from source file in foreign operating system(external file) to
//existing or newly created destination file given in the command.
void cpin(char* src, char* targ)
{
int indirect = 0;
int indirectfn_return =1;
char reader[BLOCK_SIZE];
int bytes_read;
int srcfd;
int extfilesize =0;
//open external file
if((srcfd = open(src, O_RDONLY)) == -1)
{
printf("\nerror opening file: %s \n",src);
return;
}
unsigned int inumber = allocateinode();
if(inumber < 0)
{
printf("Error : ran out of inodes \n");
return;
}
unsigned int newblocknum;
//preapare new file in V6 file system
newdir.inode = inumber;
memcpy(newdir.filename,targ,strlen(targ));
//write inode for the new file
inoderef.flags = inode_alloc | plainfile | 000777;
inoderef.nlinks = 1;
inoderef.uid = '0';
inoderef.gid = '0';
inoderef.size0='0';
int i =0;
//start reading external file and perform file size calculation simultaneously
while(1)
{
if((bytes_read=read(srcfd,reader,BLOCK_SIZE)) != 0 )
{
newblocknum = allocatedatablock();
blockwriterchar(reader,newblocknum);
inoderef.addr[i] = newblocknum;
// When bytes returned by read() system call falls below the block size of
//2048, reading and writing are complete. Print file size in bytes and exit
if(bytes_read < BLOCK_SIZE)
{
extfilesize = i*BLOCK_SIZE + bytes_read;
printf("Small file copied\n");
inoderef.size1 = extfilesize;
printf("File size = %d bytes\n",extfilesize);
break;
}
i++;
//if the counter i exceeds 27(maximum number of elements in addr[] array,
//transfer control to new function that creates indirect blocks which
//handles large files(file size > 56 KB).
if(i>27)
{
indirectfn_return=makeindirectblocks(srcfd,inoderef.addr[0]);
indirect = 1;
break;
}
}
// When bytes returned by read() system call is 0,
// reading and writing are complete. Print file size in bytes and exit
else
{
extfilesize = i*BLOCK_SIZE;
printf("Small file copied\n");
inoderef.size1 = extfilesize;
printf("File size = %d bytes\n",extfilesize);
break;
}
}
inoderef.actime[0] = 0;
inoderef.modtime[0] = 0;
inoderef.modtime[1] = 0;
//if call is made to function that creates indirect blocks,
//it is a large file. Set flags for large file to 1.
if(indirect == 1)
{
inoderef.flags = inoderef.flags | largefile;
}
//write to inode and directory data block
if( indirectfn_return > -1)
{
inodewriter(inoderef,inumber);
lseek(fd,2*BLOCK_SIZE,0);
read(fd,&inoderef,ISIZE);
inoderef.nlinks++;
directorywriter(inoderef,newdir);
}
if(indirectfn_return == -1)
{
printf("\nExitting as file is large..");
}
}
//function that creates indirect blocks. handles large file (file size > 56 KB)
//largest file size handled : ( 28 * 512 * 2048 ) /1024 = 28672 KB.
int makeindirectblocks(int fd,int block_num)
{
char reader[BLOCK_SIZE];
unsigned int indirectblocknum[512]; //integer array to store indirect blocknumbers
int i=0;
int j=0;
int bytes_read;
int blocks_written = 0;
int extfilesize = 28 * BLOCK_SIZE; //filesize is initialized to small file size since data would have been read upto this size.
for(i=0;i<28;i++)
indirectblocknum[i] = inoderef.addr[i]; //transfer existing block numbers in addr[] array to new temporary array
inoderef.addr[0] = allocatedatablock(); //allocate a data block which will be used to store the temporary integer array of indirect block numbers
for(i=1;i<28;i++)
inoderef.addr[i] = 0;
i=28;
while(1)
{
if((bytes_read=read(fd,reader,BLOCK_SIZE)) != 0 )
{
indirectblocknum[i] = allocatedatablock(); //allocate a data block which will be used to store the temporary integer array of indirect block numbers
blockwriterchar(reader,indirectblocknum[i]);
i++;
// When bytes returned by read() system call falls below the block size of
//2048, reading and writing are complete. Print file size in bytes and exit
if(bytes_read < BLOCK_SIZE)
{
blockwriterint(indirectblocknum, inoderef.addr[j]);
printf("Large File copied\n");
extfilesize = extfilesize + blocks_written*BLOCK_SIZE + bytes_read;
inoderef.size1 = extfilesize;
printf("File size = %d bytes\n",extfilesize);
break;
}
blocks_written++;
//When counter i reaches 512, first indirect block is full. So reset counters to 0
//allocate new block to store it in addr[] array that will be the new indirect block.
if(i>511 && j<=27)
{
blockwriterint(indirectblocknum, inoderef.addr[j]);
inoderef.addr[++j] = allocatedatablock();
i=0;
extfilesize = extfilesize + 512*BLOCK_SIZE;
blocks_written=0;
}
//if all the elements in addr[] array have been exhausted with indirect blocks, maximum capacity of
//28672 KB has been reached. Throw an error that the file is too large for this file system.
if(j>27)
{
printf("This file copy is not supported by the file system as the file is very large\n");
return -1;
break;
}
}
// When bytes returned by read() system call is 0,
// reading and writing are complete. Print file size in bytes and exit
else
{
blockwriterint(indirectblocknum, inoderef.addr[j]);
inoderef.size1 = extfilesize;
printf("Large File copied\n");
printf("File size = %d bytes\n",extfilesize);
break;
}
}
return 0;
}
//cpout or copy out function :
//command : cpout <source_file_name_in_v6> <destination_filename_in_foreign_operating_system>
//performs of copy of contents from existing source file in V6 to
//existing or newly created destination file in foreign operating system(external file)
//given in the command.
void cpout(char* src, char* targ)
{
int indirect = 0;
int found_dir = 0;
int src_inumber;
char reader[BLOCK_SIZE]; //reader array to read characters (contents of blocks or file contents)
int reader1[BLOCK_SIZE]; //reader array to read integers (block numbers contained in add[] array)
int bytes_read;
int targfd;
int i=0;
int j=0;
int addrcount=0;
int total_blocks=0;
int remaining_bytes =0;
int indirect_block_chunks = 0; //each chunk of indirect blocks contain 512 elements that point to data blocks
int remaining_indirectblks=0;
int indirectblk_counter=0;
int bytes_written=0;
//open or create external file(target file) for read and write
if((targfd = open(targ, O_RDWR | O_CREAT, 0600)) == -1)
{
printf("\nerror opening file: %s\n",targ);
return;
}
lseek(fd,2*BLOCK_SIZE,0);
read(fd,&inoderef,ISIZE);
//find the source V6 file in the root directory
for (addrcount=0;addrcount <= 27;addrcount++)
{
if(found_dir !=1)
{
lseek(fd,(inoderef.addr[addrcount]*BLOCK_SIZE), 0);
for (i=0;i<128;i++)
{ if(found_dir !=1)
{
read(fd, &dir1, 16);
if(strcmp(dir1.filename,src) == 0)
{
src_inumber = dir1.inode;
found_dir =1;
}
}
}
}
}
if(src_inumber == 0)
{
printf("File not found in the file system. Unable to proceed\n");
return;
}
lseek(fd, (2*BLOCK_SIZE + ISIZE*src_inumber), 0);
read(fd, &inoderef, 128);
//check if file is directory. If so display information and return.
if(inoderef.flags & directory)
{
printf("The given file name is a directory. A file is required. Please retry.\n");
return;
}
//check if file is a plainfile. If so display information and return.
if((inoderef.flags & plainfile))
{
printf("The file name is not a plain file. A plain file is required. Please retry.\n");
return;
}
//check if file is a large file
if(inoderef.flags & largefile)
{
indirect = 1;
}
total_blocks = (int)ceil(inoderef.size1 / 2048.0);
remaining_bytes = inoderef.size1 % BLOCK_SIZE;
//read and write small file to external file
if(indirect == 0) //check if it is a small file. indirect = 0 implies the function that makes indirect blocks was not called during cpin.
{
printf("file size = %d \n",inoderef.size1);
for(i=0 ; i < total_blocks ; i++)
{
blockreaderchar(reader,inoderef.addr[i]);
//if counter reaches end of the blocks, write remaining bytes(bytes < 2048) and return.
if( i == (total_blocks - 1))
{
write(targfd, reader, remaining_bytes);
printf("Contents were transferred to external file\n");
return;
}
write(targfd, reader, BLOCK_SIZE);
}
}
//read and write large file to external file
if(indirect == 1) //check if it is a large file. indirect = 1 implies the function that makes indirect blocks was called during cpin.
{
total_blocks = inoderef.size1 / 2048;
indirect_block_chunks = (int)ceil(total_blocks/512.0); //each chunk of indirect blocks contain 512 elements that point to data blocks
remaining_indirectblks = total_blocks%512;
printf("file size = %d \n",inoderef.size1);
//Loop for chunks of indirect blocks
for(i=0 ;i < indirect_block_chunks; i++)
{
blockreaderint(reader1,inoderef.addr[i]); //store block numbers contained in addr[] array in integer reader array )
//if counter reaches last chunk of indirect blocks, program loops the remaining and exits after writing the remaining bytes
if(i == (indirect_block_chunks - 1))
total_blocks = remaining_indirectblks;
for(j=0; j < 512 && j < total_blocks; j++)
{
blockreaderchar(reader,reader1[j]); //store block contents pointed by addr[] array in character reader array )
if((bytes_written = write(targfd, reader, BLOCK_SIZE)) == -1)
{
printf("\n Error in writing to external file\n");
return;
}
if( j == (total_blocks - 1))
{
write(targfd, reader, remaining_bytes);
printf("Contents were transferred to external file\n");
return;
}
}
}
}
}
/*****************************END*********************************************************************************/
/*****************************************************************************************************************/
/*****************************************************************************************************************/
/*****************************************************************************************************************/
/*****************************************************************************************************************/
/*****************************************************************************************************************/