backup.cpp revision 8ae2335a3c93d0c00e998fdec18f64dfe43b94cb
1d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu/* 2d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * Copyright (C) 2009 The Android Open Source Project 3d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * 4d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * Licensed under the Apache License, Version 2.0 (the "License"); 5d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * you may not use this file except in compliance with the License. 6d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * You may obtain a copy of the License at 7d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * 8d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * http://www.apache.org/licenses/LICENSE-2.0 9d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * 10d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * Unless required by applicable law or agreed to in writing, software 11d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * distributed under the License is distributed on an "AS IS" BASIS, 12d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * See the License for the specific language governing permissions and 14d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu * limitations under the License. 15d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu */ 16d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu 17d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <utils/BackupHelpers.h> 18d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <utils/String8.h> 19d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu 20d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <fcntl.h> 21d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <stdio.h> 22d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <string.h> 23d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu 24d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhuusing namespace android; 25d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu 26d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu#include <unistd.h> 27d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu 28d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhuint 29d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhuusage(int argc, const char** argv) 30d367349218c5e12feb6f0de732293da45f5e9af3Guang Zhu{ 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