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