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 { |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 26 | BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian) |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 27 | }; |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 28 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 29 | typedef struct { |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 30 | int type; // BACKUP_HEADER_ENTITY_V1 |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 31 | int keyLen; // length of the key name, not including the null terminator |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 32 | 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] | 33 | } entity_header_v1; |
| 34 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 35 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 36 | /** |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 37 | * Writes the data. |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 38 | * |
| 39 | * If an error occurs, it poisons this object and all write calls will fail |
| 40 | * with the error that occurred. |
| 41 | */ |
| 42 | class BackupDataWriter |
| 43 | { |
| 44 | public: |
| 45 | BackupDataWriter(int fd); |
| 46 | // does not close fd |
| 47 | ~BackupDataWriter(); |
| 48 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 49 | status_t WriteEntityHeader(const String8& key, size_t dataSize); |
| 50 | status_t WriteEntityData(const void* data, size_t size); |
| 51 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 52 | private: |
| 53 | explicit BackupDataWriter(); |
| 54 | status_t write_padding_for(int n); |
| 55 | |
| 56 | int m_fd; |
| 57 | status_t m_status; |
| 58 | ssize_t m_pos; |
| 59 | int m_entityCount; |
| 60 | }; |
| 61 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 62 | /** |
| 63 | * Reads the data. |
| 64 | * |
| 65 | * If an error occurs, it poisons this object and all write calls will fail |
| 66 | * with the error that occurred. |
| 67 | */ |
| 68 | class BackupDataReader |
| 69 | { |
| 70 | public: |
| 71 | BackupDataReader(int fd); |
| 72 | // does not close fd |
| 73 | ~BackupDataReader(); |
| 74 | |
| 75 | status_t Status(); |
Joe Onorato | 03aa8d7 | 2009-06-16 16:31:35 -0400 | [diff] [blame^] | 76 | status_t ReadNextHeader(bool* done, int* type); |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 77 | |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 78 | bool HasEntities(); |
| 79 | status_t ReadEntityHeader(String8* key, size_t* dataSize); |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 80 | 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] | 81 | status_t ReadEntityData(void* data, size_t size); |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | explicit BackupDataReader(); |
| 85 | status_t skip_padding(); |
| 86 | |
| 87 | int m_fd; |
Joe Onorato | 03aa8d7 | 2009-06-16 16:31:35 -0400 | [diff] [blame^] | 88 | bool m_done; |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 89 | status_t m_status; |
| 90 | ssize_t m_pos; |
Joe Onorato | 03aa8d7 | 2009-06-16 16:31:35 -0400 | [diff] [blame^] | 91 | ssize_t m_dataEndPos; |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 92 | int m_entityCount; |
| 93 | union { |
| 94 | int type; |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 95 | entity_header_v1 entity; |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 96 | } m_header; |
| 97 | }; |
| 98 | |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 99 | int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, |
Joe Onorato | 0ad6120 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 100 | char const* const* files, char const* const *keys, int fileCount); |
Joe Onorato | 473b6e2 | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | #define TEST_BACKUP_HELPERS 1 |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 104 | |
| 105 | #if TEST_BACKUP_HELPERS |
| 106 | int backup_helper_test_empty(); |
| 107 | int backup_helper_test_four(); |
| 108 | int backup_helper_test_files(); |
Joe Onorato | 0ad6120 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 109 | int backup_helper_test_null_base(); |
Joe Onorato | 1a9e19a | 2009-06-11 11:27:16 -0700 | [diff] [blame] | 110 | int backup_helper_test_missing_file(); |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 111 | int backup_helper_test_data_writer(); |
Joe Onorato | d502f05 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 112 | int backup_helper_test_data_reader(); |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 113 | #endif |
| 114 | |
Joe Onorato | 8d626d6 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 115 | } // namespace android |
| 116 | |
Joe Onorato | 0c4863b | 2009-05-05 11:50:51 -0700 | [diff] [blame] | 117 | #endif // _UTILS_BACKUP_HELPERS_H |