blob: f963058fab6d3e99c081d3a9e887f965613d6e13 [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>
Christopher Tate4a627c72011-04-01 14:43:32 -070023#include <string.h>
Joe Onorato4535e402009-05-15 09:07:06 -040024#include <unistd.h>
25
Joe Onorato2e1da322009-05-15 18:20:19 -040026#include <cutils/log.h>
27
Joe Onorato4535e402009-05-15 09:07:06 -040028namespace android {
29
Christopher Tate4a627c72011-04-01 14:43:32 -070030static const bool DEBUG = false;
31
Joe Onorato4535e402009-05-15 09:07:06 -040032/*
33 * File Format (v1):
34 *
35 * All ints are stored little-endian.
36 *
37 * - An app_header_v1 struct.
38 * - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
39 * - A sequence of zero or more key/value paires (entities), each with
40 * - A entity_header_v1 struct
41 * - The key, utf-8, null terminated, padded to 4-byte boundary.
42 * - The value, padded to 4 byte boundary
43 */
44
Joe Onorato4535e402009-05-15 09:07:06 -040045const static int ROUND_UP[4] = { 0, 3, 2, 1 };
46
47static inline size_t
48round_up(size_t n)
49{
50 return n + ROUND_UP[n % 4];
51}
52
53static inline size_t
54padding_extra(size_t n)
55{
56 return ROUND_UP[n % 4];
57}
58
59BackupDataWriter::BackupDataWriter(int fd)
60 :m_fd(fd),
61 m_status(NO_ERROR),
62 m_pos(0),
63 m_entityCount(0)
64{
65}
66
67BackupDataWriter::~BackupDataWriter()
68{
69}
70
71// Pad out anything they've previously written to the next 4 byte boundary.
72status_t
73BackupDataWriter::write_padding_for(int n)
74{
75 ssize_t amt;
76 ssize_t paddingSize;
77
78 paddingSize = padding_extra(n);
79 if (paddingSize > 0) {
80 uint32_t padding = 0xbcbcbcbc;
Christopher Tate4a627c72011-04-01 14:43:32 -070081 if (DEBUG) LOGI("writing %d padding bytes for %d", paddingSize, n);
Joe Onorato4535e402009-05-15 09:07:06 -040082 amt = write(m_fd, &padding, paddingSize);
83 if (amt != paddingSize) {
84 m_status = errno;
85 return m_status;
86 }
87 m_pos += amt;
88 }
89 return NO_ERROR;
90}
91
92status_t
Joe Onorato4535e402009-05-15 09:07:06 -040093BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
94{
95 if (m_status != NO_ERROR) {
96 return m_status;
97 }
98
99 ssize_t amt;
100
101 amt = write_padding_for(m_pos);
102 if (amt != 0) {
103 return amt;
104 }
105
Joe Onorato06290a42009-06-18 20:10:37 -0700106 String8 k;
107 if (m_keyPrefix.length() > 0) {
108 k = m_keyPrefix;
109 k += ":";
110 k += key;
111 } else {
112 k = key;
113 }
Christopher Tate4a627c72011-04-01 14:43:32 -0700114 if (DEBUG) {
115 LOGD("Writing header: prefix='%s' key='%s' dataSize=%d", m_keyPrefix.string(), key.string(),
Joe Onorato568bc322009-06-26 17:19:11 -0400116 dataSize);
117 }
Joe Onorato06290a42009-06-18 20:10:37 -0700118
Joe Onorato4535e402009-05-15 09:07:06 -0400119 entity_header_v1 header;
120 ssize_t keyLen;
121
Joe Onorato06290a42009-06-18 20:10:37 -0700122 keyLen = k.length();
Joe Onorato4535e402009-05-15 09:07:06 -0400123
Joe Onoratod2110db2009-05-19 13:41:21 -0700124 header.type = tolel(BACKUP_HEADER_ENTITY_V1);
Joe Onorato4535e402009-05-15 09:07:06 -0400125 header.keyLen = tolel(keyLen);
126 header.dataSize = tolel(dataSize);
127
Christopher Tate4a627c72011-04-01 14:43:32 -0700128 if (DEBUG) LOGI("writing entity header, %d bytes", sizeof(entity_header_v1));
Joe Onorato4535e402009-05-15 09:07:06 -0400129 amt = write(m_fd, &header, sizeof(entity_header_v1));
130 if (amt != sizeof(entity_header_v1)) {
131 m_status = errno;
132 return m_status;
133 }
134 m_pos += amt;
135
Christopher Tate4a627c72011-04-01 14:43:32 -0700136 if (DEBUG) LOGI("writing entity header key, %d bytes", keyLen+1);
Joe Onorato06290a42009-06-18 20:10:37 -0700137 amt = write(m_fd, k.string(), keyLen+1);
Joe Onorato4535e402009-05-15 09:07:06 -0400138 if (amt != keyLen+1) {
139 m_status = errno;
140 return m_status;
141 }
142 m_pos += amt;
143
144 amt = write_padding_for(keyLen+1);
145
146 m_entityCount++;
147
148 return amt;
149}
150
151status_t
152BackupDataWriter::WriteEntityData(const void* data, size_t size)
153{
Christopher Tate4a627c72011-04-01 14:43:32 -0700154 if (DEBUG) LOGD("Writing data: size=%lu", (unsigned long) size);
155
Joe Onorato4535e402009-05-15 09:07:06 -0400156 if (m_status != NO_ERROR) {
Christopher Tate4a627c72011-04-01 14:43:32 -0700157 if (DEBUG) {
158 LOGD("Not writing data - stream in error state %d (%s)", m_status, strerror(m_status));
159 }
Joe Onorato4535e402009-05-15 09:07:06 -0400160 return m_status;
161 }
162
163 // We don't write padding here, because they're allowed to call this several
164 // times with smaller buffers. We write it at the end of WriteEntityHeader
165 // instead.
166 ssize_t amt = write(m_fd, data, size);
167 if (amt != (ssize_t)size) {
168 m_status = errno;
Christopher Tate4a627c72011-04-01 14:43:32 -0700169 if (DEBUG) LOGD("write returned error %d (%s)", m_status, strerror(m_status));
Joe Onorato4535e402009-05-15 09:07:06 -0400170 return m_status;
171 }
172 m_pos += amt;
173 return NO_ERROR;
174}
175
Joe Onorato06290a42009-06-18 20:10:37 -0700176void
177BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
178{
179 m_keyPrefix = keyPrefix;
180}
Joe Onorato4535e402009-05-15 09:07:06 -0400181
Joe Onorato2e1da322009-05-15 18:20:19 -0400182
183BackupDataReader::BackupDataReader(int fd)
184 :m_fd(fd),
Joe Onorato5f15d152009-06-16 16:31:35 -0400185 m_done(false),
Joe Onorato2e1da322009-05-15 18:20:19 -0400186 m_status(NO_ERROR),
187 m_pos(0),
188 m_entityCount(0)
189{
190 memset(&m_header, 0, sizeof(m_header));
191}
192
193BackupDataReader::~BackupDataReader()
194{
195}
196
197status_t
198BackupDataReader::Status()
199{
200 return m_status;
201}
202
203#define CHECK_SIZE(actual, expected) \
204 do { \
205 if ((actual) != (expected)) { \
206 if ((actual) == 0) { \
207 m_status = EIO; \
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700208 m_done = true; \
Joe Onorato2e1da322009-05-15 18:20:19 -0400209 } else { \
210 m_status = errno; \
Christopher Tate4cc86e12009-09-21 19:36:51 -0700211 LOGD("CHECK_SIZE(a=%ld e=%ld) failed at line %d m_status='%s'", \
212 long(actual), long(expected), __LINE__, strerror(m_status)); \
Joe Onorato2e1da322009-05-15 18:20:19 -0400213 } \
214 return m_status; \
215 } \
216 } while(0)
217#define SKIP_PADDING() \
218 do { \
219 status_t err = skip_padding(); \
220 if (err != NO_ERROR) { \
Joe Onorato8d725692009-07-28 18:23:05 -0700221 LOGD("SKIP_PADDING FAILED at line %d", __LINE__); \
Joe Onorato2e1da322009-05-15 18:20:19 -0400222 m_status = err; \
223 return err; \
224 } \
225 } while(0)
226
227status_t
Joe Onorato5f15d152009-06-16 16:31:35 -0400228BackupDataReader::ReadNextHeader(bool* done, int* type)
Joe Onorato2e1da322009-05-15 18:20:19 -0400229{
Joe Onorato5f15d152009-06-16 16:31:35 -0400230 *done = m_done;
Joe Onorato2e1da322009-05-15 18:20:19 -0400231 if (m_status != NO_ERROR) {
232 return m_status;
233 }
234
235 int amt;
236
Joe Onorato8d725692009-07-28 18:23:05 -0700237 amt = skip_padding();
238 if (amt == EIO) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700239 *done = m_done = true;
Joe Onorato8d725692009-07-28 18:23:05 -0700240 return NO_ERROR;
241 }
242 else if (amt != NO_ERROR) {
243 return amt;
244 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400245 amt = read(m_fd, &m_header, sizeof(m_header));
Joe Onorato5f15d152009-06-16 16:31:35 -0400246 *done = m_done = (amt == 0);
Joe Onorato8d725692009-07-28 18:23:05 -0700247 if (*done) {
248 return NO_ERROR;
249 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400250 CHECK_SIZE(amt, sizeof(m_header));
Joe Onorato5d605dc2009-06-18 18:23:43 -0700251 m_pos += sizeof(m_header);
252 if (type) {
253 *type = m_header.type;
254 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400255
256 // validate and fix up the fields.
257 m_header.type = fromlel(m_header.type);
258 switch (m_header.type)
259 {
Joe Onoratod2110db2009-05-19 13:41:21 -0700260 case BACKUP_HEADER_ENTITY_V1:
Joe Onorato5d605dc2009-06-18 18:23:43 -0700261 {
Joe Onorato2e1da322009-05-15 18:20:19 -0400262 m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
263 if (m_header.entity.keyLen <= 0) {
264 LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
265 (int)m_header.entity.keyLen);
266 m_status = EINVAL;
267 }
268 m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
Joe Onorato2e1da322009-05-15 18:20:19 -0400269 m_entityCount++;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700270
271 // read the rest of the header (filename)
272 size_t size = m_header.entity.keyLen;
273 char* buf = m_key.lockBuffer(size);
274 if (buf == NULL) {
275 m_status = ENOMEM;
276 return m_status;
277 }
278 int amt = read(m_fd, buf, size+1);
279 CHECK_SIZE(amt, (int)size+1);
280 m_key.unlockBuffer(size);
281 m_pos += size+1;
282 SKIP_PADDING();
283 m_dataEndPos = m_pos + m_header.entity.dataSize;
284
Joe Onorato2e1da322009-05-15 18:20:19 -0400285 break;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700286 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400287 default:
288 LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type);
289 m_status = EINVAL;
290 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400291
292 return m_status;
293}
294
Joe Onorato2e1da322009-05-15 18:20:19 -0400295bool
296BackupDataReader::HasEntities()
297{
Joe Onoratod2110db2009-05-19 13:41:21 -0700298 return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400299}
300
301status_t
302BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
303{
304 if (m_status != NO_ERROR) {
305 return m_status;
306 }
Joe Onoratod2110db2009-05-19 13:41:21 -0700307 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
Joe Onorato2e1da322009-05-15 18:20:19 -0400308 return EINVAL;
309 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700310 *key = m_key;
Joe Onorato2e1da322009-05-15 18:20:19 -0400311 *dataSize = m_header.entity.dataSize;
Joe Onorato2e1da322009-05-15 18:20:19 -0400312 return NO_ERROR;
313}
314
315status_t
Joe Onoratod2110db2009-05-19 13:41:21 -0700316BackupDataReader::SkipEntityData()
317{
318 if (m_status != NO_ERROR) {
319 return m_status;
320 }
321 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
322 return EINVAL;
323 }
324 if (m_header.entity.dataSize > 0) {
Joe Onorato5f15d152009-06-16 16:31:35 -0400325 int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
Joe Onoratoee5bbb72009-07-13 14:44:07 -0700326 if (pos == -1) {
327 return errno;
328 }
Joe Onoratod2110db2009-05-19 13:41:21 -0700329 }
Joe Onoratoee5bbb72009-07-13 14:44:07 -0700330 SKIP_PADDING();
331 return NO_ERROR;
Joe Onoratod2110db2009-05-19 13:41:21 -0700332}
333
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700334ssize_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400335BackupDataReader::ReadEntityData(void* data, size_t size)
336{
337 if (m_status != NO_ERROR) {
Joe Onorato5d605dc2009-06-18 18:23:43 -0700338 return -1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400339 }
Joe Onorato5f15d152009-06-16 16:31:35 -0400340 int remaining = m_dataEndPos - m_pos;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700341 //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
342 // size, m_pos, m_dataEndPos, remaining);
Joe Onorato5f15d152009-06-16 16:31:35 -0400343 if (remaining <= 0) {
344 return 0;
345 }
Joe Onorato06290a42009-06-18 20:10:37 -0700346 if (((int)size) > remaining) {
Joe Onorato5d605dc2009-06-18 18:23:43 -0700347 size = remaining;
348 }
349 //LOGD(" reading %d bytes", size);
Joe Onorato2e1da322009-05-15 18:20:19 -0400350 int amt = read(m_fd, data, size);
Joe Onorato5d605dc2009-06-18 18:23:43 -0700351 if (amt < 0) {
352 m_status = errno;
353 return -1;
354 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700355 if (amt == 0) {
356 m_status = EIO;
357 m_done = true;
358 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700359 m_pos += amt;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700360 return amt;
Joe Onorato2e1da322009-05-15 18:20:19 -0400361}
362
363status_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400364BackupDataReader::skip_padding()
365{
366 ssize_t amt;
367 ssize_t paddingSize;
368
369 paddingSize = padding_extra(m_pos);
370 if (paddingSize > 0) {
371 uint32_t padding;
372 amt = read(m_fd, &padding, paddingSize);
373 CHECK_SIZE(amt, paddingSize);
374 m_pos += amt;
375 }
376 return NO_ERROR;
377}
378
379
Joe Onorato4535e402009-05-15 09:07:06 -0400380} // namespace android