2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _UTILS_BACKUP_HELPERS_H
18 #define _UTILS_BACKUP_HELPERS_H
22 #include <utils/Errors.h>
23 #include <utils/String8.h>
24 #include <utils/KeyedVector.h>
29 BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
33 int type; // BACKUP_HEADER_ENTITY_V1
34 int keyLen; // length of the key name, not including the null terminator
35 int dataSize; // size of the data, not including the padding, -1 means delete
38 struct SnapshotHeader {
64 * If an error occurs, it poisons this object and all write calls will fail
65 * with the error that occurred.
67 class BackupDataWriter
70 BackupDataWriter(int fd);
74 status_t WriteEntityHeader(const String8& key, size_t dataSize);
76 /* Note: WriteEntityData will write arbitrary data into the file without
77 * validation or a previously-supplied header. The full backup implementation
78 * uses it this way to generate a controlled binary stream that is not
79 * entity-structured. If the implementation here is changed, either this
80 * use case must remain valid, or the full backup implementation should be
81 * adjusted to use some other appropriate mechanism.
83 status_t WriteEntityData(const void* data, size_t size);
85 void SetKeyPrefix(const String8& keyPrefix);
88 explicit BackupDataWriter();
89 status_t write_padding_for(int n);
101 * If an error occurs, it poisons this object and all write calls will fail
102 * with the error that occurred.
104 class BackupDataReader
107 BackupDataReader(int fd);
112 status_t ReadNextHeader(bool* done, int* type);
115 status_t ReadEntityHeader(String8* key, size_t* dataSize);
116 status_t SkipEntityData(); // must be called with the pointer at the beginning of the data.
117 ssize_t ReadEntityData(void* data, size_t size);
120 explicit BackupDataReader();
121 status_t skip_padding();
127 ssize_t m_dataEndPos;
131 entity_header_v1 entity;
136 int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
137 char const* const* files, char const* const *keys, int fileCount);
139 int write_tarfile(const String8& packageName, const String8& domain,
140 const String8& rootPath, const String8& filePath, off_t* outSize,
141 BackupDataWriter* outputStream);
143 class RestoreHelperBase
147 ~RestoreHelperBase();
149 status_t WriteFile(const String8& filename, BackupDataReader* in);
150 status_t WriteSnapshot(int fd);
154 bool m_loggedUnknownMetadata;
155 KeyedVector<String8,FileRec> m_files;
158 //#define TEST_BACKUP_HELPERS 1
160 #if TEST_BACKUP_HELPERS
161 int backup_helper_test_empty();
162 int backup_helper_test_four();
163 int backup_helper_test_files();
164 int backup_helper_test_null_base();
165 int backup_helper_test_missing_file();
166 int backup_helper_test_data_writer();
167 int backup_helper_test_data_reader();
170 } // namespace android
172 #endif // _UTILS_BACKUP_HELPERS_H