blob: 6a7f0564647259d62edafa107e0a33b04d4981ef [file] [log] [blame]
Joe Onorato4535e402009-05-15 09:07:06 -04001/*
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 Onorato2e1da322009-05-15 18:20:19 -040017#define LOG_TAG "backup_data"
18
Mathias Agopian8ae23352009-06-04 13:53:57 -070019#include <utils/BackupHelpers.h>
Joe Onorato4535e402009-05-15 09:07:06 -040020#include <utils/ByteOrder.h>
21
22#include <stdio.h>
23#include <unistd.h>
24
Joe Onorato2e1da322009-05-15 18:20:19 -040025#include <cutils/log.h>
26
Joe Onorato4535e402009-05-15 09:07:06 -040027namespace android {
28
29/*
30 * File Format (v1):
31 *
32 * All ints are stored little-endian.
33 *
34 * - An app_header_v1 struct.
35 * - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
36 * - A sequence of zero or more key/value paires (entities), each with
37 * - A entity_header_v1 struct
38 * - The key, utf-8, null terminated, padded to 4-byte boundary.
39 * - The value, padded to 4 byte boundary
40 */
41
Joe Onorato4535e402009-05-15 09:07:06 -040042const static int ROUND_UP[4] = { 0, 3, 2, 1 };
43
44static inline size_t
45round_up(size_t n)
46{
47 return n + ROUND_UP[n % 4];
48}
49
50static inline size_t
51padding_extra(size_t n)
52{
53 return ROUND_UP[n % 4];
54}
55
56BackupDataWriter::BackupDataWriter(int fd)
57 :m_fd(fd),
58 m_status(NO_ERROR),
59 m_pos(0),
60 m_entityCount(0)
61{
62}
63
64BackupDataWriter::~BackupDataWriter()
65{
66}
67
68// Pad out anything they've previously written to the next 4 byte boundary.
69status_t
70BackupDataWriter::write_padding_for(int n)
71{
72 ssize_t amt;
73 ssize_t paddingSize;
74
75 paddingSize = padding_extra(n);
76 if (paddingSize > 0) {
77 uint32_t padding = 0xbcbcbcbc;
78 amt = write(m_fd, &padding, paddingSize);
79 if (amt != paddingSize) {
80 m_status = errno;
81 return m_status;
82 }
83 m_pos += amt;
84 }
85 return NO_ERROR;
86}
87
88status_t
Joe Onorato4535e402009-05-15 09:07:06 -040089BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
90{
91 if (m_status != NO_ERROR) {
92 return m_status;
93 }
94
95 ssize_t amt;
96
97 amt = write_padding_for(m_pos);
98 if (amt != 0) {
99 return amt;
100 }
101
102 entity_header_v1 header;
103 ssize_t keyLen;
104
105 keyLen = key.length();
106
Joe Onoratod2110db2009-05-19 13:41:21 -0700107 header.type = tolel(BACKUP_HEADER_ENTITY_V1);
Joe Onorato4535e402009-05-15 09:07:06 -0400108 header.keyLen = tolel(keyLen);
109 header.dataSize = tolel(dataSize);
110
111 amt = write(m_fd, &header, sizeof(entity_header_v1));
112 if (amt != sizeof(entity_header_v1)) {
113 m_status = errno;
114 return m_status;
115 }
116 m_pos += amt;
117
118 amt = write(m_fd, key.string(), keyLen+1);
119 if (amt != keyLen+1) {
120 m_status = errno;
121 return m_status;
122 }
123 m_pos += amt;
124
125 amt = write_padding_for(keyLen+1);
126
127 m_entityCount++;
128
129 return amt;
130}
131
132status_t
133BackupDataWriter::WriteEntityData(const void* data, size_t size)
134{
135 if (m_status != NO_ERROR) {
136 return m_status;
137 }
138
139 // We don't write padding here, because they're allowed to call this several
140 // times with smaller buffers. We write it at the end of WriteEntityHeader
141 // instead.
142 ssize_t amt = write(m_fd, data, size);
143 if (amt != (ssize_t)size) {
144 m_status = errno;
145 return m_status;
146 }
147 m_pos += amt;
148 return NO_ERROR;
149}
150
Joe Onorato4535e402009-05-15 09:07:06 -0400151
Joe Onorato2e1da322009-05-15 18:20:19 -0400152
153BackupDataReader::BackupDataReader(int fd)
154 :m_fd(fd),
Joe Onorato5f15d152009-06-16 16:31:35 -0400155 m_done(false),
Joe Onorato2e1da322009-05-15 18:20:19 -0400156 m_status(NO_ERROR),
157 m_pos(0),
158 m_entityCount(0)
159{
160 memset(&m_header, 0, sizeof(m_header));
161}
162
163BackupDataReader::~BackupDataReader()
164{
165}
166
167status_t
168BackupDataReader::Status()
169{
170 return m_status;
171}
172
173#define CHECK_SIZE(actual, expected) \
174 do { \
175 if ((actual) != (expected)) { \
176 if ((actual) == 0) { \
177 m_status = EIO; \
178 } else { \
179 m_status = errno; \
180 } \
181 return m_status; \
182 } \
183 } while(0)
184#define SKIP_PADDING() \
185 do { \
186 status_t err = skip_padding(); \
187 if (err != NO_ERROR) { \
188 m_status = err; \
189 return err; \
190 } \
191 } while(0)
192
193status_t
Joe Onorato5f15d152009-06-16 16:31:35 -0400194BackupDataReader::ReadNextHeader(bool* done, int* type)
Joe Onorato2e1da322009-05-15 18:20:19 -0400195{
Joe Onorato5f15d152009-06-16 16:31:35 -0400196 *done = m_done;
Joe Onorato2e1da322009-05-15 18:20:19 -0400197 if (m_status != NO_ERROR) {
198 return m_status;
199 }
200
201 int amt;
202
Joe Onorato5f15d152009-06-16 16:31:35 -0400203 // No error checking here, in case we're at the end of the stream. Just let read() fail.
204 skip_padding();
Joe Onorato2e1da322009-05-15 18:20:19 -0400205 amt = read(m_fd, &m_header, sizeof(m_header));
Joe Onorato5f15d152009-06-16 16:31:35 -0400206 *done = m_done = (amt == 0);
Joe Onorato2e1da322009-05-15 18:20:19 -0400207 CHECK_SIZE(amt, sizeof(m_header));
Joe Onorato5d605dc2009-06-18 18:23:43 -0700208 m_pos += sizeof(m_header);
209 if (type) {
210 *type = m_header.type;
211 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400212
213 // validate and fix up the fields.
214 m_header.type = fromlel(m_header.type);
215 switch (m_header.type)
216 {
Joe Onoratod2110db2009-05-19 13:41:21 -0700217 case BACKUP_HEADER_ENTITY_V1:
Joe Onorato5d605dc2009-06-18 18:23:43 -0700218 {
Joe Onorato2e1da322009-05-15 18:20:19 -0400219 m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
220 if (m_header.entity.keyLen <= 0) {
221 LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
222 (int)m_header.entity.keyLen);
223 m_status = EINVAL;
224 }
225 m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
Joe Onorato2e1da322009-05-15 18:20:19 -0400226 m_entityCount++;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700227
228 // read the rest of the header (filename)
229 size_t size = m_header.entity.keyLen;
230 char* buf = m_key.lockBuffer(size);
231 if (buf == NULL) {
232 m_status = ENOMEM;
233 return m_status;
234 }
235 int amt = read(m_fd, buf, size+1);
236 CHECK_SIZE(amt, (int)size+1);
237 m_key.unlockBuffer(size);
238 m_pos += size+1;
239 SKIP_PADDING();
240 m_dataEndPos = m_pos + m_header.entity.dataSize;
241
Joe Onorato2e1da322009-05-15 18:20:19 -0400242 break;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700243 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400244 default:
245 LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type);
246 m_status = EINVAL;
247 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400248
249 return m_status;
250}
251
Joe Onorato2e1da322009-05-15 18:20:19 -0400252bool
253BackupDataReader::HasEntities()
254{
Joe Onoratod2110db2009-05-19 13:41:21 -0700255 return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400256}
257
258status_t
259BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
260{
261 if (m_status != NO_ERROR) {
262 return m_status;
263 }
Joe Onoratod2110db2009-05-19 13:41:21 -0700264 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
Joe Onorato2e1da322009-05-15 18:20:19 -0400265 return EINVAL;
266 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700267 *key = m_key;
Joe Onorato2e1da322009-05-15 18:20:19 -0400268 *dataSize = m_header.entity.dataSize;
Joe Onorato2e1da322009-05-15 18:20:19 -0400269 return NO_ERROR;
270}
271
272status_t
Joe Onoratod2110db2009-05-19 13:41:21 -0700273BackupDataReader::SkipEntityData()
274{
275 if (m_status != NO_ERROR) {
276 return m_status;
277 }
278 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
279 return EINVAL;
280 }
281 if (m_header.entity.dataSize > 0) {
Joe Onorato5f15d152009-06-16 16:31:35 -0400282 int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
Joe Onoratod2110db2009-05-19 13:41:21 -0700283 return pos == -1 ? (int)errno : (int)NO_ERROR;
284 } else {
285 return NO_ERROR;
286 }
287}
288
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700289ssize_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400290BackupDataReader::ReadEntityData(void* data, size_t size)
291{
292 if (m_status != NO_ERROR) {
Joe Onorato5d605dc2009-06-18 18:23:43 -0700293 return -1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400294 }
Joe Onorato5f15d152009-06-16 16:31:35 -0400295 int remaining = m_dataEndPos - m_pos;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700296 //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
297 // size, m_pos, m_dataEndPos, remaining);
Joe Onorato5f15d152009-06-16 16:31:35 -0400298 if (remaining <= 0) {
299 return 0;
300 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700301 if (size > remaining) {
302 size = remaining;
303 }
304 //LOGD(" reading %d bytes", size);
Joe Onorato2e1da322009-05-15 18:20:19 -0400305 int amt = read(m_fd, data, size);
Joe Onorato5d605dc2009-06-18 18:23:43 -0700306 if (amt < 0) {
307 m_status = errno;
308 return -1;
309 }
310 m_pos += amt;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700311 return amt;
Joe Onorato2e1da322009-05-15 18:20:19 -0400312}
313
314status_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400315BackupDataReader::skip_padding()
316{
317 ssize_t amt;
318 ssize_t paddingSize;
319
320 paddingSize = padding_extra(m_pos);
321 if (paddingSize > 0) {
322 uint32_t padding;
323 amt = read(m_fd, &padding, paddingSize);
324 CHECK_SIZE(amt, paddingSize);
325 m_pos += amt;
326 }
327 return NO_ERROR;
328}
329
330
Joe Onorato4535e402009-05-15 09:07:06 -0400331} // namespace android