-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.c
44 lines (38 loc) · 1.13 KB
/
format.c
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
#include "fs.h"
status format_filesystem(FILE* file) {
superblock_t superblock = {
.total_number_of_inodes = NUM_INODES,
.number_of_data_block_present = NUMBER_OF_SECTOR - DATA_BLOCK_START
};
fseek(file, 0, SEEK_SET);
fwrite(&superblock, sizeof(superblock_t), 1, file);
inode_t inode = {
.location_on_the_disk_from_where_os_will_read = -1,
.size_of_file_in_bytes = 0,
.reference_count_of_the_file = 0,
.name_of_the_file = ""
};
fseek(file, SECTOR_SIZE, SEEK_SET);
for (int i = 0; i < NUM_INODES; i++) {
fwrite(&inode, sizeof(inode), 1, file);
}
return OK;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <image file>\n", argv[0]);
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r+b");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
if (format_filesystem(file) == OK) {
puts("File system formatted successfully");
} else {
puts("Failed to format file system");
}
fclose(file);
return EXIT_SUCCESS;
}