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    bool done;
68    int type;
69
70    while (reader.ReadNextHeader(&done, &type) == 0) {
71        if (done) {
72            break;
73        }
74        switch (type) {
75            case BACKUP_HEADER_ENTITY_V1:
76            {
77                String8 key;
78                size_t dataSize;
79                err = reader.ReadEntityHeader(&key, &dataSize);
80                if (err == 0) {
81                    printf("   entity: %s (%d bytes)\n", key.string(), dataSize);
82                } else {
83                    printf("   Error reading entity header\n");
84                }
85                break;
86            }
87            default:
88            {
89                printf("Unknown chunk type: 0x%08x\n", type);
90                break;
91            }
92        }
93    }
94
95    return 0;
96}
97
98int perform_print(const char* entityname, const char* filename)
99{
100    printf("perform_print(%s, %s);", entityname, filename);
101    return 0;
102}
103
104int
105main(int argc, const char** argv)
106{
107    if (argc <= 1) {
108        return perform_full_backup();
109    }
110    if (argc == 3 && 0 == strcmp(argv[1], "list")) {
111        return perform_list(argv[2]);
112    }
113    if (argc == 4 && 0 == strcmp(argv[1], "print")) {
114        return perform_print(argv[2], argv[3]);
115    }
116    return usage(argc, argv);
117}
118
119