-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
183 lines (153 loc) · 4.13 KB
/
Copy pathmain.c
File metadata and controls
183 lines (153 loc) · 4.13 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
//
// Created by jade on 6/6/22.
//
#include "main.h"
#include <stdlib.h>
#include <bits/types/FILE.h>
#include <stdio.h>
#include <string.h>
struct superblock sb;
//array of what currently is open
struct inode *inodes;
struct block *dbs;
//Initializing new filesystem
void createFileSystem()
{
sb.numInodes = 10;
sb.numBlocks = 100;
sb.sizeOfBlocks = sizeof (struct block);
int i;
inodes = malloc (sizeof (struct inode) * sb.numInodes);
for (i = 0; i < sb.numInodes; i++)
{
inodes[i]. size =-1;
inodes[i].firstBlock = -1;
strcpy(inodes[i].name, "emptyfi");
} //initializing nodes
dbs = malloc (sizeof (struct block) * sb.numBlocks);
for (i = 0; i < sb.numBlocks; i++){
dbs[i].nextBlockNumber = -1;
}
}
void mountFileSystem()
{
FILE *file;
file = fopen("fs_data", "r");
inodes = malloc (sizeof (struct inode) * sb.numInodes);
dbs = malloc (sizeof (struct block) * sb.numBlocks);
//SUPERBLOCK
fread (&sb, sizeof(struct superblock), 1, file);
fread(inodes, sizeof (struct inode), sb.numInodes, file);
fread(dbs, sizeof(struct block), sb.numBlocks, file);
fclose(file);
}
void syncFileSystem()
{
FILE *file;
file = fopen("fs_data", "w+");
// SUPERBLOCK
fwrite(&sb, sizeof (struct superblock), 1, file);
//inodes
fwrite(inodes, sizeof(struct inode), sb.numInodes, file);
fwrite(dbs, sizeof(struct block), sb.numBlocks, file);
fclose(file);
}//SYNC FILE SYSTEM
void printFileSystem() {
printf ("Superblock INFO\n");
printf("\tnum inodes %d\n", sb.numInodes);
printf("\tnum blocks %d\n", sb.numBlocks);
printf("\tsize blocks %d\n", sb.sizeOfBlocks);
printf("inodes\n");
int i;
//inodes = malloc (sizeof (struct inode) * sb.numInodes);
for(i=0; i< sb.numInodes; i++){
printf("\tsize: %d block : %d name: %s\n", inodes[i].size, inodes[i].firstBlock, inodes[i].name);
} //initialize inodes
//dbs = malloc(sizeof (struct block) * sb.numBlocks);
for(i=0; i< sb.numBlocks; i++){
printf("\tblock num: %d next block %d\n", i, dbs[i].nextBlockNumber);
}
}
int allocate_file (char name[8])
{
//find an empty inode
int in = find_empty_inode();
//claim it
int block = find_empty_block();
//find/claim a disk block
//claim them
inodes[in].firstBlock = block;
dbs[block].nextBlockNumber = -2;
strcpy (inodes[in].name, name);
//return file descriptor
return in;
} //allocate_file
int find_empty_inode()
{
int i;
for( i=0; i < sb.numInodes; i++) {
if(inodes[i].firstBlock == -1) {
return i;
}
}
return -1;
}
int find_empty_block()
{
int i;
for( i=0; i < sb.numBlocks; i++) {
if(dbs[i].nextBlockNumber == -1) {
return i;
}
}
return -1;
}
int get_block_num (int file, int offset){
int togo = offset;
int bn = inodes[file].firstBlock;
while(togo > 0){
bn = dbs[bn].nextBlockNumber;
togo--;
}
return bn;
}
void set_filesize (int filenum, int size){
// how many blocks should we have
int empty;
int tmp = size + BLOCKSIZE - 1;
int num = tmp / BLOCKSIZE;
int bn = inodes[filenum].firstBlock;
num--;
//grow the file if necessary
while(num>0){
//check next block number
int next_num = dbs[bn].nextBlockNumber;
if (next_num == -2) {
empty = find_empty_block();
dbs[bn].nextBlockNumber = empty;
dbs[empty].nextBlockNumber = -2;
}
bn = dbs[bn].nextBlockNumber;
num--;
}
//shorten if necessary
shortenFile(bn);
dbs[bn].nextBlockNumber = -2;
}
void write_data (int filenum, int pos, char *data){
//calculate which block
int relative_block = pos / BLOCKSIZE;
//find the block number
int bn = get_block_num(filenum, relative_block);
//calculate the offset in the block
int offset = pos % BLOCKSIZE;
// write the data
dbs[bn].data[offset] = (*data);
}
void shortenFile(int bn){
int nn = dbs[bn].nextBlockNumber;
if (nn >=0){
shortenFile(nn);
}
dbs[bn].nextBlockNumber = -1;
}