-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytar.c
553 lines (454 loc) · 10.9 KB
/
mytar.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#define LIST_OPT 't'
#define ARCHIVE_OPT 'f'
#define VERBOSE_OPT 'v'
#define EXTRACT_OPT 'x'
#define LIST_FLAG 0x01
#define ARCHIVE_FLAG 0x02
#define EXTRACT_FLAG 0x04
#define VERBOSE_FLAG 0x08
#define BLOCK_SIZE 512
#define HEADER_SIZE 500
#define REGTYPE '0' /* regular file */
#define AREGTYPE '\0' /* regular file */
#define TMAGIC "ustar " /* ustar space and a null */
#define TMAGLEN 6
const char USAGE_STR[] = "usage: %s -t [ file1 file2 ... ] -f ARCHIVE ";
/*
* POSIX header.
* Source: https://www.gnu.org/software/tar/manual/html_node/Standard.html
*/
struct posix_header
{ /* byte offset */
char name[100]; /* 0 */
char mode[8]; /* 100 */
char uid[8]; /* 108 */
char gid[8]; /* 116 */
char size[12]; /* 124 */
char mtime[12]; /* 136 */
char chksum[8]; /* 148 */
char typeflag; /* 156 */
char linkname[100]; /* 157 */
char magic[6]; /* 257 */
char version[2]; /* 263 */
char uname[32]; /* 265 */
char gname[32]; /* 297 */
char devmajor[8]; /* 329 */
char devminor[8]; /* 337 */
char prefix[155]; /* 345 */
/* 500 */
};
typedef struct {
FILE *fp;
long record_offset;
long len;
} Archive;
typedef struct {
struct posix_header header;
long data_offset;
} Entry;
/* NOTE: Structure doesn't maintain a copy of the data */
typedef struct
{
char flags;
char *archive; /* Filename of the archive */
int len; /* Number of files */
char **files; /* Filenames of files */
} Args;
Args * args_new();
void args_free(Args *args);
Archive * archive_open(char *archive_fname);
int is_tar_archive(FILE *fp);
void archive_close(Archive *arch);
void process_args(int argc, char *argv[], Args *args);
void process_archive_arg(char ***argv, Args* args);
void process_list_arg(Args* args);
void process_verbose_arg(Args* args);
void process_extract_arg(Args* args);
void validate_args(Args* args);
int next_entry(Archive *arch, Entry *ent);
int is_empty_block(char *arr, size_t size);
void list(Archive *arch, char *list_files[], int lf_count);
int remove_str(char *arr[], int size, char *str);
int report_missing(char *files[], int size);
void extract(Archive *arch, char *files[], int count, int verbose);
Args *
args_new()
{
Args *args;
if ((args = calloc(1, sizeof (Args))) == NULL)
err(2, "malloc");
return (args);
}
void
args_free(Args *args)
{
free(args->files);
free(args);
}
/*
* Parse command line arguments.
*/
void
process_args(int argc, char *argv[], Args *args)
{
if (argc == 1)
errx(2, USAGE_STR, argv[0]);
args->len = 0;
if ((args->files = malloc((argc - 1) * sizeof (char *))) == NULL)
err(2, "malloc");
char **p = argv + 1;
while (*p != NULL) {
if (*p[0] != '-') {
args->files[args->len++] = *p;
p++;
continue;
}
char shortarg = (*p)[1];
p++; // Move pointer to parameters
switch (shortarg) {
case LIST_OPT:
process_list_arg(args);
break;
case ARCHIVE_OPT:
process_archive_arg(&p, args);
break;
case VERBOSE_OPT:
process_verbose_arg(args);
break;
case EXTRACT_OPT:
process_extract_arg(args);
break;
default:
errx(2, "invalid option -- '%c'", shortarg);
}
}
validate_args(args);
}
/*
* Process arguments associated with the -f file flag. Leave pointer on
* the next unused argument.
*/
void
process_archive_arg(char **pargv[], Args* args)
{
if (**pargv == NULL)
errx(64, "option requires an argument -- 'f'");
if (args->flags & ARCHIVE_FLAG)
errx(2, "Multiple archive files not supported");
args->flags = args->flags | ARCHIVE_FLAG;
args->archive = **pargv;
// Move pointer to next unused argument
*pargv = *pargv + 1;
}
/*
* Process arguments associated with the -t file flag.
*/
void
process_list_arg(Args* args)
{
if (args->flags & LIST_FLAG)
errx(2, "Multiple uses of -t not supported");
args->flags = args->flags | LIST_FLAG;
}
/*
* Process arguments associated with the -v file flag.
*/
void
process_verbose_arg(Args* args)
{
if (args->flags & VERBOSE_FLAG)
errx(2, "Multiple uses of -v not supported");
args->flags = args->flags | VERBOSE_FLAG;
}
/*
* Process arguments associated with the -v file flag.
*/
void
process_extract_arg(Args* args)
{
args->flags = args->flags | EXTRACT_FLAG;
}
/*
* Validate given command line arguments.
*/
void
validate_args(Args* args)
{
if (!(args->flags & ARCHIVE_FLAG))
errx(2, "Refusing to read archive contents from terminal"
"(missing -f option?)");
if (!(args->flags & LIST_FLAG) && !(args->flags & EXTRACT_FLAG))
errx(2, "You must specify one of the -tx options.");
if ((args->flags & LIST_FLAG) && (args->flags & EXTRACT_FLAG))
errx(2, "You may not specify more than one -tx options.");
}
/*
* Crate a new archive object.
*/
Archive *
archive_open(char *archive_fname)
{
FILE *fp;
if ((fp = fopen(archive_fname, "r")) == NULL)
err(2, "%s: Cannot open", archive_fname);
Archive *arch;
if ((arch = malloc(sizeof (Archive))) == NULL)
err(2, "malloc");
if (!is_tar_archive(fp))
return (NULL);
fseek(fp, 0L, SEEK_END);
arch->len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
arch->fp = fp;
arch->record_offset = 0;
return (arch);
}
/*
* Check if given file is a tar archive.
*/
int
is_tar_archive(FILE *fp)
{
rewind(fp);
struct posix_header header;
int items_read = fread(&header, sizeof (struct posix_header), 1, fp);
if (items_read == 0)
return (0);
// Check magic field
if (strncmp(TMAGIC, header.magic, TMAGLEN)) {
warnx("This does not look like a tar archive");
return (0);
}
return (1);
}
/*
* Safely close archive.
*/
void
archive_close(Archive *arch)
{
if (fclose(arch->fp))
err(2, "Cannot close");
free(arch);
}
/*
* Sets ent to the next entry of the file.
* On seccessful read return 1, 0 if there are no entries left or 2
* to indicate unrecoverable error.
*/
int
next_entry(Archive *arch, Entry *ent)
{
char *buffer;
if ((buffer = malloc(BLOCK_SIZE)) == NULL)
err(2, "malloc");
fseek(arch->fp, arch->record_offset, SEEK_SET);
int bytes_read;
bytes_read = fread(buffer, 1, BLOCK_SIZE, arch->fp);
if (arch->record_offset > arch->len) {
warnx("Unexpected EOF in archive");
free(buffer);
return (2);
}
// Already at the end of the archive
if (bytes_read == 0) {
free(buffer);
return (0);
}
if (bytes_read != BLOCK_SIZE) {
warnx("Unexpected EOF in archive");
free(buffer);
return (2);
}
int status;
if (buffer[0]) {
memcpy(&(ent->header), buffer, HEADER_SIZE);
ent->data_offset = ftell(arch->fp);
// Set record offset to point to the next record block
char **endptr = NULL;
int data_size = strtol(ent->header.size, endptr, 8);
int data_blocks = (data_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
arch->record_offset += (1 + data_blocks) * BLOCK_SIZE;
status = 1;
}
else
{
// Check for two zero blocks
if (!is_empty_block(buffer, BLOCK_SIZE)) {
free(buffer);
errx(2, "Invalid block");
}
// Read next block
bytes_read = fread(buffer, 1, BLOCK_SIZE, arch->fp);
if (bytes_read != BLOCK_SIZE ||
!is_empty_block(buffer, BLOCK_SIZE)) {
int block_ord = arch->record_offset / BLOCK_SIZE + 1;
warnx("A lone zero block at %d", block_ord);
}
arch->record_offset += bytes_read + BLOCK_SIZE;
status = 0;
}
free(buffer);
return (status);
}
/*
* Check if given block of memory is zeroed out.
*/
int
is_empty_block(char *arr, size_t size)
{
for (size_t i = 0; i < size; i++)
if (arr[i] != '\0')
return (0);
return (1);
}
/*
* List specified files.
*/
void
list(Archive *arch, char *list_files[], int lf_count)
{
Entry *ent;
if ((ent = malloc(sizeof (Entry))) == NULL)
err(2, "malloc");
int status;
status = next_entry(arch, ent);
while (status == 1) {
if (ent->header.typeflag != REGTYPE &&
ent->header.typeflag != AREGTYPE) {
errx(2, "Unsupported header type: %d",
ent->header.typeflag);
}
if (lf_count == 0) {
printf("%s\n", ent->header.name);
} else if (remove_str(list_files, lf_count, ent->header.name)) {
printf("%s\n", ent->header.name);
}
fflush(stdout);
status = next_entry(arch, ent);
}
if (status == 2) {
free(ent);
errx(2, "Error is not recoverable: exiting now");
}
// List files not found in archive
if (lf_count != 0)
if (report_missing(list_files, lf_count)) {
free(ent);
errx(2, "Exiting with failure status due to previous "
"errors");
}
free(ent);
}
/*
* Remove the first occurence of string str form arr.
*/
int
remove_str(char *arr[], int size, char *str)
{
for (int i = 0; i < size; i++) {
if (strcmp(arr[i], str) == 0) {
arr[i][0] = '\0';
return (1);
}
}
return (0);
}
/*
* Report files not found in the archive to stderr. Return number of files
* reported.
*/
int
report_missing(char *files[], int size)
{
int found = 0;
for (int i = 0; i < size; i++)
if (strlen(files[i])) {
warnx("%s: Not found in archive", files[i]);
found++;
}
return (found);
}
/*
* Extract file pointed to by ent from archive. Extract as much data as
* possible. File is created with the filename specified in the archive.
*
* Returns number of bytes written.
*/
int
extract_file(Archive *arch, Entry *ent)
{
char *filename = (ent->header).name;
char **endptr = NULL;
int data_size = strtol(ent->header.size, endptr, 8);
FILE *fout;
if ((fout = fopen(filename, "w")) == NULL)
err(2, "%s: Cannot open for writing", filename);
FILE *fin = arch->fp;
fseek(fin, ent->data_offset, SEEK_SET);
char *buffer;
if ((buffer = malloc(data_size)) == NULL)
err(2, "malloc");
int bytes_read = fread(buffer, sizeof (char), data_size, fin);
int bytes_written = fwrite(buffer, sizeof (char), bytes_read, fout);
free(buffer);
fclose(fout);
return (bytes_written);
}
/*
* Extract specified files from archive. If verbose is non-zero print
* currently extracted file to stdout.
*/
void
extract(Archive *arch, char *files[], int count, int verbose)
{
Entry *ent;
if ((ent = malloc(sizeof (Entry))) == NULL)
err(2, "malloc");
int status;
status = next_entry(arch, ent);
while (status == 1) {
if (ent->header.typeflag != REGTYPE &&
ent->header.typeflag != AREGTYPE) {
errx(2, "Unsupported header type: %d",
ent->header.typeflag);
}
if (count == 0) {
if (verbose)
printf("%s\n", ent->header.name);
extract_file(arch, ent);
} else if (remove_str(files, count, ent->header.name)) {
if (verbose)
printf("%s\n", ent->header.name);
extract_file(arch, ent);
}
fflush(stdout);
status = next_entry(arch, ent);
}
if (status == 2)
errx(2, "Error is not recoverable: exiting now");
free(ent);
}
int
main(int argc, char *argv[])
{
Args *args = args_new();
process_args(argc, argv, args);
int verbose = 0;
if (args->flags & VERBOSE_FLAG)
verbose = 1;
Archive* arch = archive_open(args->archive);
if (arch == NULL)
errx(2, "Exiting with failure status due to previous errors");
if (args->flags & LIST_FLAG)
list(arch, args->files, args->len);
else if (args->flags & EXTRACT_FLAG)
extract(arch, args->files, args->len, verbose);
archive_close(arch);
args_free(args);
return (0);
}