-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunpacker.c
More file actions
55 lines (48 loc) · 1.65 KB
/
unpacker.c
File metadata and controls
55 lines (48 loc) · 1.65 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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define NOB_IMPLEMENTATION
#include "nob.h"
#include "tar.h"
void print_usage(const char *program_name)
{
fprintf(stderr, "Usage: %s <input.tar> <output/>\n", program_name);
}
int main(int argc, char **argv)
{
const char *program_name = shift(argv, argc);
if (argc <= 0) {
print_usage(program_name);
fprintf(stderr, "ERROR: no input is provided\n");
return 1;
}
const char *input_path = shift(argv, argc);
if (argc <= 0) {
print_usage(program_name);
fprintf(stderr, "ERROR: no output is provided\n");
return 1;
}
const char *output_path = shift(argv, argc);
String_Builder sb = {0};
if (!read_entire_file(input_path, &sb)) return 1;
printf("%s contains %zu bytes (%zu)\n", input_path, sb.count, sb.count%512);
assert(sb.count%512 == 0);
Header *records = (Header *)sb.items;
size_t mark = temp_save();
while (records->file_path[0]) {
temp_rewind(mark);
size_t file_path_len = strlen(records->file_path);
char *output_file_path = temp_sprintf("%s/%s", output_path, records->file_path);
if (records->file_path[file_path_len - 1] == '/') {
if (!mkdir_if_not_exists(output_file_path)) return 1;
records += 1;
} else {
unsigned long long file_size = strtoull(records->file_size, NULL, 8);
records += 1;
if (!write_entire_file(output_file_path, records, file_size)) return 1;
nob_log(INFO, "created file `%s`", output_file_path);
records += (file_size + 511)/512;
}
}
return 0;
}