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); |
| 73 | status_t WriteEntityData(const void* data, size_t size); |
| 74 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 75 | void SetKeyPrefix(const String8& keyPrefix); |
| 76 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 77 | private: |
| 78 | explicit BackupDataWriter(); |
| 79 | status_t write_padding_for(int n); |
| 80 | |
| 81 | int m_fd; |
| 82 | status_t m_status; |
| 83 | ssize_t m_pos; |
| 84 | int m_entityCount; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 85 | String8 m_keyPrefix; |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 86 | }; |
| 87 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Reads the data. |
| 90 | * |
| 91 | * If an error occurs, it poisons this object and all write calls will fail |
| 92 | * with the error that occurred. |
| 93 | */ |
| 94 | class BackupDataReader |
| 95 | { |
| 96 | public: |
| 97 | BackupDataReader(int fd); |
| 98 | // does not close fd |
| 99 | ~BackupDataReader(); |
| 100 | |
| 101 | status_t Status(); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 102 | status_t ReadNextHeader(bool* done, int* type); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 103 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 104 | bool HasEntities(); |
| 105 | status_t ReadEntityHeader(String8* key, size_t* dataSize); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 106 | status_t SkipEntityData(); // must be called with the pointer at the begining of the data. |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 107 | ssize_t ReadEntityData(void* data, size_t size); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | explicit BackupDataReader(); |
| 111 | status_t skip_padding(); |
| 112 | |
| 113 | int m_fd; |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 114 | bool m_done; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 115 | status_t m_status; |
| 116 | ssize_t m_pos; |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 117 | ssize_t m_dataEndPos; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 118 | int m_entityCount; |
| 119 | union { |
| 120 | int type; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 121 | entity_header_v1 entity; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 122 | } m_header; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 123 | String8 m_key; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 126 | int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 127 | char const* const* files, char const* const *keys, int fileCount); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 128 | |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 129 | class RestoreHelperBase |
| 130 | { |
| 131 | public: |
| 132 | RestoreHelperBase(); |
| 133 | ~RestoreHelperBase(); |
| 134 | |
| 135 | status_t WriteFile(const String8& filename, BackupDataReader* in); |
| 136 | status_t WriteSnapshot(int fd); |
| 137 | |
| 138 | private: |
| 139 | void* m_buf; |
Christopher Tate | 63bcb79 | 2009-06-24 13:57:29 -0700 | [diff] [blame] | 140 | bool m_loggedUnknownMetadata; |
Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 141 | KeyedVector<String8,FileRec> m_files; |
| 142 | }; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 143 | |
| 144 | #define TEST_BACKUP_HELPERS 1 |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 145 | |
| 146 | #if TEST_BACKUP_HELPERS |
| 147 | int backup_helper_test_empty(); |
| 148 | int backup_helper_test_four(); |
| 149 | int backup_helper_test_files(); |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 150 | int backup_helper_test_null_base(); |
Joe Onorato | ce88cb1 | 2009-06-11 11:27:16 -0700 | [diff] [blame] | 151 | int backup_helper_test_missing_file(); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 152 | int backup_helper_test_data_writer(); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 153 | int backup_helper_test_data_reader(); |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 154 | #endif |
| 155 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 156 | } // namespace android |
| 157 | |
Joe Onorato | 3ad977b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 158 | #endif // _UTILS_BACKUP_HELPERS_H |