-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsInit.c
More file actions
171 lines (147 loc) · 4.29 KB
/
Copy pathfsInit.c
File metadata and controls
171 lines (147 loc) · 4.29 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
/**************************************************************
* Class:: CSC-415-02 Spring 2025
* Name:: Nabeel Rana, Leigh Ann Apotheker, Ethan Zheng, Bryan Mendez
* Student IDs:: 924432311, 923514173, 922474550, 922744724
* GitHub-Name:: nabware
* Group-Name:: Team 42
* 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 <time.h>
#include "fsLow.h"
#include "mfs.h"
int initRootDirectory()
{
int numOfEntries = 50;
int requiredNumOfBytes = numOfEntries * sizeof(DE);
int requiredNumOfBlocks = (requiredNumOfBytes + (vcb->blockSize - 1)) /
vcb->blockSize;
// Check if you can fit more entries into the number of blocks we're asking for.
int extraEntries = ((requiredNumOfBlocks * vcb->blockSize) -
requiredNumOfBytes) / sizeof(DE);
numOfEntries += extraEntries;
requiredNumOfBytes += extraEntries * sizeof(DE);
DE *directoryEntries = malloc(requiredNumOfBytes);
if (directoryEntries == NULL)
{
printf("Error allocating memory for directory entries.\n");
return -1;
}
for (int i = 0; i < numOfEntries; i++)
{
directoryEntries[i].used = 0;
}
// Initialize the two directories "." and ".."
int startBlock = allocateBlocks(vcb->freeBlockNum, requiredNumOfBlocks);
strcpy(directoryEntries[0].name, ".");
strcpy(directoryEntries[1].name, "..");
time_t timeNow;
time(&timeNow);
for (int i = 0; i < 2; i++)
{
directoryEntries[i].used = 1;
directoryEntries[i].location = startBlock;
directoryEntries[i].size = 0;
directoryEntries[i].isDirectory = 1;
directoryEntries[i].createdAt = timeNow;
directoryEntries[i].modifiedAt = timeNow;
}
LBAwrite(directoryEntries, requiredNumOfBlocks, startBlock);
free(directoryEntries);
vcb->numOfEntries = numOfEntries;
return startBlock;
}
int initFreeSpace()
{
int blocksRequiredForFATTable = (vcb->numOfBlocks * sizeof(int) +
(vcb->blockSize - 1)) / vcb->blockSize;
fatTable = malloc(vcb->numOfBlocks * sizeof(int));
if (fatTable == NULL)
{
printf("Error allocating memory for free space.\n");
return -1;
}
fatTable[0] = END_OF_FILE_FLAG;
for (int i = 1; i < vcb->numOfBlocks; i++)
{
if (i < blocksRequiredForFATTable)
{
fatTable[i] = i + 1;
}
else if (i == blocksRequiredForFATTable)
{
fatTable[i] = END_OF_FILE_FLAG;
}
else
{
fatTable[i] = FREE_BLOCK_FLAG;
}
}
LBAwrite(fatTable, blocksRequiredForFATTable, 1);
vcb->fatBlockNum = 1;
vcb->freeBlockNum = 1 + blocksRequiredForFATTable;
return blocksRequiredForFATTable;
}
int initFileSystem (uint64_t numberOfBlocks, uint64_t blockSize)
{
printf ("Initializing File System with %ld blocks with a block size of %ld\n", numberOfBlocks, blockSize);
/* TODO: Add any code you need to initialize your file system. */
vcb = malloc(blockSize);
if (vcb == NULL)
{
printf("Error allocating memory for VCB.\n");
return -1;
}
LBAread(vcb, 1, 0);
if (vcb->signature != TEAM_42_FS_SIGNATURE)
{
vcb->signature = TEAM_42_FS_SIGNATURE;
vcb->volumeSize = numberOfBlocks * blockSize;
vcb->blockSize = blockSize;
vcb->numOfBlocks = numberOfBlocks;
vcb->fsBlockNum = 0;
// Initialize free space
int fatNumOfBlocks = initFreeSpace();
if (fatNumOfBlocks == -1)
{
return -1;
}
vcb->fatNumOfBlocks = fatNumOfBlocks;
// Initialize root directory
int rootDirBlockNum = initRootDirectory();
if (rootDirBlockNum == -1)
{
return -1;
}
vcb->rootDirBlockNum = rootDirBlockNum;
LBAwrite(vcb, 1, 0);
}
else
{
fatTable = malloc(vcb->fatNumOfBlocks * vcb->blockSize);
if (fatTable == NULL)
{
printf("Error allocating memory for free space.\n");
return -1;
}
LBAread(fatTable, vcb->fatNumOfBlocks, vcb->fatBlockNum);
}
return 0;
}
void exitFileSystem ()
{
printf ("System exiting\n");
free(vcb);
free(fatTable);
}