forked from navcoin/navcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuntar.cpp
171 lines (158 loc) · 4.35 KB
/
untar.cpp
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
// Based on original code by Tim Kientzle, March 2009.
#include <util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <boost/filesystem.hpp>
/* Parse an octal number, ignoring leading and trailing nonsense. */
static int
parseoct(const char *p, size_t n)
{
int i = 0;
while ((*p < '0' || *p > '7') && n > 0) {
++p;
--n;
}
while (*p >= '0' && *p <= '7' && n > 0) {
i *= 8;
i += *p - '0';
++p;
--n;
}
return (i);
}
/* Returns true if this is 512 zero bytes. */
static int
is_end_of_archive(const char *p)
{
int n;
for (n = 511; n >= 0; --n)
if (p[n] != '\0')
return (0);
return (1);
}
/* Create a directory, including parent directories as necessary. */
static void
create_dir(const char *pathname, int mode)
{
std::string sFile = (GetDataDir(true) / pathname).string();
fprintf(stdout, "[UNTAR] Creating folder %s\n", sFile.c_str());
boost::filesystem::path p = sFile;
boost::filesystem::create_directories(p);
}
/* Create a file, including parent directory as necessary. */
static FILE *
create_file(char *pathname, int mode)
{
FILE *f;
std::string sFile = (GetDataDir(true) / pathname).string();
f = fopen(sFile.c_str(), "wb+");
fprintf(stdout, "[UNTAR] Creating file %s\n", sFile.c_str());
if (f == nullptr) {
/* Try creating parent dir and then creating file. */
char *p = strrchr(pathname, '/');
if (p != nullptr) {
*p = '\0';
create_dir(pathname, 0755);
*p = '/';
f = fopen(sFile.c_str(), "wb+");
}
}
return (f);
}
/* Verify the tar checksum. */
static int
verify_checksum(const char *p)
{
int n, u = 0;
for (n = 0; n < 512; ++n) {
if (n < 148 || n > 155)
/* Standard tar checksum adds unsigned bytes. */
u += ((unsigned char *)p)[n];
else
u += 0x20;
}
return (u == parseoct(p + 148, 8));
}
/* Extract a tar archive. */
bool
untar(FILE *a, const char *path)
{
char buff[512];
FILE *f = nullptr;
size_t bytes_read;
int filesize;
fprintf(stdout, "[UNTAR] Extracting from %s\n", path);
for (;;) {
bytes_read = fread(buff, 1, 512, a);
if (bytes_read < 512) {
fprintf(stderr, "[UNTAR] Short read on %s: expected 512, got %d\n",
path, (int)bytes_read);
return false;
}
if (is_end_of_archive(buff)) {
return true;
}
if (!verify_checksum(buff)) {
fprintf(stderr, "[UNTAR] Checksum failure\n");
return false;
}
filesize = parseoct(buff + 124, 12);
switch (buff[156]) {
case '1':
// Ignoring hardlink
break;
case '2':
// Ignoring symlink
break;
case '3':
// Ignoring character device
break;
case '4':
// Ignoring block device
break;
case '5':
// Extracting dir
create_dir(buff, parseoct(buff + 100, 8));
filesize = 0;
break;
case '6':
// Ignoring FIFO
break;
default:
// Extracting file
if(!strcmp(buff, "wallet.dat")) {
fprintf(stderr, "[UNTAR] Wrong bootstrap, it includes a wallet.dat file\n");
} else {
f = create_file(buff, parseoct(buff + 100, 8));
}
break;
}
while (filesize > 0) {
bytes_read = fread(buff, 1, 512, a);
if (bytes_read < 512) {
fprintf(stderr, "[UNTAR] Short read on %s: Expected 512, got %d\n",
path, (int)bytes_read);
return false;
}
if (filesize < 512)
bytes_read = filesize;
if (f != nullptr) {
if (fwrite(buff, 1, bytes_read, f)
!= bytes_read)
{
fprintf(stderr, "[UNTAR] Failed write\n");
fclose(f);
f = nullptr;
return false;
}
}
filesize -= bytes_read;
}
if (f != nullptr) {
fclose(f);
f = nullptr;
}
}
return true;
}