-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyfs.c
More file actions
354 lines (323 loc) · 11.5 KB
/
myfs.c
File metadata and controls
354 lines (323 loc) · 11.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//Cahit Yusuf Taş -1937465
//cyusuftas@gmail.com
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
//Structs required for File System implementation
typedef struct{
char file_name[248];
uint32_t first_block;
uint32_t file_size;
} FileList;
typedef struct{
uint32_t FAT[4096];
FileList File_List[128];
} FS_Header;
typedef struct{
FS_Header header;
char Data[4096][512];
} FileSystem;
FileSystem FS;
//Global variables
char *src_file;
char *dest_file;
char *disk;
char buffer[512];
FILE *infile;
FILE *outfile;
/****************
Format function is used to clear FAT table
and file list entries.
*****************/
void Format(){
//Clear FAT
for(int i=1; i<4096; i++){
FS.header.FAT[i] = 0;
}
//Clear File List
char empty_file_name = 0x0;
for(int i=0; i<128; i++){
//To make it efficient only first byte of the file name is written
//with special byte(0x0 - NULL) instead of clearing all 248 name bytes.
//Name bytes will be cleared and written with new name when new file
//is written to corresponding block.
FS.header.File_List[i].file_name[0] = empty_file_name;
FS.header.File_List[i].first_block = 0;
FS.header.File_List[i].file_size = 0;
}
//Write formatted FS.header to fs_header_info.dat file
FILE *outfile;
outfile = fopen("./disk/fs_header_info.dat", "w+");
if(outfile != NULL){
fwrite(&FS.header, sizeof(FS.header), 1, outfile);
}
}
/**************
Write function is used to write new file to disk.
It updates the FAT table and name, file size and first
entry is set. Then updated header file is written too.
**************/
void Write(char *srcPath, char *destFileName){
uint32_t first_block;
uint32_t block_size = 0;
uint32_t prev_block;
uint8_t list_index;
uint8_t first_time = 1;
uint32_t read_offset = 0;
uint32_t write_offset = 0;
//Open source file
infile = fopen(srcPath, "r+");
//Set current pointer to start of the file
fseek(infile, 0, SEEK_SET);
if(infile != NULL){
FILE *dest;
char d[256] = "";
strcat(d,"./disk/");
strcat(d, destFileName);
//Open destination file
dest = fopen(d, "w+");
fseek(dest, 0, SEEK_SET);
//count stores how many bytes are read
size_t count;
while((count = fread(buffer, 1, sizeof(buffer), infile)) > 0){
//printf("%c %c %c %c %zu\n", buffer[0], buffer[1], buffer[2], buffer[3], count);
uint32_t FAT_index = 0;
//if whole chunk is read
if(count > 0){
//Search for an empty block
for(int i=0; i<4096; i++){
if(FS.header.FAT[i] == 0){
FAT_index = i;
break;
}
}
//If an empty entry found for the data
if(FAT_index != 0){
//First Time
if(first_time == 1){
first_block = FAT_index;
//Find empty list index
for(int j=0; j<128; j++){
if(FS.header.File_List[j].first_block == 0){
list_index = j;
break;
}
}
//Update first block information
FS.header.File_List[list_index].first_block = first_block;
//Update file name information
for(int m=0; m<strlen(destFileName); m++){
FS.header.File_List[list_index].file_name[m] = destFileName[m];
}
//Add string delimiter to the end of the name
FS.header.File_List[list_index].file_name[strlen(destFileName)] = '\0';
first_time = 0;
prev_block = FAT_index;
}
//If count < 512 it means that the last chunk is read
if(count < 512){
FS.header.FAT[prev_block] = FAT_index;
//Indicate that it is the last FAT entry.
FS.header.FAT[FAT_index] = 0xFFFFFFFF;
block_size += count;
//Update block size before leaving
FS.header.File_List[list_index].file_size = block_size;
fwrite(buffer, 1, count, dest);
//Close files after write operation
fclose(dest);
fclose(infile);
//Open FS_header.dat and update FS headers
FILE *fs_h;
fs_h = fopen("./disk/fs_header_info.dat", "w+");
if(fs_h != NULL){
fwrite(&FS.header, sizeof(FS.header), 1, fs_h);
}
//If last chunk is read exit from while loop
break;
}else{
//Update table entries
FS.header.FAT[prev_block] = FAT_index;
prev_block = FAT_index;
//Keep writing to destination file
fwrite(buffer, 1, sizeof(buffer), dest);
//Update offset values
read_offset += 512;
write_offset += 512;
block_size += 512;
//Update current pointers for destination and source files
fseek(infile, read_offset, SEEK_SET);
fseek(dest, write_offset, SEEK_SET);
}
}
}else{
printf("Couldn't open source file\n");
}
}
}
}
/*************
Read a file from disk and write to destination file.
*************/
void Read(char *srcFileName, char *destPath){
//Try to find matching file
uint8_t match = 0;
for(int i=0; i<128; i++){
if(strcmp(FS.header.File_List[i].file_name, srcFileName) == 0){
match = 1;
break;
}
}
if(match == 1){
FILE *f;
char n[256] = "";
strcat(n,"./disk/");
strcat(n, srcFileName);
//Open source file
f = fopen(n, "r");
fseek(f, 0, SEEK_SET);
if(f != NULL){
FILE *d;
//Open destination file with w+
d = fopen(destPath, "w+");
fseek(d, 0, SEEK_SET);
if(d != NULL){
uint32_t offset = 0;
size_t count;
//Read by chunks from source and write to destination
while((count = fread(&buffer, 1, sizeof(buffer), f)) > 0){
if(count == 512){
fwrite(&buffer, sizeof(buffer), 1, d);
offset += 512;
fseek(f, offset, SEEK_SET);
fseek(d, offset, SEEK_SET);
}else{
fwrite(&buffer, count, 1, d);
fclose(d);
fclose(f);
}
}
}
}
}else{
printf("Couldn't find file in disk with name: %s\n", srcFileName);
}
}
/**************
Delete a file from disk
**************/
//https://programmingsimplified.com/c-program-delete-file
void Delete(char *FileName){
//Delete from file
int status;
char fn[256] = "";
strcat(fn,"./disk/");
strcat(fn,FileName);
if((status = remove(fn)) != 0){
printf("An error occured during Delete operation.\n");
}
//Find index of the matching file
uint8_t index;
for(int i=0; i<128; i++){
if(strcmp(FS.header.File_List[i].file_name, FileName) == 0){
index = i;
break;
}
}
//Clear the file_list entry of deleted file
uint32_t first_block = FS.header.File_List[index].first_block;
FS.header.File_List[index].file_name[0] = 0;
FS.header.File_List[index].file_size = 0;
FS.header.File_List[index].first_block = 0;
//Clear the FAT entry of deleted file by following next pointer until it is 0xFFFFFFFF(EOF)
uint32_t next= 0;
uint32_t k = first_block;
while(next != 0xFFFFFFFF){
next = FS.header.FAT[k];
FS.header.FAT[k] = 0;
k = next;
}
//Update header file
FILE *f;
f = fopen("./disk/fs_header_info.dat", "w+");
if(f != NULL){
fwrite(&FS.header, sizeof(FS.header), 1, f);
}
}
/*************
List all of the legitimate files(size > 0)
*************/
void List(){
printf("file name file size\n");
for(int i=0; i< 128; i++){
if(FS.header.File_List[i].file_size > 0){
printf("%s", FS.header.File_List[i].file_name);
for(int j=0; j<(32-strlen(FS.header.File_List[i].file_name)); j++){
printf(" ");
}
printf("%d\n", FS.header.File_List[i].file_size);
}
}
}
int main(int argc, char** argv){
//Read disk name
//Caution: disk.image is not used. Use "disk"
disk = argv[1];
//Header of the file system is stored as a file in the disk.
//So when program starts it reads the header information from that file
//if it exists, otherwise it creates an empty header information file(fs_header_info.dat)
infile = fopen("./disk/fs_header_info.dat","rb");
if(infile == NULL){
printf("Couldnt find header info file. New header info file is created.\n");
//Create empty File System headers
FS.header.FAT[0] = 0xFFFFFFFF; //System is Little-Endian by default
FS.header.File_List[0].file_name[0] = 0;
FS.header.File_List[0].file_size = 0;
FS.header.File_List[0].first_block = 0;
for(int i=1; i<4096; i++){
FS.header.FAT[i] = 0;
FS.header.File_List[i].file_name[0] = 0;
FS.header.File_List[i].file_size = 0;
FS.header.File_List[i].first_block = 0;
}
//Open header info file(if it does not exist, it is created)
outfile = fopen("./disk/fs_header_info.dat", "w+");
if(outfile != NULL){
fwrite(&FS.header, sizeof(FS.header), 1, outfile);
fclose(outfile);
}
}else{
//If header info file exists, read info and store it in FS.header structure
while(fread(&FS.header, sizeof(FS.header), 1, infile));
fclose(infile);
}
//Read arguments and run corresponding function.
if(argv[2] != NULL){
if(strcmp(argv[2],"-format") == 0){
Format();
}else if(strcmp(argv[2],"-write") == 0){
if(argv[3] != NULL && argv[4] != NULL){
src_file = argv[3];
dest_file = argv[4];
Write(src_file, dest_file);
}
}else if(strcmp(argv[2],"-read") == 0){
if(argv[3] != NULL && argv[4] != NULL){
src_file = argv[3];
dest_file = argv[4];
Read(src_file, dest_file);
}
}else if(strcmp(argv[2],"-delete") == 0){
if(argv[3] != NULL){
src_file = argv[3];
Delete(src_file);
}
}else if(strcmp(argv[2],"-list") == 0){
List();
}
}
// for(int i=0; i<50; i++){
// printf("%x %d %s\n", FS.header.FAT[i], FS.header.File_List[i].file_size, FS.header.File_List[i].file_name);
// }
return 0;
}