-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
112 lines (93 loc) · 2.69 KB
/
main.c
File metadata and controls
112 lines (93 loc) · 2.69 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
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "ospfsfix.h"
void loads_file(int argc, char **argv);
void writes_fixed_image();
int main (int argc, char** argv){
printf("Running Filesystem Image Fixer\n");
loads_file(argc, argv);
switch(fix_file_system()){
case FS_OK:
printf("Filesystem has no errors\n");
break;
case FS_FIXED:
printf("All errors have been fixed\n");
writes_fixed_image();
break;
case FS_BROKEN:
printf("Errors with filesystem are unfixable\n");
break;
default:
ERROR("You broke the file system fixer");
exit(1);
}
return 0;
}
void loads_file(int argc, char **argv){
if (argc == 2) {
// Open file
FILE *filp;
if (MAX_FILENAME_LEN < strlen(argv[1])) {
printf("Error: filename must not exceed %d characters\n", MAX_FILENAME_LEN);
exit(1);
}
if (NULL == (filp = fopen(argv[1], "r"))) {
perror("Error: failed to open file");
exit(1);
}
// Get file length
if (-1 == fseek(filp, 0L, SEEK_END)) {
ERROR("Error: problem reading file1");
exit(1);
}
if (-1 == (fs.buffer_size = ftell(filp))) {
ERROR("Error: problem with reading file2");
exit(1);
}
if (-1 == fseek(filp, 0L, SEEK_SET)) {
ERROR("Error: problem with reading file3");
exit(1);
}
// Load file contents into filesystem
if (NULL == (fs.buffer = malloc(fs.buffer_size + 1))) {
ERROR("Error: problem with reading file");
}
fread(fs.buffer, 1, fs.buffer_size, filp);
fclose(filp);
} else {
printf("Error: use format \'ospfsfix [image-file]\'\n");
exit(1);
}
}
void writes_fixed_image()
{
FILE* filp = fopen("fixed.img", "w");
if (filp == NULL)
{
printf("Error: unable to open file for writing\n");
return;
}
fwrite(block_pointer(0), OSPFS_BLKSIZE, 1, filp);
// write superblock
size_t pad_size = OSPFS_BLKSIZE - sizeof(ospfs_super_t);
void *pad = malloc(pad_size);
memset(pad, 0, pad_size);
fwrite(&fs.super, sizeof(ospfs_super_t), 1, filp);
fwrite(pad, pad_size, 1, filp);
// write bitmap
fwrite(fs.bitmap, OSPFS_BLKSIZE, fs.num_bitmap_blocks, filp);
// write inodes
fwrite(fs.inodes, sizeof(ospfs_inode_t), fs.super.os_ninodes, filp);
size_t num_bytes_written_inode = sizeof(ospfs_inode_t) * fs.super.os_ninodes;
size_t num_written_last_inode = num_bytes_written_inode % OSPFS_BLKSIZE;
if (num_written_last_inode != 0)
fwrite(fs.inodes, 1, OSPFS_BLKSIZE - num_written_last_inode, filp);
// write remaining data blocks
size_t inodes_block_size = fs.super.os_ninodes / OSPFS_BLKINODES;
if (fs.super.os_ninodes % OSPFS_BLKINODES)
inodes_block_size++;
fwrite(block_pointer(fs.super.os_firstinob + inodes_block_size), OSPFS_BLKSIZE, fs.super.os_nblocks, filp);
fclose(filp);
}