blob: 8d9b528ae6a94976f2a4c7491c2da59f3300df06 [file] [log] [blame]
Mathias Agopian8ae23352009-06-04 13:53:57 -07001/*
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 Onoratod2110db2009-05-19 13:41:21 -070016
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080017#include <androidfw/BackupHelpers.h>
Joe Onoratod2110db2009-05-19 13:41:21 -070018#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
Andreas Gampecfedceb2014-09-30 21:48:18 -070028static int usage(int /* argc */, const char** argv)
Joe Onoratod2110db2009-05-19 13:41:21 -070029{
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 Gampecfedceb2014-09-30 21:48:18 -070046static int perform_full_backup()
Joe Onoratod2110db2009-05-19 13:41:21 -070047{
48 printf("this would have written all of your data to stdout\n");
49 return 0;
50}
51
Andreas Gampecfedceb2014-09-30 21:48:18 -070052static int perform_list(const char* filename)
Joe Onoratod2110db2009-05-19 13:41:21 -070053{
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 Onorato5f15d152009-06-16 16:31:35 -040064 bool done;
Joe Onoratod2110db2009-05-19 13:41:21 -070065 int type;
66
Joe Onorato5f15d152009-06-16 16:31:35 -040067 while (reader.ReadNextHeader(&done, &type) == 0) {
68 if (done) {
69 break;
70 }
Joe Onoratod2110db2009-05-19 13:41:21 -070071 switch (type) {
Joe Onoratod2110db2009-05-19 13:41:21 -070072 case BACKUP_HEADER_ENTITY_V1:
73 {
74 String8 key;
75 size_t dataSize;
76 err = reader.ReadEntityHeader(&key, &dataSize);
77 if (err == 0) {
Andreas Gampeebee1372014-11-07 16:28:19 -080078 printf(" entity: %s (%zu bytes)\n", key.string(), dataSize);
Joe Onoratod2110db2009-05-19 13:41:21 -070079 } else {
80 printf(" Error reading entity header\n");
81 }
82 break;
83 }
Joe Onoratod2110db2009-05-19 13:41:21 -070084 default:
85 {
86 printf("Unknown chunk type: 0x%08x\n", type);
87 break;
88 }
89 }
90 }
91
92 return 0;
93}
94
Andreas Gampecfedceb2014-09-30 21:48:18 -070095static int perform_print(const char* entityname, const char* filename)
Joe Onoratod2110db2009-05-19 13:41:21 -070096{
97 printf("perform_print(%s, %s);", entityname, filename);
98 return 0;
99}
100
Andreas Gampecfedceb2014-09-30 21:48:18 -0700101int main(int argc, const char** argv)
Joe Onoratod2110db2009-05-19 13:41:21 -0700102{
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