Repository navigation
Expand file tree
/
Copy pathfsInit.c
More file actions
112 lines (93 loc) · 3.15 KB
/
Copy pathfsInit.c
File metadata and controls
112 lines (93 loc) · 3.15 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
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name::David Chang
* Yuiel Gebredngl
* Jiqing Liu
* Chia-Tung Liu
* Student IDs::920460954
* 922729168
* 922056041
* 923408847
* GitHub-Name::JunN0tJune
* yuiela
* YTDY3867
* Mob1001
* Group-Name::Alt+F4
* 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"
#include "VCB.h"
#include "FreeSpaceManager.h"
#include "Directory.h"
#define MAX_DIR_ENTRIES 128 // Adjust based on your system's needs
#define MAX_DIR_NAME_SIZE 255 // Adjust according to your definition
//We define our global variables here once, and they will be shared
//across the other files using globalVF
VCB_Block* VCBHolder = NULL;
FAT_Entry* FATTable = NULL;
DIR_Entry* root_directory = NULL;
char* currentDirectory = NULL;
int initFileSystem(uint64_t numberOfBlocks, uint64_t blockSize) {
printf("Initializing File System with %ld blocks with a block size of %ld\n", numberOfBlocks, blockSize);
// Allocate space for VCB and attempt to read it from LBA 0
VCBHolder = (VCB_Block*)malloc(blockSize);
if (VCBHolder == NULL) {
return -1; // Allocation failed
}
// Read the VCB from the disk
LBAread(VCBHolder, 1, 0);
// Check if the file system has been initialized by checking magic number
if (VCBHolder->magic_number == MAGIC_NUMBER) {
// Attempt to read the FAT table and root directory
FATTable = readFATTable(VCBHolder->FAT_reference);
root_directory = readRoot(VCBHolder->root_reference);
if (FATTable == NULL || root_directory == NULL) {
free(VCBHolder);
VCBHolder=NULL;
return -1; // Reading FAT or root directory failed
}
// Set the current directory to root
currentDirectory = strdup("/");
return 0; // Success, file system is initialized from disk
} else {
// No valid file system found, create a new one
free(VCBHolder);
VCBHolder=NULL;
VCBHolder = createVCB(numberOfBlocks, blockSize);
FATTable = initFATTable(numberOfBlocks);
VCBHolder->root_reference = initDirectory(MAX_DIR_ENTRIES);
VCBHolder->root_size = root_directory->size;
// Write the new VCB back to the disk
writeToLBA(VCBHolder, 1, 0);
// Initialize current directory to root
currentDirectory = strdup("/");
return 0; // Success, new file system is formatted and initialized
}
}
void exitFileSystem ()
{
// main strucutres are freed
free(VCBHolder);
VCBHolder = NULL;
free(FATTable);
FATTable = NULL;
free(root_directory);
root_directory = NULL;
free(currentDirectory);
currentDirectory = NULL;
printf ("System exiting\n");
}