-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsInit.c
More file actions
122 lines (98 loc) · 3.09 KB
/
fsInit.c
File metadata and controls
122 lines (98 loc) · 3.09 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
/**************************************************************
* Class:: CSC-415-02 Summer 2024
* Name:: Preet Vithani, Igor Tello, Phillip Davis, Jared Aung
* Student IDs:: 923575806, 923043807, 923980431, 922772159
* GitHub-Name:: R3plug
* Group-Name:: Team Kentucky Kernels
* Project:: Basic File System
*
* File:: fsInit.c
*
* Description:: Main driver for file system assignment.
*
* This file is where you will start and initialize your system
*
**************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include "fsLow.h"
#include "mfs.h"
#include "fsInit.h"
#include "dirLow.h"
//DE* root;
int initFileSystem(uint64_t numberOfBlocks, uint64_t blockSize)
{
//printf("Kentucky Kernels\n");
printf("Initializing File System with %ld blocks with a block size of %ld\n", numberOfBlocks, blockSize);
// allocate Volume Control Block
VCB *vcb = (VCB *)calloc(1, blockSize);
if (!vcb) // check allocation was successful
{
perror("VCB Allocation failed");
return -1;
}
int result = LBAread(vcb, 1, 0);
if(result != 1){
printf("could not LBAread vcb\n");
return -1;
}
if(strcmp(vcb->signature, FS_SIGNATURE) == 0){
printf("System already initialized\n");
DE *root = malloc(vcb->rootDirBlocks * blockSize);
if(!root){
printf("Failed to allocate root\n");
free(vcb);
return -1;
}
printf("Reading root: start=%d, blocks=%d\n", vcb->rootDirStart, vcb->rootDirBlocks);
int res = LBAread(root, vcb->rootDirBlocks, vcb->rootDirStart);
printf("LBA returned: %d\n", res);
if(res != vcb->rootDirBlocks){
printf("Failed to read root from disk\n");
free(root);
free(vcb);
return -1;
}
setRootDir(root);
printf("Root ptr: %p\n", getRootDir());
free(vcb);
return 0;
}
if (initFreeSpace(numberOfBlocks, BLOCK_SIZE) != 0)
{
printf("Error allocating free space\n");
free(vcb);
return -1;
}
// create Root Directory containing 50 directory entires
DE *root = createDir(50, NULL);
setRootDir(root);
//Fill VCB
strncpy(vcb->signature, FS_SIGNATURE, 8);
vcb->blockSize = blockSize;
vcb->totalBlocks = numberOfBlocks;
vcb->extentTableStart = 1;
vcb->extentTableBlocks = EXTENT_TABLE_BLOCKS;
vcb->rootDirStart = root->mem.extents[0].block;
vcb->rootDirBlocks = root->mem.extents[0].count;
vcb->freeBlockStart = 1;
vcb->createTime = time(NULL); //current time
vcb->lastMountTime = time(NULL); //current time
// write VCB to block
if(1!=LBAwrite(vcb, 1, 0)){
printf("VCB write fail\n");
return -1;
};
//printf("blockSize: %d total blocks: %d Extent table start: %d extent table blocks: %d \n", vcb->blockSize,vcb->totalBlocks,vcb->extentTableStart,vcb->extentTableBlocks);
//printf("rootDir start: %d root dir blocks %d free block start %d create %ld mount %ld\n", vcb->rootDirStart, vcb->rootDirBlocks, vcb->freeBlockStart, vcb->createTime, vcb->lastMountTime);
//setRootDir(root);//Sets a variable to hold the root while running
free(vcb); // free allocated memory
return 0;
}
void exitFileSystem()
{
printf("System exiting\n");
}