-
Notifications
You must be signed in to change notification settings - Fork 0
/
minget.c
236 lines (199 loc) · 5.29 KB
/
minget.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
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
/* Authors: Geoffrey Lawson, Orion Miller
* Class: CPE 453
* Assignment 4
*
* Main Function for minget and arguments parsing.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include "fslib.h"
#include "debug.h"
#include "verbose.h"
#define OPT_STR "psv"
#define OPTIONS_SIZE (sizeof(struct param_options))
typedef struct param_options {
uint8_t part;
int16_t part_num;
uint8_t subpart;
int16_t subpart_num;
uint8_t verbose;
char *imagefile;
char *path;
char *dstpath;
}options;
options * handleOptions(int argc, char *argv[]);
void printFile(FILE *output, uint8_t *data, uint32_t data_size)
{
uint32_t pos;
for (pos = 0; pos < data_size; pos++)
{
fprintf(output, "%c", data[pos]);
}
fflush(output);
}
int main(int argc, char *argv[])
{
s_block *S_block;
p_table *P_table=NULL;
inode *Inode = NULL;
options *Opt;
FILE *fs;
FILE *output; /*where all the data will be outputed*/
uint32_t part_start; /*beginning file system*/
uint32_t inode_off; /*beginning of inode zone*/
uint32_t zone_size;
uint8_t *data;
char **path;
if ((Opt = handleOptions(argc, argv))==NULL)
{
printf("minget [-v] [-p part [-s subpart]] imagefile srcpath [dstpath]\n");
exit(EXIT_FAILURE);
}
FATALCALL((fs = fopen(Opt->imagefile, "rb"))==NULL,"fopen");
/*check super block*/
output = stdout;
if (Opt->dstpath)
FATALCALL((output = fopen(Opt->dstpath, "w+b"))==NULL,"fopen");
part_start = 0; /*0 is start of image*/
if (Opt->part_num == -1 && Opt->subpart_num == -1) /*no partition or subpartition*/
{
if((S_block = getSuperBlock(fs, 0)) == NULL) /*MAGIC NUMBER*/
{
free(S_block);
exit(EXIT_FAILURE);
}
zone_size = S_block->block_size << S_block->log_zone_size;
inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
}
else if (Opt->part && !Opt->subpart) /*partition but no subpartition*/
{
P_table = getPartTable(fs, part_start, Opt->part_num);
if (P_table == NULL)
exit(EXIT_FAILURE);
part_start = P_table->lFirst * SECTOR_SIZE;
if((S_block = getSuperBlock(fs, 0)) == NULL) /*MAGIC NUMBER*/
{
free(S_block);
exit(EXIT_FAILURE);
}
zone_size = S_block->block_size << S_block->log_zone_size;
inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
}
else if (Opt->part && Opt->subpart) /* partition and subpartition */
{
P_table = getPartTable(fs, part_start, Opt->part_num); /*0 is start of image*/
if (P_table == NULL)
{
free(P_table);
exit(EXIT_FAILURE);
}
part_start = P_table->lFirst * SECTOR_SIZE;
free(P_table);
P_table = getPartTable(fs, part_start, Opt->subpart_num);
if (P_table == NULL)
{
free(P_table);
exit(EXIT_FAILURE);
}
part_start = P_table->lFirst * SECTOR_SIZE;
if((S_block = getSuperBlock(fs, part_start)) == NULL) /*MAGIC NUMBER*/
{
free(S_block);
exit(EXIT_FAILURE);
}
zone_size = S_block->block_size << S_block->log_zone_size;
inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
}
else
{
exit(EXIT_FAILURE);
}
path =tokstr(Opt->path, "/");
if ((Inode = getFile(fs, path, inode_off, part_start, zone_size))==NULL)
{
fprintf(stderr, "File Doesn't Exist\n");
exit(EXIT_FAILURE);
}
data = getData(fs, Inode, part_start, zone_size);
if (Opt->verbose)
verbose(S_block, Inode);
printFile(output, data, Inode->size);
fclose(fs); /*check for errors*/
if(P_table!=NULL)
{
free(P_table);
}
free(Opt);
free(S_block);
return EXIT_SUCCESS;
}
options * handleOptions(int argc, char *argv[])
{
uint32_t opt;
options *Opt;
extern int optind;
uint32_t non_opt_args = argc; /*number of non - option arguments*/
FATALCALL((Opt=(options *)malloc(OPTIONS_SIZE))==NULL, "malloc");
Opt->part_num = -1;
Opt->subpart_num = -1;
Opt->verbose = 0;
Opt->imagefile = NULL;
Opt->path = NULL;
Opt->part = 0;
Opt->subpart = 0;
while ((opt = getopt(argc, argv, OPT_STR)) != -1)
{
switch (opt)
{
case 'v':
non_opt_args -= 1;
Opt->verbose = 1;
break;
case 'p':
non_opt_args -= 2;
if (atoi(argv[optind]) < 0 || atoi(argv[optind]) > 3)
return NULL;
Opt->part_num = atoi(argv[optind]);
Opt->part = 1;
break;
case 's':
non_opt_args -= 2;
if (atoi(argv[optind]) < 0 || atoi(argv[optind]) > 3)
return NULL;
Opt->subpart_num = atoi(argv[optind]);
Opt->subpart = 1;
break;
default:
return NULL;
break;
}
}
if (Opt->subpart == 1 && Opt->part == 0)
{
return NULL;
}
switch (non_opt_args)
{
case 2: /*imagefile given by user*/
Opt->imagefile = argv[argc-1];
FATALCALL((Opt->path=(char *)calloc(2*sizeof(char),
2*sizeof(char)))==NULL, "calloc");
Opt->path[0] = '/';
break;
case 3: /*imagefile and path given by user*/
Opt->imagefile = argv[argc-2];
Opt->path = argv[argc-1];
break;
case 4: /*imagefile, path, and dstpath given by user*/
Opt->imagefile = argv[argc-3];
Opt->path = argv[argc-2];
Opt->dstpath = argv[argc-1];
break;
default:
return NULL;
break;
}
return Opt;
}