backup.cpp revision 8ae2335a3c93d0c00e998fdec18f64dfe43b94cb
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 <utils/BackupHelpers.h>
18#include <utils/String8.h>
19
20#include <fcntl.h>
21#include <stdio.h>
22#include <string.h>
23
24using namespace android;
25
26#include <unistd.h>
27
28int
29usage(int argc, const char** argv)
30{
31    const char* p = argv[0];
32
33    fprintf(stderr, "%s: Backs up your data.\n"
34                    "\n"
35                    "usage: %s\n"
36                    "  Prints all of the data that can be backed up to stdout.\n"
37                    "\n"
38                    "usage: %s list FILE\n"
39                    "  Lists the backup entities in the file.\n"
40                    "\n"
41                    "usage: %s print NAME FILE\n"
42                    "  Prints the entity named NAME in FILE.\n",
43                    p, p, p, p);
44    return 1;
45}
46
47int
48perform_full_backup()
49{
50    printf("this would have written all of your data to stdout\n");
51    return 0;
52}
53
54int
55perform_list(const char* filename)
56{
57    int err;
58    int fd;
59
60    fd = open(filename, O_RDONLY);
61    if (fd == -1) {
62        fprintf(stderr, "Error opening: %s\n", filename);
63        return 1;
64    }
65
66    BackupDataReader reader(fd);
67    int type;
68
69    while (reader.ReadNextHeader(&type) == 0) {
70        switch (type) {
71            case BACKUP_HEADER_APP_V1:
72            {
73                String8 packageName;
74                int cookie;
75                err = reader.ReadAppHeader(&packageName, &cookie);
76                if (err == 0) {
77                    printf("App header: %s 0x%08x (%d)\n", packageName.string(), cookie, cookie);
78                } else {
79                    printf("Error reading app header\n");
80                }
81                break;
82            }
83            case BACKUP_HEADER_ENTITY_V1:
84            {
85                String8 key;
86                size_t dataSize;
87                err = reader.ReadEntityHeader(&key, &dataSize);
88                if (err == 0) {
89                    printf("   entity: %s (%d bytes)\n", key.string(), dataSize);
90                } else {
91                    printf("   Error reading entity header\n");
92                }
93                break;
94            }
95            case BACKUP_FOOTER_APP_V1:
96            {
97                int cookie;
98                err = reader.ReadAppFooter(&cookie);
99                if (err == 0) {
100                    printf("   App footer: 0x%08x (%d)\n", cookie, cookie);
101                } else {
102                    printf("   Error reading entity header\n");
103                }
104                break;
105            }
106            default:
107            {
108                printf("Unknown chunk type: 0x%08x\n", type);
109                break;
110            }
111        }
112    }
113
114    return 0;
115}
116
117int perform_print(const char* entityname, const char* filename)
118{
119    printf("perform_print(%s, %s);", entityname, filename);
120    return 0;
121}
122
123int
124main(int argc, const char** argv)
125{
126    if (argc <= 1) {
127        return perform_full_backup();
128    }
129    if (argc == 3 && 0 == strcmp(argv[1], "list")) {
130        return perform_list(argv[2]);
131    }
132    if (argc == 4 && 0 == strcmp(argv[1], "print")) {
133        return perform_print(argv[2], argv[3]);
134    }
135    return usage(argc, argv);
136}
137
138