|
| 1 | +#include "createDisk.h" |
| 2 | + |
| 3 | + |
| 4 | +/* |
| 5 | + createDisk creates the disk file if not present. |
| 6 | + if format is equal to zero then the function creates the disk but does not format it. |
| 7 | + if format is not equal to zero then the function will create and format the disk. |
| 8 | + Formatting is done as follows: |
| 9 | + 1. A memory copy of the disk is maintained. This copy contains NO_BLOCKS_TO_COPY + EXTRA_BLOCKS (in this case 13 + 1) blocks. |
| 10 | + The extra block is a temporary block. This memory copy is called the virtual disk. This is first cleared. |
| 11 | + 2. Then the memory freelist is initialised. |
| 12 | + 3. The fat blocks are also initialised. The basic block entries are all set to -1. The memory copy is then committed to the |
| 13 | + disk file. |
| 14 | + 4. Finally the entry for init process is made. |
| 15 | +*/ |
| 16 | +void createDisk(int format){ |
| 17 | + int fd; |
| 18 | + if(format){ |
| 19 | + fd = open(DISK_NAME, O_CREAT | O_TRUNC | O_SYNC, 0666); |
| 20 | + clearVirtDisk(); |
| 21 | + close(fd); |
| 22 | + // loadFileToVirtualDisk(); note: commented this line |
| 23 | + int i=0,j=0; |
| 24 | + for(j=0; j<NO_OF_FREE_LIST_BLOCKS; j++){ |
| 25 | + if(j == 0) |
| 26 | + for(i=0;i<DATA_START_BLOCK + INIT_SIZE ;i++) |
| 27 | + storeInteger(disk[FREE_LIST_START_BLOCK].word[i], 1); |
| 28 | + else |
| 29 | + i=0; |
| 30 | + |
| 31 | + for( ;i<BLOCK_SIZE;i++) |
| 32 | + storeInteger(disk[FREE_LIST_START_BLOCK + j].word[i], 0); |
| 33 | + writeToDisk(FREE_LIST_START_BLOCK + j, FREE_LIST_START_BLOCK+j); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + for(j=0; j<NO_OF_FAT_BLOCKS; j++){ |
| 38 | + for(i=FAT_BASICBLOCK; i<BLOCK_SIZE; i=i+FAT_ENTRY_SIZE){ |
| 39 | + storeInteger(disk[FAT_START_BLOCK + j].word[i], -1); |
| 40 | + } |
| 41 | + writeToDisk(FAT_START_BLOCK+j, FAT_START_BLOCK+j); |
| 42 | + } |
| 43 | + |
| 44 | + initializeINIT(); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + fd = open(DISK_NAME, O_CREAT, 0666); |
| 49 | + close(fd); |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | + |
0 commit comments