Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 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 | |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 17 | #ifndef _UTILS_BACKUP_HELPERS_H |
| 18 | #define _UTILS_BACKUP_HELPERS_H |
| 19 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 20 | #include <utils/Errors.h> |
| 21 | #include <utils/String8.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 25 | enum { |
| 26 | BACKUP_HEADER_APP_V1 = 0x31707041, // App1 (little endian) |
| 27 | BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian) |
| 28 | BACKUP_FOOTER_APP_V1 = 0x746f6f46, // Foot (little endian) |
| 29 | }; |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 30 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 31 | // the sizes of all of these match. |
| 32 | typedef struct { |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 33 | int type; // == BACKUP_HEADER_APP_V1 |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 34 | int packageLen; // length of the name of the package that follows, not including the null. |
| 35 | int cookie; |
| 36 | } app_header_v1; |
| 37 | |
| 38 | typedef struct { |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 39 | int type; // BACKUP_HEADER_ENTITY_V1 |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 40 | int keyLen; // length of the key name, not including the null terminator |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 41 | int dataSize; // size of the data, not including the padding, -1 means delete |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 42 | } entity_header_v1; |
| 43 | |
| 44 | typedef struct { |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 45 | int type; // BACKUP_FOOTER_APP_V1 |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 46 | int entityCount; // the number of entities that were written |
| 47 | int cookie; |
| 48 | } app_footer_v1; |
| 49 | |
| 50 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 51 | /** |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 52 | * Writes the data. |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 53 | * |
| 54 | * If an error occurs, it poisons this object and all write calls will fail |
| 55 | * with the error that occurred. |
| 56 | */ |
| 57 | class BackupDataWriter |
| 58 | { |
| 59 | public: |
| 60 | BackupDataWriter(int fd); |
| 61 | // does not close fd |
| 62 | ~BackupDataWriter(); |
| 63 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 64 | status_t WriteAppHeader(const String8& packageName, int cookie); |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 65 | |
| 66 | status_t WriteEntityHeader(const String8& key, size_t dataSize); |
| 67 | status_t WriteEntityData(const void* data, size_t size); |
| 68 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 69 | status_t WriteAppFooter(int cookie); |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | explicit BackupDataWriter(); |
| 73 | status_t write_padding_for(int n); |
| 74 | |
| 75 | int m_fd; |
| 76 | status_t m_status; |
| 77 | ssize_t m_pos; |
| 78 | int m_entityCount; |
| 79 | }; |
| 80 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Reads the data. |
| 83 | * |
| 84 | * If an error occurs, it poisons this object and all write calls will fail |
| 85 | * with the error that occurred. |
| 86 | */ |
| 87 | class BackupDataReader |
| 88 | { |
| 89 | public: |
| 90 | BackupDataReader(int fd); |
| 91 | // does not close fd |
| 92 | ~BackupDataReader(); |
| 93 | |
| 94 | status_t Status(); |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 95 | status_t ReadNextHeader(int* type = NULL); |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 96 | |
| 97 | status_t ReadAppHeader(String8* packageName, int* cookie); |
| 98 | bool HasEntities(); |
| 99 | status_t ReadEntityHeader(String8* key, size_t* dataSize); |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 100 | status_t SkipEntityData(); // must be called with the pointer at the begining of the data. |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 101 | status_t ReadEntityData(void* data, size_t size); |
| 102 | status_t ReadAppFooter(int* cookie); |
| 103 | |
| 104 | private: |
| 105 | explicit BackupDataReader(); |
| 106 | status_t skip_padding(); |
| 107 | |
| 108 | int m_fd; |
| 109 | status_t m_status; |
| 110 | ssize_t m_pos; |
| 111 | int m_entityCount; |
| 112 | union { |
| 113 | int type; |
| 114 | app_header_v1 app; |
| 115 | entity_header_v1 entity; |
| 116 | app_footer_v1 footer; |
| 117 | } m_header; |
| 118 | }; |
| 119 | |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 120 | int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, |
Joe Onorato | 0ad6120 | 2009-06-10 17:07:15 -0700 | [diff] [blame^] | 121 | char const* const* files, char const* const *keys, int fileCount); |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | #define TEST_BACKUP_HELPERS 1 |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 125 | |
| 126 | #if TEST_BACKUP_HELPERS |
| 127 | int backup_helper_test_empty(); |
| 128 | int backup_helper_test_four(); |
| 129 | int backup_helper_test_files(); |
Joe Onorato | 0ad6120 | 2009-06-10 17:07:15 -0700 | [diff] [blame^] | 130 | int backup_helper_test_null_base(); |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 131 | int backup_helper_test_data_writer(); |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 132 | int backup_helper_test_data_reader(); |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 133 | #endif |
| 134 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 135 | } // namespace android |
| 136 | |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 137 | #endif // _UTILS_BACKUP_HELPERS_H |