-
Notifications
You must be signed in to change notification settings - Fork 28
/
bu.cpp
338 lines (281 loc) · 6.94 KB
/
bu.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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/vfs.h>
#include <cutils/properties.h>
#include <cutils/log.h>
#include <selinux/label.h>
#include "roots.h"
#include "bu.h"
#include "messagesocket.h"
#define PATHNAME_RC "/tmp/burc"
using namespace android;
struct selabel_handle *sehandle;
int sockfd;
TAR* tar;
gzFile gzf;
char* hash_name;
size_t hash_datalen;
SHA1_CTX sha1_ctx;
MD5_CTX md5_ctx;
static MessageSocket ms;
void
ui_print(const char* format, ...) {
char buffer[256];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
fputs(buffer, stdout);
}
void logmsg(const char *fmt, ...)
{
char msg[1024];
FILE* fp;
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
fp = fopen("/tmp/bu.log", "a");
if (fp) {
fprintf(fp, "[%d] %s", getpid(), msg);
fclose(fp);
}
}
static partspec partlist[MAX_PART];
static partspec* curpart;
int part_add(const char* name)
{
fstab_rec* vol = NULL;
char* path = NULL;
int i;
path = (char*)malloc(1+strlen(name)+1);
sprintf(path, "/%s", name);
vol = volume_for_path(path);
if (vol == NULL || vol->fs_type == NULL) {
logmsg("missing vol info for %s, ignoring\n", name);
goto err;
}
for (i = 0; i < MAX_PART; ++i) {
if (partlist[i].name == NULL) {
partlist[i].name = strdup(name);
partlist[i].path = path;
partlist[i].vol = vol;
logmsg("part_add: i=%d, name=%s, path=%s\n", i, name, path);
return 0;
}
if (strcmp(partlist[i].name, name) == 0) {
logmsg("duplicate partition %s, ignoring\n", name);
goto err;
}
}
err:
free(path);
return -1;
}
partspec* part_get(int i)
{
if (i >= 0 && i < MAX_PART) {
if (partlist[i].name != NULL) {
return &partlist[i];
}
}
return NULL;
}
partspec* part_find(const char* name)
{
for (int i = 0; i < MAX_PART; ++i) {
if (partlist[i].name && !strcmp(name, partlist[i].name)) {
return &partlist[i];
}
}
return NULL;
}
void part_set(partspec* part)
{
curpart = part;
curpart->off = 0;
}
int update_progress(uint64_t off)
{
if (curpart) {
int oldpct = min(100, (int)((uint64_t)100*curpart->off/curpart->used));
curpart->off += off;
int newpct = min(100, (int)((uint64_t)100*curpart->off/curpart->used));
if (newpct > oldpct) {
char msg[256];
sprintf(msg, "%s: %d%% complete", curpart->name, newpct);
ms.Show(msg);
}
}
return 0;
}
static int tar_cb_open(const char* path, int mode, ...)
{
errno = EINVAL;
return -1;
}
static int tar_cb_close(int fd)
{
return 0;
}
static ssize_t tar_cb_read(int fd, void* buf, size_t len)
{
ssize_t nread;
nread = ::read(fd, buf, len);
if (nread > 0 && hash_name) {
SHA1Update(&sha1_ctx, (u_char*)buf, nread);
MD5_Update(&md5_ctx, buf, nread);
hash_datalen += nread;
}
update_progress(nread);
return nread;
}
static ssize_t tar_cb_write(int fd, const void* buf, size_t len)
{
ssize_t written = 0;
if (hash_name) {
SHA1Update(&sha1_ctx, (u_char*)buf, len);
MD5_Update(&md5_ctx, buf, len);
hash_datalen += len;
}
while (len > 0) {
ssize_t n = ::write(fd, buf, len);
if (n < 0) {
logmsg("tar_cb_write: error: n=%d\n", n);
return n;
}
if (n == 0)
break;
buf = (const char *)buf + n;
len -= n;
written += n;
}
update_progress(written);
return written;
}
static tartype_t tar_io = {
tar_cb_open,
tar_cb_close,
tar_cb_read,
tar_cb_write
};
static ssize_t tar_gz_cb_read(int fd, void* buf, size_t len)
{
int nread;
nread = gzread(gzf, buf, len);
if (nread > 0 && hash_name) {
SHA1Update(&sha1_ctx, (u_char*)buf, nread);
MD5_Update(&md5_ctx, buf, nread);
hash_datalen += nread;
}
update_progress(nread);
return nread;
}
static ssize_t tar_gz_cb_write(int fd, const void* buf, size_t len)
{
ssize_t written = 0;
if (hash_name) {
SHA1Update(&sha1_ctx, (u_char*)buf, len);
MD5_Update(&md5_ctx, buf, len);
hash_datalen += len;
}
while (len > 0) {
ssize_t n = gzwrite(gzf, buf, len);
if (n < 0) {
logmsg("tar_gz_cb_write: error: n=%d\n", n);
return n;
}
if (n == 0)
break;
buf = (const char *)buf + n;
len -= n;
written += n;
}
update_progress(written);
return written;
}
static tartype_t tar_io_gz = {
tar_cb_open,
tar_cb_close,
tar_gz_cb_read,
tar_gz_cb_write
};
int create_tar(const char* compress, const char* mode)
{
int rc = -1;
SHA1Init(&sha1_ctx);
MD5_Init(&md5_ctx);
if (!compress || strcasecmp(compress, "none") == 0) {
rc = tar_fdopen(&tar, sockfd, "foobar", &tar_io,
0, /* oflags: unused */
0, /* mode: unused */
TAR_GNU | TAR_STORE_SELINUX /* options */);
}
else if (strcasecmp(compress, "gzip") == 0) {
gzf = gzdopen(sockfd, mode);
if (gzf != NULL) {
rc = tar_fdopen(&tar, 0, "foobar", &tar_io_gz,
0, /* oflags: unused */
0, /* mode: unused */
TAR_GNU | TAR_STORE_SELINUX /* options */);
}
}
return rc;
}
static void do_exit(int rc)
{
char rcstr[80];
int len;
len = sprintf(rcstr, "%d\n", rc);
unlink(PATHNAME_RC);
int fd = open(PATHNAME_RC, O_RDWR|O_CREAT, 0644);
write(fd, rcstr, len);
close(fd);
exit(rc);
}
int main(int argc, char **argv)
{
int n;
int rc = 1;
logmsg("bu: invoked with %d args\n", argc);
if (argc < 3) {
logmsg("Not enough args (%d)\n", argc);
do_exit(1);
}
// progname sockfd args...
int optidx = 1;
sockfd = atoi(argv[optidx++]);
const char* opname = argv[optidx++];
struct selinux_opt seopts[] = {
{ SELABEL_OPT_PATH, "/file_contexts" }
};
sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
load_volume_table();
// vold_client_start(&v_callbacks, 1);
ms.ClientInit();
if (!strcmp(opname, "backup")) {
ms.Show("Backup in progress...");
rc = do_backup(argc-optidx, &argv[optidx]);
}
else if (!strcmp(opname, "restore")) {
ms.Show("Restore in progress...");
rc = do_restore(argc-optidx, &argv[optidx]);
}
else {
logmsg("Unknown operation %s\n", opname);
do_exit(1);
}
ms.Dismiss();
sleep(1);
close(sockfd);
logmsg("bu exiting\n");
do_exit(rc);
return rc;
}