Joe Onorato | 4535e40 | 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 | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 17 | #ifndef _UTILS_BACKUP_HELPERS_H |
| 18 | #define _UTILS_BACKUP_HELPERS_H |
| 19 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 20 | #include <utils/Errors.h> |
| 21 | #include <utils/String8.h> |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 22 | #include <utils/KeyedVector.h> |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 26 | enum { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 27 | BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 28 | }; |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 29 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 30 | typedef struct { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 31 | int type; // BACKUP_HEADER_ENTITY_V1 |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 32 | int keyLen; // length of the key name, not including the null terminator |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 33 | int dataSize; // size of the data, not including the padding, -1 means delete |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 34 | } entity_header_v1; |
| 35 | |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 36 | struct SnapshotHeader { |
| 37 | int magic0; |
| 38 | int fileCount; |
| 39 | int magic1; |
| 40 | int totalSize; |
| 41 | }; |
| 42 | |
| 43 | struct FileState { |
| 44 | int modTime_sec; |
| 45 | int modTime_nsec; |
Christopher Tate | 11b1577 | 2009-06-23 13:03:00 -0700 | [diff] [blame] | 46 | int mode; |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 47 | int size; |
| 48 | int crc32; |
| 49 | int nameLen; |
| 50 | }; |
| 51 | |
| 52 | struct FileRec { |
| 53 | String8 file; |
| 54 | bool deleted; |
| 55 | FileState s; |
| 56 | }; |
| 57 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 58 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 59 | /** |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 60 | * Writes the data. |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 61 | * |
| 62 | * If an error occurs, it poisons this object and all write calls will fail |
| 63 | * with the error that occurred. |
| 64 | */ |
| 65 | class BackupDataWriter |
| 66 | { |
| 67 | public: |
| 68 | BackupDataWriter(int fd); |
| 69 | // does not close fd |
| 70 | ~BackupDataWriter(); |
| 71 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 72 | status_t WriteEntityHeader(const String8& key, size_t dataSize); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 73 | |
| 74 | /* Note: WriteEntityData will write arbitrary data into the file without |
| 75 | * validation or a previously-supplied header. The full backup implementation |
| 76 | * uses it this way to generate a controlled binary stream that is not |
| 77 | * entity-structured. If the implementation here is changed, either this |
| 78 | * use case must remain valid, or the full backup implementation should be |
| 79 | * adjusted to use some other appropriate mechanism. |
| 80 | */ |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 81 | status_t WriteEntityData(const void* data, size_t size); |
| 82 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 83 | void SetKeyPrefix(const String8& keyPrefix); |
| 84 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 85 | private: |
| 86 | explicit BackupDataWriter(); |
| 87 | status_t write_padding_for(int n); |
| 88 | |
| 89 | int m_fd; |
| 90 | status_t m_status; |
| 91 | ssize_t m_pos; |
| 92 | int m_entityCount; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 93 | String8 m_keyPrefix; |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 96 | /** |
| 97 | * Reads the data. |
| 98 | * |
| 99 | * If an error occurs, it poisons this object and all write calls will fail |
| 100 | * with the error that occurred. |
| 101 | */ |
| 102 | class BackupDataReader |
| 103 | { |
| 104 | public: |
| 105 | BackupDataReader(int fd); |
| 106 | // does not close fd |
| 107 | ~BackupDataReader(); |
| 108 | |
| 109 | status_t Status(); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 110 | status_t ReadNextHeader(bool* done, int* type); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 111 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 112 | bool HasEntities(); |
| 113 | status_t ReadEntityHeader(String8* key, size_t* dataSize); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 114 | status_t SkipEntityData(); // must be called with the pointer at the beginning of the data. |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 115 | ssize_t ReadEntityData(void* data, size_t size); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | explicit BackupDataReader(); |
| 119 | status_t skip_padding(); |
| 120 | |
| 121 | int m_fd; |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 122 | bool m_done; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 123 | status_t m_status; |
| 124 | ssize_t m_pos; |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 125 | ssize_t m_dataEndPos; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 126 | int m_entityCount; |
| 127 | union { |
| 128 | int type; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 129 | entity_header_v1 entity; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 130 | } m_header; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 131 | String8 m_key; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 134 | int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 135 | char const* const* files, char const* const *keys, int fileCount); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 136 | |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 137 | int write_tarfile(const String8& packageName, const String8& domain, |
| 138 | const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream); |
| 139 | |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 140 | class RestoreHelperBase |
| 141 | { |
| 142 | public: |
| 143 | RestoreHelperBase(); |
| 144 | ~RestoreHelperBase(); |
| 145 | |
| 146 | status_t WriteFile(const String8& filename, BackupDataReader* in); |
| 147 | status_t WriteSnapshot(int fd); |
| 148 | |
| 149 | private: |
| 150 | void* m_buf; |
Christopher Tate | 63bcb79 | 2009-06-24 13:57:29 -0700 | [diff] [blame] | 151 | bool m_loggedUnknownMetadata; |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 152 | KeyedVector<String8,FileRec> m_files; |
| 153 | }; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 154 | |
| 155 | #define TEST_BACKUP_HELPERS 1 |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 156 | |
| 157 | #if TEST_BACKUP_HELPERS |
| 158 | int backup_helper_test_empty(); |
| 159 | int backup_helper_test_four(); |
| 160 | int backup_helper_test_files(); |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 161 | int backup_helper_test_null_base(); |
Joe Onorato | ce88cb1 | 2009-06-11 11:27:16 -0700 | [diff] [blame] | 162 | int backup_helper_test_missing_file(); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 163 | int backup_helper_test_data_writer(); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 164 | int backup_helper_test_data_reader(); |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 165 | #endif |
| 166 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 167 | } // namespace android |
| 168 | |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 169 | #endif // _UTILS_BACKUP_HELPERS_H |