Mathias Agopian | 8ae2335 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 16 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 17 | #include <androidfw/BackupHelpers.h> |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 18 | #include <utils/String8.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | using namespace android; |
| 25 | |
| 26 | #include <unistd.h> |
| 27 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 28 | static int usage(int /* argc */, const char** argv) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 29 | { |
| 30 | const char* p = argv[0]; |
| 31 | |
| 32 | fprintf(stderr, "%s: Backs up your data.\n" |
| 33 | "\n" |
| 34 | "usage: %s\n" |
| 35 | " Prints all of the data that can be backed up to stdout.\n" |
| 36 | "\n" |
| 37 | "usage: %s list FILE\n" |
| 38 | " Lists the backup entities in the file.\n" |
| 39 | "\n" |
| 40 | "usage: %s print NAME FILE\n" |
| 41 | " Prints the entity named NAME in FILE.\n", |
| 42 | p, p, p, p); |
| 43 | return 1; |
| 44 | } |
| 45 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 46 | static int perform_full_backup() |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 47 | { |
| 48 | printf("this would have written all of your data to stdout\n"); |
| 49 | return 0; |
| 50 | } |
| 51 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 52 | static int perform_list(const char* filename) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 53 | { |
| 54 | int err; |
| 55 | int fd; |
| 56 | |
| 57 | fd = open(filename, O_RDONLY); |
| 58 | if (fd == -1) { |
| 59 | fprintf(stderr, "Error opening: %s\n", filename); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | BackupDataReader reader(fd); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 64 | bool done; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 65 | int type; |
| 66 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 67 | while (reader.ReadNextHeader(&done, &type) == 0) { |
| 68 | if (done) { |
| 69 | break; |
| 70 | } |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 71 | switch (type) { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 72 | case BACKUP_HEADER_ENTITY_V1: |
| 73 | { |
| 74 | String8 key; |
| 75 | size_t dataSize; |
| 76 | err = reader.ReadEntityHeader(&key, &dataSize); |
| 77 | if (err == 0) { |
Andreas Gampe | ebee137 | 2014-11-07 16:28:19 -0800 | [diff] [blame] | 78 | printf(" entity: %s (%zu bytes)\n", key.string(), dataSize); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 79 | } else { |
| 80 | printf(" Error reading entity header\n"); |
| 81 | } |
| 82 | break; |
| 83 | } |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 84 | default: |
| 85 | { |
| 86 | printf("Unknown chunk type: 0x%08x\n", type); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 95 | static int perform_print(const char* entityname, const char* filename) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 96 | { |
| 97 | printf("perform_print(%s, %s);", entityname, filename); |
| 98 | return 0; |
| 99 | } |
| 100 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 101 | int main(int argc, const char** argv) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 102 | { |
| 103 | if (argc <= 1) { |
| 104 | return perform_full_backup(); |
| 105 | } |
| 106 | if (argc == 3 && 0 == strcmp(argv[1], "list")) { |
| 107 | return perform_list(argv[2]); |
| 108 | } |
| 109 | if (argc == 4 && 0 == strcmp(argv[1], "print")) { |
| 110 | return perform_print(argv[2], argv[3]); |
| 111 | } |
| 112 | return usage(argc, argv); |
| 113 | } |
| 114 | |