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