No Description

backup.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <dirent.h>
  9. #include <sys/vfs.h>
  10. #include <time.h>
  11. #include "cutils/properties.h"
  12. #include <fs_mgr.h>
  13. #include "roots.h"
  14. #include "bu.h"
  15. using namespace android;
  16. static int append_sod(const char* opt_hash)
  17. {
  18. const char* key;
  19. char value[PROPERTY_VALUE_MAX];
  20. char sodbuf[PROP_LINE_LEN*10];
  21. char* p = sodbuf;
  22. key = "hash.name";
  23. strcpy(value, opt_hash);
  24. p += sprintf(p, "%s=%s\n", key, value);
  25. key = "ro.product.device";
  26. property_get(key, value, "");
  27. p += sprintf(p, "%s=%s\n", key, value);
  28. for (int i = 0; i < MAX_PART; ++i) {
  29. partspec* part = part_get(i);
  30. if (!part)
  31. break;
  32. int fd = open(part->vol->blk_device, O_RDONLY);
  33. part->size = part->used = lseek64(fd, 0, SEEK_END);
  34. close(fd);
  35. p += sprintf(p, "fs.%s.size=%llu\n", part->name, (unsigned long long)part->size);
  36. p += sprintf(p, "fs.%s.used=%llu\n", part->name, (unsigned long long)part->used);
  37. }
  38. int rc = tar_append_file_contents(tar, "SOD", 0600,
  39. getuid(), getgid(), sodbuf, p-sodbuf);
  40. return rc;
  41. }
  42. static int append_eod(const char* opt_hash)
  43. {
  44. char eodbuf[PROP_LINE_LEN*10];
  45. char* p = eodbuf;
  46. int n;
  47. p += sprintf(p, "hash.datalen=%lu\n", (unsigned long)hash_datalen);
  48. unsigned char digest[HASH_MAX_LENGTH];
  49. char hexdigest[HASH_MAX_STRING_LENGTH];
  50. if (!strcasecmp(opt_hash, "sha1")) {
  51. SHA1_Final(digest, &sha_ctx);
  52. for (n = 0; n < SHA_DIGEST_LENGTH; ++n) {
  53. sprintf(hexdigest+2*n, "%02x", digest[n]);
  54. }
  55. p += sprintf(p, "hash.value=%s\n", hexdigest);
  56. }
  57. else { // default to md5
  58. MD5_Final(digest, &md5_ctx);
  59. for (n = 0; n < MD5_DIGEST_LENGTH; ++n) {
  60. sprintf(hexdigest+2*n, "%02x", digest[n]);
  61. }
  62. p += sprintf(p, "hash.value=%s\n", hexdigest);
  63. }
  64. int rc = tar_append_file_contents(tar, "EOD", 0600,
  65. getuid(), getgid(), eodbuf, p-eodbuf);
  66. return rc;
  67. }
  68. static int tar_append_device_contents(TAR* t, const char* devname, const char* savename)
  69. {
  70. struct stat st;
  71. memset(&st, 0, sizeof(st));
  72. if (lstat(devname, &st) != 0) {
  73. logmsg("tar_append_device_contents: lstat %s failed\n", devname);
  74. return -1;
  75. }
  76. st.st_mode = 0644 | S_IFREG;
  77. int fd = open(devname, O_RDONLY);
  78. if (fd < 0) {
  79. logmsg("tar_append_device_contents: open %s failed\n", devname);
  80. return -1;
  81. }
  82. st.st_size = lseek64(fd, 0, SEEK_END);
  83. close(fd);
  84. th_set_from_stat(t, &st);
  85. th_set_path(t, savename);
  86. if (th_write(t) != 0) {
  87. logmsg("tar_append_device_contents: th_write failed\n");
  88. return -1;
  89. }
  90. if (tar_append_regfile(t, devname) != 0) {
  91. logmsg("tar_append_device_contents: tar_append_regfile %s failed\n", devname);
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. int do_backup(int argc, char **argv)
  97. {
  98. int rc = 1;
  99. int n;
  100. int i;
  101. const char* opt_compress = "gzip";
  102. const char* opt_hash = "md5";
  103. int optidx = 0;
  104. while (optidx < argc && argv[optidx][0] == '-' && argv[optidx][1] == '-') {
  105. char* optname = &argv[optidx][2];
  106. ++optidx;
  107. char* optval = strchr(optname, '=');
  108. if (optval) {
  109. *optval = '\0';
  110. ++optval;
  111. }
  112. else {
  113. if (optidx >= argc) {
  114. logmsg("No argument to --%s\n", optname);
  115. return -1;
  116. }
  117. optval = argv[optidx];
  118. ++optidx;
  119. }
  120. if (!strcmp(optname, "compress")) {
  121. opt_compress = optval;
  122. logmsg("do_backup: compress=%s\n", opt_compress);
  123. }
  124. else if (!strcmp(optname, "hash")) {
  125. opt_hash = optval;
  126. logmsg("do_backup: hash=%s\n", opt_hash);
  127. }
  128. else {
  129. logmsg("do_backup: invalid option name \"%s\"\n", optname);
  130. return -1;
  131. }
  132. }
  133. for (n = optidx; n < argc; ++n) {
  134. const char* partname = argv[n];
  135. if (*partname == '-')
  136. ++partname;
  137. if (part_add(partname) != 0) {
  138. logmsg("Failed to add partition %s\n", partname);
  139. return -1;
  140. }
  141. }
  142. rc = create_tar(adb_ofd, opt_compress, "w");
  143. if (rc != 0) {
  144. logmsg("do_backup: cannot open tar stream\n");
  145. return rc;
  146. }
  147. append_sod(opt_hash);
  148. hash_name = strdup(opt_hash);
  149. for (i = 0; i < MAX_PART; ++i) {
  150. partspec* curpart = part_get(i);
  151. if (!curpart)
  152. break;
  153. part_set(curpart);
  154. rc = tar_append_device_contents(tar, curpart->vol->blk_device, curpart->name);
  155. }
  156. free(hash_name);
  157. hash_name = NULL;
  158. append_eod(opt_hash);
  159. tar_append_eof(tar);
  160. if (opt_compress)
  161. gzflush(gzf, Z_FINISH);
  162. logmsg("backup complete: rc=%d\n", rc);
  163. return rc;
  164. }