forked from ProjectOpenCannibal/android_bootable_recovery-cm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.cpp
346 lines (297 loc) · 9.5 KB
/
backup.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
339
340
341
342
343
344
345
#include <stdlib.h>
#include <stdio.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 "roots.h"
#include "bu.h"
#include "messagesocket.h"
using namespace android;
#define MAX_PART 8
struct partspec {
const char* name;
char* path;
fstab_rec* vol;
};
struct partspec partlist[MAX_PART];
static int append_sod(const char* opt_hash)
{
int fd;
const char* key;
char value[PROPERTY_VALUE_MAX];
int len;
char buf[PROP_LINE_LEN];
unlink(PATHNAME_SOD);
fd = open(PATHNAME_SOD, O_RDWR|O_CREAT, 0600);
if (fd < 0) {
return -1;
}
key = "hash.name";
strcpy(value, opt_hash);
len = sprintf(buf, "%s=%s\n", key, value);
write(fd, buf, len);
key = "ro.build.product";
property_get(key, value, "");
len = sprintf(buf, "%s=%s\n", key, value);
write(fd, buf, len);
for (int i = 0; partlist[i].name; ++i) {
const char* fstype = partlist[i].vol->fs_type;
uint64_t size, used;
if (!strcmp(fstype, "mtd") || !strcmp(fstype, "bml") || !strcmp(fstype, "emmc")) {
int fd = open(partlist[i].vol->blk_device, O_RDONLY);
size = used = lseek(fd, 0, SEEK_END);
close(fd);
}
else {
struct statfs stfs;
memset(&stfs, 0, sizeof(stfs));
if (ensure_path_mounted(partlist[i].path) != 0) {
logmsg("append_sod: failed to mount %s\n", partlist[i].path);
continue;
}
if (statfs(partlist[i].path, &stfs) == 0) {
size = (stfs.f_blocks) * stfs.f_bsize;
used = (stfs.f_blocks - stfs.f_bfree) * stfs.f_bsize;
}
else {
logmsg("Failed to statfs %s: %s\n", partlist[i].path, strerror(errno));
}
ensure_path_unmounted(partlist[i].path);
}
len = sprintf(buf, "fs.%s.size=%llu\n", partlist[i].name, size);
write(fd, buf, len);
len = sprintf(buf, "fs.%s.used=%llu\n", partlist[i].name, used);
write(fd, buf, len);
}
close(fd);
int rc = tar_append_file(tar, PATHNAME_SOD, "SOD");
unlink(PATHNAME_SOD);
return rc;
}
static int append_eod(const char* opt_hash)
{
int fd;
char buf[PROP_LINE_LEN];
int len;
int n;
unlink(PATHNAME_EOD);
fd = open(PATHNAME_EOD, O_RDWR|O_CREAT, 0600);
if (fd < 0) {
return -1;
}
len = sprintf(buf, "hash.datalen=%u\n", hash_datalen);
write(fd, buf, len);
unsigned char digest[HASH_MAX_LENGTH];
char hexdigest[HASH_MAX_STRING_LENGTH];
if (!strcasecmp(opt_hash, "sha1")) {
SHA1Final(digest, &sha1_ctx);
for (n = 0; n < SHA1_DIGEST_LENGTH; ++n) {
sprintf(hexdigest+2*n, "%02x", digest[n]);
}
len = sprintf(buf, "hash.value=%s\n", hexdigest);
}
else { // default to md5
MD5_Final(digest, &md5_ctx);
for (n = 0; n < MD5_DIGEST_LENGTH; ++n) {
sprintf(hexdigest+2*n, "%02x", digest[n]);
}
len = sprintf(buf, "hash.value=%s\n", hexdigest);
}
write(fd, buf, len);
close(fd);
int rc = tar_append_file(tar, PATHNAME_EOD, "EOD");
unlink(PATHNAME_EOD);
return rc;
}
static int do_backup_tree(const String8& path)
{
int rc = 0;
bool path_is_data = !strcmp(path.string(), "/data");
DIR *dp;
dp = opendir(path.string());
struct dirent *de;
while ((de = readdir(dp)) != NULL) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
continue;
}
if (path_is_data && !strcmp(de->d_name, "media") && is_data_media()) {
logmsg("do_backup_tree: skipping datamedia\n");
continue;
}
struct stat st;
String8 filepath(path);
filepath += "/";
filepath += de->d_name;
memset(&st, 0, sizeof(st));
rc = lstat(filepath.string(), &st);
if (rc != 0) {
logmsg("do_backup_tree: path=%s, lstat failed, rc=%d\n", path.string(), rc);
break;
}
if (!(S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))) {
logmsg("do_backup_tree: path=%s, ignoring special file\n", path.string());
continue;
}
if (S_ISDIR(st.st_mode)) {
rc = tar_append_file(tar, filepath.string(), filepath.string());
if (rc != 0) {
logmsg("do_backup_tree: path=%s, tar_append_file failed, rc=%d\n", path.string(), rc);
break;
}
rc = do_backup_tree(filepath);
if (rc != 0) {
logmsg("do_backup_tree: path=%s, recursion failed, rc=%d\n", path.string(), rc);
break;
}
}
else {
rc = tar_append_file(tar, filepath.string(), filepath.string());
if (rc != 0) {
logmsg("do_backup_tree: path=%s, tar_append_file failed, rc=%d\n", path.string(), rc);
break;
}
}
}
closedir(dp);
return rc;
}
static int tar_append_device_contents(TAR* t, const char* devname, const char* savename)
{
struct stat st;
memset(&st, 0, sizeof(st));
if (lstat(devname, &st) != 0) {
logmsg("tar_append_device_contents: lstat %s failed\n", devname);
return -1;
}
st.st_mode = 0644 | S_IFREG;
int fd = open(devname, O_RDONLY);
if (fd < 0) {
logmsg("tar_append_device_contents: open %s failed\n", devname);
return -1;
}
st.st_size = lseek(fd, 0, SEEK_END);
close(fd);
th_set_from_stat(t, &st);
th_set_path(t, savename);
if (th_write(t) != 0) {
logmsg("tar_append_device_contents: th_write failed\n");
return -1;
}
if (tar_append_regfile(t, devname) != 0) {
logmsg("tar_append_device_contents: tar_append_regfile %s failed\n", devname);
return -1;
}
return 0;
}
int do_backup(int argc, char **argv)
{
int rc = 1;
int n;
int i;
int len;
int written;
const char* opt_compress = "gzip";
const char* opt_hash = "md5";
int optidx = 0;
while (optidx < argc && argv[optidx][0] == '-' && argv[optidx][1] == '-') {
char* optname = &argv[optidx][2];
++optidx;
char* optval = strchr(optname, '=');
if (optval) {
*optval = '\0';
++optval;
}
else {
if (optidx >= argc) {
logmsg("No argument to --%s\n", optname);
return -1;
}
optval = argv[optidx];
++optidx;
}
if (!strcmp(optname, "compress")) {
opt_compress = optval;
logmsg("do_backup: compress=%s\n", opt_compress);
}
else if (!strcmp(optname, "hash")) {
opt_hash = optval;
logmsg("do_backup: hash=%s\n", opt_hash);
}
else {
logmsg("do_backup: invalid option name \"%s\"\n", optname);
return -1;
}
}
if (argc - optidx >= MAX_PART) {
logmsg("Too many partitions (%d > %d)\n", (argc-optidx), MAX_PART);
return -1;
}
for (n = optidx; n < argc; ++n) {
const char* partname = argv[n];
if (*partname == '-')
++partname;
for (i = 0; i < MAX_PART; ++i) {
if (partlist[i].name == NULL) {
partlist[i].name = partname;
partlist[i].path = (char *)malloc(1+strlen(partname)+1);
sprintf(partlist[i].path, "/%s", partname);
partlist[i].vol = volume_for_path(partlist[i].path);
if (partlist[i].vol == NULL || partlist[i].vol->fs_type == NULL) {
logmsg("do_backup: missing vol info for %s, ignoring\n", partname);
partlist[i].vol = NULL;
free(partlist[i].path);
partlist[i].path = NULL;
partlist[i].name = NULL;
}
break;
}
if (strcmp(partname, partlist[i].name) == 0) {
logmsg("Ignoring duplicate partition %s\n", partname);
break;
}
}
}
MessageSocket ms;
ms.ClientInit();
ms.Show("Backup in progress...");
rc = create_tar(opt_compress, "w");
if (rc != 0) {
logmsg("do_backup: cannot open tar stream\n");
return rc;
}
append_sod(opt_hash);
hash_name = strdup(opt_hash);
for (i = 0; partlist[i].name; ++i) {
logmsg("do_backup: %s\n", partlist[i].name);
String8 msg("Backing up ");
msg += partlist[i].name;
ms.Show(msg.string());
const char* fstype = partlist[i].vol->fs_type;
if (!strcmp(fstype, "mtd") || !strcmp(fstype, "bml") || !strcmp(fstype, "emmc")) {
rc = tar_append_device_contents(tar, partlist[i].vol->blk_device, partlist[i].name);
}
else {
if (ensure_path_mounted(partlist[i].path) != 0) {
logmsg("do_backup: cannot mount %s\n", partlist[i].path);
continue;
}
String8 path(partlist[i].path);
rc = do_backup_tree(path);
ensure_path_unmounted(partlist[i].path);
}
}
free(hash_name);
hash_name = NULL;
append_eod(opt_hash);
tar_append_eof(tar);
if (opt_compress)
gzflush(gzf, Z_FINISH);
ms.Dismiss();
logmsg("backup complete: rc=%d\n", rc);
return rc;
}