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  */
16 
17 #include <androidfw/BackupHelpers.h>
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 
usage(int,const char ** argv)28 static int usage(int /* argc */, const char** argv)
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 
perform_full_backup()46 static int perform_full_backup()
47 {
48     printf("this would have written all of your data to stdout\n");
49     return 0;
50 }
51 
perform_list(const char * filename)52 static int perform_list(const char* filename)
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);
64     bool done;
65     int type;
66 
67     while (reader.ReadNextHeader(&done, &type) == 0) {
68         if (done) {
69             break;
70         }
71         switch (type) {
72             case BACKUP_HEADER_ENTITY_V1:
73             {
74                 String8 key;
75                 size_t dataSize;
76                 err = reader.ReadEntityHeader(&key, &dataSize);
77                 if (err == 0) {
78                     printf("   entity: %s (%zu bytes)\n", key.string(), dataSize);
79                 } else {
80                     printf("   Error reading entity header\n");
81                 }
82                 break;
83             }
84             default:
85             {
86                 printf("Unknown chunk type: 0x%08x\n", type);
87                 break;
88             }
89         }
90     }
91 
92     return 0;
93 }
94 
perform_print(const char * entityname,const char * filename)95 static int perform_print(const char* entityname, const char* filename)
96 {
97     printf("perform_print(%s, %s);", entityname, filename);
98     return 0;
99 }
100 
main(int argc,const char ** argv)101 int main(int argc, const char** argv)
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 
115